Skip to content

Commit ea59c48

Browse files
committed
Dataset refactoring after SequenceAlike simplification
1 parent 1065565 commit ea59c48

File tree

15 files changed

+81
-119
lines changed

15 files changed

+81
-119
lines changed

chartsy-core/src/main/java/one/chartsy/base/AbstractRingBuffer.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
package one.chartsy.base;
66

7-
public abstract class AbstractRingBuffer {
7+
public abstract class AbstractRingBuffer implements SequenceAlike {
88

99
protected final int capacity;
1010
protected final int mask;
@@ -23,26 +23,36 @@ static int nextPowerOfTwo(int x) {
2323
return 1 << -Integer.numberOfLeadingZeros(x - 1);
2424
}
2525

26+
@Override
2627
public final SequenceAlike.Order getOrder() {
2728
return SequenceAlike.Order.INDEX_DESC;
2829
}
2930

3031
/**
31-
* Returns the buffer's capacity, or {@code Integer.MAX_VALUE} if there is no intrinsic limit.
32+
* Gets the number of elements currently held in the buffer.
3233
*
33-
* @return the buffer's capacity
34+
* @return the number of elements held in the buffer
3435
*/
35-
public int capacity() {
36-
return capacity;
36+
@Override
37+
public int length() {
38+
return (int) Math.min(capacity(), nextWrite);
3739
}
3840

3941
/**
40-
* Gets the number of elements currently held in the buffer.
42+
* Is the buffer currently empty?
43+
*/
44+
@Override
45+
public boolean isEmpty() {
46+
return length() == 0;
47+
}
48+
49+
/**
50+
* Returns the buffer's capacity, or {@code Integer.MAX_VALUE} if there is no intrinsic limit.
4151
*
42-
* @return the number of elements held in the buffer
52+
* @return the buffer's capacity
4353
*/
44-
public int length() {
45-
return (int) Math.min(capacity(), nextWrite);
54+
public int capacity() {
55+
return capacity;
4656
}
4757

4858
/**
@@ -56,13 +66,6 @@ public int remainingCapacity() {
5666
return (capacity == Integer.MAX_VALUE) ? Integer.MAX_VALUE : capacity - length();
5767
}
5868

59-
/**
60-
* Is the buffer currently empty?
61-
*/
62-
public boolean isEmpty() {
63-
return length() == 0;
64-
}
65-
6669
/**
6770
* Is the buffer currently empty?
6871
*/

chartsy-core/src/main/java/one/chartsy/base/Dataset.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import one.chartsy.util.Pair;
1313

1414
import java.util.AbstractList;
15+
import java.util.ArrayList;
16+
import java.util.Collections;
1517
import java.util.Iterator;
1618
import java.util.List;
1719
import java.util.Objects;
@@ -29,7 +31,7 @@
2931
*
3032
* @param <E> the type of elements stored in this dataset
3133
*/
32-
public interface Dataset<E> extends SequenceAlike<E, Dataset<E>> {
34+
public interface Dataset<E> extends Iterable<E>, SequenceAlike {
3335

3436
/**
3537
* Returns the element at the specified position in the dataset. Depending
@@ -43,7 +45,7 @@ public interface Dataset<E> extends SequenceAlike<E, Dataset<E>> {
4345
*/
4446
E get(int index);
4547

46-
@Override Stream<E> stream();
48+
Stream<E> stream();
4749

4850
@Override Iterator<E> iterator();
4951

@@ -53,6 +55,12 @@ default Dataset<E> toImmutable() {
5355
return ImmutableDataset.from(this);
5456
}
5557

58+
default List<E> toImmutableList() {
59+
List<E> list = new ArrayList<>(length());
60+
forEach(list::add);
61+
return Collections.unmodifiableList(list);
62+
}
63+
5664
default List<E> values() {
5765
return new Values<>(this);
5866
}

chartsy-core/src/main/java/one/chartsy/base/DoubleDataset.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public interface DoubleDataset extends PrimitiveDataset<Double, DoubleDataset, S
3838
*/
3939
double get(int index);
4040

41-
@Override
4241
default DoubleStream stream() {
4342
return StreamSupport.doubleStream(spliterator(), false);
4443
}

chartsy-core/src/main/java/one/chartsy/base/IntDataset.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public interface IntDataset extends PrimitiveDataset<Integer, IntDataset, Splite
3838
*/
3939
int get(int index);
4040

41-
@Override
4241
default IntStream stream() {
4342
return StreamSupport.intStream(spliterator(), false);
4443
}

chartsy-core/src/main/java/one/chartsy/base/LongDataset.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public interface LongDataset extends PrimitiveDataset<Long, LongDataset, Spliter
3838
*/
3939
long get(int index);
4040

41-
@Override
4241
default LongStream stream() {
4342
return StreamSupport.longStream(spliterator(), false);
4443
}
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
package one.chartsy.base;
22

3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
36
import java.util.Spliterator;
47

58
public interface PrimitiveDataset<E,
69
T_SEQ extends PrimitiveDataset<E, T_SEQ, T_SPLITR>,
710
T_SPLITR extends Spliterator.OfPrimitive<E, ?, T_SPLITR>>
8-
extends SequenceAlike<E, T_SEQ> {
11+
extends SequenceAlike, Iterable<E> {
912

1013
/**
1114
* Returns a primitive spliterator over the elements in the window.
1215
*
1316
* @return a primitive spliterator
1417
*/
15-
@Override
1618
T_SPLITR spliterator();
1719

20+
default List<E> toImmutableList() {
21+
List<E> list = new ArrayList<>(length());
22+
forEach(list::add);
23+
return Collections.unmodifiableList(list);
24+
}
1825
}

chartsy-core/src/main/java/one/chartsy/base/SequenceAlike.java

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
*/
55
package one.chartsy.base;
66

7-
import java.util.ArrayList;
8-
import java.util.Collections;
9-
import java.util.List;
10-
import java.util.stream.BaseStream;
117
import java.util.stream.DoubleStream;
128
import java.util.stream.IntStream;
139
import java.util.stream.LongStream;
@@ -19,7 +15,7 @@
1915
*
2016
* @author Mariusz Bernacki
2117
*/
22-
public interface SequenceAlike<E, T_SEQ extends SequenceAlike<E, T_SEQ>> extends Iterable<E> {
18+
public interface SequenceAlike {
2319

2420
int length();
2521

@@ -29,16 +25,8 @@ default boolean isUndefined(int index) {
2925
return index < 0 || index >= length();
3026
}
3127

32-
BaseStream<E, ?> stream();
33-
3428
Order getOrder();
3529

36-
default List<E> toImmutableList() {
37-
List<E> list = new ArrayList<>(length());
38-
forEach(list::add);
39-
return Collections.unmodifiableList(list);
40-
}
41-
4230
enum Order {
4331
/** Specifies that encounter order is guaranteed to be ascending index order. */
4432
INDEX_ASC,
@@ -47,12 +35,16 @@ enum Order {
4735
/** Indicates that encounter order is unspecified or not known. */
4836
UNSPECIFIED;
4937

50-
public IntStream indexes(SequenceAlike<?, ?> seq) {
38+
public IntStream indexes(SequenceAlike seq) {
5139
int length = seq.length();
5240
IntStream stream = IntStream.range(0, length);
5341
return (this == INDEX_DESC) ? stream.map(i -> length - i - 1) : stream;
5442
}
5543

44+
public boolean isAscending() {
45+
return this == INDEX_ASC;
46+
}
47+
5648
public boolean isDescending() {
5749
return this == INDEX_DESC;
5850
}
@@ -101,71 +93,71 @@ public static void reverse(long[] arr) {
10193
}
10294
}
10395

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) {
10597
return switch (this) {
10698
case INDEX_ASC -> s.skip(n);
10799
case INDEX_DESC -> s.limit(Math.max(0, seq.length() - n));
108100
default -> throw new UnsupportedOperationException("Operation `drop` not implemented for order " + this);
109101
};
110102
}
111103

112-
public DoubleStream drop(int n, DoubleStream s, SequenceAlike<Double, ?> seq) {
104+
public DoubleStream drop(int n, DoubleStream s, SequenceAlike seq) {
113105
return switch (this) {
114106
case INDEX_ASC -> s.skip(n);
115107
case INDEX_DESC -> s.limit(Math.max(0, seq.length() - n));
116108
default -> throw new UnsupportedOperationException("Operation `drop` not implemented for order " + this);
117109
};
118110
}
119111

120-
public IntStream drop(int n, IntStream s, SequenceAlike<Integer, ?> seq) {
112+
public IntStream drop(int n, IntStream s, SequenceAlike seq) {
121113
return switch (this) {
122114
case INDEX_ASC -> s.skip(n);
123115
case INDEX_DESC -> s.limit(Math.max(0, seq.length() - n));
124116
default -> throw new UnsupportedOperationException("Operation `drop` not implemented for order " + this);
125117
};
126118
}
127119

128-
public LongStream drop(int n, LongStream s, SequenceAlike<Long, ?> seq) {
120+
public LongStream drop(int n, LongStream s, SequenceAlike seq) {
129121
return switch (this) {
130122
case INDEX_ASC -> s.skip(n);
131123
case INDEX_DESC -> s.limit(Math.max(0, seq.length() - n));
132124
default -> throw new UnsupportedOperationException("Operation `drop` not implemented for order " + this);
133125
};
134126
}
135127

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) {
137129
return switch (this) {
138130
case INDEX_ASC -> s.limit(maxCount);
139131
case INDEX_DESC -> s.skip(Math.max(0, seq.length() - maxCount));
140132
default -> throw new UnsupportedOperationException("Operation `take` not implemented for order " + this);
141133
};
142134
}
143135

144-
public DoubleStream take(int maxCount, DoubleStream s, SequenceAlike<Double, ?> seq) {
136+
public DoubleStream take(int maxCount, DoubleStream s, SequenceAlike seq) {
145137
return switch (this) {
146138
case INDEX_ASC -> s.limit(maxCount);
147139
case INDEX_DESC -> s.skip(Math.max(0, seq.length() - maxCount));
148140
default -> throw new UnsupportedOperationException("Operation `take` not implemented for order " + this);
149141
};
150142
}
151143

152-
public IntStream take(int maxCount, IntStream s, SequenceAlike<Integer, ?> seq) {
144+
public IntStream take(int maxCount, IntStream s, SequenceAlike seq) {
153145
return switch (this) {
154146
case INDEX_ASC -> s.limit(maxCount);
155147
case INDEX_DESC -> s.skip(Math.max(0, seq.length() - maxCount));
156148
default -> throw new UnsupportedOperationException("Operation `take` not implemented for order " + this);
157149
};
158150
}
159151

160-
public LongStream take(int maxCount, LongStream s, SequenceAlike<Long, ?> seq) {
152+
public LongStream take(int maxCount, LongStream s, SequenceAlike seq) {
161153
return switch (this) {
162154
case INDEX_ASC -> s.limit(maxCount);
163155
case INDEX_DESC -> s.skip(Math.max(0, seq.length() - maxCount));
164156
default -> throw new UnsupportedOperationException("Operation `take` not implemented for order " + this);
165157
};
166158
}
167159

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) {
169161
return switch (this) {
170162
case INDEX_ASC -> s.skip(fromIndex).limit(maxCount);
171163
case INDEX_DESC -> {
@@ -176,7 +168,7 @@ public <E> Stream<E> dropTake(int fromIndex, int maxCount, Stream<E> s, Sequence
176168
};
177169
}
178170

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) {
180172
return switch (this) {
181173
case INDEX_ASC -> s.skip(fromIndex).limit(maxCount);
182174
case INDEX_DESC -> {
@@ -187,7 +179,7 @@ public DoubleStream dropTake(int fromIndex, int maxCount, DoubleStream s, Sequen
187179
};
188180
}
189181

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) {
191183
return switch (this) {
192184
case INDEX_ASC -> s.skip(fromIndex).limit(maxCount);
193185
case INDEX_DESC -> {
@@ -198,7 +190,7 @@ public IntStream dropTake(int fromIndex, int maxCount, IntStream s, SequenceAlik
198190
};
199191
}
200192

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) {
202194
return switch (this) {
203195
case INDEX_ASC -> s.skip(fromIndex).limit(maxCount);
204196
case INDEX_DESC -> {

chartsy-core/src/main/java/one/chartsy/base/dataset/AbstractDataset.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public Spliterator<E> spliterator() {
4242
return stream().spliterator();
4343
}
4444

45-
public static <E, T extends SequenceAlike<?, T>>
45+
public static <E, T extends SequenceAlike>
4646
AbstractDataset<E> from(T origin, IndexedFunction<T, E> getter) {
4747
return new From<>(origin) {
4848
@Override
@@ -52,7 +52,7 @@ public E get(int index) {
5252
};
5353
}
5454

55-
public static <E, T extends SequenceAlike<?, T>>
55+
public static <E, T extends SequenceAlike>
5656
AbstractDataset<E> from(T origin, IndexedFunction<T, E> getter, Function<T, Stream<E>> stream) {
5757
return new From<>(origin) {
5858
@Override
@@ -67,7 +67,7 @@ public Stream<E> stream() {
6767
};
6868
}
6969

70-
public static <E, T extends SequenceAlike<?, T>>
70+
public static <E, T extends SequenceAlike>
7171
AbstractDataset<E> from(T origin, ToIntFunction<T> length, IndexedFunction<T, E> getter) {
7272
return new From<>(origin) {
7373
@Override
@@ -82,7 +82,7 @@ public E get(int index) {
8282
};
8383
}
8484

85-
public static <E, T extends SequenceAlike<?, T>>
85+
public static <E, T extends SequenceAlike>
8686
AbstractDataset<E> from(T origin, ToIntFunction<T> length, IndexedFunction<T, E> getter, Function<T, Stream<E>> stream) {
8787
return new From<>(origin) {
8888
@Override
@@ -102,7 +102,7 @@ public Stream<E> stream() {
102102
};
103103
}
104104

105-
public static abstract class From<E, T extends SequenceAlike<?, T>> extends AbstractDataset<E> {
105+
public static abstract class From<E, T extends SequenceAlike> extends AbstractDataset<E> {
106106
protected final T origin;
107107

108108
public From(T origin) {

0 commit comments

Comments
 (0)