Skip to content

Commit

Permalink
Optional<> replaced with NullableValue<>
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Komarov (k0m@) committed Aug 17, 2021
1 parent 634cebd commit 10e9c7f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/main/java/pw/komarov/streamer/Streamer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pw.komarov.streamer;

import pw.komarov.utils.NullableValue;

import java.util.*;
import java.util.function.*;
import java.util.stream.*;
Expand Down Expand Up @@ -213,7 +215,7 @@ public T next() {
return next;
}

@SuppressWarnings({"OptionalAssignedToNull","unchecked"})
@SuppressWarnings("unchecked")
private void calculateSorted() {
for (int i = 1; i <= sortedCount; i++) {
//building local operations list (from general operations list, by extracting sublist)
Expand All @@ -234,13 +236,13 @@ private void calculateSorted() {

//data collecting
final List<T> data = new ArrayList<>();
Optional<T> nextOpt;
NullableValue<T> nextValue;
do {
nextOpt = getNext(localOperations);
if (nextOpt != null)
data.add(nextOpt.orElse(null));
nextValue = getNext(localOperations);
if (nextValue != null)
data.add(nextValue.get());

} while (nextOpt != null);
} while (nextValue != null);

//sorting...
if (sortedOperation != null)
Expand All @@ -252,16 +254,15 @@ private void calculateSorted() {
}

private void calcNextAndHasNext() { //calculating next and getNext
Optional<T> opt = getNext(intermediateOperations);
NullableValue<T> nextValue = getNext(intermediateOperations);

//noinspection OptionalAssignedToNull
hasNext = opt != null;
hasNext = nextValue != null;
if (hasNext)
next = opt.orElse(null);
next = nextValue.get();
}

@SuppressWarnings({"unchecked"})
private Optional<T> getNext(List<IntermediateOperation> operations) {
private NullableValue<T> getNext(List<IntermediateOperation> operations) {
T next = null;

boolean hasNext = !noNext && sourceIterator.hasNext();
Expand Down Expand Up @@ -293,10 +294,9 @@ private Optional<T> getNext(List<IntermediateOperation> operations) {
for (Consumer<? super T> peekSequence : peekSequences)
peekSequence.accept(next);

return Optional.ofNullable(next);
return NullableValue.of(next);
}

//noinspection OptionalAssignedToNull
return null;
}

Expand Down
20 changes: 20 additions & 0 deletions src/main/java/pw/komarov/utils/NullableValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package pw.komarov.utils;

public class NullableValue<E> {
private static final NullableValue<?> NULLABLE = new NullableValue<>(null);

private final E value;

private NullableValue(E value) {
this.value = value;
}

@SuppressWarnings("unchecked")
public static <E> NullableValue<E> of(E value) {
return value == null ? (NullableValue<E>) NULLABLE : new NullableValue<>(value);
}

public E get() {
return value;
}
}

0 comments on commit 10e9c7f

Please sign in to comment.