Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into issue/amaembo#185-…
Browse files Browse the repository at this point in the history
…without

# Conflicts:
#	wiki/CHANGES.md
  • Loading branch information
born-to-be-mad committed Dec 4, 2020
2 parents 56aaa86 + 49e46f9 commit 74cf4df
Show file tree
Hide file tree
Showing 25 changed files with 4,691 additions and 4,496 deletions.
13 changes: 0 additions & 13 deletions .idea/libraries/Maven__junit_junit_4_12.xml

This file was deleted.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__junit_junit_4_13.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .idea/streamex.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/one/util/streamex/StreamEx.java
Original file line number Diff line number Diff line change
Expand Up @@ -1759,16 +1759,17 @@ public <U> StreamEx<U> intervalMap(BiPredicate<? super T, ? super T> sameInterva

/**
* Returns a stream consisting of the results of applying the given function
* to the the first element and every other element of this stream.
* to the the first element and every single element of this stream.
* When the mapper is called for the first element of the resulting stream,
* both its arguments are the same and equal to the first element of this stream.
*
* <p>
* This is a <a href="package-summary.html#StreamOps">quasi-intermediate
* operation</a>.
*
* <p>
* The size of the resulting stream is one element less than the input
* stream. If the input stream is empty or contains just one element, then
* the output stream will be empty.
* The size of the resulting stream is exactly the same as the size of the input
* stream.
*
* @param <R> The element type of the new stream
* @param mapper a non-interfering, stateless function to apply to the first
Expand All @@ -1786,16 +1787,16 @@ public <R> StreamEx<R> withFirst(BiFunction<? super T, ? super T, ? extends R> m
/**
* Creates an {@link EntryStream} consisting of the {@link Entry} objects
* which keys are all the same and equal to the first element of this stream
* and values are the rest elements of this stream.
* and values are the original elements of this stream. The first element
* of the resulting stream has the same key and value.
*
* <p>
* This is a <a href="package-summary.html#StreamOps">quasi-intermediate
* operation</a>.
*
* <p>
* The size of the resulting stream is one element less than the input
* stream. If the input stream is empty or contains just one element, then
* the output stream will be empty.
* The size of the resulting stream is exactly the same as the size of the input
* stream.
*
* @return the new stream
* @see #withFirst(BiFunction)
Expand Down
218 changes: 109 additions & 109 deletions src/test/java/one/util/streamex/AverageLongTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
49 changes: 49 additions & 0 deletions src/test/java/one/util/streamex/EntryStreamInternalTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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() {
EntryStream<String, Integer> stream = EntryStream.of(createMap());
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;
}

}
Loading

0 comments on commit 74cf4df

Please # to comment.