-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
239 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ | |
|
||
# IntelliJ IDEA | ||
.idea/dictionaries/ | ||
*.iml | ||
|
||
# Mac OS/X | ||
.DS_Store |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package one.util.streamex; | ||
|
||
import java.util.Spliterator; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.function.Consumer; | ||
|
||
/* package */ class IfEmptySpliterator<T> extends StreamExInternals.CloneableSpliterator<T, IfEmptySpliterator<T>> { | ||
// alt == null --> decision is made | ||
private Spliterator<? extends T> spltr, alt; | ||
// positive = number of non-exhausted spliterators; negative = surely non-empty | ||
private final AtomicInteger state = new AtomicInteger(1); | ||
|
||
public IfEmptySpliterator(Spliterator<? extends T> spltr, Spliterator<? extends T> alt) { | ||
this.spltr = spltr; | ||
this.alt = alt; | ||
} | ||
|
||
void tryInit() { | ||
if (alt != null && spltr.hasCharacteristics(SIZED)) { | ||
if (spltr.estimateSize() == 0) { | ||
spltr = alt; | ||
} | ||
alt = null; | ||
} | ||
} | ||
|
||
@Override | ||
public boolean tryAdvance(Consumer<? super T> action) { | ||
if (alt != null) { | ||
if (spltr.tryAdvance(action)) { | ||
state.set(-1); | ||
alt = null; | ||
return true; | ||
} | ||
if (drawState()) { | ||
spltr = alt; | ||
} | ||
alt = null; | ||
} | ||
return spltr.tryAdvance(action); | ||
} | ||
|
||
@Override | ||
public void forEachRemaining(Consumer<? super T> action) { | ||
tryInit(); | ||
if (alt == null) { | ||
spltr.forEachRemaining(action); | ||
} else { | ||
boolean[] empty = {true}; | ||
spltr.forEachRemaining(e -> { | ||
empty[0] = false; | ||
action.accept(e); | ||
}); | ||
if (empty[0]) { | ||
if (drawState()) { | ||
(spltr = alt).forEachRemaining(action); | ||
} | ||
} else { | ||
state.set(-1); | ||
} | ||
alt = null; | ||
} | ||
} | ||
|
||
boolean drawState() { | ||
return state.updateAndGet(x -> x > 0 ? x - 1 : x) == 0; | ||
} | ||
|
||
@Override | ||
public Spliterator<T> trySplit() { | ||
Spliterator<? extends T> prefix = spltr.trySplit(); | ||
if (prefix == null) { | ||
return null; | ||
} | ||
tryInit(); | ||
if (alt != null) { | ||
state.updateAndGet(x -> x > 0 ? x + 1 : x); | ||
} | ||
IfEmptySpliterator<T> clone = doClone(); | ||
clone.spltr = prefix; | ||
return clone; | ||
} | ||
|
||
@Override | ||
public long estimateSize() { | ||
tryInit(); | ||
long size = spltr.estimateSize(); | ||
return alt != null && size == 0 ? alt.estimateSize() : size; | ||
} | ||
|
||
@Override | ||
public int characteristics() { | ||
if(alt == null) { | ||
return spltr.characteristics() & (~SORTED); | ||
} | ||
return (spltr.characteristics() & alt.characteristics() & (~SORTED)) | | ||
((spltr.characteristics() | alt.characteristics()) & (ORDERED)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/test/java/one/util/streamex/IfEmptySpliteratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package one.util.streamex; | ||
|
||
import static one.util.streamex.TestHelpers.*; | ||
import org.junit.Test; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Spliterator; | ||
import java.util.Spliterators; | ||
import java.util.function.Supplier; | ||
|
||
public class IfEmptySpliteratorTest { | ||
@Test | ||
public void testSpliterator() { | ||
List<Integer> data = IntStreamEx.range(1000).boxed().toList(); | ||
checkSpliterator("++", data, () -> new IfEmptySpliterator<>(data.spliterator(), data.spliterator())); | ||
checkSpliterator("-+", data, () -> new IfEmptySpliterator<>(Spliterators.emptySpliterator(), data.spliterator())); | ||
checkSpliterator("+-", data, () -> new IfEmptySpliterator<>(data.spliterator(), Spliterators.emptySpliterator())); | ||
checkSpliterator("--", Collections.emptyList(), () -> new IfEmptySpliterator<>(Spliterators.emptySpliterator(), Spliterators.emptySpliterator())); | ||
} | ||
|
||
@Test | ||
public void testFiltered() { | ||
List<Integer> data = IntStreamEx.range(1000).boxed().toList(); | ||
Supplier<Spliterator<Integer>> allMatch = () -> data.parallelStream().filter(x -> x >= 0).spliterator(); | ||
Supplier<Spliterator<Integer>> noneMatch = () -> data.parallelStream().filter(x -> x < 0).spliterator(); | ||
Supplier<Spliterator<Integer>> lastMatch = () -> data.parallelStream().filter(x -> x == 999).spliterator(); | ||
checkSpliterator("++", data, () -> new IfEmptySpliterator<>(allMatch.get(), allMatch.get())); | ||
checkSpliterator("l+", Collections.singletonList(999), () -> new IfEmptySpliterator<>(lastMatch.get(), allMatch.get())); | ||
checkSpliterator("-+", data, () -> new IfEmptySpliterator<>(noneMatch.get(), allMatch.get())); | ||
checkSpliterator("+-", data, () -> new IfEmptySpliterator<>(allMatch.get(), noneMatch.get())); | ||
checkSpliterator("--", Collections.emptyList(), () -> new IfEmptySpliterator<>(noneMatch.get(), noneMatch.get())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8"> | ||
<output url="file://$MODULE_DIR$/target/classes" /> | ||
<output-test url="file://$MODULE_DIR$/target/test-classes" /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> | ||
<excludeFolder url="file://$MODULE_DIR$/target" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" /> | ||
</component> | ||
</module> |