Skip to content

Commit

Permalink
Adds support for other stream shapes (IntStream, LongStream, DoubleSt…
Browse files Browse the repository at this point in the history
…ream)
  • Loading branch information
bric3 authored and joel-costigliola committed Feb 12, 2017
1 parent b42f523 commit 73e79f7
Show file tree
Hide file tree
Showing 11 changed files with 548 additions and 67 deletions.
21 changes: 12 additions & 9 deletions src/main/java/org/assertj/core/api/AbstractBDDSoftAssertions.java
Expand Up @@ -12,8 +12,6 @@
*/
package org.assertj.core.api;

import org.assertj.core.util.CheckReturnValue;

import java.nio.file.Path;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand All @@ -31,7 +29,9 @@
import java.util.function.IntPredicate;
import java.util.function.LongPredicate;
import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.BaseStream;

import org.assertj.core.util.CheckReturnValue;

public abstract class AbstractBDDSoftAssertions extends Java6AbstractBDDSoftAssertions {

Expand Down Expand Up @@ -229,18 +229,21 @@ public LongPredicateAssert then(LongPredicate actual) {
}

/**
* Creates a new instance of <code>{@link ListAssert}</code> from the given {@link Stream}.
* Creates a new instance of <code>{@link ListAssert}</code> from the given {@link BaseStream}.
* <p>
* <b>Be aware that to create the returned {@link ListAssert} the given the {@link Stream} is consumed so it won't be
* <b>Be aware that to create the returned {@link ListAssert} the given the {@link BaseStream} is consumed so it won't be
* possible to use it again.</b> Calling multiple methods on the returned {@link ListAssert} is safe as it only
* interacts with the {@link List} built from the {@link Stream}.
* interacts with the {@link List} built from the {@link BaseStream}.
*
* <p>This method accepts {@link java.util.stream.Stream} and primitive stream variants
* {@link java.util.stream.IntStream}, {@link java.util.stream.LongStream} and {@link java.util.stream.DoubleStream}.
*
* @param actual the actual {@link Stream} value.
* @param actual the actual {@link BaseStream} value.
* @return the created assertion object.
*/
@SuppressWarnings("unchecked")
@CheckReturnValue
public <ELEMENT> ListAssert<ELEMENT> then(Stream<? extends ELEMENT> actual) {
return proxy(ListAssert.class, Stream.class, actual);
public <ELEMENT, STREAM extends BaseStream<ELEMENT, STREAM>> ListAssert<ELEMENT> then(BaseStream<? extends ELEMENT, STREAM> actual) {
return proxy(ListAssert.class, BaseStream.class, actual);
}
}
Expand Up @@ -12,8 +12,6 @@
*/
package org.assertj.core.api;

import org.assertj.core.util.CheckReturnValue;

import java.nio.file.Path;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand All @@ -31,7 +29,9 @@
import java.util.function.IntPredicate;
import java.util.function.LongPredicate;
import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.BaseStream;

import org.assertj.core.util.CheckReturnValue;

public abstract class AbstractStandardSoftAssertions extends Java6AbstractStandardSoftAssertions {

Expand Down Expand Up @@ -69,7 +69,7 @@ public <T> OptionalAssert<T> assertThat(Optional<T> actual) {
*/
@CheckReturnValue
public OptionalDoubleAssert assertThat(OptionalDouble actual) {
return proxy(OptionalDoubleAssert.class, OptionalDouble.class, actual);
return proxy(OptionalDoubleAssert.class, OptionalDouble.class, actual);
}

/**
Expand All @@ -81,7 +81,7 @@ public OptionalDoubleAssert assertThat(OptionalDouble actual) {
*/
@CheckReturnValue
public OptionalLongAssert assertThat(OptionalLong actual) {
return proxy(OptionalLongAssert.class, OptionalLong.class, actual);
return proxy(OptionalLongAssert.class, OptionalLong.class, actual);
}

/**
Expand All @@ -93,7 +93,7 @@ public OptionalLongAssert assertThat(OptionalLong actual) {
*/
@CheckReturnValue
public OptionalIntAssert assertThat(OptionalInt actual) {
return proxy(OptionalIntAssert.class, OptionalInt.class, actual);
return proxy(OptionalIntAssert.class, OptionalInt.class, actual);
}

/**
Expand Down Expand Up @@ -148,7 +148,7 @@ public LocalTimeAssert assertThat(LocalTime actual) {
*/
@CheckReturnValue
public OffsetTimeAssert assertThat(OffsetTime actual) {
return proxy(OffsetTimeAssert.class, OffsetTime.class, actual);
return proxy(OffsetTimeAssert.class, OffsetTime.class, actual);
}

/**
Expand Down Expand Up @@ -229,19 +229,22 @@ public LongPredicateAssert assertThat(LongPredicate actual) {
}

/**
* Creates a new instance of <code>{@link ListAssert}</code> from the given {@link Stream}.
* Creates a new instance of <code>{@link ListAssert}</code> from the given {@link BaseStream}.
* <p>
* <b>Be aware that to create the returned {@link ListAssert} the given the {@link Stream} is consumed so it won't be
* <b>Be aware that to create the returned {@link ListAssert} the given the {@link BaseStream} is consumed so it won't be
* possible to use it again.</b> Calling multiple methods on the returned {@link ListAssert} is safe as it only
* interacts with the {@link List} built from the {@link Stream}.
* interacts with the {@link List} built from the {@link BaseStream}.
*
* @param actual the actual {@link Stream} value.
* <p>This method accepts {@link java.util.stream.Stream} and primitive stream variants
* {@link java.util.stream.IntStream}, {@link java.util.stream.LongStream} and {@link java.util.stream.DoubleStream}.
*
* @param actual the actual {@link BaseStream} value.
* @return the created assertion object.
*/
@SuppressWarnings("unchecked")
@CheckReturnValue
public <ELEMENT> ListAssert<ELEMENT> assertThat(Stream<? extends ELEMENT> actual) {
return proxy(ListAssert.class, Stream.class, actual);
@SuppressWarnings("unchecked")
public <ELEMENT, STREAM extends BaseStream<ELEMENT, STREAM>> ListAssert<ELEMENT> assertThat(BaseStream<? extends ELEMENT, STREAM> actual) {
return proxy(ListAssert.class, BaseStream.class, actual);
}

}
25 changes: 14 additions & 11 deletions src/main/java/org/assertj/core/api/Assertions.java
Expand Up @@ -41,6 +41,8 @@
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;
Expand All @@ -53,14 +55,12 @@
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.concurrent.atomic.AtomicStampedReference;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.function.DoublePredicate;
import java.util.function.Function;
import java.util.function.IntPredicate;
import java.util.function.LongPredicate;
import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.BaseStream;

import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.assertj.core.api.exception.RuntimeIOException;
Expand Down Expand Up @@ -900,7 +900,7 @@ public static AtomicIntegerArrayAssert assertThat(AtomicIntegerArray actual) {
*/
@CheckReturnValue
public static <OBJECT> AtomicIntegerFieldUpdaterAssert<OBJECT> assertThat(AtomicIntegerFieldUpdater<OBJECT> actual) {
return new AtomicIntegerFieldUpdaterAssert<OBJECT>(actual);
return new AtomicIntegerFieldUpdaterAssert<>(actual);
}

/**
Expand Down Expand Up @@ -937,7 +937,7 @@ public static AtomicLongArrayAssert assertThat(AtomicLongArray actual) {
*/
@CheckReturnValue
public static <OBJECT> AtomicLongFieldUpdaterAssert<OBJECT> assertThat(AtomicLongFieldUpdater<OBJECT> actual) {
return new AtomicLongFieldUpdaterAssert<OBJECT>(actual);
return new AtomicLongFieldUpdaterAssert<>(actual);
}

/**
Expand All @@ -950,7 +950,7 @@ public static <OBJECT> AtomicLongFieldUpdaterAssert<OBJECT> assertThat(AtomicLon
*/
@CheckReturnValue
public static <VALUE> AtomicReferenceAssert<VALUE> assertThat(AtomicReference<VALUE> actual) {
return new AtomicReferenceAssert<VALUE>(actual);
return new AtomicReferenceAssert<>(actual);
}

/**
Expand Down Expand Up @@ -2276,17 +2276,20 @@ public static <ELEMENT> ListAssert<ELEMENT> assertThat(List<? extends ELEMENT> a
}

/**
* Creates a new instance of <code>{@link ListAssert}</code> from the given {@link Stream}.
* Creates a new instance of <code>{@link ListAssert}</code> from the given {@link BaseStream}.
* <p>
* <b>Be aware that to create the returned {@link ListAssert} the given the {@link Stream} is consumed so it won't be
* <b>Be aware that to create the returned {@link ListAssert} the given the {@link BaseStream} is consumed so it won't be
* possible to use it again.</b> Calling multiple methods on the returned {@link ListAssert} is safe as it only
* interacts with the {@link List} built from the {@link Stream}.
* interacts with the {@link List} built from the {@link BaseStream}.
*
* <p>This method accepts {@link java.util.stream.Stream} and primitive stream variants
* {@link java.util.stream.IntStream}, {@link java.util.stream.LongStream} and {@link java.util.stream.DoubleStream}.
*
* @param actual the actual {@link Stream} value.
* @param actual the actual {@link BaseStream} value.
* @return the created assertion object.
*/
@CheckReturnValue
public static <ELEMENT> AbstractListAssert<?, List<? extends ELEMENT>, ELEMENT, ObjectAssert<ELEMENT>> assertThat(Stream<? extends ELEMENT> actual) {
public static <ELEMENT, STREAM extends BaseStream<ELEMENT, STREAM>> AbstractListAssert<?, List<? extends ELEMENT>, ELEMENT, ObjectAssert<ELEMENT>> assertThat(BaseStream<? extends ELEMENT, STREAM> actual) {
return AssertionsForInterfaceTypes.assertThat(actual);
}

Expand Down
Expand Up @@ -12,8 +12,6 @@
*/
package org.assertj.core.api;

import org.assertj.core.util.CheckReturnValue;

import java.nio.file.Path;
import java.util.Iterator;
import java.util.List;
Expand All @@ -22,7 +20,9 @@
import java.util.function.IntPredicate;
import java.util.function.LongPredicate;
import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.BaseStream;

import org.assertj.core.util.CheckReturnValue;

/**
* Entry point for assertion methods for different data types. Each method in this class is a static factory for the
Expand Down Expand Up @@ -127,20 +127,22 @@ public static <ELEMENT> ListAssert<ELEMENT> assertThat(List<? extends ELEMENT> a
}

/**
* Creates a new instance of <code>{@link ListAssert}</code> from the given {@link Stream}.
* Creates a new instance of <code>{@link ListAssert}</code> from the given {@link BaseStream}.
* <p>
* <b>Be aware that to create the returned {@link ListAssert} the given the {@link Stream} is consumed so it won't be
* <b>Be aware that to create the returned {@link ListAssert} the given the {@link BaseStream} is consumed so it won't be
* possible to use it again.</b> Calling multiple methods on the returned {@link ListAssert} is safe as it only
* interacts with the {@link List} built from the {@link Stream}.
* interacts with the {@link List} built from the {@link BaseStream}.
*
* <p>This method accepts {@link java.util.stream.Stream} and primitive stream variants
* {@link java.util.stream.IntStream}, {@link java.util.stream.LongStream} and {@link java.util.stream.DoubleStream}.
*
* @param actual the actual {@link Stream} value.
* @param actual the actual {@link BaseStream} value.
* @return the created assertion object.
*/
@CheckReturnValue
public static <ELEMENT> ListAssert<ELEMENT> assertThat(Stream<? extends ELEMENT> actual) {
public static <ELEMENT, STREAM extends BaseStream<ELEMENT, STREAM>> ListAssert<ELEMENT> assertThat(BaseStream<? extends ELEMENT, STREAM> actual) {
return new ListAssert<>(actual);
}


/**
* Creates a new instance of <code>{@link IterableAssert}</code>.
Expand Down
24 changes: 14 additions & 10 deletions src/main/java/org/assertj/core/api/BDDAssertions.java
Expand Up @@ -18,13 +18,13 @@
import java.math.BigInteger;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZonedDateTime;
import java.nio.file.Path;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
Expand All @@ -33,6 +33,8 @@
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicIntegerArray;
Expand All @@ -45,13 +47,11 @@
import java.util.concurrent.atomic.AtomicReferenceArray;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
import java.util.concurrent.atomic.AtomicStampedReference;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.function.DoublePredicate;
import java.util.function.IntPredicate;
import java.util.function.LongPredicate;
import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.BaseStream;

import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.assertj.core.util.CheckReturnValue;
Expand Down Expand Up @@ -139,6 +139,7 @@ public static LongPredicateAssert then(LongPredicate actual) {
public static DoublePredicateAssert then(DoublePredicate actual) {
return assertThat(actual);
}

/**
* Create assertion for {@link java.util.Optional}.
*
Expand Down Expand Up @@ -652,7 +653,7 @@ public static AbstractIntegerAssert<?> then(Integer actual) {
* @return the created assertion object.
*/
@CheckReturnValue
public static <T> ListAssert<T> then(List<? extends T> actual){
public static <T> ListAssert<T> then(List<? extends T> actual) {
return assertThat(actual);
}

Expand Down Expand Up @@ -1137,17 +1138,20 @@ public static <T> T then(final AssertProvider<T> component) {
}

/**
* Creates a new instance of <code>{@link ListAssert}</code> from the given {@link Stream}.
* Creates a new instance of <code>{@link ListAssert}</code> from the given {@link BaseStream}.
* <p>
* <b>Be aware that to create the returned {@link ListAssert} the given the {@link Stream} is consumed so it won't be
* <b>Be aware that to create the returned {@link ListAssert} the given the {@link BaseStream} is consumed so it won't be
* possible to use it again.</b> Calling multiple methods on the returned {@link ListAssert} is safe as it only
* interacts with the {@link List} built from the {@link Stream}.
* interacts with the {@link List} built from the {@link BaseStream}.
*
* <p>This method accepts {@link java.util.stream.Stream} and primitive stream variants
* {@link java.util.stream.IntStream}, {@link java.util.stream.LongStream} and {@link java.util.stream.DoubleStream}.
*
* @param actual the actual {@link Stream} value.
* @param actual the actual {@link BaseStream} value.
* @return the created assertion object.
*/
@CheckReturnValue
public static <ELEMENT> AbstractListAssert<?, List<? extends ELEMENT>, ELEMENT, ObjectAssert<ELEMENT>> then(Stream<? extends ELEMENT> actual) {
public static <ELEMENT, STREAM extends BaseStream<ELEMENT, STREAM>> AbstractListAssert<?, List<? extends ELEMENT>, ELEMENT, ObjectAssert<ELEMENT>> then(BaseStream<? extends ELEMENT, STREAM> actual) {
return assertThat(actual);
}

Expand Down
19 changes: 10 additions & 9 deletions src/main/java/org/assertj/core/api/ListAssert.java
Expand Up @@ -18,10 +18,11 @@
import java.util.AbstractList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.BaseStream;
import java.util.stream.Stream;

import org.assertj.core.internal.Failures;
import org.assertj.core.util.Lists;
import org.assertj.core.util.VisibleForTesting;

/**
Expand All @@ -44,8 +45,9 @@ public ListAssert(List<? extends ELEMENT> actual) {
super(actual, ListAssert.class, new ObjectAssertFactory<ELEMENT>());
}

protected ListAssert(Stream<? extends ELEMENT> actual) {
this(actual == null ? null : new ListFromStream<>(actual));
@SuppressWarnings("unchecked")
protected <STREAM extends BaseStream<ELEMENT, STREAM>> ListAssert(BaseStream<? extends ELEMENT, STREAM> actual) {
this(actual == null ? null : new ListFromStream<>((BaseStream<ELEMENT, STREAM>) actual));
}

@Override
Expand Down Expand Up @@ -183,23 +185,22 @@ private ListFromStream asListFromStream() {
}

@VisibleForTesting
static class ListFromStream<ELEMENT> extends AbstractList<ELEMENT> {
private Stream<ELEMENT> stream;
static class ListFromStream<ELEMENT, STREAM extends BaseStream<ELEMENT, STREAM>> extends AbstractList<ELEMENT> {
private BaseStream<ELEMENT, STREAM> stream;
private List<ELEMENT> list;

public ListFromStream(Stream<ELEMENT> stream) {
public ListFromStream(BaseStream<ELEMENT, STREAM> stream) {
this.stream = stream;
}

@Override
public Stream<ELEMENT> stream() {
initList();
return list.stream();
}

private List<ELEMENT> initList() {
if (list == null) {
list = stream.collect(Collectors.toList());
}
if (list == null) list = Lists.newArrayList(stream.iterator());
return list;
}

Expand Down

0 comments on commit 73e79f7

Please sign in to comment.