Skip to content

Commit

Permalink
Merge 36d9302 into adbaa5c
Browse files Browse the repository at this point in the history
  • Loading branch information
born-to-be-mad committed Jun 19, 2020
2 parents adbaa5c + 36d9302 commit 4b93ae0
Show file tree
Hide file tree
Showing 22 changed files with 4,638 additions and 4,407 deletions.
6 changes: 3 additions & 3 deletions README.md
@@ -1,4 +1,4 @@
# StreamEx 0.7.2
# StreamEx 0.7.3
Enhancing Java Stream API.

[![Maven Central](https://img.shields.io/maven-central/v/one.util/streamex.svg)](https://maven-badges.herokuapp.com/maven-central/one.util/streamex/)
Expand Down Expand Up @@ -133,7 +133,7 @@ Add this snippet to the pom.xml `dependencies` section:
<dependency>
<groupId>one.util</groupId>
<artifactId>streamex</artifactId>
<version>0.7.2</version>
<version>0.7.3</version>
</dependency>
```

Expand All @@ -142,7 +142,7 @@ Add this snippet to the pom.xml `dependencies` section:
Add this snippet to the build.gradle `dependencies` section:

```groovy
implementation 'one.util:streamex:0.7.2'
implementation 'one.util:streamex:0.7.3'
```

Pull requests are welcome.
218 changes: 109 additions & 109 deletions src/test/java/one/util/streamex/AverageLongTest.java
@@ -1,109 +1,109 @@
/*
* Copyright 2015, 2019 StreamEx contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package one.util.streamex;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.util.Arrays;
import java.util.OptionalDouble;
import java.util.function.BiFunction;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;

import org.junit.Test;

import static one.util.streamex.Internals.AverageLong;
import static one.util.streamex.TestHelpers.repeat;
import static one.util.streamex.TestHelpers.withRandom;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

/**
* @author Tagir Valeev
*/
public class AverageLongTest {

@Test
public void testAverageLongNoOverflow() {
AverageLong avg = new AverageLong();
assertFalse(avg.result().isPresent());
avg.accept(1);
avg.accept(2);
avg.accept(3);
assertEquals(2.0, avg.result().getAsDouble(), 0.0);

avg.accept(2);
avg.accept(-4);
avg.accept(8);
assertEquals(2.0, avg.result().getAsDouble(), 0.0);

AverageLong avg1 = new AverageLong();
avg1.accept(-2);
AverageLong avg2 = new AverageLong();
avg2.accept(-2);
assertEquals(-2.0, avg1.combine(avg2).result().getAsDouble(), 0.0);

withRandom(r -> {
int[] input = r.ints(1000).toArray();
OptionalDouble expected = IntStream.of(input).average();
assertEquals(expected, Arrays.stream(input)
.collect(AverageLong::new, AverageLong::accept, AverageLong::combine).result());

assertEquals(expected, Arrays.stream(input).parallel().collect(AverageLong::new, AverageLong::accept,
AverageLong::combine).result());
});
}

@Test
public void testCombine() {
withRandom(r -> repeat(100, i -> {
AverageLong avg1 = new AverageLong();
AverageLong avg2 = new AverageLong();
long[] set1 = r.longs(100).toArray();
long[] set2 = r.longs(100).toArray();
double expected = LongStreamEx.of(set1).append(set2).boxed().collect(getBigIntegerAverager()).getAsDouble();
LongStream.of(set1).forEach(avg1::accept);
LongStream.of(set2).forEach(avg2::accept);
assertEquals(expected, avg1.combine(avg2).result().getAsDouble(), Math.abs(expected / 1e14));
}));
}

@Test
public void testCompareToBigInteger() {
withRandom(r -> {
long[] input = LongStreamEx.of(r, 1000).toArray();
Supplier<LongStream> supplier = () -> Arrays.stream(input);
double expected = supplier.get().boxed().collect(getBigIntegerAverager()).getAsDouble();
assertEquals(expected, supplier.get().collect(AverageLong::new, AverageLong::accept, AverageLong::combine)
.result().getAsDouble(), Math.abs(expected) / 1e14);
assertEquals(expected, supplier.get().parallel().collect(AverageLong::new, AverageLong::accept,
AverageLong::combine).result().getAsDouble(), Math.abs(expected) / 1e14);
});
}

private static Collector<Long, ?, OptionalDouble> getBigIntegerAverager() {
BiFunction<BigInteger, Long, OptionalDouble> finisher = (BigInteger sum, Long cnt) -> cnt == 0L ? OptionalDouble
.empty()
: OptionalDouble.of(new BigDecimal(sum).divide(BigDecimal.valueOf(cnt), MathContext.DECIMAL64)
.doubleValue());
return MoreCollectors.pairing(Collectors.reducing(BigInteger.ZERO,
BigInteger::valueOf, BigInteger::add), Collectors.counting(), finisher);
}
}
/*
* Copyright 2015, 2019 StreamEx contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package one.util.streamex;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.util.Arrays;
import java.util.OptionalDouble;
import java.util.function.BiFunction;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;

import org.junit.Test;

import static one.util.streamex.Internals.AverageLong;
import static one.util.streamex.TestHelpers.repeat;
import static one.util.streamex.TestHelpers.withRandom;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

/**
* @author Tagir Valeev
*/
public class AverageLongTest {

@Test
public void testAverageLongNoOverflow() {
AverageLong avg = new AverageLong();
assertFalse(avg.result().isPresent());
avg.accept(1);
avg.accept(2);
avg.accept(3);
assertEquals(2.0, avg.result().getAsDouble(), 0.0);

avg.accept(2);
avg.accept(-4);
avg.accept(8);
assertEquals(2.0, avg.result().getAsDouble(), 0.0);

AverageLong avg1 = new AverageLong();
avg1.accept(-2);
AverageLong avg2 = new AverageLong();
avg2.accept(-2);
assertEquals(-2.0, avg1.combine(avg2).result().getAsDouble(), 0.0);

withRandom(r -> {
int[] input = r.ints(1000).toArray();
OptionalDouble expected = IntStream.of(input).average();
assertEquals(expected, Arrays.stream(input)
.collect(AverageLong::new, AverageLong::accept, AverageLong::combine).result());

assertEquals(expected, Arrays.stream(input).parallel().collect(AverageLong::new, AverageLong::accept,
AverageLong::combine).result());
});
}

@Test
public void testCombine() {
withRandom(r -> repeat(100, i -> {
AverageLong avg1 = new AverageLong();
AverageLong avg2 = new AverageLong();
long[] set1 = r.longs(100).toArray();
long[] set2 = r.longs(100).toArray();
double expected = LongStreamEx.of(set1).append(set2).boxed().collect(getBigIntegerAverager()).getAsDouble();
LongStream.of(set1).forEach(avg1::accept);
LongStream.of(set2).forEach(avg2::accept);
assertEquals(expected, avg1.combine(avg2).result().getAsDouble(), Math.abs(expected / 1e14));
}));
}

@Test
public void testCompareToBigInteger() {
withRandom(r -> {
long[] input = LongStreamEx.of(r, 1000).toArray();
Supplier<LongStream> supplier = () -> Arrays.stream(input);
double expected = supplier.get().boxed().collect(getBigIntegerAverager()).getAsDouble();
assertEquals(expected, supplier.get().collect(AverageLong::new, AverageLong::accept, AverageLong::combine)
.result().getAsDouble(), Math.abs(expected) / 1e14);
assertEquals(expected, supplier.get().parallel().collect(AverageLong::new, AverageLong::accept,
AverageLong::combine).result().getAsDouble(), Math.abs(expected) / 1e14);
});
}

private static Collector<Long, ?, OptionalDouble> getBigIntegerAverager() {
BiFunction<BigInteger, Long, OptionalDouble> finisher = (BigInteger sum, Long cnt) -> cnt == 0L ? OptionalDouble
.empty()
: OptionalDouble.of(new BigDecimal(sum).divide(BigDecimal.valueOf(cnt), MathContext.DECIMAL64)
.doubleValue());
return MoreCollectors.pairing(Collectors.reducing(BigInteger.ZERO,
BigInteger::valueOf, BigInteger::add), Collectors.counting(), finisher);
}
}
53 changes: 53 additions & 0 deletions src/test/java/one/util/streamex/EntryStreamInternalTest.java
@@ -0,0 +1,53 @@
/*
* Copyright 2015, 2020 StreamEx contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package one.util.streamex;

import java.util.LinkedHashMap;
import java.util.Map;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;

/**
* @author Tagir Valeev
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class EntryStreamInternalTest {

@Test
public void testCreate() {
Map<String, Integer> data = createMap();
assertEquals(data, EntryStream.of(data).toMap());
assertEquals(data, EntryStream.of(data.entrySet().stream()).toMap());

EntryStream<String, Integer> stream = EntryStream.of(data);
assertSame(stream.stream(), EntryStream.of(stream).stream());
assertSame(stream.stream(), EntryStream.of(StreamEx.of(EntryStream.of(stream))).stream());
}

private static Map<String, Integer> createMap() {
Map<String, Integer> data = new LinkedHashMap<>();
data.put("a", 1);
data.put("bb", 22);
data.put("ccc", 33);
return data;
}

}
64 changes: 64 additions & 0 deletions src/test/java/one/util/streamex/MoreCollectorsInternalTest.java
@@ -0,0 +1,64 @@
/*
* Copyright 2015, 2020 StreamEx contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package one.util.streamex;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Stream;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import one.util.streamex.Internals.BooleanMap;

import static java.util.Arrays.asList;
import static one.util.streamex.TestHelpers.checkCollector;
import static one.util.streamex.TestHelpers.checkShortCircuitCollector;

/**
* @author Tagir Valeev
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MoreCollectorsInternalTest {
@Test
public void testPartitioningBy() {
Collector<Integer, ?, Map<Boolean, Optional<Integer>>> by20 = MoreCollectors.partitioningBy(x -> x % 20 == 0,
MoreCollectors.first());
Collector<Integer, ?, Map<Boolean, Optional<Integer>>> by200 = MoreCollectors.partitioningBy(x -> x % 200 == 0,
MoreCollectors.first());
Supplier<Stream<Integer>> supplier = () -> IntStreamEx.range(1, 100).boxed();
checkShortCircuitCollector("by20", new BooleanMap<>(Optional.of(20), Optional.of(1)), 20, supplier, by20);
checkShortCircuitCollector("by200", new BooleanMap<>(Optional.empty(), Optional.of(1)), 99, supplier, by200);
}

@Test
public void testMapping() {
List<String> input = asList("Capital", "lower", "Foo", "bar");
Collector<String, ?, Map<Boolean, Optional<Integer>>> collector = MoreCollectors
.partitioningBy(str -> Character.isUpperCase(str.charAt(0)), MoreCollectors.mapping(String::length,
MoreCollectors.first()));
checkShortCircuitCollector("mapping", new BooleanMap<>(Optional.of(7), Optional.of(5)), 2, input::stream,
collector);

Collector<String, ?, Map<Boolean, Optional<Integer>>> collectorLast = MoreCollectors.partitioningBy(
str -> Character.isUpperCase(str.charAt(0)), MoreCollectors.mapping(String::length, MoreCollectors.last()));
checkCollector("last", new BooleanMap<>(Optional.of(3), Optional.of(3)), input::stream, collectorLast);
}
}

0 comments on commit 4b93ae0

Please sign in to comment.