├── versions.gradle ├── jitpack.yml ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── stream ├── gradle.properties └── src │ ├── main │ └── java │ │ └── com │ │ └── annimon │ │ └── stream │ │ ├── function │ │ ├── BooleanSupplier.java │ │ ├── ObjIntConsumer.java │ │ ├── ThrowableIntSupplier.java │ │ ├── IntToLongFunction.java │ │ ├── LongToIntFunction.java │ │ ├── ThrowableLongSupplier.java │ │ ├── DoubleToIntFunction.java │ │ ├── DoubleToLongFunction.java │ │ ├── IntToDoubleFunction.java │ │ ├── ThrowableDoubleSupplier.java │ │ ├── LongToDoubleFunction.java │ │ ├── ObjLongConsumer.java │ │ ├── ObjDoubleConsumer.java │ │ ├── ThrowableSupplier.java │ │ ├── ToIntFunction.java │ │ ├── ToBooleanFunction.java │ │ ├── ToLongFunction.java │ │ ├── ThrowableIntConsumer.java │ │ ├── BooleanFunction.java │ │ ├── ToDoubleFunction.java │ │ ├── ThrowableLongConsumer.java │ │ ├── ThrowableDoubleConsumer.java │ │ ├── ThrowableConsumer.java │ │ ├── LongBinaryOperator.java │ │ ├── DoubleBinaryOperator.java │ │ ├── ThrowableIntPredicate.java │ │ ├── ThrowableLongPredicate.java │ │ ├── ThrowableIntFunction.java │ │ ├── ThrowableDoublePredicate.java │ │ ├── ThrowableLongFunction.java │ │ ├── ThrowableDoubleFunction.java │ │ ├── ThrowableFunction.java │ │ ├── ThrowablePredicate.java │ │ ├── IntBinaryOperator.java │ │ ├── UnaryOperator.java │ │ ├── IntUnaryOperator.java │ │ ├── LongUnaryOperator.java │ │ └── DoubleUnaryOperator.java │ │ ├── operator │ │ ├── ObjArray.java │ │ ├── IntArray.java │ │ ├── ObjGenerate.java │ │ ├── LongArray.java │ │ ├── IntGenerate.java │ │ ├── DoubleArray.java │ │ ├── LongGenerate.java │ │ ├── DoubleGenerate.java │ │ ├── IntIterate.java │ │ ├── LongIterate.java │ │ ├── DoubleIterate.java │ │ ├── IntLimit.java │ │ ├── ObjIterate.java │ │ ├── LongLimit.java │ │ ├── DoubleLimit.java │ │ ├── ObjDistinct.java │ │ ├── IntMap.java │ │ ├── IntRangeClosed.java │ │ ├── ObjLimit.java │ │ ├── LongRangeClosed.java │ │ ├── LongMap.java │ │ ├── IntMapToLong.java │ │ ├── LongMapToInt.java │ │ ├── DoubleMap.java │ │ ├── DoubleMapToInt.java │ │ ├── IntMapToDouble.java │ │ ├── IntPeek.java │ │ ├── DoubleMapToLong.java │ │ ├── LongMapToDouble.java │ │ ├── LongPeek.java │ │ ├── ObjMapToInt.java │ │ ├── ObjMapToLong.java │ │ ├── ObjPeek.java │ │ ├── ObjTakeUntil.java │ │ ├── IntMapToObj.java │ │ ├── IntSample.java │ │ ├── ObjMap.java │ │ ├── DoublePeek.java │ │ ├── LongMapToObj.java │ │ ├── ObjMapToDouble.java │ │ ├── LongSample.java │ │ ├── DoubleMapToObj.java │ │ ├── IntSkip.java │ │ ├── IntTakeUntil.java │ │ ├── DoubleSample.java │ │ ├── LongSkip.java │ │ ├── LongTakeUntil.java │ │ ├── IntMapIndexed.java │ │ ├── ObjSkip.java │ │ ├── DoubleSkip.java │ │ ├── IntSorted.java │ │ ├── DoubleTakeUntil.java │ │ ├── LongSorted.java │ │ ├── ObjMapIndexed.java │ │ ├── DoubleSorted.java │ │ ├── LongMapIndexed.java │ │ ├── ObjTakeUntilIndexed.java │ │ ├── DoubleMapIndexed.java │ │ ├── ObjScan.java │ │ ├── IntScan.java │ │ ├── LongScan.java │ │ ├── ObjZip.java │ │ ├── DoubleScan.java │ │ ├── ObjDistinctBy.java │ │ ├── ObjDropWhile.java │ │ └── IntDropWhile.java │ │ ├── iterator │ │ ├── LsaIterator.java │ │ └── LazyIterator.java │ │ ├── internal │ │ └── Params.java │ │ └── Collector.java │ └── test │ └── java │ └── com │ └── annimon │ └── stream │ ├── streamtests │ ├── OfArrayTest.java │ ├── EmptyTest.java │ ├── ToListTest.java │ ├── IteratorTest.java │ ├── EqualsOnlyTest.java │ ├── FilterNotTest.java │ ├── AllMatchTest.java │ ├── AnyMatchTest.java │ ├── ReduceIndexedTest.java │ ├── NoneMatchTest.java │ ├── NullsOnlyTest.java │ ├── OfListTest.java │ ├── CountTest.java │ ├── GenerateTest.java │ ├── OfIteratorTest.java │ ├── ChunkByTest.java │ ├── SelectTest.java │ ├── WithoutNullsTest.java │ ├── FindFirstOrElseTest.java │ ├── PeekTest.java │ ├── TakeUntilTest.java │ ├── OfIterableTest.java │ ├── MapToIntTest.java │ ├── MapToLongTest.java │ ├── SampleTest.java │ ├── OfMapTest.java │ ├── MapToDoubleTest.java │ ├── FlatMapToIntTest.java │ ├── GroupByTest.java │ ├── FlatMapToDoubleTest.java │ ├── ToArrayTest.java │ └── FlatMapToLongTest.java │ ├── intstreamtests │ ├── EmptyTest.java │ ├── FindFirstOrElseTest.java │ ├── SumTest.java │ ├── OfIntTest.java │ ├── BoxedTest.java │ ├── FindFirstTest.java │ ├── FilterNotTest.java │ ├── MaxTest.java │ ├── MinTest.java │ ├── FindLastTest.java │ ├── GenerateTest.java │ ├── CountTest.java │ ├── CollectTest.java │ ├── MapToObjTest.java │ ├── MapToLongTest.java │ ├── ToArrayTest.java │ ├── OfArrayTest.java │ ├── MapToDoubleTest.java │ ├── AnyMatchTest.java │ ├── NoneMatchTest.java │ ├── ForEachTest.java │ ├── SampleTest.java │ ├── OfPrimitiveIteratorTest.java │ └── AllMatchTest.java │ ├── longstreamtests │ ├── EmptyTest.java │ ├── FindFirstOrElseTest.java │ ├── SumTest.java │ ├── CountTest.java │ ├── OfLongTest.java │ ├── BoxedTest.java │ ├── MinTest.java │ ├── MaxTest.java │ ├── IteratorTest.java │ ├── ToArrayTest.java │ ├── FindFirstTest.java │ ├── FindLastTest.java │ ├── AllMatchTest.java │ ├── AnyMatchTest.java │ ├── DistinctTest.java │ ├── NoneMatchTest.java │ ├── CollectTest.java │ ├── MapToIntTest.java │ ├── OfArrayTest.java │ ├── GenerateTest.java │ ├── FilterNotTest.java │ ├── MapTest.java │ ├── ForEachTest.java │ ├── MapToObjTest.java │ ├── MapToDoubleTest.java │ ├── PeekTest.java │ └── OfPrimitiveIteratorTest.java │ ├── doublestreamtests │ ├── EmptyTest.java │ ├── FindFirstOrElseTest.java │ ├── CountTest.java │ ├── SumTest.java │ ├── OfDoubleTest.java │ ├── BoxedTest.java │ ├── MaxTest.java │ ├── MinTest.java │ ├── DistinctTest.java │ ├── ToArrayTest.java │ ├── IteratorTest.java │ ├── FindFirstTest.java │ ├── AverageTest.java │ ├── MapToIntTest.java │ ├── FindLastTest.java │ ├── MapToLongTest.java │ ├── CollectTest.java │ ├── AllMatchTest.java │ ├── AnyMatchTest.java │ ├── NoneMatchTest.java │ ├── GenerateTest.java │ ├── OfArrayTest.java │ ├── FilterNotTest.java │ ├── MapTest.java │ ├── ForEachTest.java │ ├── SampleTest.java │ └── MapToObjTest.java │ ├── internal │ ├── OperatorsTest.java │ └── CompatTest.java │ └── function │ ├── LongUnaryOperatorTest.java │ ├── DoubleUnaryOperatorTest.java │ ├── IntUnaryOperatorTest.java │ ├── IndexedLongUnaryOperatorTest.java │ ├── IndexedDoubleUnaryOperatorTest.java │ ├── ThrowableFunctionTest.java │ ├── ThrowableConsumerTest.java │ └── ThrowablePredicateTest.java ├── streamTest ├── gradle.properties └── build.gradle ├── .gitignore ├── .travis.yml ├── settings.gradle └── .github └── workflows └── gradle.yml /versions.gradle: -------------------------------------------------------------------------------- 1 | ext { 2 | libVersion = "1.2.3-SNAPSHOT" 3 | } 4 | -------------------------------------------------------------------------------- /jitpack.yml: -------------------------------------------------------------------------------- 1 | jdk: 2 | - openjdk8 3 | install: 4 | - ./gradlew clean -xtest build publishToMavenLocal 5 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aNNiMON/Lightweight-Stream-API/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /stream/gradle.properties: -------------------------------------------------------------------------------- 1 | signing.keyId= 2 | signing.password= 3 | signing.secretKeyRingFile= 4 | nexusUsername= 5 | nexusPassword= -------------------------------------------------------------------------------- /streamTest/gradle.properties: -------------------------------------------------------------------------------- 1 | signing.keyId= 2 | signing.password= 3 | signing.secretKeyRingFile= 4 | nexusUsername= 5 | nexusPassword= -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStorePath=wrapper/dists 5 | zipStoreBase=GRADLE_USER_HOME 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | /dist/ 3 | /proguard/ 4 | /nbproject/private/ 5 | /nbproject/cfg_hints.xml 6 | /.nb-gradle/ 7 | /.gradle/ 8 | /stream/target/ 9 | /stream/build/ 10 | /stream/out/ 11 | /streamTest/target/ 12 | /streamTest/build/ 13 | /streamTest/out/ 14 | /.idea/ 15 | *.iml 16 | local.properties 17 | 18 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/BooleanSupplier.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a supplier of {@code boolean}-valued results. 5 | * 6 | * @since 1.1.8 7 | * @see Supplier 8 | */ 9 | public interface BooleanSupplier { 10 | 11 | /** 12 | * Gets a result. 13 | * 14 | * @return a result 15 | */ 16 | boolean getAsBoolean(); 17 | } 18 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/OfArrayTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Stream; 4 | import com.annimon.stream.test.hamcrest.StreamMatcher; 5 | import org.junit.Test; 6 | 7 | public final class OfArrayTest { 8 | 9 | @Test 10 | public void testStreamOfEmptyArray() { 11 | Stream.of(new String[0]) 12 | .custom(StreamMatcher.assertIsEmpty()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/EmptyTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import org.junit.Test; 5 | import static org.junit.Assert.assertEquals; 6 | 7 | public final class EmptyTest { 8 | 9 | @Test 10 | public void testStreamEmpty() { 11 | assertEquals(0, IntStream.empty().count()); 12 | assertEquals(0, IntStream.empty().iterator().nextInt()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/EmptyTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Stream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.StreamMatcher.isEmpty; 6 | import static org.hamcrest.MatcherAssert.assertThat; 7 | 8 | public final class EmptyTest { 9 | 10 | @Test 11 | public void testStreamEmpty() { 12 | assertThat(Stream.empty(), isEmpty()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/EmptyTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.LongStreamMatcher.isEmpty; 6 | import static org.hamcrest.MatcherAssert.assertThat; 7 | 8 | public final class EmptyTest { 9 | 10 | @Test 11 | public void testStreamEmpty() { 12 | assertThat(LongStream.empty(), isEmpty()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/ToListTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Stream; 4 | import org.junit.Test; 5 | import static org.hamcrest.MatcherAssert.assertThat; 6 | import static org.hamcrest.Matchers.contains; 7 | 8 | public final class ToListTest { 9 | 10 | @Test 11 | public void testToList() { 12 | assertThat(Stream.range(0, 5).toList(), 13 | contains(0, 1, 2, 3, 4)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/EmptyTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.DoubleStreamMatcher.isEmpty; 6 | import static org.hamcrest.MatcherAssert.assertThat; 7 | 8 | public final class EmptyTest { 9 | 10 | @Test 11 | public void testStreamEmpty() { 12 | assertThat(DoubleStream.empty(), isEmpty()); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/FindFirstOrElseTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import org.junit.Test; 5 | import static org.junit.Assert.assertEquals; 6 | 7 | public final class FindFirstOrElseTest { 8 | 9 | @Test 10 | public void testFindFirstOrElse() { 11 | assertEquals(42, IntStream.of(42).findFirstOrElse(10)); 12 | assertEquals(10, IntStream.empty().findFirstOrElse(10)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/SumTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import org.junit.Test; 5 | import static org.junit.Assert.assertEquals; 6 | 7 | public final class SumTest { 8 | 9 | @Test 10 | public void testSum() { 11 | assertEquals(IntStream.empty().sum(), 0); 12 | assertEquals(IntStream.of(42).sum(), 42); 13 | assertEquals(IntStream.rangeClosed(4, 8).sum(), 30); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/OfIntTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import org.junit.Test; 5 | import static org.junit.Assert.assertEquals; 6 | import static org.junit.Assert.assertTrue; 7 | 8 | public final class OfIntTest { 9 | 10 | @Test 11 | public void testStreamOfInt() { 12 | assertEquals(1, IntStream.of(42).count()); 13 | assertTrue(IntStream.of(42).findFirst().isPresent()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/FindFirstOrElseTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import org.junit.Test; 5 | import static org.junit.Assert.assertEquals; 6 | 7 | public final class FindFirstOrElseTest { 8 | 9 | @Test 10 | public void testFindFirstOrElse() { 11 | assertEquals(42, LongStream.of(42).findFirstOrElse(10)); 12 | assertEquals(10, LongStream.empty().findFirstOrElse(10)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/SumTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import org.junit.Test; 5 | import static org.hamcrest.MatcherAssert.assertThat; 6 | import static org.hamcrest.Matchers.is; 7 | 8 | public final class SumTest { 9 | 10 | @Test 11 | public void testSum() { 12 | assertThat(LongStream.of(100, 20, 3).sum(), is(123L)); 13 | assertThat(LongStream.empty().sum(), is(0L)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ObjIntConsumer.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents an operation on two input arguments. 5 | * 6 | * @param the type of the first argument 7 | * @see BiConsumer 8 | */ 9 | public interface ObjIntConsumer { 10 | 11 | /** 12 | * Performs operation on two arguments. 13 | * 14 | * @param t the first argument 15 | * @param value the second argument 16 | */ 17 | void accept(T t, int value); 18 | } 19 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ThrowableIntSupplier.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a supplier of {@code int}-valued results. 5 | * 6 | * @param the type of the exception 7 | * @since 1.1.7 8 | * @see IntSupplier 9 | */ 10 | public interface ThrowableIntSupplier { 11 | 12 | /** 13 | * Gets a result. 14 | * 15 | * @return a result 16 | * @throws E an exception 17 | */ 18 | int getAsInt() throws E; 19 | } 20 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/FindFirstOrElseTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import org.junit.Test; 5 | import static org.junit.Assert.assertEquals; 6 | 7 | public final class FindFirstOrElseTest { 8 | 9 | @Test 10 | public void testFindFirstOrElse() { 11 | assertEquals(42, DoubleStream.of(42).findFirstOrElse(10), 0.1); 12 | assertEquals(10, DoubleStream.empty().findFirstOrElse(10), 0.1); 13 | } 14 | } -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/internal/OperatorsTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.internal; 2 | 3 | import org.junit.Test; 4 | import static com.annimon.stream.test.hamcrest.CommonMatcher.hasOnlyPrivateConstructors; 5 | import static org.hamcrest.MatcherAssert.assertThat; 6 | 7 | /** 8 | * Tests for {@link Operators} 9 | */ 10 | public class OperatorsTest { 11 | 12 | @Test 13 | public void testPrivateConstructor() { 14 | assertThat(Operators.class, hasOnlyPrivateConstructors()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/CountTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import org.junit.Test; 5 | import static org.hamcrest.MatcherAssert.assertThat; 6 | import static org.hamcrest.Matchers.is; 7 | 8 | public final class CountTest { 9 | 10 | @Test 11 | public void testCount() { 12 | assertThat(LongStream.of(100, 20, 3).count(), is(3L)); 13 | assertThat(LongStream.empty().count(), is(0L)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/IntToLongFunction.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a function which produces an {@code long}-valued result from input argument. 5 | * 6 | * @since 1.1.4 7 | * @see Function 8 | */ 9 | public interface IntToLongFunction { 10 | 11 | /** 12 | * Applies this function to the given argument. 13 | * 14 | * @param value an argument 15 | * @return the function result 16 | */ 17 | long applyAsLong(int value); 18 | } 19 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/LongToIntFunction.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a function which produces an {@code int}-valued result from input argument. 5 | * 6 | * @since 1.1.4 7 | * @see Function 8 | */ 9 | public interface LongToIntFunction { 10 | 11 | /** 12 | * Applies this function to the given argument. 13 | * 14 | * @param value an argument 15 | * @return the function result 16 | */ 17 | int applyAsInt(long value); 18 | } 19 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ThrowableLongSupplier.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a supplier of {@code long}-valued results. 5 | * 6 | * @param the type of the exception 7 | * @since 1.1.7 8 | * @see LongSupplier 9 | */ 10 | public interface ThrowableLongSupplier { 11 | 12 | /** 13 | * Gets a result. 14 | * 15 | * @return a result 16 | * @throws E an exception 17 | */ 18 | long getAsLong() throws E; 19 | } 20 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | sudo: false 3 | install: true 4 | 5 | jdk: 6 | - openjdk8 7 | - oraclejdk11 8 | 9 | addons: 10 | sonarcloud: 11 | organization: "annimon-github" 12 | 13 | cache: 14 | directories: 15 | - $HOME/.m2 16 | - $HOME/.gradle 17 | - $HOME/.sonar/cache 18 | 19 | branches: 20 | except: 21 | - javame 22 | 23 | before_install: 24 | - chmod +x gradlew 25 | 26 | after_success: 27 | - ./gradlew jacocoTestReport coveralls sonarqube 28 | # - mvn cobertura:cobertura coveralls:report 29 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/DoubleToIntFunction.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a function which produces an {@code int}-valued result from input argument. 5 | * 6 | * @since 1.1.4 7 | * @see Function 8 | */ 9 | public interface DoubleToIntFunction { 10 | 11 | /** 12 | * Applies this function to the given argument. 13 | * 14 | * @param value an argument 15 | * @return the function result 16 | */ 17 | int applyAsInt(double value); 18 | } 19 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/DoubleToLongFunction.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a function which produces an {@code long}-valued result from input argument. 5 | * 6 | * @since 1.1.4 7 | * @see Function 8 | */ 9 | public interface DoubleToLongFunction { 10 | 11 | /** 12 | * Applies this function to the given argument. 13 | * 14 | * @param value an argument 15 | * @return the function result 16 | */ 17 | long applyAsLong(double value); 18 | } 19 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/IntToDoubleFunction.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a function which produces an {@code double}-valued result from input argument. 5 | * 6 | * @since 1.1.4 7 | * @see Function 8 | */ 9 | public interface IntToDoubleFunction { 10 | 11 | /** 12 | * Applies this function to the given argument. 13 | * 14 | * @param value an argument 15 | * @return the function result 16 | */ 17 | double applyAsDouble(int value); 18 | } 19 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ThrowableDoubleSupplier.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a supplier of {@code double}-valued results. 5 | * 6 | * @param the type of the exception 7 | * @since 1.1.7 8 | * @see DoubleSupplier 9 | */ 10 | public interface ThrowableDoubleSupplier { 11 | 12 | /** 13 | * Gets a result. 14 | * 15 | * @return a result 16 | * @throws E an exception 17 | */ 18 | double getAsDouble() throws E; 19 | } 20 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/CountTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import org.junit.Test; 5 | import static org.hamcrest.MatcherAssert.assertThat; 6 | import static org.hamcrest.Matchers.is; 7 | 8 | public final class CountTest { 9 | 10 | @Test 11 | public void testCount() { 12 | assertThat(DoubleStream.of(0.1, 0.02, 0.003).count(), is(3L)); 13 | assertThat(DoubleStream.empty().count(), is(0L)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/LongToDoubleFunction.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a function which produces an {@code double}-valued result from input argument. 5 | * 6 | * @since 1.1.4 7 | * @see Function 8 | */ 9 | public interface LongToDoubleFunction { 10 | 11 | /** 12 | * Applies this function to the given argument. 13 | * 14 | * @param value an argument 15 | * @return the function result 16 | */ 17 | double applyAsDouble(long value); 18 | } 19 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ObjLongConsumer.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents an operation on two input arguments. 5 | * 6 | * @param the type of the first argument 7 | * @since 1.1.4 8 | * @see BiConsumer 9 | */ 10 | public interface ObjLongConsumer { 11 | 12 | /** 13 | * Performs operation on two arguments. 14 | * 15 | * @param t the first argument 16 | * @param value the second argument 17 | */ 18 | void accept(T t, long value); 19 | } 20 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ObjDoubleConsumer.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents an operation on two input arguments. 5 | * 6 | * @param the type of the first argument 7 | * @since 1.1.4 8 | * @see BiConsumer 9 | */ 10 | public interface ObjDoubleConsumer { 11 | 12 | /** 13 | * Performs operation on two arguments. 14 | * 15 | * @param t the first argument 16 | * @param value the second argument 17 | */ 18 | void accept(T t, double value); 19 | } 20 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ThrowableSupplier.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a function for supplying result which can throw an exception. 5 | * 6 | * @param the type of the result 7 | * @param the type of the exception 8 | * @see Supplier 9 | */ 10 | public interface ThrowableSupplier { 11 | 12 | /** 13 | * Gets a result. 14 | * 15 | * @return a result 16 | * @throws E an exception 17 | */ 18 | T get() throws E; 19 | } 20 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ToIntFunction.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a function which produces an {@code int}-valued result from input argument. 5 | * 6 | * @param the type of the input of the function 7 | * @see Function 8 | */ 9 | public interface ToIntFunction { 10 | 11 | /** 12 | * Applies this function to the given argument. 13 | * 14 | * @param t an argument 15 | * @return the function result 16 | */ 17 | int applyAsInt(T t); 18 | } 19 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ToBooleanFunction.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a function which produces an {@code boolean}-valued result from input argument. 5 | * 6 | * @param the type of the input of the function 7 | * @see Function 8 | */ 9 | public interface ToBooleanFunction { 10 | 11 | /** 12 | * Applies this function to the given argument. 13 | * 14 | * @param t an argument 15 | * @return the function result 16 | */ 17 | boolean applyAsBoolean(T t); 18 | } 19 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/IteratorTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Stream; 4 | import org.junit.Test; 5 | import static org.hamcrest.CoreMatchers.not; 6 | import static org.hamcrest.CoreMatchers.nullValue; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | import static org.hamcrest.Matchers.is; 9 | 10 | public final class IteratorTest { 11 | 12 | @Test 13 | public void testIterator() { 14 | assertThat(Stream.of(1).iterator(), is(not(nullValue()))); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ToLongFunction.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a function which produces an {@code long}-valued result from input argument. 5 | * 6 | * @param the type of the input of the function 7 | * @since 1.1.3 8 | * @see Function 9 | */ 10 | public interface ToLongFunction { 11 | 12 | /** 13 | * Applies this function to the given argument. 14 | * 15 | * @param t an argument 16 | * @return the function result 17 | */ 18 | long applyAsLong(T t); 19 | } 20 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/OfLongTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.LongStreamMatcher.elements; 6 | import static org.hamcrest.MatcherAssert.assertThat; 7 | import static org.hamcrest.Matchers.arrayContaining; 8 | 9 | public final class OfLongTest { 10 | 11 | @Test 12 | public void testStreamOfLong() { 13 | assertThat(LongStream.of(1234), elements(arrayContaining(1234L))); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ThrowableIntConsumer.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents an operation on {@code int}-valued input argument. 5 | * 6 | * @param the type of the exception 7 | * @since 1.1.7 8 | * @see IntConsumer 9 | */ 10 | public interface ThrowableIntConsumer { 11 | 12 | /** 13 | * Performs operation on the given argument. 14 | * 15 | * @param value the input argument 16 | * @throws E an exception 17 | */ 18 | void accept(int value) throws E; 19 | } 20 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/BooleanFunction.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a function which produces result from {@code boolean}-valued input argument. 5 | * 6 | * @param the type of the result of the function 7 | * 8 | * @since 1.1.8 9 | * @see Function 10 | */ 11 | public interface BooleanFunction { 12 | 13 | /** 14 | * Applies this function to the given argument. 15 | * 16 | * @param value an argument 17 | * @return the function result 18 | */ 19 | R apply(boolean value); 20 | } -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ToDoubleFunction.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a function which produces an {@code double}-valued result from input argument. 5 | * 6 | * @param the type of the input of the function 7 | * @since 1.1.3 8 | * @see Function 9 | */ 10 | public interface ToDoubleFunction { 11 | 12 | /** 13 | * Applies this function to the given argument. 14 | * 15 | * @param t an argument 16 | * @return the function result 17 | */ 18 | double applyAsDouble(T t); 19 | } 20 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ThrowableLongConsumer.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents an operation on a {@code long}-valued input argument. 5 | * 6 | * @param the type of the exception 7 | * @since 1.1.7 8 | * @see LongConsumer 9 | */ 10 | public interface ThrowableLongConsumer { 11 | 12 | /** 13 | * Performs operation on the given argument. 14 | * 15 | * @param value the input argument 16 | * @throws E an exception 17 | */ 18 | void accept(long value) throws E; 19 | } 20 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/EqualsOnlyTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Stream; 4 | import java.util.List; 5 | import org.junit.Test; 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public final class EqualsOnlyTest { 9 | 10 | @Test 11 | public void testEqualsOnly() { 12 | List match = Stream.of("a", "b", "c") 13 | .equalsOnly("b") 14 | .toList(); 15 | assertEquals(1, match.size()); 16 | assertEquals("b", match.get(0)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/SumTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import org.junit.Test; 5 | import static org.hamcrest.MatcherAssert.assertThat; 6 | import static org.hamcrest.Matchers.closeTo; 7 | import static org.hamcrest.Matchers.is; 8 | 9 | public final class SumTest { 10 | 11 | @Test 12 | public void testSum() { 13 | assertThat(DoubleStream.of(0.1, 0.02, 0.003).sum(), closeTo(0.123, 0.0001)); 14 | assertThat(DoubleStream.empty().sum(), is(0d)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ThrowableDoubleConsumer.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents an operation on a {@code double}-valued input argument. 5 | * 6 | * @param the type of the exception 7 | * @since 1.1.7 8 | * @see DoubleConsumer 9 | */ 10 | public interface ThrowableDoubleConsumer { 11 | 12 | /** 13 | * Performs operation on the given argument. 14 | * 15 | * @param value the input argument 16 | * @throws E an exception 17 | */ 18 | void accept(double value) throws E; 19 | } 20 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ThrowableConsumer.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents an operation on input argument and can throw an exception. 5 | * 6 | * @param the type of the input to the operation 7 | * @param the type of the exception 8 | * @see Consumer 9 | */ 10 | public interface ThrowableConsumer { 11 | 12 | /** 13 | * Performs operation on argument. 14 | * 15 | * @param value the input argument 16 | * @throws E an exception 17 | */ 18 | void accept(T value) throws E; 19 | } 20 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/BoxedTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.StreamMatcher.assertElements; 6 | import static org.hamcrest.Matchers.contains; 7 | 8 | public final class BoxedTest { 9 | 10 | @Test 11 | public void testBoxed() { 12 | IntStream.of(1, 10, 20) 13 | .boxed() 14 | .custom(assertElements(contains( 15 | 1, 10, 20 16 | ))); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/OfDoubleTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.DoubleStreamMatcher.assertElements; 6 | import static org.hamcrest.Matchers.arrayContaining; 7 | 8 | public final class OfDoubleTest { 9 | 10 | @Test 11 | public void testStreamOfDouble() { 12 | DoubleStream.of(1.234) 13 | .custom(assertElements(arrayContaining( 14 | 1.234 15 | ))); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/BoxedTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.StreamMatcher.assertElements; 6 | import static org.hamcrest.Matchers.contains; 7 | 8 | public final class BoxedTest { 9 | 10 | @Test 11 | public void testBoxed() { 12 | LongStream.of(10L, 20L, 30L) 13 | .boxed() 14 | .custom(assertElements(contains( 15 | 10L, 20L, 30L 16 | ))); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/LongBinaryOperator.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents an operation on two {@code long}-valued operands 5 | * that produces a {@code long}-valued result. 6 | * 7 | * @since 1.1.4 8 | * @see BinaryOperator 9 | */ 10 | public interface LongBinaryOperator { 11 | 12 | /** 13 | * Applies this operator to the given operands. 14 | * 15 | * @param left the first operand 16 | * @param right the second operand 17 | * @return the operator result 18 | */ 19 | long applyAsLong(long left, long right); 20 | } 21 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/BoxedTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.StreamMatcher.assertElements; 6 | import static org.hamcrest.Matchers.contains; 7 | 8 | public final class BoxedTest { 9 | 10 | @Test 11 | public void testBoxed() { 12 | DoubleStream.of(0.1, 0.2, 0.3) 13 | .boxed() 14 | .custom(assertElements(contains( 15 | 0.1, 0.2, 0.3 16 | ))); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/DoubleBinaryOperator.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents an operation on two {@code double}-valued operands 5 | * that produces a {@code double}-valued result. 6 | * 7 | * @since 1.1.4 8 | * @see BinaryOperator 9 | */ 10 | public interface DoubleBinaryOperator { 11 | 12 | /** 13 | * Applies this operator to the given operands. 14 | * 15 | * @param left the first operand 16 | * @param right the second operand 17 | * @return the operator result 18 | */ 19 | double applyAsDouble(double left, double right); 20 | } 21 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/MinTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.OptionalLongMatcher.hasValue; 6 | import static com.annimon.stream.test.hamcrest.OptionalLongMatcher.isEmpty; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | 9 | public final class MinTest { 10 | 11 | @Test 12 | public void testMin() { 13 | assertThat(LongStream.of(100, 20, 3).min(), hasValue(3L)); 14 | assertThat(LongStream.empty().min(), isEmpty()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/MaxTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.OptionalLongMatcher.hasValue; 6 | import static com.annimon.stream.test.hamcrest.OptionalLongMatcher.isEmpty; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | 9 | public final class MaxTest { 10 | 11 | @Test 12 | public void testMax() { 13 | assertThat(LongStream.of(100, 20, 3).max(), hasValue(100L)); 14 | assertThat(LongStream.empty().max(), isEmpty()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/IteratorTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import org.junit.Test; 5 | import static org.hamcrest.MatcherAssert.assertThat; 6 | import static org.hamcrest.Matchers.is; 7 | 8 | public final class IteratorTest { 9 | 10 | @Test 11 | public void testIterator() { 12 | assertThat(LongStream.of(1234567L).iterator().nextLong(), is(1234567L)); 13 | 14 | assertThat(LongStream.empty().iterator().hasNext(), is(false)); 15 | 16 | assertThat(LongStream.empty().iterator().nextLong(), is(0L)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/MaxTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.OptionalDoubleMatcher.hasValue; 6 | import static com.annimon.stream.test.hamcrest.OptionalDoubleMatcher.isEmpty; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | 9 | public final class MaxTest { 10 | 11 | @Test 12 | public void testMax() { 13 | assertThat(DoubleStream.of(0.1, 0.02, 0.003).max(), hasValue(0.1)); 14 | assertThat(DoubleStream.empty().max(), isEmpty()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/MinTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.OptionalDoubleMatcher.hasValue; 6 | import static com.annimon.stream.test.hamcrest.OptionalDoubleMatcher.isEmpty; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | 9 | public final class MinTest { 10 | 11 | @Test 12 | public void testMin() { 13 | assertThat(DoubleStream.of(0.1, 0.02, 0.003).min(), hasValue(0.003)); 14 | assertThat(DoubleStream.empty().min(), isEmpty()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/FindFirstTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import org.junit.Test; 5 | import static org.junit.Assert.assertEquals; 6 | import static org.junit.Assert.assertFalse; 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public final class FindFirstTest { 10 | 11 | @Test 12 | public void testFindFirst() { 13 | assertFalse(IntStream.empty().findFirst().isPresent()); 14 | assertEquals(IntStream.of(42).findFirst().getAsInt(), 42); 15 | assertTrue(IntStream.rangeClosed(2, 5).findFirst().isPresent()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/FilterNotTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.Stream; 5 | import org.junit.Test; 6 | import static com.annimon.stream.test.hamcrest.StreamMatcher.assertElements; 7 | import static org.hamcrest.Matchers.contains; 8 | 9 | public final class FilterNotTest { 10 | 11 | @Test 12 | public void testFilterNot() { 13 | Stream.range(0, 10) 14 | .filterNot(Functions.remainder(2)) 15 | .custom(assertElements(contains( 16 | 1, 3, 5, 7, 9 17 | ))); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/ToArrayTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import org.junit.Test; 5 | import static org.hamcrest.MatcherAssert.assertThat; 6 | import static org.hamcrest.Matchers.is; 7 | 8 | public final class ToArrayTest { 9 | 10 | @Test 11 | public void testToArray() { 12 | assertThat(LongStream.of(12L, 32L, 22L, 9L).toArray(), 13 | is(new long[] {12L, 32L, 22L, 9L})); 14 | } 15 | 16 | @Test 17 | public void testToArrayOnEmptyStream() { 18 | assertThat(LongStream.empty().toArray().length, is(0)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/ObjArray.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.iterator.LsaIterator; 4 | import org.jetbrains.annotations.NotNull; 5 | 6 | public class ObjArray extends LsaIterator { 7 | 8 | private final T[] elements; 9 | private int index; 10 | 11 | public ObjArray(@NotNull T[] elements) { 12 | this.elements = elements; 13 | index = 0; 14 | } 15 | 16 | @Override 17 | public boolean hasNext() { 18 | return index < elements.length; 19 | } 20 | 21 | @Override 22 | public T nextIteration() { 23 | return elements[index++]; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/IntArray.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.iterator.PrimitiveIterator; 4 | import org.jetbrains.annotations.NotNull; 5 | 6 | public class IntArray extends PrimitiveIterator.OfInt { 7 | 8 | private final int[] values; 9 | private int index; 10 | 11 | public IntArray(@NotNull int[] values) { 12 | this.values = values; 13 | index = 0; 14 | } 15 | 16 | @Override 17 | public boolean hasNext() { 18 | return index < values.length; 19 | } 20 | 21 | @Override 22 | public int nextInt() { 23 | return values[index++]; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/ObjGenerate.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.Supplier; 4 | import com.annimon.stream.iterator.LsaIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class ObjGenerate extends LsaIterator { 8 | 9 | private final Supplier supplier; 10 | 11 | public ObjGenerate(@NotNull Supplier supplier) { 12 | this.supplier = supplier; 13 | } 14 | 15 | @Override 16 | public boolean hasNext() { 17 | return true; 18 | } 19 | 20 | @Override 21 | public T nextIteration() { 22 | return supplier.get(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'Lightweight-Stream-API' 2 | 3 | // Find the directories containing a "build.gradle" file in the root directory 4 | // of the project. That is, every directory containing a "build.gradle" will 5 | // be automatically the subproject of this project. 6 | def subDirs = rootDir.listFiles(new FileFilter() { 7 | public boolean accept(File file) { 8 | if (!file.isDirectory()) { 9 | return false 10 | } 11 | if (file.name == 'buildSrc') { 12 | return false 13 | } 14 | return new File(file, 'build.gradle').isFile() 15 | } 16 | }) 17 | 18 | subDirs.each { File dir -> 19 | include dir.name 20 | } 21 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/LongArray.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.iterator.PrimitiveIterator; 4 | import org.jetbrains.annotations.NotNull; 5 | 6 | public class LongArray extends PrimitiveIterator.OfLong { 7 | 8 | private final long[] values; 9 | private int index; 10 | 11 | public LongArray(@NotNull long[] values) { 12 | this.values = values; 13 | index = 0; 14 | } 15 | 16 | @Override 17 | public long nextLong() { 18 | return values[index++]; 19 | } 20 | 21 | @Override 22 | public boolean hasNext() { 23 | return index < values.length; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/DistinctTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.DoubleStreamMatcher.assertElements; 6 | import static org.hamcrest.Matchers.arrayContaining; 7 | 8 | public final class DistinctTest { 9 | 10 | @Test 11 | public void testDistinct() { 12 | DoubleStream.of(0.09, 1.2, 0, 2.2, 0.09, 1.2, 3.2, 0.09) 13 | .distinct() 14 | .custom(assertElements(arrayContaining( 15 | 0.09, 1.2, 0d, 2.2, 3.2 16 | ))); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ThrowableIntPredicate.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents an {@code int}-valued predicate (function with boolean type result). 5 | * 6 | * @param the type of the exception 7 | * @since 1.1.7 8 | * @see IntPredicate 9 | */ 10 | public interface ThrowableIntPredicate { 11 | 12 | /** 13 | * Tests the value for satisfying predicate. 14 | * 15 | * @param value the value to be tested 16 | * @return {@code true} if the value matches the predicate, otherwise {@code false} 17 | * @throws E an exception 18 | */ 19 | boolean test(int value) throws E; 20 | } 21 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ThrowableLongPredicate.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a {@code long}-valued predicate (function with boolean type result). 5 | * 6 | * @param the type of the exception 7 | * @since 1.1.7 8 | * @see LongPredicate 9 | */ 10 | public interface ThrowableLongPredicate { 11 | 12 | /** 13 | * Tests the value for satisfying predicate. 14 | * 15 | * @param value the value to be tested 16 | * @return {@code true} if the value matches the predicate, otherwise {@code false} 17 | * @throws E an exception 18 | */ 19 | boolean test(long value) throws E; 20 | } 21 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/IntGenerate.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.IntSupplier; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class IntGenerate extends PrimitiveIterator.OfInt { 8 | 9 | private final IntSupplier supplier; 10 | 11 | public IntGenerate(@NotNull IntSupplier supplier) { 12 | this.supplier = supplier; 13 | } 14 | 15 | @Override 16 | public boolean hasNext() { 17 | return true; 18 | } 19 | 20 | @Override 21 | public int nextInt() { 22 | return supplier.getAsInt(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ThrowableIntFunction.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a function which produces result from {@code int}-valued input argument. 5 | * 6 | * @param the type of the result of the function 7 | * @param the type of the exception 8 | * 9 | * @since 1.1.7 10 | * @see IntFunction 11 | */ 12 | public interface ThrowableIntFunction { 13 | 14 | /** 15 | * Applies this function to the given argument. 16 | * 17 | * @param value the function argument 18 | * @return the function result 19 | * @throws E an exception 20 | */ 21 | R apply(int value) throws E; 22 | } -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/DoubleArray.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.iterator.PrimitiveIterator; 4 | import org.jetbrains.annotations.NotNull; 5 | 6 | public class DoubleArray extends PrimitiveIterator.OfDouble { 7 | 8 | private final double[] values; 9 | private int index; 10 | 11 | public DoubleArray(@NotNull double[] values) { 12 | this.values = values; 13 | index = 0; 14 | } 15 | 16 | @Override 17 | public double nextDouble() { 18 | return values[index++]; 19 | } 20 | 21 | @Override 22 | public boolean hasNext() { 23 | return index < values.length; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ThrowableDoublePredicate.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a {@code double}-valued predicate (function with boolean type result). 5 | * 6 | * @param the type of the exception 7 | * @since 1.1.7 8 | * @see DoublePredicate 9 | */ 10 | public interface ThrowableDoublePredicate { 11 | 12 | /** 13 | * Tests the value for satisfying predicate. 14 | * 15 | * @param value the value to be tested 16 | * @return {@code true} if the value matches the predicate, otherwise {@code false} 17 | * @throws E an exception 18 | */ 19 | boolean test(double value) throws E; 20 | } 21 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ThrowableLongFunction.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a function which produces result from {@code long}-valued input argument. 5 | * 6 | * @param the type of the result of the function 7 | * @param the type of the exception 8 | * 9 | * @since 1.1.7 10 | * @see LongFunction 11 | */ 12 | public interface ThrowableLongFunction { 13 | 14 | /** 15 | * Applies this function to the given argument. 16 | * 17 | * @param value the function argument 18 | * @return the function result 19 | * @throws E an exception 20 | */ 21 | R apply(long value) throws E; 22 | } -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/LongGenerate.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.LongSupplier; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class LongGenerate extends PrimitiveIterator.OfLong { 8 | 9 | private final LongSupplier supplier; 10 | 11 | public LongGenerate(@NotNull LongSupplier supplier) { 12 | this.supplier = supplier; 13 | } 14 | 15 | @Override 16 | public boolean hasNext() { 17 | return true; 18 | } 19 | 20 | @Override 21 | public long nextLong() { 22 | return supplier.getAsLong(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/FilterNotTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.IntStream; 5 | import org.junit.Test; 6 | import static com.annimon.stream.test.hamcrest.IntStreamMatcher.assertElements; 7 | import static org.hamcrest.Matchers.arrayContaining; 8 | 9 | public final class FilterNotTest { 10 | 11 | @Test 12 | public void testFilterNot() { 13 | IntStream.rangeClosed(1, 10) 14 | .filterNot(Functions.remainderInt(2)) 15 | .custom(assertElements(arrayContaining( 16 | 1, 3, 5, 7, 9 17 | ))); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/ToArrayTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import org.junit.Test; 5 | import static org.hamcrest.MatcherAssert.assertThat; 6 | import static org.hamcrest.Matchers.is; 7 | 8 | public final class ToArrayTest { 9 | 10 | @Test 11 | public void testToArray() { 12 | assertThat(DoubleStream.of(0.012, 10.347, 3.039, 19.84, 100d).toArray(), 13 | is(new double[] {0.012, 10.347, 3.039, 19.84, 100d})); 14 | } 15 | 16 | @Test 17 | public void testToArrayOnEmptyStream() { 18 | assertThat(DoubleStream.empty().toArray().length, is(0)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ThrowableDoubleFunction.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a function which produces result from {@code double}-valued input argument. 5 | * 6 | * @param the type of the result of the function 7 | * @param the type of the exception 8 | * 9 | * @since 1.1.7 10 | * @see DoubleFunction 11 | */ 12 | public interface ThrowableDoubleFunction { 13 | 14 | /** 15 | * Applies this function to the given argument. 16 | * 17 | * @param value the function argument 18 | * @return the function result 19 | * @throws E an exception 20 | */ 21 | R apply(double value) throws E; 22 | } -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ThrowableFunction.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a function which produces result from input arguments and can throw an exception. 5 | * 6 | * @param the type of the input of the function 7 | * @param the type of the result of the function 8 | * @param the type of the exception 9 | * @see Function 10 | */ 11 | public interface ThrowableFunction { 12 | 13 | /** 14 | * Applies this function to the given argument. 15 | * 16 | * @param i an argument 17 | * @return the function result 18 | * @throws E an exception 19 | */ 20 | R apply(I i) throws E; 21 | } 22 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/FindFirstTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.OptionalLongMatcher.hasValue; 6 | import static com.annimon.stream.test.hamcrest.OptionalLongMatcher.isEmpty; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | 9 | public final class FindFirstTest { 10 | 11 | @Test 12 | public void testFindFirst() { 13 | assertThat(LongStream.of(3, 10, 19, 4, 50).findFirst(), 14 | hasValue(3L)); 15 | 16 | assertThat(LongStream.empty().findFirst(), 17 | isEmpty()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/IteratorTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import org.junit.Test; 5 | import static org.hamcrest.MatcherAssert.assertThat; 6 | import static org.hamcrest.Matchers.closeTo; 7 | import static org.hamcrest.Matchers.is; 8 | 9 | public final class IteratorTest { 10 | 11 | @Test 12 | public void testIterator() { 13 | assertThat(DoubleStream.of(1.0123).iterator().nextDouble(), closeTo(1.0123, 0.0001)); 14 | 15 | assertThat(DoubleStream.empty().iterator().hasNext(), is(false)); 16 | 17 | assertThat(DoubleStream.empty().iterator().nextDouble(), is(0d)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/DoubleGenerate.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.DoubleSupplier; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class DoubleGenerate extends PrimitiveIterator.OfDouble { 8 | 9 | private final DoubleSupplier supplier; 10 | 11 | public DoubleGenerate(@NotNull DoubleSupplier supplier) { 12 | this.supplier = supplier; 13 | } 14 | 15 | @Override 16 | public boolean hasNext() { 17 | return true; 18 | } 19 | 20 | @Override 21 | public double nextDouble() { 22 | return supplier.getAsDouble(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/ThrowablePredicate.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents a predicate (function with boolean type result) which can throw an exception. 5 | * 6 | * @param the type of the input to the function 7 | * @param the type of the exception 8 | * @see Predicate 9 | */ 10 | public interface ThrowablePredicate { 11 | 12 | /** 13 | * Tests the value for satisfying predicate. 14 | * 15 | * @param value the value to be tested 16 | * @return {@code true} if the value matches the predicate, otherwise {@code false} 17 | * @throws E an exception 18 | */ 19 | boolean test(T value) throws E; 20 | } 21 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/FindFirstTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.OptionalDoubleMatcher.hasValue; 6 | import static com.annimon.stream.test.hamcrest.OptionalDoubleMatcher.isEmpty; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | 9 | public final class FindFirstTest { 10 | 11 | @Test 12 | public void testFindFirst() { 13 | assertThat(DoubleStream.of(0.012, 10.347, 3.039, 19.84, 100d).findFirst(), 14 | hasValue(0.012)); 15 | 16 | assertThat(DoubleStream.empty().findFirst(), 17 | isEmpty()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/iterator/LsaIterator.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.iterator; 2 | 3 | import java.util.Iterator; 4 | import java.util.NoSuchElementException; 5 | 6 | /** 7 | * Abstract iterator with implemented {@code remove} method. 8 | * @param the type of the elements 9 | */ 10 | public abstract class LsaIterator implements Iterator { 11 | 12 | @Override 13 | public void remove() { 14 | throw new UnsupportedOperationException("remove not supported"); 15 | } 16 | 17 | @Override 18 | public final T next() { 19 | if (!hasNext()) { 20 | throw new NoSuchElementException(); 21 | } 22 | return nextIteration(); 23 | } 24 | 25 | public abstract T nextIteration(); 26 | } 27 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/MaxTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import org.junit.Test; 5 | import static org.junit.Assert.assertEquals; 6 | import static org.junit.Assert.assertFalse; 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public final class MaxTest { 10 | 11 | @Test 12 | public void testMax() { 13 | assertFalse(IntStream.empty().max().isPresent()); 14 | 15 | assertTrue(IntStream.of(42).max().isPresent()); 16 | assertEquals(IntStream.of(42).max().getAsInt(), 42); 17 | 18 | assertEquals(IntStream.of(-1, -2, -3, -2, -3, -5, -2, Integer.MIN_VALUE, Integer.MAX_VALUE) 19 | .max().getAsInt(), Integer.MAX_VALUE); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/MinTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import org.junit.Test; 5 | import static org.junit.Assert.assertEquals; 6 | import static org.junit.Assert.assertFalse; 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public final class MinTest { 10 | 11 | @Test 12 | public void testMin() { 13 | assertFalse(IntStream.empty().min().isPresent()); 14 | 15 | assertTrue(IntStream.of(42).min().isPresent()); 16 | assertEquals(IntStream.of(42).min().getAsInt(), 42); 17 | 18 | assertEquals(IntStream.of(-1, -2, -3, -2, -3, -5, -2, Integer.MIN_VALUE, Integer.MAX_VALUE) 19 | .min().getAsInt(), Integer.MIN_VALUE); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/AverageTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.OptionalDoubleMatcher.hasValueThat; 6 | import static com.annimon.stream.test.hamcrest.OptionalDoubleMatcher.isEmpty; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | import static org.hamcrest.Matchers.closeTo; 9 | 10 | public final class AverageTest { 11 | 12 | @Test 13 | public void testAverage() { 14 | assertThat( 15 | DoubleStream.of(0.1, 0.02, 0.003).average(), 16 | hasValueThat(closeTo(0.041, 0.00001))); 17 | 18 | assertThat(DoubleStream.empty().average(), isEmpty()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/FindLastTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.OptionalIntMatcher.hasValue; 6 | import static com.annimon.stream.test.hamcrest.OptionalIntMatcher.isEmpty; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | 9 | public final class FindLastTest { 10 | 11 | @Test 12 | public void testFindLast() { 13 | assertThat(IntStream.of(3, 10, 19, 4, 50).findLast(), 14 | hasValue(50)); 15 | 16 | assertThat(IntStream.of(50).findLast(), 17 | hasValue(50)); 18 | 19 | assertThat(IntStream.empty().findFirst(), 20 | isEmpty()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/AllMatchTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.Stream; 5 | import org.junit.Test; 6 | import static org.junit.Assert.assertFalse; 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public final class AllMatchTest { 10 | 11 | @Test 12 | public void testAllMatchWithFalseResult() { 13 | boolean match = Stream.range(0, 10) 14 | .allMatch(Functions.remainder(2)); 15 | assertFalse(match); 16 | } 17 | 18 | @Test 19 | public void testAllMatchWithTrueResult() { 20 | boolean match = Stream.of(2, 4, 6, 8, 10) 21 | .allMatch(Functions.remainder(2)); 22 | assertTrue(match); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/AnyMatchTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.Stream; 5 | import org.junit.Test; 6 | import static org.junit.Assert.assertFalse; 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public final class AnyMatchTest { 10 | 11 | @Test 12 | public void testAnyMatchWithTrueResult() { 13 | boolean match = Stream.range(0, 10) 14 | .anyMatch(Functions.remainder(2)); 15 | assertTrue(match); 16 | } 17 | 18 | @Test 19 | public void testAnyMatchWithFalseResult() { 20 | boolean match = Stream.of(2, 3, 5, 8, 13) 21 | .anyMatch(Functions.remainder(10)); 22 | assertFalse(match); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/ReduceIndexedTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.Stream; 5 | import org.junit.Test; 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public final class ReduceIndexedTest { 9 | 10 | @Test 11 | public void testReduceIndexed() { 12 | int result = Stream.rangeClosed(1, 5) 13 | .reduceIndexed(10, Functions.indexedAddition()); 14 | assertEquals(35, result); 15 | } 16 | 17 | @Test 18 | public void testReduceIndexedWithStartAndStep() { 19 | int result = Stream.rangeClosed(1, 5) 20 | .reduceIndexed(1, 2, 0, Functions.indexedAddition()); 21 | assertEquals(40, result); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/NoneMatchTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.Stream; 5 | import org.junit.Test; 6 | import static org.junit.Assert.assertFalse; 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public final class NoneMatchTest { 10 | 11 | @Test 12 | public void testNoneMatchWithFalseResult() { 13 | boolean match = Stream.range(0, 10) 14 | .noneMatch(Functions.remainder(2)); 15 | assertFalse(match); 16 | } 17 | 18 | @Test 19 | public void testNoneMatchWithTrueResult() { 20 | boolean match = Stream.of(2, 3, 5, 8, 13) 21 | .noneMatch(Functions.remainder(10)); 22 | assertTrue(match); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/function/LongUnaryOperatorTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | import org.junit.Test; 4 | import static com.annimon.stream.test.hamcrest.CommonMatcher.hasOnlyPrivateConstructors; 5 | import static org.hamcrest.MatcherAssert.assertThat; 6 | import static org.hamcrest.Matchers.is; 7 | 8 | /** 9 | * Tests {@link LongUnaryOperator} 10 | */ 11 | public class LongUnaryOperatorTest { 12 | 13 | @Test 14 | public void testIdentity() { 15 | LongUnaryOperator identity = LongUnaryOperator.Util.identity(); 16 | 17 | assertThat(identity.applyAsLong(3228), is(3228L)); 18 | } 19 | 20 | @Test 21 | public void testPrivateUtilConstructor() { 22 | assertThat(LongUnaryOperator.Util.class, hasOnlyPrivateConstructors()); 23 | } 24 | } -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/FindLastTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.OptionalLongMatcher.hasValue; 6 | import static com.annimon.stream.test.hamcrest.OptionalLongMatcher.isEmpty; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | 9 | public final class FindLastTest { 10 | 11 | @Test 12 | public void testFindLast() { 13 | assertThat(LongStream.of(3, 10, 19, 4, 50).findLast(), 14 | hasValue(50L)); 15 | 16 | assertThat(LongStream.of(50).findLast(), 17 | hasValue(50L)); 18 | 19 | assertThat(LongStream.empty().findLast(), 20 | isEmpty()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/internal/Params.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.internal; 2 | 3 | import org.jetbrains.annotations.NotNull; 4 | 5 | /** 6 | * Parameters for streams. 7 | */ 8 | public final class Params { 9 | 10 | public Runnable closeHandler; 11 | 12 | public static Params wrapWithCloseHandler(Params params, @NotNull Runnable closeHandler) { 13 | final Params newParams; 14 | if (params == null) { 15 | newParams = new Params(); 16 | newParams.closeHandler = closeHandler; 17 | } else { 18 | newParams = params; 19 | final Runnable firstHandler = newParams.closeHandler; 20 | newParams.closeHandler = Compose.runnables(firstHandler, closeHandler); 21 | } 22 | return newParams; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/IntIterate.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.IntUnaryOperator; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class IntIterate extends PrimitiveIterator.OfInt { 8 | 9 | private final IntUnaryOperator op; 10 | private int current; 11 | 12 | public IntIterate(int seed, @NotNull IntUnaryOperator f) { 13 | this.op = f; 14 | current = seed; 15 | } 16 | 17 | @Override 18 | public boolean hasNext() { 19 | return true; 20 | } 21 | 22 | @Override 23 | public int nextInt() { 24 | final int old = current; 25 | current = op.applyAsInt(current); 26 | return old; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/GenerateTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import com.annimon.stream.function.IntSupplier; 5 | import org.junit.Test; 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public final class GenerateTest { 9 | 10 | @Test 11 | public void testStreamGenerate() { 12 | IntSupplier s = new IntSupplier() { 13 | @Override 14 | public int getAsInt() { 15 | return 42; 16 | } 17 | }; 18 | 19 | assertEquals(42, IntStream.generate(s).findFirst().getAsInt()); 20 | } 21 | 22 | @Test(expected = NullPointerException.class) 23 | public void testStreamGenerateNull() { 24 | IntStream.generate(null); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/MapToIntTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import com.annimon.stream.function.DoubleToIntFunction; 5 | import org.junit.Test; 6 | import static org.hamcrest.MatcherAssert.assertThat; 7 | import static org.hamcrest.Matchers.is; 8 | 9 | public final class MapToIntTest { 10 | 11 | @Test 12 | public void testMapToInt() { 13 | DoubleToIntFunction mapper = new DoubleToIntFunction() { 14 | @Override 15 | public int applyAsInt(double value) { 16 | return (int) (value * 10); 17 | } 18 | }; 19 | assertThat(DoubleStream.of(0.2, 0.3, 0.4).mapToInt(mapper).toArray(), 20 | is(new int[] {2, 3, 4})); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/NullsOnlyTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Stream; 4 | import com.annimon.stream.function.Function; 5 | import org.junit.Test; 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public final class NullsOnlyTest { 9 | 10 | @Test 11 | public void testNullsOnly() { 12 | final long nullsAmount = Stream.range(0, 10) 13 | .map(new Function() { 14 | @Override 15 | public String apply(Integer integer) { 16 | return integer % 3 == 0 ? null : ""; 17 | } 18 | }) 19 | .nullsOnly() 20 | .count(); 21 | assertEquals(4, nullsAmount); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/LongIterate.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.LongUnaryOperator; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class LongIterate extends PrimitiveIterator.OfLong { 8 | 9 | private final LongUnaryOperator op; 10 | private long current; 11 | 12 | public LongIterate(long seed, @NotNull LongUnaryOperator f) { 13 | this.op = f; 14 | current = seed; 15 | } 16 | 17 | @Override 18 | public boolean hasNext() { 19 | return true; 20 | } 21 | 22 | @Override 23 | public long nextLong() { 24 | final long old = current; 25 | current = op.applyAsLong(current); 26 | return old; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/OfListTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Collectors; 4 | import com.annimon.stream.Stream; 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import org.junit.Test; 8 | import static org.hamcrest.MatcherAssert.assertThat; 9 | import static org.hamcrest.Matchers.is; 10 | 11 | public final class OfListTest { 12 | 13 | @Test 14 | public void testStreamOfList() { 15 | final List list = new ArrayList(4); 16 | list.add("This"); 17 | list.add(" is "); 18 | list.add("a"); 19 | list.add(" test"); 20 | 21 | String result = Stream.of(list) 22 | .collect(Collectors.joining()); 23 | assertThat(result, is("This is a test")); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/CountTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Stream; 4 | import org.junit.Test; 5 | import static org.junit.Assert.assertEquals; 6 | 7 | public final class CountTest { 8 | 9 | @Test 10 | public void testCount() { 11 | long count = Stream.range(10000000000L, 10000002000L).count(); 12 | assertEquals(2000, count); 13 | } 14 | 15 | @Test 16 | public void testCountMinValue() { 17 | long count = Stream.range(Integer.MIN_VALUE, Integer.MIN_VALUE + 100).count(); 18 | assertEquals(100, count); 19 | } 20 | 21 | @Test 22 | public void testCountMaxValue() { 23 | long count = Stream.range(Long.MAX_VALUE - 100, Long.MAX_VALUE).count(); 24 | assertEquals(100, count); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/GenerateTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.Stream; 5 | import org.junit.Test; 6 | import static com.annimon.stream.test.hamcrest.StreamMatcher.assertElements; 7 | import static org.hamcrest.Matchers.contains; 8 | 9 | public final class GenerateTest { 10 | 11 | @Test 12 | public void testGenerate() { 13 | Stream.generate(Functions.fibonacci()) 14 | .limit(10) 15 | .custom(assertElements(contains( 16 | 0L, 1L, 1L, 2L, 3L, 5L, 8L, 13L, 21L, 34L 17 | ))); 18 | } 19 | 20 | @Test(expected = NullPointerException.class) 21 | public void testGenerateNull() { 22 | Stream.generate(null); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/FindLastTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.OptionalDoubleMatcher.hasValue; 6 | import static com.annimon.stream.test.hamcrest.OptionalDoubleMatcher.isEmpty; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | 9 | public final class FindLastTest { 10 | 11 | @Test 12 | public void testFindLast() { 13 | assertThat(DoubleStream.of(0.012, 10.347, 3.039, 19.84, 100d).findLast(), 14 | hasValue(100d)); 15 | 16 | assertThat(DoubleStream.of(100d).findLast(), 17 | hasValue(100d)); 18 | 19 | assertThat(DoubleStream.empty().findLast(), 20 | isEmpty()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/function/DoubleUnaryOperatorTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | import org.junit.Test; 4 | import static com.annimon.stream.test.hamcrest.CommonMatcher.hasOnlyPrivateConstructors; 5 | import static org.hamcrest.MatcherAssert.assertThat; 6 | import static org.hamcrest.Matchers.closeTo; 7 | 8 | /** 9 | * Tests {@link DoubleUnaryOperator} 10 | */ 11 | public class DoubleUnaryOperatorTest { 12 | 13 | @Test 14 | public void testIdentity() { 15 | DoubleUnaryOperator identity = DoubleUnaryOperator.Util.identity(); 16 | 17 | assertThat(identity.applyAsDouble(3.228), closeTo(3.228, 0.001)); 18 | } 19 | 20 | @Test 21 | public void testPrivateUtilConstructor() { 22 | assertThat(DoubleUnaryOperator.Util.class, hasOnlyPrivateConstructors()); 23 | } 24 | } -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/DoubleIterate.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.DoubleUnaryOperator; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class DoubleIterate extends PrimitiveIterator.OfDouble { 8 | 9 | private final DoubleUnaryOperator op; 10 | private double current; 11 | 12 | public DoubleIterate(double seed, @NotNull DoubleUnaryOperator f) { 13 | this.op = f; 14 | current = seed; 15 | } 16 | 17 | @Override 18 | public boolean hasNext() { 19 | return true; 20 | } 21 | 22 | @Override 23 | public double nextDouble() { 24 | final double old = current; 25 | current = op.applyAsDouble(current); 26 | return old; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/IntLimit.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.iterator.PrimitiveIterator; 4 | import org.jetbrains.annotations.NotNull; 5 | 6 | public class IntLimit extends PrimitiveIterator.OfInt { 7 | 8 | private final PrimitiveIterator.OfInt iterator; 9 | private final long maxSize; 10 | private long index; 11 | 12 | public IntLimit(@NotNull PrimitiveIterator.OfInt iterator, long maxSize) { 13 | this.iterator = iterator; 14 | this.maxSize = maxSize; 15 | index = 0; 16 | } 17 | 18 | @Override 19 | public boolean hasNext() { 20 | return (index < maxSize) && iterator.hasNext(); 21 | } 22 | 23 | @Override 24 | public int nextInt() { 25 | index++; 26 | return iterator.nextInt(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/ObjIterate.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.UnaryOperator; 4 | import com.annimon.stream.iterator.LsaIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | import org.jetbrains.annotations.Nullable; 7 | 8 | public class ObjIterate extends LsaIterator { 9 | 10 | private final UnaryOperator op; 11 | private T current; 12 | 13 | public ObjIterate(@Nullable T seed, @NotNull UnaryOperator op) { 14 | this.op = op; 15 | current = seed; 16 | } 17 | 18 | @Override 19 | public boolean hasNext() { 20 | return true; 21 | } 22 | 23 | @Override 24 | public T nextIteration() { 25 | final T old = current; 26 | current = op.apply(current); 27 | return old; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/LongLimit.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.iterator.PrimitiveIterator; 4 | import org.jetbrains.annotations.NotNull; 5 | 6 | public class LongLimit extends PrimitiveIterator.OfLong { 7 | 8 | private final PrimitiveIterator.OfLong iterator; 9 | private final long maxSize; 10 | private long index; 11 | 12 | public LongLimit(@NotNull PrimitiveIterator.OfLong iterator, long maxSize) { 13 | this.iterator = iterator; 14 | this.maxSize = maxSize; 15 | index = 0; 16 | } 17 | 18 | @Override 19 | public boolean hasNext() { 20 | return (index < maxSize) && iterator.hasNext(); 21 | } 22 | 23 | @Override 24 | public long nextLong() { 25 | index++; 26 | return iterator.nextLong(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/CountTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import com.annimon.stream.function.IntSupplier; 5 | import org.junit.Test; 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public final class CountTest { 9 | 10 | @Test 11 | public void testCount() { 12 | assertEquals(IntStream.empty().count(), 0); 13 | assertEquals(IntStream.of(42).count(), 1); 14 | assertEquals(IntStream.range(1, 7).count(), 6); 15 | assertEquals(IntStream.generate(new IntSupplier() { 16 | @Override 17 | public int getAsInt() { 18 | return 1; 19 | } 20 | }).limit(10).count(), 10); 21 | 22 | assertEquals(IntStream.rangeClosed(1, 7).skip(3).count(), 4); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/OfIteratorTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.Stream; 5 | import java.util.Iterator; 6 | import org.junit.Test; 7 | import static com.annimon.stream.test.hamcrest.StreamMatcher.assertElements; 8 | import static org.hamcrest.Matchers.contains; 9 | 10 | public final class OfIteratorTest { 11 | 12 | @Test 13 | public void testStreamOfIterator() { 14 | Stream.of(Functions.counterIterator()) 15 | .limit(5) 16 | .custom(assertElements(contains( 17 | 0, 1, 2, 3, 4 18 | ))); 19 | } 20 | 21 | @Test(expected = NullPointerException.class) 22 | public void testStreamOfIteratorNull() { 23 | Stream.of((Iterator)null); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/function/IntUnaryOperatorTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | import com.annimon.stream.IntStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.CommonMatcher.hasOnlyPrivateConstructors; 6 | import static org.hamcrest.MatcherAssert.assertThat; 7 | import static org.junit.Assert.assertEquals; 8 | 9 | /** 10 | * Tests {@link IntUnaryOperator} 11 | */ 12 | public class IntUnaryOperatorTest { 13 | 14 | @Test 15 | public void testIdentity() { 16 | IntUnaryOperator identity = IntUnaryOperator.Util.identity(); 17 | 18 | assertEquals(15, IntStream.of(1, 2, 3, 4, 5).map(identity).sum()); 19 | } 20 | 21 | @Test 22 | public void testPrivateUtilConstructor() { 23 | assertThat(IntUnaryOperator.Util.class, hasOnlyPrivateConstructors()); 24 | } 25 | } -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/CollectTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.IntStream; 5 | import com.annimon.stream.function.ObjIntConsumer; 6 | import org.junit.Test; 7 | import static org.hamcrest.CoreMatchers.is; 8 | import static org.hamcrest.MatcherAssert.assertThat; 9 | 10 | public final class CollectTest { 11 | 12 | @Test 13 | public void testCollect() { 14 | String result = IntStream.of(0, 1, 5, 10) 15 | .collect(Functions.stringBuilderSupplier(), new ObjIntConsumer() { 16 | @Override 17 | public void accept(StringBuilder t, int value) { 18 | t.append(value); 19 | } 20 | }).toString(); 21 | assertThat(result, is("01510")); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/IntBinaryOperator.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents an operation upon two {@code int}-valued operands and producing an 5 | * {@code int}-valued result. This is the primitive type specialization of 6 | * {@link com.annimon.stream.function.BinaryOperator} for {@code int}. 7 | * 8 | *

This is a functional interface whose functional method is {@link #applyAsInt(int, int)}. 9 | * 10 | * @see com.annimon.stream.function.BinaryOperator 11 | * @see IntUnaryOperator 12 | */ 13 | public interface IntBinaryOperator { 14 | 15 | /** 16 | * Applies this operator to the given operands. 17 | * 18 | * @param left the first operand 19 | * @param right the second operand 20 | * @return the operator result 21 | */ 22 | int applyAsInt(int left, int right); 23 | } 24 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/MapToLongTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import com.annimon.stream.function.DoubleToLongFunction; 5 | import org.junit.Test; 6 | import static org.hamcrest.MatcherAssert.assertThat; 7 | import static org.hamcrest.Matchers.is; 8 | 9 | public final class MapToLongTest { 10 | 11 | @Test 12 | public void testMapToLong() { 13 | DoubleToLongFunction mapper = new DoubleToLongFunction() { 14 | @Override 15 | public long applyAsLong(double value) { 16 | return (long) (value * 10000000000L); 17 | } 18 | }; 19 | assertThat(DoubleStream.of(0.2, 0.3, 0.004).mapToLong(mapper).toArray(), 20 | is(new long[] {2000000000L, 3000000000L, 40000000L})); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/DoubleLimit.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.iterator.PrimitiveIterator; 4 | import org.jetbrains.annotations.NotNull; 5 | 6 | public class DoubleLimit extends PrimitiveIterator.OfDouble { 7 | 8 | private final PrimitiveIterator.OfDouble iterator; 9 | private final long maxSize; 10 | private long index; 11 | 12 | public DoubleLimit(@NotNull PrimitiveIterator.OfDouble iterator, long maxSize) { 13 | this.iterator = iterator; 14 | this.maxSize = maxSize; 15 | index = 0; 16 | } 17 | 18 | @Override 19 | public boolean hasNext() { 20 | return (index < maxSize) && iterator.hasNext(); 21 | } 22 | 23 | @Override 24 | public double nextDouble() { 25 | index++; 26 | return iterator.nextDouble(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/ObjDistinct.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.iterator.LsaExtIterator; 4 | import java.util.HashSet; 5 | import java.util.Iterator; 6 | import java.util.Set; 7 | import org.jetbrains.annotations.NotNull; 8 | 9 | public class ObjDistinct extends LsaExtIterator { 10 | 11 | private final Iterator iterator; 12 | private final Set set; 13 | 14 | public ObjDistinct(@NotNull Iterator iterator) { 15 | this.iterator = iterator; 16 | set = new HashSet(); 17 | } 18 | 19 | @Override 20 | protected void nextIteration() { 21 | while (hasNext = iterator.hasNext()) { 22 | next = iterator.next(); 23 | if (set.add(next)) { 24 | return; 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/AllMatchTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.LongStream; 5 | import org.junit.Test; 6 | import static org.junit.Assert.assertFalse; 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public final class AllMatchTest { 10 | 11 | @Test 12 | public void testAllMatch() { 13 | assertFalse(LongStream.of(3, 10, 19, 4, 50) 14 | .allMatch(Functions.remainderLong(2))); 15 | 16 | assertTrue(LongStream.of(10, 4, 50) 17 | .allMatch(Functions.remainderLong(2))); 18 | 19 | assertFalse(LongStream.of(3, 19) 20 | .allMatch(Functions.remainderLong(2))); 21 | 22 | assertTrue(LongStream.empty() 23 | .allMatch(Functions.remainderLong(2))); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/AnyMatchTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.LongStream; 5 | import org.junit.Test; 6 | import static org.junit.Assert.assertFalse; 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public final class AnyMatchTest { 10 | 11 | @Test 12 | public void testAnyMatch() { 13 | assertTrue(LongStream.of(3, 10, 19, 4, 50) 14 | .anyMatch(Functions.remainderLong(2))); 15 | 16 | assertTrue(LongStream.of(10, 4, 50) 17 | .anyMatch(Functions.remainderLong(2))); 18 | 19 | assertFalse(LongStream.of(3, 19) 20 | .anyMatch(Functions.remainderLong(2))); 21 | 22 | assertFalse(LongStream.empty() 23 | .anyMatch(Functions.remainderLong(2))); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/IntMap.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.IntUnaryOperator; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class IntMap extends PrimitiveIterator.OfInt { 8 | 9 | private final PrimitiveIterator.OfInt iterator; 10 | private final IntUnaryOperator mapper; 11 | 12 | public IntMap( 13 | @NotNull PrimitiveIterator.OfInt iterator, 14 | @NotNull IntUnaryOperator mapper) { 15 | this.iterator = iterator; 16 | this.mapper = mapper; 17 | } 18 | 19 | @Override 20 | public boolean hasNext() { 21 | return iterator.hasNext(); 22 | } 23 | 24 | @Override 25 | public int nextInt() { 26 | return mapper.applyAsInt(iterator.nextInt()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/IntRangeClosed.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.iterator.PrimitiveIterator; 4 | 5 | public class IntRangeClosed extends PrimitiveIterator.OfInt { 6 | 7 | private final int endInclusive; 8 | private int current; 9 | private boolean hasNext; 10 | 11 | public IntRangeClosed(int startInclusive, int endInclusive) { 12 | this.endInclusive = endInclusive; 13 | current = startInclusive; 14 | hasNext = current <= endInclusive; 15 | } 16 | 17 | @Override 18 | public boolean hasNext() { 19 | return hasNext; 20 | } 21 | 22 | @Override 23 | public int nextInt() { 24 | if (current >= endInclusive) { 25 | hasNext = false; 26 | return endInclusive; 27 | } 28 | return current++; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/ObjLimit.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.iterator.LsaIterator; 4 | import java.util.Iterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class ObjLimit extends LsaIterator { 8 | 9 | private final Iterator iterator; 10 | private final long maxSize; 11 | private long index; 12 | 13 | public ObjLimit( 14 | @NotNull Iterator iterator, long maxSize) { 15 | this.iterator = iterator; 16 | this.maxSize = maxSize; 17 | index = 0; 18 | } 19 | 20 | @Override 21 | public boolean hasNext() { 22 | return (index < maxSize) && iterator.hasNext(); 23 | } 24 | 25 | @Override 26 | public T nextIteration() { 27 | index++; 28 | return iterator.next(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/DistinctTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.LongStreamMatcher.assertElements; 6 | import static org.hamcrest.Matchers.arrayContaining; 7 | 8 | public final class DistinctTest { 9 | 10 | @Test 11 | public void testDistinct() { 12 | LongStream.of(9, 12, 0, 22, 9, 12, 32, 9) 13 | .distinct() 14 | .custom(assertElements(arrayContaining( 15 | 9L, 12L, 0L, 22L, 32L 16 | ))); 17 | 18 | LongStream.of(8, 800, 5, 5, 5, 3, 5, 3, 5) 19 | .distinct() 20 | .custom(assertElements(arrayContaining( 21 | 8L, 800L, 5L, 3L 22 | ))); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/internal/CompatTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.internal; 2 | 3 | import org.junit.Test; 4 | import static org.junit.Assert.assertEquals; 5 | import static org.junit.Assert.assertNull; 6 | 7 | public final class CompatTest { 8 | 9 | @Test 10 | public void testNewArrayCompat() { 11 | String[] strings = new String[] {"abc", "def", "fff"}; 12 | 13 | String[] copy = Compat.newArrayCompat(strings, 5); 14 | 15 | assertEquals(5, copy.length); 16 | assertEquals("abc", copy[0]); 17 | assertNull(copy[3]); 18 | 19 | String[] empty = new String[0]; 20 | 21 | String[] emptyCopy = Compat.newArrayCompat(empty, 3); 22 | 23 | assertEquals(3, emptyCopy.length); 24 | 25 | emptyCopy = Compat.newArrayCompat(empty, 0); 26 | 27 | assertEquals(0, emptyCopy.length); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/NoneMatchTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.LongStream; 5 | import org.junit.Test; 6 | import static org.junit.Assert.assertFalse; 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public final class NoneMatchTest { 10 | 11 | @Test 12 | public void testNoneMatch() { 13 | assertFalse(LongStream.of(3, 10, 19, 4, 50) 14 | .noneMatch(Functions.remainderLong(2))); 15 | 16 | assertFalse(LongStream.of(10, 4, 50) 17 | .noneMatch(Functions.remainderLong(2))); 18 | 19 | assertTrue(LongStream.of(3, 19) 20 | .noneMatch(Functions.remainderLong(2))); 21 | 22 | assertTrue(LongStream.empty() 23 | .noneMatch(Functions.remainderLong(2))); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/LongRangeClosed.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.iterator.PrimitiveIterator; 4 | 5 | public class LongRangeClosed extends PrimitiveIterator.OfLong { 6 | 7 | private final long endInclusive; 8 | private long current; 9 | private boolean hasNext; 10 | 11 | public LongRangeClosed(long startInclusive, long endInclusive) { 12 | this.endInclusive = endInclusive; 13 | current = startInclusive; 14 | hasNext = current <= endInclusive; 15 | } 16 | 17 | @Override 18 | public boolean hasNext() { 19 | return hasNext; 20 | } 21 | 22 | @Override 23 | public long nextLong() { 24 | if (current >= endInclusive) { 25 | hasNext = false; 26 | return endInclusive; 27 | } 28 | return current++; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/MapToObjTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import com.annimon.stream.function.IntFunction; 5 | import org.junit.Test; 6 | import static com.annimon.stream.test.hamcrest.StreamMatcher.assertElements; 7 | import static org.hamcrest.Matchers.contains; 8 | 9 | public final class MapToObjTest { 10 | 11 | @Test 12 | public void testMapToObj() { 13 | IntStream.rangeClosed(2, 4) 14 | .mapToObj(new IntFunction() { 15 | @Override 16 | public String apply(int value) { 17 | return Integer.toString(value); 18 | } 19 | }) 20 | .custom(assertElements(contains( 21 | "2", "3", "4" 22 | ))); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/LongMap.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.LongUnaryOperator; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class LongMap extends PrimitiveIterator.OfLong { 8 | 9 | private final PrimitiveIterator.OfLong iterator; 10 | private final LongUnaryOperator mapper; 11 | 12 | public LongMap( 13 | @NotNull PrimitiveIterator.OfLong iterator, 14 | @NotNull LongUnaryOperator mapper) { 15 | this.iterator = iterator; 16 | this.mapper = mapper; 17 | } 18 | 19 | @Override 20 | public boolean hasNext() { 21 | return iterator.hasNext(); 22 | } 23 | 24 | @Override 25 | public long nextLong() { 26 | return mapper.applyAsLong(iterator.nextLong()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/function/IndexedLongUnaryOperatorTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | import org.junit.Test; 4 | import static com.annimon.stream.test.hamcrest.CommonMatcher.hasOnlyPrivateConstructors; 5 | import static org.hamcrest.MatcherAssert.assertThat; 6 | import static org.hamcrest.Matchers.is; 7 | 8 | /** 9 | * Tests {@link IndexedLongUnaryOperator} 10 | */ 11 | public class IndexedLongUnaryOperatorTest { 12 | 13 | @Test 14 | public void testWrap() { 15 | IndexedLongUnaryOperator identity = IndexedLongUnaryOperator.Util 16 | .wrap(LongUnaryOperator.Util.identity()); 17 | 18 | assertThat(identity.applyAsLong(1, 3228), is(3228L)); 19 | } 20 | 21 | @Test 22 | public void testPrivateUtilConstructor() { 23 | assertThat(IndexedLongUnaryOperator.Util.class, hasOnlyPrivateConstructors()); 24 | } 25 | } -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/IntMapToLong.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.IntToLongFunction; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class IntMapToLong extends PrimitiveIterator.OfLong { 8 | 9 | private final PrimitiveIterator.OfInt iterator; 10 | private final IntToLongFunction mapper; 11 | 12 | public IntMapToLong( 13 | @NotNull PrimitiveIterator.OfInt iterator, 14 | @NotNull IntToLongFunction mapper) { 15 | this.iterator = iterator; 16 | this.mapper = mapper; 17 | } 18 | 19 | @Override 20 | public boolean hasNext() { 21 | return iterator.hasNext(); 22 | } 23 | 24 | @Override 25 | public long nextLong() { 26 | return mapper.applyAsLong(iterator.nextInt()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/LongMapToInt.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.LongToIntFunction; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class LongMapToInt extends PrimitiveIterator.OfInt { 8 | 9 | private final PrimitiveIterator.OfLong iterator; 10 | private final LongToIntFunction mapper; 11 | 12 | public LongMapToInt( 13 | @NotNull PrimitiveIterator.OfLong iterator, 14 | @NotNull LongToIntFunction mapper) { 15 | this.iterator = iterator; 16 | this.mapper = mapper; 17 | } 18 | 19 | @Override 20 | public boolean hasNext() { 21 | return iterator.hasNext(); 22 | } 23 | 24 | @Override 25 | public int nextInt() { 26 | return mapper.applyAsInt(iterator.nextLong()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/CollectTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.LongStream; 5 | import com.annimon.stream.function.ObjLongConsumer; 6 | import org.junit.Test; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | import static org.hamcrest.Matchers.is; 9 | 10 | public final class CollectTest { 11 | 12 | @Test 13 | public void testCollect() { 14 | String result = LongStream.of(10, 20, 30) 15 | .collect(Functions.stringBuilderSupplier(), new ObjLongConsumer() { 16 | @Override 17 | public void accept(StringBuilder t, long value) { 18 | t.append(value); 19 | } 20 | }).toString(); 21 | assertThat(result, is("102030")); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/ChunkByTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Stream; 4 | import com.annimon.stream.function.UnaryOperator; 5 | import java.util.Arrays; 6 | import org.junit.Test; 7 | import static com.annimon.stream.test.hamcrest.StreamMatcher.assertElements; 8 | import static org.hamcrest.Matchers.contains; 9 | 10 | public final class ChunkByTest { 11 | 12 | @Test 13 | @SuppressWarnings("unchecked") 14 | public void testChunkBy() { 15 | Stream.of(1, 1, 2, 2, 2, 3, 1) 16 | .chunkBy(UnaryOperator.Util.identity()) 17 | .custom(assertElements(contains( 18 | Arrays.asList(1, 1), 19 | Arrays.asList(2, 2, 2), 20 | Arrays.asList(3), 21 | Arrays.asList(1) 22 | ))); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/DoubleMap.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.DoubleUnaryOperator; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class DoubleMap extends PrimitiveIterator.OfDouble { 8 | 9 | private final PrimitiveIterator.OfDouble iterator; 10 | private final DoubleUnaryOperator mapper; 11 | 12 | public DoubleMap( 13 | @NotNull PrimitiveIterator.OfDouble iterator, 14 | @NotNull DoubleUnaryOperator mapper) { 15 | this.iterator = iterator; 16 | this.mapper = mapper; 17 | } 18 | 19 | @Override 20 | public boolean hasNext() { 21 | return iterator.hasNext(); 22 | } 23 | 24 | @Override 25 | public double nextDouble() { 26 | return mapper.applyAsDouble(iterator.nextDouble()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/DoubleMapToInt.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.DoubleToIntFunction; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class DoubleMapToInt extends PrimitiveIterator.OfInt { 8 | 9 | private final PrimitiveIterator.OfDouble iterator; 10 | private final DoubleToIntFunction mapper; 11 | 12 | public DoubleMapToInt( 13 | @NotNull PrimitiveIterator.OfDouble iterator, 14 | @NotNull DoubleToIntFunction mapper) { 15 | this.iterator = iterator; 16 | this.mapper = mapper; 17 | } 18 | 19 | @Override 20 | public boolean hasNext() { 21 | return iterator.hasNext(); 22 | } 23 | 24 | @Override 25 | public int nextInt() { 26 | return mapper.applyAsInt(iterator.nextDouble()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/IntMapToDouble.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.IntToDoubleFunction; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class IntMapToDouble extends PrimitiveIterator.OfDouble { 8 | 9 | private final PrimitiveIterator.OfInt iterator; 10 | private final IntToDoubleFunction mapper; 11 | 12 | public IntMapToDouble( 13 | @NotNull PrimitiveIterator.OfInt iterator, 14 | @NotNull IntToDoubleFunction mapper) { 15 | this.iterator = iterator; 16 | this.mapper = mapper; 17 | } 18 | 19 | @Override 20 | public boolean hasNext() { 21 | return iterator.hasNext(); 22 | } 23 | 24 | @Override 25 | public double nextDouble() { 26 | return mapper.applyAsDouble(iterator.nextInt()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/IntPeek.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.IntConsumer; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class IntPeek extends PrimitiveIterator.OfInt { 8 | 9 | private final PrimitiveIterator.OfInt iterator; 10 | private final IntConsumer action; 11 | 12 | public IntPeek( 13 | @NotNull PrimitiveIterator.OfInt iterator, 14 | @NotNull IntConsumer action) { 15 | this.iterator = iterator; 16 | this.action = action; 17 | } 18 | 19 | @Override 20 | public boolean hasNext() { 21 | return iterator.hasNext(); 22 | } 23 | 24 | @Override 25 | public int nextInt() { 26 | final int value = iterator.nextInt(); 27 | action.accept(value); 28 | return value; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/CollectTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import com.annimon.stream.Functions; 5 | import com.annimon.stream.function.ObjDoubleConsumer; 6 | import org.junit.Test; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | import static org.hamcrest.Matchers.is; 9 | 10 | public final class CollectTest { 11 | 12 | @Test 13 | public void testCollect() { 14 | String result = DoubleStream.of(1.0, 2.0, 3.0) 15 | .collect(Functions.stringBuilderSupplier(), new ObjDoubleConsumer() { 16 | @Override 17 | public void accept(StringBuilder t, double value) { 18 | t.append(value); 19 | } 20 | }).toString(); 21 | assertThat(result, is("1.02.03.0")); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/function/IndexedDoubleUnaryOperatorTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | import org.junit.Test; 4 | import static com.annimon.stream.test.hamcrest.CommonMatcher.hasOnlyPrivateConstructors; 5 | import static org.hamcrest.MatcherAssert.assertThat; 6 | import static org.hamcrest.Matchers.closeTo; 7 | 8 | /** 9 | * Tests {@link IndexedDoubleUnaryOperator} 10 | */ 11 | public class IndexedDoubleUnaryOperatorTest { 12 | 13 | @Test 14 | public void testWrap() { 15 | IndexedDoubleUnaryOperator identity = IndexedDoubleUnaryOperator.Util 16 | .wrap(DoubleUnaryOperator.Util.identity()); 17 | 18 | assertThat(identity.applyAsDouble(1, 0.2), closeTo(0.2, 0.001)); 19 | } 20 | 21 | @Test 22 | public void testPrivateUtilConstructor() { 23 | assertThat(IndexedDoubleUnaryOperator.Util.class, hasOnlyPrivateConstructors()); 24 | } 25 | } -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/MapToIntTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import com.annimon.stream.function.LongToIntFunction; 5 | import org.junit.Test; 6 | import static com.annimon.stream.test.hamcrest.IntStreamMatcher.assertElements; 7 | import static org.hamcrest.Matchers.arrayContaining; 8 | 9 | public final class MapToIntTest { 10 | 11 | @Test 12 | public void testMapToInt() { 13 | LongToIntFunction mapper = new LongToIntFunction() { 14 | @Override 15 | public int applyAsInt(long value) { 16 | return (int) (value / 10); 17 | } 18 | }; 19 | LongStream.of(10L, 20L, 30L, 40L) 20 | .mapToInt(mapper) 21 | .custom(assertElements(arrayContaining( 22 | 1, 2, 3, 4 23 | ))); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/SelectTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Stream; 4 | import com.annimon.stream.function.Predicate; 5 | import org.junit.Test; 6 | import static com.annimon.stream.test.hamcrest.StreamMatcher.assertElements; 7 | import static org.hamcrest.Matchers.contains; 8 | 9 | public final class SelectTest { 10 | 11 | @Test 12 | @SuppressWarnings("unchecked") 13 | public void testSelect() { 14 | Stream.of(1, "a", 2, "b", 3, "cc").select(String.class) 15 | .filter(new Predicate() { 16 | @Override 17 | public boolean test(String value) { 18 | return value.length() == 1; 19 | } 20 | }) 21 | .custom(assertElements(contains( 22 | "a", "b" 23 | ))); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/DoubleMapToLong.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.DoubleToLongFunction; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class DoubleMapToLong extends PrimitiveIterator.OfLong { 8 | 9 | private final PrimitiveIterator.OfDouble iterator; 10 | private final DoubleToLongFunction mapper; 11 | 12 | public DoubleMapToLong( 13 | @NotNull PrimitiveIterator.OfDouble iterator, 14 | @NotNull DoubleToLongFunction mapper) { 15 | this.iterator = iterator; 16 | this.mapper = mapper; 17 | } 18 | 19 | @Override 20 | public boolean hasNext() { 21 | return iterator.hasNext(); 22 | } 23 | 24 | @Override 25 | public long nextLong() { 26 | return mapper.applyAsLong(iterator.nextDouble()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/LongMapToDouble.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.LongToDoubleFunction; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class LongMapToDouble extends PrimitiveIterator.OfDouble { 8 | 9 | private final PrimitiveIterator.OfLong iterator; 10 | private final LongToDoubleFunction mapper; 11 | 12 | public LongMapToDouble( 13 | @NotNull PrimitiveIterator.OfLong iterator, 14 | @NotNull LongToDoubleFunction mapper) { 15 | this.iterator = iterator; 16 | this.mapper = mapper; 17 | } 18 | 19 | @Override 20 | public boolean hasNext() { 21 | return iterator.hasNext(); 22 | } 23 | 24 | @Override 25 | public double nextDouble() { 26 | return mapper.applyAsDouble(iterator.nextLong()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/LongPeek.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.LongConsumer; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class LongPeek extends PrimitiveIterator.OfLong { 8 | 9 | private final PrimitiveIterator.OfLong iterator; 10 | private final LongConsumer action; 11 | 12 | public LongPeek( 13 | @NotNull PrimitiveIterator.OfLong iterator, 14 | @NotNull LongConsumer action) { 15 | this.iterator = iterator; 16 | this.action = action; 17 | } 18 | 19 | @Override 20 | public boolean hasNext() { 21 | return iterator.hasNext(); 22 | } 23 | 24 | @Override 25 | public long nextLong() { 26 | final long value = iterator.nextLong(); 27 | action.accept(value); 28 | return value; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/ObjMapToInt.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.ToIntFunction; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import java.util.Iterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class ObjMapToInt extends PrimitiveIterator.OfInt { 9 | 10 | private final Iterator iterator; 11 | private final ToIntFunction mapper; 12 | 13 | public ObjMapToInt( 14 | @NotNull Iterator iterator, 15 | @NotNull ToIntFunction mapper) { 16 | this.iterator = iterator; 17 | this.mapper = mapper; 18 | } 19 | 20 | @Override 21 | public boolean hasNext() { 22 | return iterator.hasNext(); 23 | } 24 | 25 | @Override 26 | public int nextInt() { 27 | return mapper.applyAsInt(iterator.next()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/AllMatchTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import com.annimon.stream.Functions; 5 | import org.junit.Test; 6 | import static org.junit.Assert.assertFalse; 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public final class AllMatchTest { 10 | 11 | @Test 12 | public void testAllMatch() { 13 | assertFalse(DoubleStream.of(0.012, 10.347, 3.039, 19.84, 100d) 14 | .allMatch(Functions.greaterThan(Math.PI))); 15 | 16 | assertTrue(DoubleStream.of(10.347, 19.84, 100d) 17 | .allMatch(Functions.greaterThan(Math.PI))); 18 | 19 | assertFalse(DoubleStream.of(0.012, 3.039) 20 | .allMatch(Functions.greaterThan(Math.PI))); 21 | 22 | assertTrue(DoubleStream.empty() 23 | .allMatch(Functions.greaterThan(Math.PI))); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/AnyMatchTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import com.annimon.stream.Functions; 5 | import org.junit.Test; 6 | import static org.junit.Assert.assertFalse; 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public final class AnyMatchTest { 10 | 11 | @Test 12 | public void testAnyMatch() { 13 | assertTrue(DoubleStream.of(0.012, 10.347, 3.039, 19.84, 100d) 14 | .anyMatch(Functions.greaterThan(Math.PI))); 15 | 16 | assertTrue(DoubleStream.of(10.347, 19.84, 100d) 17 | .anyMatch(Functions.greaterThan(Math.PI))); 18 | 19 | assertFalse(DoubleStream.of(0.012, 3.039) 20 | .anyMatch(Functions.greaterThan(Math.PI))); 21 | 22 | assertFalse(DoubleStream.empty() 23 | .anyMatch(Functions.greaterThan(Math.PI))); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/ObjMapToLong.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.ToLongFunction; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import java.util.Iterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class ObjMapToLong extends PrimitiveIterator.OfLong { 9 | 10 | private final Iterator iterator; 11 | private final ToLongFunction mapper; 12 | 13 | public ObjMapToLong( 14 | @NotNull Iterator iterator, 15 | @NotNull ToLongFunction mapper) { 16 | this.iterator = iterator; 17 | this.mapper = mapper; 18 | } 19 | 20 | @Override 21 | public boolean hasNext() { 22 | return iterator.hasNext(); 23 | } 24 | 25 | @Override 26 | public long nextLong() { 27 | return mapper.applyAsLong(iterator.next()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/ObjPeek.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.Consumer; 4 | import com.annimon.stream.iterator.LsaIterator; 5 | import java.util.Iterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class ObjPeek extends LsaIterator { 9 | 10 | private final Iterator iterator; 11 | private final Consumer action; 12 | 13 | public ObjPeek( 14 | @NotNull Iterator iterator, 15 | @NotNull Consumer action) { 16 | this.iterator = iterator; 17 | this.action = action; 18 | } 19 | 20 | @Override 21 | public boolean hasNext() { 22 | return iterator.hasNext(); 23 | } 24 | 25 | @Override 26 | public T nextIteration() { 27 | final T value = iterator.next(); 28 | action.accept(value); 29 | return value; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/ObjTakeUntil.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.Predicate; 4 | import com.annimon.stream.iterator.LsaExtIterator; 5 | import java.util.Iterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class ObjTakeUntil extends LsaExtIterator { 9 | 10 | private final Iterator iterator; 11 | private final Predicate stopPredicate; 12 | 13 | public ObjTakeUntil( 14 | @NotNull Iterator iterator, 15 | @NotNull Predicate predicate) { 16 | this.iterator = iterator; 17 | this.stopPredicate = predicate; 18 | } 19 | 20 | @Override 21 | protected void nextIteration() { 22 | hasNext = iterator.hasNext() && !(isInit && stopPredicate.test(next)); 23 | if (hasNext) { 24 | next = iterator.next(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/NoneMatchTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import com.annimon.stream.Functions; 5 | import org.junit.Test; 6 | import static org.junit.Assert.assertFalse; 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public final class NoneMatchTest { 10 | 11 | @Test 12 | public void testNoneMatch() { 13 | assertFalse(DoubleStream.of(0.012, 10.347, 3.039, 19.84, 100d) 14 | .noneMatch(Functions.greaterThan(Math.PI))); 15 | 16 | assertFalse(DoubleStream.of(10.347, 19.84, 100d) 17 | .noneMatch(Functions.greaterThan(Math.PI))); 18 | 19 | assertTrue(DoubleStream.of(0.012, 3.039) 20 | .noneMatch(Functions.greaterThan(Math.PI))); 21 | 22 | assertTrue(DoubleStream.empty() 23 | .noneMatch(Functions.greaterThan(Math.PI))); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/IntMapToObj.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.IntFunction; 4 | import com.annimon.stream.iterator.LsaIterator; 5 | import com.annimon.stream.iterator.PrimitiveIterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class IntMapToObj extends LsaIterator { 9 | 10 | private final PrimitiveIterator.OfInt iterator; 11 | private final IntFunction mapper; 12 | 13 | public IntMapToObj( 14 | @NotNull PrimitiveIterator.OfInt iterator, 15 | @NotNull IntFunction mapper) { 16 | this.iterator = iterator; 17 | this.mapper = mapper; 18 | } 19 | 20 | @Override 21 | public boolean hasNext() { 22 | return iterator.hasNext(); 23 | } 24 | 25 | @Override 26 | public R nextIteration() { 27 | return mapper.apply(iterator.nextInt()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/IntSample.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.iterator.PrimitiveIterator; 4 | import org.jetbrains.annotations.NotNull; 5 | 6 | public class IntSample extends PrimitiveIterator.OfInt { 7 | 8 | private final PrimitiveIterator.OfInt iterator; 9 | private final int stepWidth; 10 | 11 | public IntSample(@NotNull PrimitiveIterator.OfInt iterator, int stepWidth) { 12 | this.iterator = iterator; 13 | this.stepWidth = stepWidth; 14 | } 15 | 16 | @Override 17 | public boolean hasNext() { 18 | return iterator.hasNext(); 19 | } 20 | 21 | @Override 22 | public int nextInt() { 23 | final int result = iterator.nextInt(); 24 | int skip = 1; 25 | while (skip < stepWidth && iterator.hasNext()) { 26 | iterator.nextInt(); 27 | skip++; 28 | } 29 | return result; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/ObjMap.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.Function; 4 | import java.util.Iterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class ObjMap implements Iterator { 8 | 9 | private final Iterator iterator; 10 | private final Function mapper; 11 | 12 | public ObjMap( 13 | @NotNull Iterator iterator, 14 | @NotNull Function mapper) { 15 | this.iterator = iterator; 16 | this.mapper = mapper; 17 | } 18 | 19 | @Override 20 | public void remove() { 21 | iterator.remove(); 22 | } 23 | 24 | @Override 25 | public boolean hasNext() { 26 | return iterator.hasNext(); 27 | } 28 | 29 | @Override 30 | public R next() { 31 | return mapper.apply(iterator.next()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/DoublePeek.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.DoubleConsumer; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class DoublePeek extends PrimitiveIterator.OfDouble { 8 | 9 | private final PrimitiveIterator.OfDouble iterator; 10 | private final DoubleConsumer action; 11 | 12 | public DoublePeek( 13 | @NotNull PrimitiveIterator.OfDouble iterator, 14 | @NotNull DoubleConsumer action) { 15 | this.iterator = iterator; 16 | this.action = action; 17 | } 18 | 19 | @Override 20 | public boolean hasNext() { 21 | return iterator.hasNext(); 22 | } 23 | 24 | @Override 25 | public double nextDouble() { 26 | final double value = iterator.nextDouble(); 27 | action.accept(value); 28 | return value; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/LongMapToObj.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.LongFunction; 4 | import com.annimon.stream.iterator.LsaIterator; 5 | import com.annimon.stream.iterator.PrimitiveIterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class LongMapToObj extends LsaIterator { 9 | 10 | private final PrimitiveIterator.OfLong iterator; 11 | private final LongFunction mapper; 12 | 13 | public LongMapToObj( 14 | @NotNull PrimitiveIterator.OfLong iterator, 15 | @NotNull LongFunction mapper) { 16 | this.iterator = iterator; 17 | this.mapper = mapper; 18 | } 19 | 20 | @Override 21 | public boolean hasNext() { 22 | return iterator.hasNext(); 23 | } 24 | 25 | @Override 26 | public R nextIteration() { 27 | return mapper.apply(iterator.nextLong()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/WithoutNullsTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Stream; 4 | import com.annimon.stream.function.Function; 5 | import org.junit.Test; 6 | import static com.annimon.stream.test.hamcrest.StreamMatcher.assertElements; 7 | import static org.hamcrest.Matchers.contains; 8 | 9 | public final class WithoutNullsTest { 10 | 11 | @Test 12 | public void testWithoutNulls() { 13 | Stream.range(0, 10) 14 | .map(new Function() { 15 | @Override 16 | public String apply(Integer integer) { 17 | return integer % 3 == 0 ? null : integer.toString(); 18 | } 19 | }) 20 | .withoutNulls() 21 | .custom(assertElements(contains( 22 | "1", "2", "4", "5", "7", "8" 23 | ))); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/ObjMapToDouble.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.ToDoubleFunction; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import java.util.Iterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class ObjMapToDouble extends PrimitiveIterator.OfDouble { 9 | 10 | private final Iterator iterator; 11 | private final ToDoubleFunction mapper; 12 | 13 | public ObjMapToDouble( 14 | @NotNull Iterator iterator, 15 | @NotNull ToDoubleFunction mapper) { 16 | this.iterator = iterator; 17 | this.mapper = mapper; 18 | } 19 | 20 | @Override 21 | public boolean hasNext() { 22 | return iterator.hasNext(); 23 | } 24 | 25 | @Override 26 | public double nextDouble() { 27 | return mapper.applyAsDouble(iterator.next()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/LongSample.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.iterator.PrimitiveIterator; 4 | import org.jetbrains.annotations.NotNull; 5 | 6 | public class LongSample extends PrimitiveIterator.OfLong { 7 | 8 | private final PrimitiveIterator.OfLong iterator; 9 | private final int stepWidth; 10 | 11 | public LongSample(@NotNull PrimitiveIterator.OfLong iterator, int stepWidth) { 12 | this.iterator = iterator; 13 | this.stepWidth = stepWidth; 14 | } 15 | 16 | @Override 17 | public boolean hasNext() { 18 | return iterator.hasNext(); 19 | } 20 | 21 | @Override 22 | public long nextLong() { 23 | final long result = iterator.nextLong(); 24 | int skip = 1; 25 | while (skip < stepWidth && iterator.hasNext()) { 26 | iterator.nextLong(); 27 | skip++; 28 | } 29 | return result; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/DoubleMapToObj.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.DoubleFunction; 4 | import com.annimon.stream.iterator.LsaIterator; 5 | import com.annimon.stream.iterator.PrimitiveIterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class DoubleMapToObj extends LsaIterator { 9 | 10 | private final PrimitiveIterator.OfDouble iterator; 11 | private final DoubleFunction mapper; 12 | 13 | public DoubleMapToObj( 14 | @NotNull PrimitiveIterator.OfDouble iterator, 15 | @NotNull DoubleFunction mapper) { 16 | this.iterator = iterator; 17 | this.mapper = mapper; 18 | } 19 | 20 | @Override 21 | public boolean hasNext() { 22 | return iterator.hasNext(); 23 | } 24 | 25 | @Override 26 | public R nextIteration() { 27 | return mapper.apply(iterator.nextDouble()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/OfArrayTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.LongStreamMatcher.assertIsEmpty; 6 | import static com.annimon.stream.test.hamcrest.LongStreamMatcher.elements; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | import static org.hamcrest.Matchers.arrayContaining; 9 | 10 | public final class OfArrayTest { 11 | 12 | @Test 13 | public void testStreamOfLongs() { 14 | assertThat(LongStream.of(32, 28), elements(arrayContaining(32L, 28L))); 15 | } 16 | 17 | @Test(expected = NullPointerException.class) 18 | public void testStreamOfLongsNull() { 19 | LongStream.of((long[]) null); 20 | } 21 | 22 | @Test 23 | public void testStreamOfEmptyArray() { 24 | LongStream.of(new long[0]) 25 | .custom(assertIsEmpty()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/UnaryOperator.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Operation on a single operand that produces a result of the 5 | * same type as its operand. 6 | * 7 | * @param the type of the operand and result of the operator 8 | */ 9 | public interface UnaryOperator extends Function { 10 | class Util { 11 | 12 | private Util() { } 13 | 14 | /** 15 | * Returns a unary operator that always returns its input argument. 16 | * 17 | * @param the type of the input and output of the operator 18 | * @return a unary operator that always returns its input argument 19 | */ 20 | public static UnaryOperator identity() { 21 | return new UnaryOperator() { 22 | @Override 23 | public T apply(T t) { 24 | return t; 25 | } 26 | }; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/IntSkip.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.iterator.PrimitiveIterator; 4 | import org.jetbrains.annotations.NotNull; 5 | 6 | public class IntSkip extends PrimitiveIterator.OfInt { 7 | 8 | private final PrimitiveIterator.OfInt iterator; 9 | private final long n; 10 | private long skipped; 11 | 12 | public IntSkip(@NotNull PrimitiveIterator.OfInt iterator, long n) { 13 | this.iterator = iterator; 14 | this.n = n; 15 | skipped = 0; 16 | } 17 | 18 | @Override 19 | public boolean hasNext() { 20 | while (iterator.hasNext()) { 21 | if (skipped == n) { 22 | break; 23 | } 24 | iterator.nextInt(); 25 | skipped++; 26 | } 27 | 28 | return iterator.hasNext(); 29 | } 30 | 31 | @Override 32 | public int nextInt() { 33 | return iterator.nextInt(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/IntTakeUntil.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.IntPredicate; 4 | import com.annimon.stream.iterator.PrimitiveExtIterator; 5 | import com.annimon.stream.iterator.PrimitiveIterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class IntTakeUntil extends PrimitiveExtIterator.OfInt { 9 | 10 | private final PrimitiveIterator.OfInt iterator; 11 | private final IntPredicate stopPredicate; 12 | 13 | public IntTakeUntil( 14 | @NotNull PrimitiveIterator.OfInt iterator, 15 | @NotNull IntPredicate stopPredicate) { 16 | this.iterator = iterator; 17 | this.stopPredicate = stopPredicate; 18 | } 19 | 20 | @Override 21 | protected void nextIteration() { 22 | hasNext = iterator.hasNext() && !(isInit && stopPredicate.test(next)); 23 | if (hasNext) { 24 | next = iterator.nextInt(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/DoubleSample.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.iterator.PrimitiveIterator; 4 | import org.jetbrains.annotations.NotNull; 5 | 6 | public class DoubleSample extends PrimitiveIterator.OfDouble { 7 | 8 | private final PrimitiveIterator.OfDouble iterator; 9 | private final int stepWidth; 10 | 11 | public DoubleSample(@NotNull PrimitiveIterator.OfDouble iterator, int stepWidth) { 12 | this.iterator = iterator; 13 | this.stepWidth = stepWidth; 14 | } 15 | 16 | @Override 17 | public boolean hasNext() { 18 | return iterator.hasNext(); 19 | } 20 | 21 | @Override 22 | public double nextDouble() { 23 | final double result = iterator.nextDouble(); 24 | int skip = 1; 25 | while (skip < stepWidth && iterator.hasNext()) { 26 | iterator.nextDouble(); 27 | skip++; 28 | } 29 | return result; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/LongSkip.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.iterator.PrimitiveIterator; 4 | import org.jetbrains.annotations.NotNull; 5 | 6 | public class LongSkip extends PrimitiveIterator.OfLong { 7 | 8 | private final PrimitiveIterator.OfLong iterator; 9 | private final long n; 10 | private long skipped; 11 | 12 | public LongSkip(@NotNull PrimitiveIterator.OfLong iterator, long n) { 13 | this.iterator = iterator; 14 | this.n = n; 15 | skipped = 0; 16 | } 17 | 18 | @Override 19 | public boolean hasNext() { 20 | while (iterator.hasNext()) { 21 | if (skipped == n) { 22 | break; 23 | } 24 | iterator.nextLong(); 25 | skipped++; 26 | } 27 | return iterator.hasNext(); 28 | } 29 | 30 | @Override 31 | public long nextLong() { 32 | return iterator.nextLong(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/LongTakeUntil.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.LongPredicate; 4 | import com.annimon.stream.iterator.PrimitiveExtIterator; 5 | import com.annimon.stream.iterator.PrimitiveIterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class LongTakeUntil extends PrimitiveExtIterator.OfLong { 9 | 10 | private final PrimitiveIterator.OfLong iterator; 11 | private final LongPredicate stopPredicate; 12 | 13 | public LongTakeUntil( 14 | @NotNull PrimitiveIterator.OfLong iterator, 15 | @NotNull LongPredicate stopPredicate) { 16 | this.iterator = iterator; 17 | this.stopPredicate = stopPredicate; 18 | } 19 | 20 | @Override 21 | protected void nextIteration() { 22 | hasNext = iterator.hasNext() && !(isInit && stopPredicate.test(next)); 23 | if (hasNext) { 24 | next = iterator.nextLong(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/GenerateTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import com.annimon.stream.function.LongSupplier; 5 | import org.junit.Test; 6 | import static com.annimon.stream.test.hamcrest.LongStreamMatcher.elements; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | import static org.hamcrest.Matchers.arrayContaining; 9 | 10 | public final class GenerateTest { 11 | 12 | @Test 13 | public void testStreamGenerate() { 14 | LongStream stream = LongStream.generate(new LongSupplier() { 15 | @Override 16 | public long getAsLong() { 17 | return 1234L; 18 | } 19 | }); 20 | assertThat(stream.limit(3), elements(arrayContaining(1234L, 1234L, 1234L))); 21 | } 22 | 23 | @Test(expected = NullPointerException.class) 24 | public void testStreamGenerateNull() { 25 | LongStream.generate(null); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/IntMapIndexed.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.IntBinaryOperator; 4 | import com.annimon.stream.iterator.PrimitiveIndexedIterator; 5 | import com.annimon.stream.iterator.PrimitiveIterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class IntMapIndexed extends PrimitiveIterator.OfInt { 9 | 10 | private final PrimitiveIndexedIterator.OfInt iterator; 11 | private final IntBinaryOperator mapper; 12 | 13 | public IntMapIndexed( 14 | @NotNull PrimitiveIndexedIterator.OfInt iterator, 15 | @NotNull IntBinaryOperator mapper) { 16 | this.iterator = iterator; 17 | this.mapper = mapper; 18 | } 19 | 20 | @Override 21 | public boolean hasNext() { 22 | return iterator.hasNext(); 23 | } 24 | 25 | @Override 26 | public int nextInt() { 27 | return mapper.applyAsInt(iterator.getIndex(), iterator.nextInt()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/ObjSkip.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.iterator.LsaIterator; 4 | import java.util.Iterator; 5 | import org.jetbrains.annotations.NotNull; 6 | 7 | public class ObjSkip extends LsaIterator { 8 | 9 | private final Iterator iterator; 10 | private final long n; 11 | private long skipped; 12 | 13 | public ObjSkip(@NotNull Iterator iterator, long n) { 14 | this.iterator = iterator; 15 | this.n = n; 16 | skipped = 0; 17 | } 18 | 19 | @Override 20 | public boolean hasNext() { 21 | while (skipped < n) { 22 | if (!iterator.hasNext()) { 23 | return false; 24 | } 25 | iterator.next(); 26 | skipped++; 27 | } 28 | return iterator.hasNext(); 29 | } 30 | 31 | @Override 32 | public T nextIteration() { 33 | return iterator.next(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/function/ThrowableFunctionTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | import org.junit.Test; 4 | import static org.junit.Assert.assertEquals; 5 | 6 | /** 7 | * Tests {@code ThrowableFunction}. 8 | * 9 | * @see com.annimon.stream.function.ThrowableFunction 10 | */ 11 | public class ThrowableFunctionTest { 12 | 13 | @Test 14 | public void testApply() { 15 | assertEquals(100, (int) toInt.apply("100")); 16 | } 17 | 18 | 19 | @Test(expected = NumberFormatException.class) 20 | public void testApplyWithRuntimeException() { 21 | toInt.apply("oops"); 22 | } 23 | 24 | private static final ThrowableFunction toInt 25 | = new ThrowableFunction() { 26 | @Override 27 | public Integer apply(String value) throws NumberFormatException { 28 | return Integer.parseInt(value); 29 | } 30 | }; 31 | 32 | } 33 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/MapToLongTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import com.annimon.stream.function.IntToLongFunction; 5 | import org.junit.Test; 6 | import static com.annimon.stream.test.hamcrest.LongStreamMatcher.assertElements; 7 | import static org.hamcrest.Matchers.arrayContaining; 8 | 9 | public final class MapToLongTest { 10 | 11 | @Test 12 | public void testMapToLong() { 13 | IntStream.rangeClosed(2, 4) 14 | .mapToLong(new IntToLongFunction() { 15 | @Override 16 | public long applyAsLong(int value) { 17 | return value * 10000000000L; 18 | } 19 | }) 20 | .custom(assertElements(arrayContaining( 21 | 20000000000L, 22 | 30000000000L, 23 | 40000000000L 24 | ))); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/DoubleSkip.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.iterator.PrimitiveIterator; 4 | import org.jetbrains.annotations.NotNull; 5 | 6 | public class DoubleSkip extends PrimitiveIterator.OfDouble { 7 | 8 | private final PrimitiveIterator.OfDouble iterator; 9 | private final long n; 10 | private long skipped; 11 | 12 | public DoubleSkip(@NotNull PrimitiveIterator.OfDouble iterator, long n) { 13 | this.iterator = iterator; 14 | this.n = n; 15 | skipped = 0; 16 | } 17 | 18 | @Override 19 | public boolean hasNext() { 20 | while (iterator.hasNext()) { 21 | if (skipped == n) { 22 | break; 23 | } 24 | iterator.nextDouble(); 25 | skipped++; 26 | } 27 | return iterator.hasNext(); 28 | } 29 | 30 | @Override 31 | public double nextDouble() { 32 | return iterator.nextDouble(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/IntSorted.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.internal.Operators; 4 | import com.annimon.stream.iterator.PrimitiveExtIterator; 5 | import com.annimon.stream.iterator.PrimitiveIterator; 6 | import java.util.Arrays; 7 | import org.jetbrains.annotations.NotNull; 8 | 9 | public class IntSorted extends PrimitiveExtIterator.OfInt { 10 | 11 | private final PrimitiveIterator.OfInt iterator; 12 | private int index; 13 | private int[] array; 14 | 15 | public IntSorted(@NotNull PrimitiveIterator.OfInt iterator) { 16 | this.iterator = iterator; 17 | index = 0; 18 | } 19 | 20 | @Override 21 | protected void nextIteration() { 22 | if (!isInit) { 23 | array = Operators.toIntArray(iterator); 24 | Arrays.sort(array); 25 | } 26 | hasNext = index < array.length; 27 | if (hasNext) { 28 | next = array[index++]; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/DoubleTakeUntil.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.DoublePredicate; 4 | import com.annimon.stream.iterator.PrimitiveExtIterator; 5 | import com.annimon.stream.iterator.PrimitiveIterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class DoubleTakeUntil extends PrimitiveExtIterator.OfDouble { 9 | 10 | private final PrimitiveIterator.OfDouble iterator; 11 | private final DoublePredicate stopPredicate; 12 | 13 | public DoubleTakeUntil( 14 | @NotNull PrimitiveIterator.OfDouble iterator, 15 | @NotNull DoublePredicate stopPredicate) { 16 | this.iterator = iterator; 17 | this.stopPredicate = stopPredicate; 18 | } 19 | 20 | @Override 21 | protected void nextIteration() { 22 | hasNext = iterator.hasNext() && !(isInit && stopPredicate.test(next)); 23 | if (hasNext) { 24 | next = iterator.nextDouble(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/LongSorted.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.internal.Operators; 4 | import com.annimon.stream.iterator.PrimitiveExtIterator; 5 | import com.annimon.stream.iterator.PrimitiveIterator; 6 | import java.util.Arrays; 7 | import org.jetbrains.annotations.NotNull; 8 | 9 | public class LongSorted extends PrimitiveExtIterator.OfLong { 10 | 11 | private final PrimitiveIterator.OfLong iterator; 12 | private int index; 13 | private long[] array; 14 | 15 | public LongSorted(@NotNull PrimitiveIterator.OfLong iterator) { 16 | this.iterator = iterator; 17 | index = 0; 18 | } 19 | 20 | @Override 21 | protected void nextIteration() { 22 | if (!isInit) { 23 | array = Operators.toLongArray(iterator); 24 | Arrays.sort(array); 25 | } 26 | hasNext = index < array.length; 27 | if (hasNext) { 28 | next = array[index++]; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/ObjMapIndexed.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.IndexedFunction; 4 | import com.annimon.stream.iterator.IndexedIterator; 5 | import com.annimon.stream.iterator.LsaIterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class ObjMapIndexed extends LsaIterator { 9 | 10 | private final IndexedIterator iterator; 11 | private final IndexedFunction mapper; 12 | 13 | public ObjMapIndexed( 14 | @NotNull IndexedIterator iterator, 15 | @NotNull IndexedFunction mapper) { 16 | this.iterator = iterator; 17 | this.mapper = mapper; 18 | } 19 | 20 | @Override 21 | public boolean hasNext() { 22 | return iterator.hasNext(); 23 | } 24 | 25 | @Override 26 | public R nextIteration() { 27 | return mapper.apply(iterator.getIndex(), iterator.next()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/GenerateTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import com.annimon.stream.function.DoubleSupplier; 5 | import org.junit.Test; 6 | import static com.annimon.stream.test.hamcrest.DoubleStreamMatcher.elements; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | import static org.hamcrest.Matchers.arrayContaining; 9 | 10 | public final class GenerateTest { 11 | 12 | @Test 13 | public void testStreamGenerate() { 14 | DoubleStream stream = DoubleStream.generate(new DoubleSupplier() { 15 | @Override 16 | public double getAsDouble() { 17 | return 1.234; 18 | } 19 | }); 20 | assertThat(stream.limit(3), elements(arrayContaining(1.234, 1.234, 1.234))); 21 | } 22 | 23 | @Test(expected = NullPointerException.class) 24 | public void testStreamGenerateNull() { 25 | DoubleStream.generate(null); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/OfArrayTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.DoubleStreamMatcher.assertElements; 6 | import static com.annimon.stream.test.hamcrest.DoubleStreamMatcher.assertIsEmpty; 7 | import static org.hamcrest.Matchers.arrayContaining; 8 | 9 | public final class OfArrayTest { 10 | 11 | @Test 12 | public void testStreamOfDoubles() { 13 | DoubleStream.of(3.2, 2.8) 14 | .custom(assertElements(arrayContaining( 15 | 3.2, 2.8 16 | ))); 17 | } 18 | 19 | @Test(expected = NullPointerException.class) 20 | public void testStreamOfDoublesNull() { 21 | DoubleStream.of((double[]) null); 22 | } 23 | 24 | @Test 25 | public void testStreamOfEmptyArray() { 26 | DoubleStream.of(new double[0]) 27 | .custom(assertIsEmpty()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /streamTest/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java-library' 2 | apply plugin: 'maven-publish' 3 | apply from: '../versions.gradle' 4 | 5 | archivesBaseName = 'stream-test' 6 | version = ext.libVersion 7 | group = 'com.annimon' 8 | ext.isReleaseVersion = !version.contains("SNAPSHOT") 9 | 10 | sourceCompatibility = '1.6' 11 | targetCompatibility = '1.6' 12 | [compileJava, compileTestJava]*.options*.encoding = 'UTF-8' 13 | 14 | if (!hasProperty('mainClass')) { 15 | ext.mainClass = '' 16 | } 17 | 18 | repositories { 19 | mavenCentral() 20 | } 21 | 22 | dependencies { 23 | api project(':stream') 24 | api 'org.hamcrest:hamcrest-core:2.2' 25 | api 'org.mockito:mockito-core:3.8.0' 26 | testImplementation 'junit:junit:4.13.1' 27 | testImplementation 'org.hamcrest:hamcrest-library:1.3' 28 | } 29 | 30 | //cobertura { 31 | // coverageExcludes = ['.*com\\.annimon\\.stream\\.test\\.hamcrest\\.CommonMatcher.*'] 32 | //} 33 | 34 | // maven signing 35 | if (ext.isReleaseVersion) { 36 | apply from: 'signing.gradle' 37 | } 38 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/DoubleSorted.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.internal.Operators; 4 | import com.annimon.stream.iterator.PrimitiveExtIterator; 5 | import com.annimon.stream.iterator.PrimitiveIterator; 6 | import java.util.Arrays; 7 | import org.jetbrains.annotations.NotNull; 8 | 9 | public class DoubleSorted extends PrimitiveExtIterator.OfDouble { 10 | 11 | private final PrimitiveIterator.OfDouble iterator; 12 | private int index; 13 | private double[] array; 14 | 15 | public DoubleSorted(@NotNull PrimitiveIterator.OfDouble iterator) { 16 | this.iterator = iterator; 17 | index = 0; 18 | } 19 | 20 | @Override 21 | protected void nextIteration() { 22 | if (!isInit) { 23 | array = Operators.toDoubleArray(iterator); 24 | Arrays.sort(array); 25 | } 26 | hasNext = index < array.length; 27 | if (hasNext) { 28 | next = array[index++]; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/FindFirstOrElseTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.Stream; 5 | import org.junit.Test; 6 | import static org.hamcrest.CoreMatchers.is; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | 9 | public final class FindFirstOrElseTest { 10 | 11 | @Test 12 | public void testFindFirstOrElse() { 13 | Integer result = Stream.range(0, 10).findFirstOrElse(10); 14 | assertThat(result, is(0)); 15 | } 16 | 17 | @Test 18 | public void testFindFirstOrElseOnEmptyStream() { 19 | Integer result = Stream.empty().findFirstOrElse(10); 20 | assertThat(result, is(10)); 21 | } 22 | 23 | @Test 24 | public void testFindFirstOrElseAfterFiltering() { 25 | Integer result = Stream.range(1, 1000) 26 | .filter(Functions.remainder(6)) 27 | .findFirstOrElse(10); 28 | assertThat(result, is(6)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/LongMapIndexed.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.IndexedLongUnaryOperator; 4 | import com.annimon.stream.iterator.PrimitiveIndexedIterator; 5 | import com.annimon.stream.iterator.PrimitiveIterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class LongMapIndexed extends PrimitiveIterator.OfLong { 9 | 10 | private final PrimitiveIndexedIterator.OfLong iterator; 11 | private final IndexedLongUnaryOperator mapper; 12 | 13 | public LongMapIndexed( 14 | @NotNull PrimitiveIndexedIterator.OfLong iterator, 15 | @NotNull IndexedLongUnaryOperator mapper) { 16 | this.iterator = iterator; 17 | this.mapper = mapper; 18 | } 19 | 20 | @Override 21 | public boolean hasNext() { 22 | return iterator.hasNext(); 23 | } 24 | 25 | @Override 26 | public long nextLong() { 27 | return mapper.applyAsLong(iterator.getIndex(), iterator.nextLong()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/PeekTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Stream; 4 | import com.annimon.stream.function.Consumer; 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import org.junit.Test; 8 | import static org.hamcrest.MatcherAssert.assertThat; 9 | import static org.hamcrest.Matchers.contains; 10 | import static org.junit.Assert.assertEquals; 11 | 12 | public final class PeekTest { 13 | 14 | @Test 15 | public void testPeek() { 16 | final List result = new ArrayList(); 17 | long count = Stream.range(0, 5) 18 | .peek(new Consumer() { 19 | @Override 20 | public void accept(Integer t) { 21 | result.add(t); 22 | } 23 | }) 24 | .count(); 25 | assertEquals(5, count); 26 | assertThat(result, contains( 27 | 0, 1, 2, 3, 4 28 | )); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/FilterNotTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.LongStream; 5 | import com.annimon.stream.function.LongPredicate; 6 | import org.junit.Test; 7 | import static com.annimon.stream.test.hamcrest.LongStreamMatcher.assertElements; 8 | import static com.annimon.stream.test.hamcrest.LongStreamMatcher.assertIsEmpty; 9 | import static org.hamcrest.Matchers.arrayContaining; 10 | 11 | public final class FilterNotTest { 12 | 13 | @Test 14 | public void testFilterNot() { 15 | final LongPredicate predicate = Functions.remainderLong(111); 16 | LongStream.of(322, 555, 666, 1984, 1998) 17 | .filterNot(predicate) 18 | .custom(assertElements(arrayContaining( 19 | 322L, 1984L 20 | ))); 21 | 22 | LongStream.of(777, 999) 23 | .filterNot(predicate) 24 | .custom(assertIsEmpty()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /.github/workflows/gradle.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: 4 | push: 5 | branches: [ master ] 6 | pull_request: 7 | branches: [ master ] 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | strategy: 13 | matrix: 14 | java: [ '8', '11' ] 15 | 16 | name: Java ${{ matrix.java }} build 17 | steps: 18 | - uses: actions/checkout@v4 19 | - uses: actions/setup-java@v4 20 | with: 21 | java-version: ${{ matrix.java }} 22 | distribution: 'temurin' 23 | - name: Build Java ${{ matrix.java }} 24 | uses: gradle/actions/setup-gradle@v3 25 | if: matrix.java != 11 26 | with: 27 | arguments: test 28 | - name: Build Java 11 29 | uses: gradle/actions/setup-gradle@v3 30 | if: matrix.java == 11 31 | with: 32 | arguments: jacocoTestReport coveralls 33 | env: 34 | COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }} 35 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 36 | SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} 37 | 38 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/ObjTakeUntilIndexed.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.IndexedPredicate; 4 | import com.annimon.stream.iterator.IndexedIterator; 5 | import com.annimon.stream.iterator.LsaExtIterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class ObjTakeUntilIndexed extends LsaExtIterator { 9 | 10 | private final IndexedIterator iterator; 11 | private final IndexedPredicate stopPredicate; 12 | 13 | public ObjTakeUntilIndexed( 14 | @NotNull IndexedIterator iterator, 15 | @NotNull IndexedPredicate predicate) { 16 | this.iterator = iterator; 17 | this.stopPredicate = predicate; 18 | } 19 | 20 | @Override 21 | protected void nextIteration() { 22 | hasNext = iterator.hasNext() && !(isInit && stopPredicate.test(iterator.getIndex(), next)); 23 | if (hasNext) { 24 | next = iterator.next(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/TakeUntilTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.Stream; 5 | import com.annimon.stream.function.Predicate; 6 | import com.annimon.stream.test.hamcrest.StreamMatcher; 7 | import org.junit.Test; 8 | import static com.annimon.stream.test.hamcrest.StreamMatcher.assertElements; 9 | import static org.hamcrest.Matchers.contains; 10 | 11 | public final class TakeUntilTest { 12 | 13 | @Test 14 | public void testTakeUntil() { 15 | Stream.of(2, 4, 6, 7, 8, 10, 11) 16 | .takeUntil(Predicate.Util.negate(Functions.remainder(2))) 17 | .custom(assertElements(contains( 18 | 2, 4, 6, 7 19 | ))); 20 | } 21 | 22 | @Test 23 | public void testTakeUntilOnEmptyStream() { 24 | Stream.empty() 25 | .takeUntil(Functions.remainder(2)) 26 | .custom(StreamMatcher.assertIsEmpty()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/DoubleMapIndexed.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.IndexedDoubleUnaryOperator; 4 | import com.annimon.stream.iterator.PrimitiveIndexedIterator; 5 | import com.annimon.stream.iterator.PrimitiveIterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class DoubleMapIndexed extends PrimitiveIterator.OfDouble { 9 | 10 | private final PrimitiveIndexedIterator.OfDouble iterator; 11 | private final IndexedDoubleUnaryOperator mapper; 12 | 13 | public DoubleMapIndexed( 14 | @NotNull PrimitiveIndexedIterator.OfDouble iterator, 15 | @NotNull IndexedDoubleUnaryOperator mapper) { 16 | this.iterator = iterator; 17 | this.mapper = mapper; 18 | } 19 | 20 | @Override 21 | public boolean hasNext() { 22 | return iterator.hasNext(); 23 | } 24 | 25 | @Override 26 | public double nextDouble() { 27 | return mapper.applyAsDouble(iterator.getIndex(), iterator.nextDouble()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/ObjScan.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.BiFunction; 4 | import com.annimon.stream.iterator.LsaExtIterator; 5 | import java.util.Iterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class ObjScan extends LsaExtIterator { 9 | 10 | private final Iterator iterator; 11 | private final BiFunction accumulator; 12 | 13 | public ObjScan( 14 | @NotNull Iterator iterator, 15 | @NotNull BiFunction accumulator) { 16 | this.iterator = iterator; 17 | this.accumulator = accumulator; 18 | } 19 | 20 | @Override 21 | protected void nextIteration() { 22 | hasNext = iterator.hasNext(); 23 | if (hasNext) { 24 | final T value = iterator.next(); 25 | if (isInit) { 26 | next = accumulator.apply(next, value); 27 | } else { 28 | next = value; 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/FilterNotTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import com.annimon.stream.Functions; 5 | import com.annimon.stream.function.DoublePredicate; 6 | import org.junit.Test; 7 | import static com.annimon.stream.test.hamcrest.DoubleStreamMatcher.assertElements; 8 | import static com.annimon.stream.test.hamcrest.DoubleStreamMatcher.assertIsEmpty; 9 | import static org.hamcrest.Matchers.arrayContaining; 10 | 11 | public final class FilterNotTest { 12 | 13 | @Test 14 | public void testFilterNot() { 15 | final DoublePredicate predicate = Functions.greaterThan(Math.PI); 16 | DoubleStream.of(0.012, 10.347, 3.039, 19.84, 100d) 17 | .filterNot(predicate) 18 | .custom(assertElements(arrayContaining( 19 | 0.012, 3.039 20 | ))); 21 | 22 | DoubleStream.of(4.096, 12) 23 | .filterNot(predicate) 24 | .custom(assertIsEmpty()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/OfIterableTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.Stream; 5 | import java.util.Iterator; 6 | import org.junit.Test; 7 | import static com.annimon.stream.test.hamcrest.StreamMatcher.assertElements; 8 | import static org.hamcrest.Matchers.contains; 9 | 10 | public final class OfIterableTest { 11 | 12 | @Test 13 | public void testStreamOfIterable() { 14 | Iterable iterable = new Iterable() { 15 | @Override 16 | public Iterator iterator() { 17 | return Functions.counterIterator(); 18 | } 19 | }; 20 | 21 | Stream.of(iterable) 22 | .limit(5) 23 | .custom(assertElements(contains( 24 | 0, 1, 2, 3, 4 25 | ))); 26 | } 27 | 28 | @Test(expected = NullPointerException.class) 29 | public void testStreamOfIterableNull() { 30 | Stream.of((Iterable)null); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/ToArrayTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import com.annimon.stream.function.IntSupplier; 5 | import org.junit.Test; 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public final class ToArrayTest { 9 | 10 | @Test 11 | public void testToArray() { 12 | assertEquals(IntStream.empty().toArray().length, 0); 13 | assertEquals(IntStream.of(100).toArray()[0], 100); 14 | assertEquals(IntStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9).skip(4).toArray().length, 5); 15 | 16 | assertEquals(IntStream.generate(new IntSupplier() { 17 | @Override 18 | public int getAsInt() { 19 | return -1; 20 | } 21 | }).limit(14).toArray().length, 14); 22 | 23 | assertEquals(IntStream.of(IntStream.generate(new IntSupplier() { 24 | @Override 25 | public int getAsInt() { 26 | return -1; 27 | } 28 | }).limit(14).toArray()).sum(), -14); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/MapToIntTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Stream; 4 | import com.annimon.stream.function.ToIntFunction; 5 | import org.junit.Test; 6 | import static com.annimon.stream.test.hamcrest.IntStreamMatcher.assertElements; 7 | import static org.hamcrest.Matchers.arrayContaining; 8 | 9 | public final class MapToIntTest { 10 | 11 | @Test 12 | public void testMapToInt() { 13 | final ToIntFunction stringToSquareInt = new ToIntFunction() { 14 | @Override 15 | public int applyAsInt(String t) { 16 | final String str = t.substring(1, t.length() - 1); 17 | final int value = Integer.parseInt(str); 18 | return value * value; 19 | } 20 | }; 21 | 22 | Stream.of("[2]", "[3]", "[4]", "[8]", "[25]") 23 | .mapToInt(stringToSquareInt) 24 | .custom(assertElements(arrayContaining( 25 | 4, 9, 16, 64, 625 26 | ))); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/function/ThrowableConsumerTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.IOException; 5 | import java.io.OutputStream; 6 | import org.junit.Test; 7 | import static org.junit.Assert.assertEquals; 8 | 9 | /** 10 | * Tests {@code ThrowableConsumer}. 11 | * 12 | * @see com.annimon.stream.function.ThrowableConsumer 13 | */ 14 | public class ThrowableConsumerTest { 15 | 16 | @Test 17 | public void testAccept() throws IOException { 18 | ByteArrayOutputStream baos = new ByteArrayOutputStream(5); 19 | 20 | writer.accept(baos); 21 | writer.accept(baos); 22 | writer.accept(baos); 23 | assertEquals("XXX", baos.toString()); 24 | } 25 | 26 | private static final ThrowableConsumer writer 27 | = new ThrowableConsumer() { 28 | @Override 29 | public void accept(OutputStream os) throws IOException { 30 | os.write('X'); 31 | } 32 | }; 33 | } 34 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/OfArrayTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.IntStreamMatcher.assertIsEmpty; 6 | import static org.junit.Assert.assertEquals; 7 | import static org.junit.Assert.assertFalse; 8 | 9 | public final class OfArrayTest { 10 | 11 | @Test 12 | public void testStreamOfInts() { 13 | int[] data1 = {1, 2, 3, 4, 5}; 14 | int[] data2 = {42}; 15 | int[] data3 = {}; 16 | 17 | assertEquals(5, IntStream.of(data1).count()); 18 | assertEquals(42, IntStream.of(data2).findFirst().getAsInt()); 19 | assertFalse(IntStream.of(data3).findFirst().isPresent()); 20 | } 21 | 22 | @Test(expected = NullPointerException.class) 23 | public void testStreamOfIntsNull() { 24 | IntStream.of((int[]) null); 25 | } 26 | 27 | @Test 28 | public void testStreamOfEmptyArray() { 29 | IntStream.of(new int[0]) 30 | .custom(assertIsEmpty()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/MapToLongTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Stream; 4 | import com.annimon.stream.function.ToLongFunction; 5 | import org.junit.Test; 6 | import static com.annimon.stream.test.hamcrest.LongStreamMatcher.assertElements; 7 | import static org.hamcrest.Matchers.arrayContaining; 8 | 9 | public final class MapToLongTest { 10 | 11 | @Test 12 | public void testMapToLong() { 13 | final ToLongFunction stringToSquareLong = new ToLongFunction() { 14 | @Override 15 | public long applyAsLong(String t) { 16 | final String str = t.substring(1, t.length() - 1); 17 | final long value = Long.parseLong(str); 18 | return value * value; 19 | } 20 | }; 21 | Stream.of("[2]", "[3]", "[4]", "[8]", "[25]") 22 | .mapToLong(stringToSquareLong) 23 | .custom(assertElements(arrayContaining( 24 | 4L, 9L, 16L, 64L, 625L 25 | ))); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/function/ThrowablePredicateTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | import java.io.IOException; 4 | import org.junit.Test; 5 | import static org.junit.Assert.assertFalse; 6 | 7 | /** 8 | * Tests {@code ThrowablePredicate}. 9 | * 10 | * @see com.annimon.stream.function.ThrowablePredicate 11 | */ 12 | public class ThrowablePredicateTest { 13 | 14 | @Test 15 | public void testNormal() throws IOException { 16 | assertFalse(throwablePredicate.test(false)); 17 | } 18 | 19 | @Test(expected = IOException.class) 20 | public void testThrow() throws IOException { 21 | throwablePredicate.test(true); 22 | } 23 | 24 | private static final ThrowablePredicate throwablePredicate = 25 | new ThrowablePredicate() { 26 | @Override 27 | public boolean test(Boolean value) throws IOException { 28 | if (value) { 29 | throw new IOException(); 30 | } 31 | return value; 32 | } 33 | }; 34 | 35 | } 36 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/MapTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import com.annimon.stream.function.LongUnaryOperator; 5 | import org.junit.Test; 6 | import static com.annimon.stream.test.hamcrest.LongStreamMatcher.assertElements; 7 | import static com.annimon.stream.test.hamcrest.LongStreamMatcher.assertIsEmpty; 8 | import static org.hamcrest.Matchers.arrayContaining; 9 | 10 | public final class MapTest { 11 | 12 | @Test 13 | public void testMap() { 14 | LongUnaryOperator negator = new LongUnaryOperator() { 15 | @Override 16 | public long applyAsLong(long operand) { 17 | return -operand; 18 | } 19 | }; 20 | LongStream.of(10L, 20L, 30L) 21 | .map(negator) 22 | .custom(assertElements(arrayContaining( 23 | -10L, -20L, -30L 24 | ))); 25 | 26 | LongStream.empty() 27 | .map(negator) 28 | .custom(assertIsEmpty()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/Collector.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream; 2 | 3 | import com.annimon.stream.function.BiConsumer; 4 | import com.annimon.stream.function.Function; 5 | import com.annimon.stream.function.Supplier; 6 | 7 | /** 8 | * The Collector of stream data. 9 | * 10 | * @param the type of input elements to the reduction operation 11 | * @param the mutable accumulation type of the reduction operation 12 | * @param the result type of the reduction operation 13 | * @see Stream#collect(com.annimon.stream.Collector) 14 | */ 15 | public interface Collector { 16 | 17 | /** 18 | * Function provides new containers. 19 | * 20 | * @return {@code Supplier} 21 | */ 22 | Supplier supplier(); 23 | 24 | /** 25 | * Function folds elements into container. 26 | * 27 | * @return {@code BiConsumer} 28 | */ 29 | BiConsumer accumulator(); 30 | 31 | /** 32 | * Function produces result by transforming intermediate type. 33 | * 34 | * @return {@code Function} 35 | */ 36 | Function finisher(); 37 | } -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/IntUnaryOperator.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents an operation on a single int-valued operand that produces an int-valued result. 5 | * This is the primitive type specialization of UnaryOperator for int. 6 | */ 7 | public interface IntUnaryOperator { 8 | 9 | /** 10 | * Applies this operator to the given operand. 11 | * 12 | * @param operand the operand 13 | * @return the operator result 14 | */ 15 | int applyAsInt(int operand); 16 | 17 | class Util { 18 | 19 | private Util() { } 20 | 21 | /** 22 | * Returns a unary operator that always returns its input argument. 23 | * 24 | * @return a unary operator that always returns its input argument 25 | */ 26 | public static IntUnaryOperator identity() { 27 | return new IntUnaryOperator() { 28 | @Override 29 | public int applyAsInt(int operand) { 30 | return operand; 31 | } 32 | }; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/ForEachTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import com.annimon.stream.function.LongConsumer; 5 | import org.junit.Test; 6 | import static org.hamcrest.MatcherAssert.assertThat; 7 | import static org.hamcrest.Matchers.is; 8 | import static org.junit.Assert.fail; 9 | 10 | public final class ForEachTest { 11 | 12 | @Test 13 | public void testForEach() { 14 | final long[] expected = {12L, 32L, 22L, 9L}; 15 | LongStream.of(12L, 32L, 22L, 9L).forEach(new LongConsumer() { 16 | 17 | private int index = 0; 18 | 19 | @Override 20 | public void accept(long value) { 21 | assertThat(value, is(expected[index++])); 22 | } 23 | }); 24 | } 25 | 26 | @Test 27 | public void testForEachOnEmptyStream() { 28 | LongStream.empty().forEach(new LongConsumer() { 29 | @Override 30 | public void accept(long value) { 31 | fail(); 32 | } 33 | }); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/SampleTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Stream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.StreamMatcher.assertElements; 6 | import static org.hamcrest.Matchers.contains; 7 | 8 | public final class SampleTest { 9 | 10 | @Test 11 | public void testSample() { 12 | Stream.of( 1, 2, 3, 1, 2, 3, 1, 2, 3) 13 | .sample(3) 14 | .custom(assertElements(contains( 15 | 1, 1, 1 16 | ))); 17 | } 18 | 19 | @Test 20 | public void testSampleWithStep1() { 21 | Stream.of( 1, 2, 3, 1, 2, 3, 1, 2, 3) 22 | .sample(1) 23 | .custom(assertElements(contains( 24 | 1, 2, 3, 1, 2, 3, 1, 2, 3 25 | ))); 26 | } 27 | 28 | @Test(expected = IllegalArgumentException.class, timeout=1000) 29 | public void testSampleWithNegativeStep() { 30 | Stream.of(1, 2, 3, 1, 2, 3, 1, 2, 3).sample(-1).count(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/MapToDoubleTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import com.annimon.stream.function.IntToDoubleFunction; 5 | import org.junit.Test; 6 | import static com.annimon.stream.test.hamcrest.DoubleStreamMatcher.assertElements; 7 | import static org.hamcrest.Matchers.arrayContaining; 8 | import static org.hamcrest.Matchers.closeTo; 9 | 10 | public final class MapToDoubleTest { 11 | 12 | @Test 13 | @SuppressWarnings("unchecked") 14 | public void testMapToDouble() { 15 | IntStream.rangeClosed(2, 4) 16 | .mapToDouble(new IntToDoubleFunction() { 17 | @Override 18 | public double applyAsDouble(int value) { 19 | return value / 10d; 20 | } 21 | }) 22 | .custom(assertElements(arrayContaining( 23 | closeTo(0.2, 0.00001), 24 | closeTo(0.3, 0.00001), 25 | closeTo(0.4, 0.00001) 26 | ))); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/IntScan.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.IntBinaryOperator; 4 | import com.annimon.stream.iterator.PrimitiveExtIterator; 5 | import com.annimon.stream.iterator.PrimitiveIterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class IntScan extends PrimitiveExtIterator.OfInt { 9 | 10 | private final PrimitiveIterator.OfInt iterator; 11 | private final IntBinaryOperator accumulator; 12 | 13 | public IntScan( 14 | @NotNull PrimitiveIterator.OfInt iterator, 15 | @NotNull IntBinaryOperator accumulator) { 16 | this.iterator = iterator; 17 | this.accumulator = accumulator; 18 | } 19 | 20 | @Override 21 | protected void nextIteration() { 22 | hasNext = iterator.hasNext(); 23 | if (hasNext) { 24 | final int current = iterator.nextInt(); 25 | if (isInit) { 26 | next = accumulator.applyAsInt(next, current); 27 | } else { 28 | next = current; 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/AnyMatchTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.IntStream; 5 | import com.annimon.stream.function.IntPredicate; 6 | import org.junit.Test; 7 | import static org.junit.Assert.assertFalse; 8 | import static org.junit.Assert.assertTrue; 9 | 10 | public final class AnyMatchTest { 11 | 12 | @Test 13 | public void testAnyMatch() { 14 | IntStream.empty().anyMatch(new IntPredicate() { 15 | @Override 16 | public boolean test(int value) { 17 | throw new IllegalStateException(); 18 | } 19 | }); 20 | 21 | assertTrue(IntStream.of(42).anyMatch(new IntPredicate() { 22 | @Override 23 | public boolean test(int value) { 24 | return value == 42; 25 | } 26 | })); 27 | 28 | assertTrue(IntStream.of(5, 7, 9, 10, 7, 5).anyMatch(Functions.remainderInt(2))); 29 | 30 | assertFalse(IntStream.of(5, 7, 9, 11, 7, 5).anyMatch(Functions.remainderInt(2))); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/LongUnaryOperator.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents an operation on a single {@code long}-valued operand 5 | * that produces a {@code long}-valued result. 6 | * 7 | * @since 1.1.4 8 | * @see UnaryOperator 9 | */ 10 | public interface LongUnaryOperator { 11 | 12 | /** 13 | * Applies this operator to the given operand. 14 | * 15 | * @param operand the operand 16 | * @return the operator result 17 | */ 18 | long applyAsLong(long operand); 19 | 20 | class Util { 21 | 22 | private Util() { } 23 | 24 | /** 25 | * Returns a unary operator that always returns its input argument. 26 | * 27 | * @return a unary operator that always returns its input argument 28 | */ 29 | public static LongUnaryOperator identity() { 30 | return new LongUnaryOperator() { 31 | @Override 32 | public long applyAsLong(long operand) { 33 | return operand; 34 | } 35 | }; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/NoneMatchTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.IntStream; 5 | import com.annimon.stream.function.IntPredicate; 6 | import org.junit.Test; 7 | import static org.junit.Assert.assertFalse; 8 | import static org.junit.Assert.assertTrue; 9 | 10 | public final class NoneMatchTest { 11 | 12 | @Test 13 | public void testNoneMatch() { 14 | IntStream.empty().noneMatch(new IntPredicate() { 15 | @Override 16 | public boolean test(int value) { 17 | throw new IllegalStateException(); 18 | } 19 | }); 20 | 21 | assertFalse(IntStream.of(42).noneMatch(new IntPredicate() { 22 | @Override 23 | public boolean test(int value) { 24 | return value == 42; 25 | } 26 | })); 27 | 28 | assertFalse(IntStream.of(5, 7, 9, 10, 7, 5).noneMatch(Functions.remainderInt(2))); 29 | 30 | assertTrue(IntStream.of(5, 7, 9, 11, 7, 5).noneMatch(Functions.remainderInt(2))); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/LongScan.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.LongBinaryOperator; 4 | import com.annimon.stream.iterator.PrimitiveExtIterator; 5 | import com.annimon.stream.iterator.PrimitiveIterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class LongScan extends PrimitiveExtIterator.OfLong { 9 | 10 | private final PrimitiveIterator.OfLong iterator; 11 | private final LongBinaryOperator accumulator; 12 | 13 | public LongScan( 14 | @NotNull PrimitiveIterator.OfLong iterator, 15 | @NotNull LongBinaryOperator accumulator) { 16 | this.iterator = iterator; 17 | this.accumulator = accumulator; 18 | } 19 | 20 | @Override 21 | protected void nextIteration() { 22 | hasNext = iterator.hasNext(); 23 | if (hasNext) { 24 | final long current = iterator.nextLong(); 25 | if (isInit) { 26 | next = accumulator.applyAsLong(next, current); 27 | } else { 28 | next = current; 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/MapTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import com.annimon.stream.function.DoubleUnaryOperator; 5 | import org.junit.Test; 6 | import static com.annimon.stream.test.hamcrest.DoubleStreamMatcher.assertElements; 7 | import static com.annimon.stream.test.hamcrest.DoubleStreamMatcher.assertIsEmpty; 8 | import static org.hamcrest.Matchers.arrayContaining; 9 | 10 | public final class MapTest { 11 | 12 | @Test 13 | public void testMap() { 14 | DoubleUnaryOperator negator = new DoubleUnaryOperator() { 15 | @Override 16 | public double applyAsDouble(double operand) { 17 | return -operand; 18 | } 19 | }; 20 | DoubleStream.of(0.012, 3.039, 100d) 21 | .map(negator) 22 | .custom(assertElements(arrayContaining( 23 | -0.012, -3.039, -100d 24 | ))); 25 | 26 | DoubleStream.empty() 27 | .map(negator) 28 | .custom(assertIsEmpty()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/ForEachTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import com.annimon.stream.function.DoubleConsumer; 5 | import org.junit.Test; 6 | import static org.hamcrest.MatcherAssert.assertThat; 7 | import static org.hamcrest.Matchers.closeTo; 8 | import static org.junit.Assert.fail; 9 | 10 | public final class ForEachTest { 11 | 12 | @Test 13 | public void testForEach() { 14 | final double[] expected = {1.2, 3.456}; 15 | DoubleStream.of(1.2, 3.456).forEach(new DoubleConsumer() { 16 | 17 | private int index = 0; 18 | 19 | @Override 20 | public void accept(double value) { 21 | assertThat(value, closeTo(expected[index++], 0.0001)); 22 | } 23 | }); 24 | } 25 | 26 | @Test 27 | public void testForEachOnEmptyStream() { 28 | DoubleStream.empty().forEach(new DoubleConsumer() { 29 | @Override 30 | public void accept(double value) { 31 | fail(); 32 | } 33 | }); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/ForEachTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import com.annimon.stream.function.IntConsumer; 5 | import org.junit.Test; 6 | import static org.junit.Assert.assertEquals; 7 | 8 | public final class ForEachTest { 9 | 10 | @Test 11 | public void testForEach() { 12 | IntStream.empty().forEach(new IntConsumer() { 13 | @Override 14 | public void accept(int value) { 15 | throw new IllegalStateException(); 16 | } 17 | }); 18 | 19 | IntStream.of(42).forEach(new IntConsumer() { 20 | @Override 21 | public void accept(int value) { 22 | assertEquals(42, value); 23 | } 24 | }); 25 | 26 | final int[] sum = new int[1]; 27 | 28 | IntStream.rangeClosed(10, 20).forEach(new IntConsumer() { 29 | @Override 30 | public void accept(int value) { 31 | sum[0] += value; 32 | } 33 | }); 34 | 35 | assertEquals(sum[0], 165); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/MapToObjTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import com.annimon.stream.function.LongFunction; 5 | import com.annimon.stream.test.hamcrest.StreamMatcher; 6 | import org.junit.Test; 7 | import static com.annimon.stream.test.hamcrest.StreamMatcher.assertElements; 8 | import static org.hamcrest.Matchers.contains; 9 | 10 | public final class MapToObjTest { 11 | 12 | @Test 13 | public void testMapToObj() { 14 | LongFunction longToString = new LongFunction() { 15 | @Override 16 | public String apply(long value) { 17 | return Long.toString(value); 18 | } 19 | }; 20 | 21 | LongStream.of(10L, 20L, 30L) 22 | .mapToObj(longToString) 23 | .custom(assertElements(contains( 24 | "10", "20", "30" 25 | ))); 26 | 27 | LongStream.empty() 28 | .mapToObj(longToString) 29 | .custom(StreamMatcher.assertIsEmpty()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/SampleTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.DoubleStreamMatcher.assertElements; 6 | import static org.hamcrest.Matchers.arrayContaining; 7 | 8 | public final class SampleTest { 9 | 10 | @Test 11 | public void testSample() { 12 | DoubleStream.of(1.2, 3.234, 0.09, 2.2, 80d) 13 | .sample(2) 14 | .custom(assertElements(arrayContaining( 15 | 1.2, 0.09, 80d 16 | ))); 17 | } 18 | 19 | @Test 20 | public void testSampleWithStep1() { 21 | DoubleStream.of(1.2, 3.234, 0.09, 2.2, 80d) 22 | .sample(1) 23 | .custom(assertElements(arrayContaining( 24 | 1.2, 3.234, 0.09, 2.2, 80d 25 | ))); 26 | } 27 | 28 | @Test(expected = IllegalArgumentException.class, timeout=1000) 29 | public void testSampleWithNegativeStep() { 30 | DoubleStream.of(1.2, 3.234).sample(-1).count(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/SampleTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import org.junit.Test; 5 | import static com.annimon.stream.test.hamcrest.IntStreamMatcher.assertElements; 6 | import static org.hamcrest.Matchers.arrayContaining; 7 | 8 | public final class SampleTest { 9 | 10 | @Test 11 | public void testSample() { 12 | IntStream.of( 1, 2, 3, 1, 2, 3, 1, 2, 3) 13 | .sample(3) 14 | .custom(assertElements(arrayContaining( 15 | 1, 1, 1 16 | ))); 17 | } 18 | 19 | @Test 20 | public void testSampleWithStep1() { 21 | IntStream.of( 1, 2, 3, 1, 2, 3, 1, 2, 3) 22 | .sample(1) 23 | .custom(assertElements(arrayContaining( 24 | 1, 2, 3, 1, 2, 3, 1, 2, 3 25 | ))); 26 | } 27 | 28 | @Test(expected = IllegalArgumentException.class, timeout=1000) 29 | public void testSampleWithNegativeStep() { 30 | IntStream.of(1, 2, 3, 1, 2, 3, 1, 2, 3).sample(-1).count(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/function/DoubleUnaryOperator.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.function; 2 | 3 | /** 4 | * Represents an operation on a single {@code double}-valued operand 5 | * that produces a {@code double}-valued result. 6 | * 7 | * @since 1.1.4 8 | * @see UnaryOperator 9 | */ 10 | public interface DoubleUnaryOperator { 11 | 12 | /** 13 | * Applies this operator to the given operand. 14 | * 15 | * @param operand the operand 16 | * @return the operator result 17 | */ 18 | double applyAsDouble(double operand); 19 | 20 | class Util { 21 | 22 | private Util() { } 23 | 24 | /** 25 | * Returns a unary operator that always returns its input argument. 26 | * 27 | * @return a unary operator that always returns its input argument 28 | */ 29 | public static DoubleUnaryOperator identity() { 30 | return new DoubleUnaryOperator() { 31 | @Override 32 | public double applyAsDouble(double operand) { 33 | return operand; 34 | } 35 | }; 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/ObjZip.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.BiFunction; 4 | import com.annimon.stream.iterator.LsaIterator; 5 | import java.util.Iterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class ObjZip extends LsaIterator { 9 | 10 | private final Iterator iterator1; 11 | private final Iterator iterator2; 12 | private final BiFunction combiner; 13 | 14 | public ObjZip( 15 | @NotNull Iterator iterator1, 16 | @NotNull Iterator iterator2, 17 | @NotNull BiFunction combiner) { 18 | this.iterator1 = iterator1; 19 | this.iterator2 = iterator2; 20 | this.combiner = combiner; 21 | } 22 | 23 | @Override 24 | public boolean hasNext() { 25 | return iterator1.hasNext() && iterator2.hasNext(); 26 | } 27 | 28 | @Override 29 | public R nextIteration() { 30 | return combiner.apply(iterator1.next(), iterator2.next()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/OfPrimitiveIteratorTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.junit.Test; 6 | import static org.hamcrest.CoreMatchers.is; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | 9 | public final class OfPrimitiveIteratorTest { 10 | 11 | @Test 12 | public void testStreamOfPrimitiveIterator() { 13 | int[] expected = { 0, 1 }; 14 | IntStream stream = IntStream.of(new PrimitiveIterator.OfInt() { 15 | 16 | int index = 0; 17 | 18 | @Override 19 | public boolean hasNext() { 20 | return index < 2; 21 | } 22 | 23 | @Override 24 | public int nextInt() { 25 | return index++; 26 | } 27 | }); 28 | assertThat(stream.toArray(), is(expected)); 29 | } 30 | 31 | @Test(expected = NullPointerException.class) 32 | public void testStreamOfPrimitiveIteratorNull() { 33 | IntStream.of((PrimitiveIterator.OfInt) null); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/DoubleScan.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.DoubleBinaryOperator; 4 | import com.annimon.stream.iterator.PrimitiveExtIterator; 5 | import com.annimon.stream.iterator.PrimitiveIterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class DoubleScan extends PrimitiveExtIterator.OfDouble { 9 | 10 | private final PrimitiveIterator.OfDouble iterator; 11 | private final DoubleBinaryOperator accumulator; 12 | 13 | public DoubleScan( 14 | @NotNull PrimitiveIterator.OfDouble iterator, 15 | @NotNull DoubleBinaryOperator accumulator) { 16 | this.iterator = iterator; 17 | this.accumulator = accumulator; 18 | } 19 | 20 | @Override 21 | protected void nextIteration() { 22 | hasNext = iterator.hasNext(); 23 | if (hasNext) { 24 | final double current = iterator.nextDouble(); 25 | if (isInit) { 26 | next = accumulator.applyAsDouble(next, current); 27 | } else { 28 | next = current; 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/doublestreamtests/MapToObjTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.doublestreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import com.annimon.stream.function.DoubleFunction; 5 | import com.annimon.stream.test.hamcrest.StreamMatcher; 6 | import org.junit.Test; 7 | import static com.annimon.stream.test.hamcrest.StreamMatcher.assertElements; 8 | import static org.hamcrest.Matchers.contains; 9 | 10 | public final class MapToObjTest { 11 | 12 | @Test 13 | public void testMapToObj() { 14 | DoubleFunction doubleToString = new DoubleFunction() { 15 | @Override 16 | public String apply(double value) { 17 | return Double.toString(value); 18 | } 19 | }; 20 | DoubleStream.of(1.0, 2.12, 3.234) 21 | .mapToObj(doubleToString) 22 | .custom(assertElements(contains( 23 | "1.0", "2.12", "3.234" 24 | ))); 25 | 26 | DoubleStream.empty() 27 | .mapToObj(doubleToString) 28 | .custom(StreamMatcher.assertIsEmpty()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/OfMapTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Collectors; 4 | import com.annimon.stream.Functions; 5 | import com.annimon.stream.Stream; 6 | import java.util.HashMap; 7 | import java.util.Map; 8 | import org.junit.Test; 9 | import static org.hamcrest.MatcherAssert.assertThat; 10 | import static org.hamcrest.Matchers.is; 11 | 12 | public final class OfMapTest { 13 | 14 | @Test 15 | public void testStreamOfMap() { 16 | final Map map = new HashMap(4); 17 | map.put("This", 1); 18 | map.put(" is ", 2); 19 | map.put("a", 3); 20 | map.put(" test", 4); 21 | 22 | String result = Stream.of(map) 23 | .sortBy(Functions.entryValue()) 24 | .map(Functions.entryKey()) 25 | .collect(Collectors.joining()); 26 | assertThat(result, is("This is a test")); 27 | } 28 | 29 | @Test(expected = NullPointerException.class) 30 | public void testStreamOfMapNull() { 31 | Stream.of((Map)null); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/iterator/LazyIterator.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.iterator; 2 | 3 | import java.util.Iterator; 4 | 5 | /** 6 | * Lazily gets {@code Iterator} from the given {@link Iterable}. 7 | * 8 | * @param the type of the elements 9 | */ 10 | public class LazyIterator implements Iterator { 11 | private final Iterable iterable; 12 | private Iterator iterator; 13 | 14 | public LazyIterator(Iterable iterable) { 15 | this.iterable = iterable; 16 | } 17 | 18 | private void ensureIterator() { 19 | if (iterator != null) { 20 | return; 21 | } 22 | // Lazily creates Iterator object. 23 | iterator = iterable.iterator(); 24 | } 25 | 26 | @Override 27 | public boolean hasNext() { 28 | ensureIterator(); 29 | return iterator.hasNext(); 30 | } 31 | 32 | @Override 33 | public T next() { 34 | ensureIterator(); 35 | return iterator.next(); 36 | } 37 | 38 | @Override 39 | public void remove() { 40 | ensureIterator(); 41 | iterator.remove(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/ObjDistinctBy.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.Function; 4 | import com.annimon.stream.iterator.LsaExtIterator; 5 | import java.util.HashSet; 6 | import java.util.Iterator; 7 | import java.util.Set; 8 | import org.jetbrains.annotations.NotNull; 9 | 10 | public class ObjDistinctBy extends LsaExtIterator { 11 | 12 | private final Iterator iterator; 13 | private final Function classifier; 14 | private final Set set; 15 | 16 | public ObjDistinctBy( 17 | @NotNull Iterator iterator, 18 | @NotNull Function classifier) { 19 | this.iterator = iterator; 20 | this.classifier = classifier; 21 | set = new HashSet(); 22 | } 23 | 24 | @Override 25 | protected void nextIteration() { 26 | while (hasNext = iterator.hasNext()) { 27 | next = iterator.next(); 28 | final K key = classifier.apply(next); 29 | if (set.add(key)) { 30 | return; 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/MapToDoubleTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Stream; 4 | import com.annimon.stream.function.ToDoubleFunction; 5 | import org.junit.Test; 6 | import static com.annimon.stream.test.hamcrest.DoubleStreamMatcher.assertElements; 7 | import static org.hamcrest.Matchers.arrayContaining; 8 | import static org.hamcrest.Matchers.closeTo; 9 | 10 | public final class MapToDoubleTest { 11 | 12 | @Test 13 | @SuppressWarnings("unchecked") 14 | public void testMapToDouble() { 15 | final ToDoubleFunction stringToDouble = new ToDoubleFunction() { 16 | @Override 17 | public double applyAsDouble(String t) { 18 | return Double.parseDouble(t); 19 | } 20 | }; 21 | Stream.of("1.23", "4.56789", "10.1112") 22 | .mapToDouble(stringToDouble) 23 | .custom(assertElements(arrayContaining( 24 | closeTo(1.23, 0.000001), 25 | closeTo(4.56789, 0.000001), 26 | closeTo(10.1112, 0.000001) 27 | ))); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/MapToDoubleTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import com.annimon.stream.LongStream; 5 | import com.annimon.stream.function.LongToDoubleFunction; 6 | import com.annimon.stream.test.hamcrest.DoubleStreamMatcher; 7 | import org.junit.Test; 8 | import static org.hamcrest.MatcherAssert.assertThat; 9 | import static org.hamcrest.Matchers.array; 10 | import static org.hamcrest.Matchers.closeTo; 11 | 12 | public final class MapToDoubleTest { 13 | 14 | @Test 15 | @SuppressWarnings("unchecked") 16 | public void testMapToDouble() { 17 | DoubleStream stream = LongStream.rangeClosed(2, 4) 18 | .mapToDouble(new LongToDoubleFunction() { 19 | @Override 20 | public double applyAsDouble(long value) { 21 | return value / 10d; 22 | } 23 | }); 24 | assertThat(stream, DoubleStreamMatcher.elements(array( 25 | closeTo(0.2, 0.00001), 26 | closeTo(0.3, 0.00001), 27 | closeTo(0.4, 0.00001) 28 | ))); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/ObjDropWhile.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.Predicate; 4 | import com.annimon.stream.iterator.LsaExtIterator; 5 | import java.util.Iterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class ObjDropWhile extends LsaExtIterator { 9 | 10 | private final Iterator iterator; 11 | private final Predicate predicate; 12 | 13 | public ObjDropWhile( 14 | @NotNull Iterator iterator, 15 | @NotNull Predicate predicate) { 16 | this.iterator = iterator; 17 | this.predicate = predicate; 18 | } 19 | 20 | @Override 21 | protected void nextIteration() { 22 | if (!isInit) { 23 | // Skip first time 24 | while (hasNext = iterator.hasNext()) { 25 | next = iterator.next(); 26 | if (!predicate.test(next)) { 27 | return; 28 | } 29 | } 30 | } 31 | 32 | hasNext = hasNext && iterator.hasNext(); 33 | if (!hasNext) return; 34 | 35 | next = iterator.next(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/FlatMapToIntTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.IntStream; 4 | import com.annimon.stream.Stream; 5 | import com.annimon.stream.function.Function; 6 | import com.annimon.stream.function.IntUnaryOperator; 7 | import org.junit.Test; 8 | import static com.annimon.stream.test.hamcrest.IntStreamMatcher.assertElements; 9 | import static org.hamcrest.Matchers.arrayContaining; 10 | 11 | public final class FlatMapToIntTest { 12 | 13 | @Test 14 | public void testFlatMapToInt() { 15 | Stream.rangeClosed(2, 4) 16 | .flatMapToInt(new Function() { 17 | 18 | @Override 19 | public IntStream apply(Integer t) { 20 | return IntStream 21 | .iterate(t, IntUnaryOperator.Util.identity()) 22 | .limit(t); 23 | } 24 | }) 25 | .custom(assertElements(arrayContaining( 26 | 2, 2, 27 | 3, 3, 3, 28 | 4, 4, 4, 4 29 | ))); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/GroupByTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.Stream; 5 | import java.util.AbstractMap; 6 | import java.util.Arrays; 7 | import java.util.List; 8 | import java.util.Map; 9 | import org.junit.Test; 10 | import static com.annimon.stream.test.hamcrest.StreamMatcher.assertElements; 11 | import static org.hamcrest.Matchers.containsInAnyOrder; 12 | 13 | public final class GroupByTest { 14 | 15 | @Test 16 | @SuppressWarnings("unchecked") 17 | public void testGroupBy() { 18 | final Integer partitionItem = 1; 19 | 20 | Stream.of(1, 2, 3, 1, 2, 3, 1, 2, 3) 21 | .groupBy(Functions.equalityPartitionItem(partitionItem)) 22 | .custom(assertElements(containsInAnyOrder( 23 | entry(false, Arrays.asList(2, 3, 2, 3, 2, 3)), 24 | entry(true, Arrays.asList(1, 1, 1)) 25 | ))); 26 | } 27 | 28 | private Map.Entry> entry(boolean key, List value) { 29 | return new AbstractMap.SimpleEntry>(key, value); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/FlatMapToDoubleTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.DoubleStream; 4 | import com.annimon.stream.Stream; 5 | import com.annimon.stream.function.Function; 6 | import org.junit.Test; 7 | import static com.annimon.stream.test.hamcrest.DoubleStreamMatcher.assertElements; 8 | import static org.hamcrest.Matchers.array; 9 | import static org.hamcrest.Matchers.closeTo; 10 | 11 | public final class FlatMapToDoubleTest { 12 | 13 | @Test 14 | @SuppressWarnings("unchecked") 15 | public void testFlatMapToDouble() { 16 | Stream.of(2, 4) 17 | .flatMapToDouble(new Function() { 18 | @Override 19 | public DoubleStream apply(Integer t) { 20 | return DoubleStream.of(t / 10d, t / 20d); 21 | } 22 | }) 23 | .custom(assertElements(array( 24 | closeTo(0.2, 0.0001), 25 | closeTo(0.1, 0.0001), 26 | closeTo(0.4, 0.0001), 27 | closeTo(0.2, 0.0001) 28 | ))); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/ToArrayTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.Stream; 5 | import org.junit.Test; 6 | import static org.hamcrest.MatcherAssert.assertThat; 7 | import static org.hamcrest.Matchers.instanceOf; 8 | import static org.junit.Assert.assertEquals; 9 | import static org.junit.Assert.assertNotNull; 10 | import static org.junit.Assert.assertTrue; 11 | 12 | public final class ToArrayTest { 13 | 14 | @Test 15 | public void testToArray() { 16 | Object[] objects = Stream.range(0, 200) 17 | .filter(Functions.remainder(4)) 18 | .toArray(); 19 | 20 | assertEquals(50, objects.length); 21 | assertNotNull(objects[10]); 22 | assertThat(objects[0], instanceOf(Integer.class)); 23 | } 24 | 25 | @Test 26 | public void testToArrayWithGenerator() { 27 | Integer[] numbers = Stream.range(1, 1000) 28 | .filter(Functions.remainder(2)) 29 | .toArray(Functions.arrayGenerator(Integer[].class)); 30 | 31 | assertTrue(numbers.length > 0); 32 | assertNotNull(numbers[100]); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/streamtests/FlatMapToLongTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.streamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import com.annimon.stream.Stream; 5 | import com.annimon.stream.function.Function; 6 | import com.annimon.stream.function.LongUnaryOperator; 7 | import org.junit.Test; 8 | import static com.annimon.stream.test.hamcrest.LongStreamMatcher.assertElements; 9 | import static org.hamcrest.Matchers.arrayContaining; 10 | 11 | public final class FlatMapToLongTest { 12 | 13 | @Test 14 | public void testFlatMapToLong() { 15 | Stream.rangeClosed(2L, 4L) 16 | .flatMapToLong(new Function() { 17 | @Override 18 | public LongStream apply(Long t) { 19 | return LongStream 20 | .iterate(t, LongUnaryOperator.Util.identity()) 21 | .limit(t); 22 | } 23 | }) 24 | .custom(assertElements(arrayContaining( 25 | 2L, 2L, 26 | 3L, 3L, 3L, 27 | 4L, 4L, 4L, 4L 28 | ))); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/intstreamtests/AllMatchTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.intstreamtests; 2 | 3 | import com.annimon.stream.Functions; 4 | import com.annimon.stream.IntStream; 5 | import com.annimon.stream.function.IntPredicate; 6 | import org.junit.Test; 7 | import static org.junit.Assert.assertFalse; 8 | import static org.junit.Assert.assertTrue; 9 | 10 | public final class AllMatchTest { 11 | 12 | @Test 13 | public void testAllMatch() { 14 | IntStream.empty().allMatch(new IntPredicate() { 15 | @Override 16 | public boolean test(int value) { 17 | throw new IllegalStateException(); 18 | } 19 | }); 20 | 21 | assertTrue(IntStream.of(42).allMatch(new IntPredicate() { 22 | @Override 23 | public boolean test(int value) { 24 | return value == 42; 25 | } 26 | })); 27 | 28 | assertFalse(IntStream.of(5, 7, 9, 10, 7, 5).allMatch( 29 | IntPredicate.Util.negate(Functions.remainderInt(2)))); 30 | 31 | assertTrue(IntStream.of(5, 7, 9, 11, 7, 5).allMatch( 32 | IntPredicate.Util.negate(Functions.remainderInt(2)))); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/PeekTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import com.annimon.stream.function.LongConsumer; 5 | import org.junit.Test; 6 | import static com.annimon.stream.test.hamcrest.LongStreamMatcher.assertIsEmpty; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | import static org.hamcrest.Matchers.is; 9 | import static org.junit.Assert.fail; 10 | 11 | public final class PeekTest { 12 | 13 | @Test 14 | public void testPeek() { 15 | LongStream.empty() 16 | .peek(new LongConsumer() { 17 | @Override 18 | public void accept(long value) { 19 | fail(); 20 | } 21 | }) 22 | .custom(assertIsEmpty()); 23 | 24 | final long[] expected = {12, 34}; 25 | assertThat(LongStream.of(12, 34).peek(new LongConsumer() { 26 | 27 | private int index = 0; 28 | 29 | @Override 30 | public void accept(long value) { 31 | assertThat(value, is(expected[index++])); 32 | } 33 | }).count(), is(2L)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /stream/src/test/java/com/annimon/stream/longstreamtests/OfPrimitiveIteratorTest.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.longstreamtests; 2 | 3 | import com.annimon.stream.LongStream; 4 | import com.annimon.stream.iterator.PrimitiveIterator; 5 | import org.junit.Test; 6 | import static com.annimon.stream.test.hamcrest.LongStreamMatcher.elements; 7 | import static org.hamcrest.MatcherAssert.assertThat; 8 | import static org.hamcrest.Matchers.arrayContaining; 9 | 10 | public final class OfPrimitiveIteratorTest { 11 | 12 | @Test 13 | public void testStreamOfPrimitiveIterator() { 14 | LongStream stream = LongStream.of(new PrimitiveIterator.OfLong() { 15 | 16 | private int index = 0; 17 | 18 | @Override 19 | public boolean hasNext() { 20 | return index < 3; 21 | } 22 | 23 | @Override 24 | public long nextLong() { 25 | return ++index; 26 | } 27 | }); 28 | assertThat(stream, elements(arrayContaining(1L, 2L, 3L))); 29 | } 30 | 31 | @Test(expected = NullPointerException.class) 32 | public void testStreamOfPrimitiveIteratorNull() { 33 | LongStream.of((PrimitiveIterator.OfLong) null); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /stream/src/main/java/com/annimon/stream/operator/IntDropWhile.java: -------------------------------------------------------------------------------- 1 | package com.annimon.stream.operator; 2 | 3 | import com.annimon.stream.function.IntPredicate; 4 | import com.annimon.stream.iterator.PrimitiveExtIterator; 5 | import com.annimon.stream.iterator.PrimitiveIterator; 6 | import org.jetbrains.annotations.NotNull; 7 | 8 | public class IntDropWhile extends PrimitiveExtIterator.OfInt { 9 | 10 | private final PrimitiveIterator.OfInt iterator; 11 | private final IntPredicate predicate; 12 | 13 | public IntDropWhile( 14 | @NotNull PrimitiveIterator.OfInt iterator, 15 | @NotNull IntPredicate predicate) { 16 | this.iterator = iterator; 17 | this.predicate = predicate; 18 | } 19 | 20 | @Override 21 | protected void nextIteration() { 22 | if (!isInit) { 23 | // Skip first time 24 | while (hasNext = iterator.hasNext()) { 25 | next = iterator.nextInt(); 26 | if (!predicate.test(next)) { 27 | return; 28 | } 29 | } 30 | } 31 | 32 | hasNext = hasNext && iterator.hasNext(); 33 | if (!hasNext) return; 34 | 35 | next = iterator.nextInt(); 36 | } 37 | } 38 | --------------------------------------------------------------------------------