4
4
*/
5
5
package one .chartsy .base ;
6
6
7
- import java .util .ArrayList ;
8
- import java .util .Collections ;
9
- import java .util .List ;
10
- import java .util .stream .BaseStream ;
11
7
import java .util .stream .DoubleStream ;
12
8
import java .util .stream .IntStream ;
13
9
import java .util .stream .LongStream ;
19
15
*
20
16
* @author Mariusz Bernacki
21
17
*/
22
- public interface SequenceAlike < E , T_SEQ extends SequenceAlike < E , T_SEQ >> extends Iterable < E > {
18
+ public interface SequenceAlike {
23
19
24
20
int length ();
25
21
@@ -29,16 +25,8 @@ default boolean isUndefined(int index) {
29
25
return index < 0 || index >= length ();
30
26
}
31
27
32
- BaseStream <E , ?> stream ();
33
-
34
28
Order getOrder ();
35
29
36
- default List <E > toImmutableList () {
37
- List <E > list = new ArrayList <>(length ());
38
- forEach (list ::add );
39
- return Collections .unmodifiableList (list );
40
- }
41
-
42
30
enum Order {
43
31
/** Specifies that encounter order is guaranteed to be ascending index order. */
44
32
INDEX_ASC ,
@@ -47,12 +35,16 @@ enum Order {
47
35
/** Indicates that encounter order is unspecified or not known. */
48
36
UNSPECIFIED ;
49
37
50
- public IntStream indexes (SequenceAlike <?, ?> seq ) {
38
+ public IntStream indexes (SequenceAlike seq ) {
51
39
int length = seq .length ();
52
40
IntStream stream = IntStream .range (0 , length );
53
41
return (this == INDEX_DESC ) ? stream .map (i -> length - i - 1 ) : stream ;
54
42
}
55
43
44
+ public boolean isAscending () {
45
+ return this == INDEX_ASC ;
46
+ }
47
+
56
48
public boolean isDescending () {
57
49
return this == INDEX_DESC ;
58
50
}
@@ -101,71 +93,71 @@ public static void reverse(long[] arr) {
101
93
}
102
94
}
103
95
104
- public <E > Stream <E > drop (int n , Stream <E > s , SequenceAlike < E , ?> seq ) {
96
+ public <E > Stream <E > drop (int n , Stream <E > s , SequenceAlike seq ) {
105
97
return switch (this ) {
106
98
case INDEX_ASC -> s .skip (n );
107
99
case INDEX_DESC -> s .limit (Math .max (0 , seq .length () - n ));
108
100
default -> throw new UnsupportedOperationException ("Operation `drop` not implemented for order " + this );
109
101
};
110
102
}
111
103
112
- public DoubleStream drop (int n , DoubleStream s , SequenceAlike < Double , ?> seq ) {
104
+ public DoubleStream drop (int n , DoubleStream s , SequenceAlike seq ) {
113
105
return switch (this ) {
114
106
case INDEX_ASC -> s .skip (n );
115
107
case INDEX_DESC -> s .limit (Math .max (0 , seq .length () - n ));
116
108
default -> throw new UnsupportedOperationException ("Operation `drop` not implemented for order " + this );
117
109
};
118
110
}
119
111
120
- public IntStream drop (int n , IntStream s , SequenceAlike < Integer , ?> seq ) {
112
+ public IntStream drop (int n , IntStream s , SequenceAlike seq ) {
121
113
return switch (this ) {
122
114
case INDEX_ASC -> s .skip (n );
123
115
case INDEX_DESC -> s .limit (Math .max (0 , seq .length () - n ));
124
116
default -> throw new UnsupportedOperationException ("Operation `drop` not implemented for order " + this );
125
117
};
126
118
}
127
119
128
- public LongStream drop (int n , LongStream s , SequenceAlike < Long , ?> seq ) {
120
+ public LongStream drop (int n , LongStream s , SequenceAlike seq ) {
129
121
return switch (this ) {
130
122
case INDEX_ASC -> s .skip (n );
131
123
case INDEX_DESC -> s .limit (Math .max (0 , seq .length () - n ));
132
124
default -> throw new UnsupportedOperationException ("Operation `drop` not implemented for order " + this );
133
125
};
134
126
}
135
127
136
- public <E > Stream <E > take (int maxCount , Stream <E > s , SequenceAlike < E , ?> seq ) {
128
+ public <E > Stream <E > take (int maxCount , Stream <E > s , SequenceAlike seq ) {
137
129
return switch (this ) {
138
130
case INDEX_ASC -> s .limit (maxCount );
139
131
case INDEX_DESC -> s .skip (Math .max (0 , seq .length () - maxCount ));
140
132
default -> throw new UnsupportedOperationException ("Operation `take` not implemented for order " + this );
141
133
};
142
134
}
143
135
144
- public DoubleStream take (int maxCount , DoubleStream s , SequenceAlike < Double , ?> seq ) {
136
+ public DoubleStream take (int maxCount , DoubleStream s , SequenceAlike seq ) {
145
137
return switch (this ) {
146
138
case INDEX_ASC -> s .limit (maxCount );
147
139
case INDEX_DESC -> s .skip (Math .max (0 , seq .length () - maxCount ));
148
140
default -> throw new UnsupportedOperationException ("Operation `take` not implemented for order " + this );
149
141
};
150
142
}
151
143
152
- public IntStream take (int maxCount , IntStream s , SequenceAlike < Integer , ?> seq ) {
144
+ public IntStream take (int maxCount , IntStream s , SequenceAlike seq ) {
153
145
return switch (this ) {
154
146
case INDEX_ASC -> s .limit (maxCount );
155
147
case INDEX_DESC -> s .skip (Math .max (0 , seq .length () - maxCount ));
156
148
default -> throw new UnsupportedOperationException ("Operation `take` not implemented for order " + this );
157
149
};
158
150
}
159
151
160
- public LongStream take (int maxCount , LongStream s , SequenceAlike < Long , ?> seq ) {
152
+ public LongStream take (int maxCount , LongStream s , SequenceAlike seq ) {
161
153
return switch (this ) {
162
154
case INDEX_ASC -> s .limit (maxCount );
163
155
case INDEX_DESC -> s .skip (Math .max (0 , seq .length () - maxCount ));
164
156
default -> throw new UnsupportedOperationException ("Operation `take` not implemented for order " + this );
165
157
};
166
158
}
167
159
168
- public <E > Stream <E > dropTake (int fromIndex , int maxCount , Stream <E > s , SequenceAlike < E , ?> seq ) {
160
+ public <E > Stream <E > dropTake (int fromIndex , int maxCount , Stream <E > s , SequenceAlike seq ) {
169
161
return switch (this ) {
170
162
case INDEX_ASC -> s .skip (fromIndex ).limit (maxCount );
171
163
case INDEX_DESC -> {
@@ -176,7 +168,7 @@ public <E> Stream<E> dropTake(int fromIndex, int maxCount, Stream<E> s, Sequence
176
168
};
177
169
}
178
170
179
- public DoubleStream dropTake (int fromIndex , int maxCount , DoubleStream s , SequenceAlike < Double , ?> seq ) {
171
+ public DoubleStream dropTake (int fromIndex , int maxCount , DoubleStream s , SequenceAlike seq ) {
180
172
return switch (this ) {
181
173
case INDEX_ASC -> s .skip (fromIndex ).limit (maxCount );
182
174
case INDEX_DESC -> {
@@ -187,7 +179,7 @@ public DoubleStream dropTake(int fromIndex, int maxCount, DoubleStream s, Sequen
187
179
};
188
180
}
189
181
190
- public IntStream dropTake (int fromIndex , int maxCount , IntStream s , SequenceAlike < Integer , ?> seq ) {
182
+ public IntStream dropTake (int fromIndex , int maxCount , IntStream s , SequenceAlike seq ) {
191
183
return switch (this ) {
192
184
case INDEX_ASC -> s .skip (fromIndex ).limit (maxCount );
193
185
case INDEX_DESC -> {
@@ -198,7 +190,7 @@ public IntStream dropTake(int fromIndex, int maxCount, IntStream s, SequenceAlik
198
190
};
199
191
}
200
192
201
- public LongStream dropTake (int fromIndex , int maxCount , LongStream s , SequenceAlike < Long , ?> seq ) {
193
+ public LongStream dropTake (int fromIndex , int maxCount , LongStream s , SequenceAlike seq ) {
202
194
return switch (this ) {
203
195
case INDEX_ASC -> s .skip (fromIndex ).limit (maxCount );
204
196
case INDEX_DESC -> {
0 commit comments