Skip to content

Commit

Permalink
Use internal instead of external iteration.
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSchumacher committed May 28, 2018
1 parent 3aeaf37 commit 95ab1a0
Show file tree
Hide file tree
Showing 34 changed files with 137 additions and 258 deletions.
12 changes: 4 additions & 8 deletions src/main/java/org/assertj/core/api/AbstractAssert.java
Expand Up @@ -14,9 +14,9 @@

import static java.util.Objects.requireNonNull;
import static org.assertj.core.error.ShouldMatch.shouldMatch;
import static org.assertj.core.util.Lists.newArrayList;
import static org.assertj.core.util.Strings.formatIfArgs;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
Expand Down Expand Up @@ -151,13 +151,9 @@ private void removeCustomAssertRelatedElementsFromStackTraceIfNeeded(AssertionEr
if (!Failures.instance().isRemoveAssertJRelatedElementsFromStackTrace()) return;
if (isAssertjAssertClass()) return;

List<StackTraceElement> filtered = newArrayList(assertionError.getStackTrace());
for (StackTraceElement element : assertionError.getStackTrace()) {
if (isElementOfCustomAssert(element)) {
filtered.remove(element);
}
}
StackTraceElement[] newStackTrace = filtered.toArray(new StackTraceElement[filtered.size()]);
StackTraceElement[] newStackTrace = Arrays.stream(assertionError.getStackTrace())
.filter(element -> !isElementOfCustomAssert(element))
.toArray(StackTraceElement[]::new);
assertionError.setStackTrace(newStackTrace);
}

Expand Down
14 changes: 3 additions & 11 deletions src/main/java/org/assertj/core/api/AbstractDateAssert.java
Expand Up @@ -16,6 +16,7 @@
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.util.DateUtil.*;
import static org.assertj.core.util.Lists.newArrayList;
import static org.assertj.core.util.Preconditions.checkNotNull;
Expand All @@ -24,7 +25,6 @@
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Comparator;
Expand Down Expand Up @@ -524,11 +524,7 @@ public SELF isIn(String... datesAsString) {
* @throws AssertionError if one of the given date as String could not be converted to a Date.
*/
public SELF isInWithStringDateCollection(Collection<String> datesAsString) {
Collection<Date> dates = new ArrayList<>(datesAsString.size());
for (String dateAsString : datesAsString) {
dates.add(parse(dateAsString));
}
return isIn(dates);
return isIn(datesAsString.stream().map(this::parse).collect(toList()));
}

/**
Expand Down Expand Up @@ -612,11 +608,7 @@ public SELF isNotIn(String... datesAsString) {
* @throws AssertionError if one of the given date as String could not be converted to a Date.
*/
public SELF isNotInWithStringDateCollection(Collection<String> datesAsString) {
Collection<Date> dates = new ArrayList<>(datesAsString.size());
for (String dateAsString : datesAsString) {
dates.add(parse(dateAsString));
}
return isNotIn(dates);
return isNotIn(datesAsString.stream().map(this::parse).collect(toList()));
}

/**
Expand Down
16 changes: 5 additions & 11 deletions src/main/java/org/assertj/core/api/AbstractIterableAssert.java
Expand Up @@ -1297,13 +1297,7 @@ public <V, EXCEPTION extends Exception> AbstractListAssert<?, List<? extends V>,
}

private <V> AbstractListAssert<?, List<? extends V>, V, ObjectAssert<V>> doFlatExtracting(Extractor<? super ELEMENT, ? extends Collection<V>> extractor) {
List<V> result = newArrayList();
final List<? extends Collection<V>> extractedValues = FieldsOrPropertiesExtractor.extract(actual, extractor);

for (Collection<? extends V> iterable : extractedValues) {
result.addAll(iterable);
}

List<V> result = FieldsOrPropertiesExtractor.extract(actual, extractor).stream().flatMap(Collection::stream).collect(toList());
return newListAssertInstance(result).withAssertionState(myself);
}

Expand Down Expand Up @@ -1522,10 +1516,10 @@ public AbstractListAssert<?, List<? extends Tuple>, Tuple, ObjectAssert<Tuple>>
*/
@CheckReturnValue
public AbstractListAssert<?, List<? extends Object>, Object, ObjectAssert<Object>> flatExtracting(String... fieldOrPropertyNames) {
List<Object> extractedValues = newArrayList();
for (Tuple tuple : FieldsOrPropertiesExtractor.extract(actual, Extractors.byName(fieldOrPropertyNames))) {
extractedValues.addAll(tuple.toList());
}
List<Object> extractedValues = FieldsOrPropertiesExtractor.extract(actual, Extractors.byName(fieldOrPropertyNames))
.stream()
.flatMap(tuple -> tuple.toList().stream())
.collect(toList());
return newListAssertInstance(extractedValues).withAssertionState(myself);
}

Expand Down
Expand Up @@ -2202,13 +2202,8 @@ public <V, C extends Collection<V>, EXCEPTION extends Exception> AbstractListAss
}

private <V, C extends Collection<V>> AbstractListAssert<?, List<? extends V>, V, ObjectAssert<V>> doFlatExtracting(Extractor<? super ELEMENT, C> extractor) {
final List<C> extractedValues = FieldsOrPropertiesExtractor.extract(Arrays.asList(actual), extractor);

final List<V> result = newArrayList();
for (C e : extractedValues) {
result.addAll(e);
}

List<V> result = FieldsOrPropertiesExtractor.extract(Arrays.asList(actual), extractor).stream()
.flatMap(Collection::stream).collect(toList());
return newListAssertInstance(result).withAssertionState(myself);
}

Expand Down
Expand Up @@ -130,9 +130,7 @@ public boolean wasSuccess() {
}

private List<Throwable> addLineNumberToErrorMessages(List<Throwable> errors) {
for (Throwable error : errors) {
addLineNumberToErrorMessage(error);
}
errors.forEach(this::addLineNumberToErrorMessage);
return errors;
}

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

import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.filter.Filters.filter;
import static org.assertj.core.description.Description.mostRelevantDescription;
import static org.assertj.core.extractor.Extractors.byName;
Expand Down Expand Up @@ -2235,13 +2236,8 @@ public <U, C extends Collection<U>, EXCEPTION extends Exception> ObjectArrayAsse
}

private <U, C extends Collection<U>> ObjectArrayAssert<U> doFlatExtracting(Extractor<? super T, C> extractor) {
final List<C> extractedValues = FieldsOrPropertiesExtractor.extract(Arrays.asList(array), extractor);

final List<U> result = newArrayList();
for (C e : extractedValues) {
result.addAll(e);
}

List<U> result = FieldsOrPropertiesExtractor.extract(Arrays.asList(array), extractor).stream()
.flatMap(Collection::stream).collect(toList());
return new ObjectArrayAssert<>(IterableUtil.toArray(result));
}

Expand Down
14 changes: 6 additions & 8 deletions src/main/java/org/assertj/core/api/ErrorCollector.java
Expand Up @@ -14,6 +14,7 @@

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -94,14 +95,11 @@ private boolean isNestedErrorCollectorProxyCall() {
return countErrorCollectorProxyCalls() > 1;
}

private static int countErrorCollectorProxyCalls() {
int nbCalls = 0;
for (StackTraceElement stackTraceElement : Thread.currentThread().getStackTrace()) {
if (CLASS_NAME.equals(stackTraceElement.getClassName())
&& stackTraceElement.getMethodName().startsWith(INTERCEPT_METHOD_NAME))
nbCalls++;
}
return nbCalls;
private static long countErrorCollectorProxyCalls() {
return Arrays.stream(Thread.currentThread().getStackTrace())
.filter(stackTraceElement -> CLASS_NAME.equals(stackTraceElement.getClassName())
&& stackTraceElement.getMethodName().startsWith(INTERCEPT_METHOD_NAME))
.count();
}

private static class LastResult {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/assertj/core/api/SoftAssertionError.java
Expand Up @@ -52,7 +52,7 @@ private static String createMessage(List<String> errors) {
for (int i = 0; i < size; i++) {
msg.append(i + 1).append(") ").append(errors.get(i)).append("%n");
}
return formatter.format(null,null,msg.toString());
return formatter.format(null, null, msg.toString());
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/org/assertj/core/api/SoftAssertions.java
Expand Up @@ -13,6 +13,7 @@
package org.assertj.core.api;

import static java.lang.String.format;
import static java.util.stream.Collectors.joining;
import static org.assertj.core.groups.FieldsOrPropertiesExtractor.extract;

import java.util.Arrays;
Expand Down Expand Up @@ -131,11 +132,9 @@ public class SoftAssertions extends AbstractStandardSoftAssertions {
return throwable.getMessage();
}
// error has a cause, display the cause message and the first stack trace elements.
StackTraceElement[] stackTraceFirstElements = Arrays.copyOf(cause.getStackTrace(), 5);
String stackTraceDescription = "";
for (StackTraceElement stackTraceElement : stackTraceFirstElements) {
stackTraceDescription += format("\tat %s%n", stackTraceElement);
}
String stackTraceDescription = Arrays.stream(cause.getStackTrace()).limit(5)
.map(stackTraceElement -> format("\tat %s%n", stackTraceElement))
.collect(joining());
return format("%s%n" +
"cause message: %s%n" +
"cause first five stack trace elements:%n" +
Expand Down
51 changes: 17 additions & 34 deletions src/main/java/org/assertj/core/api/filter/Filters.java
Expand Up @@ -12,12 +12,12 @@
*/
package org.assertj.core.api.filter;

import static java.util.stream.Collectors.toList;
import static org.assertj.core.util.Lists.newArrayList;
import static org.assertj.core.util.Objects.areEqual;
import static org.assertj.core.util.Preconditions.checkArgument;
import static org.assertj.core.util.Preconditions.checkNotNull;

import java.util.ArrayList;
import java.util.List;

import org.assertj.core.api.Assertions;
Expand Down Expand Up @@ -196,13 +196,7 @@ public Filters<E> having(Condition<? super E> condition) {
}

private Filters<E> applyFilterCondition(Condition<? super E> condition) {
List<E> newFilteredIterable = new ArrayList<>();
for (E element : filteredIterable) {
if (condition.matches(element)) {
newFilteredIterable.add(element);
}
}
this.filteredIterable = newFilteredIterable;
this.filteredIterable = filteredIterable.stream().filter(condition::matches).collect(toList());
return this;
}

Expand Down Expand Up @@ -265,8 +259,7 @@ public Filters<E> and(String propertyOrFieldName) {

/**
* Filters the underlying iterable to keep object with property (specified by {@link #with(String)}) <b>equals to</b>
* given
* value.
* given value.
* <p>
* Typical usage :
* <pre><code class='java'> filter(employees).with("name").equalsTo("Luke").get();</code></pre>
Expand All @@ -277,12 +270,10 @@ public Filters<E> and(String propertyOrFieldName) {
*/
public Filters<E> equalsTo(Object propertyValue) {
checkPropertyNameToFilterOnIsNotNull();
List<E> newFilteredIterable = new ArrayList<>();
for (E element : filteredIterable) {
this.filteredIterable = filteredIterable.stream().filter(element -> {
Object propertyValueOfCurrentElement = propertyOrFieldSupport.getValueOf(propertyOrFieldNameToFilterOn, element);
if (areEqual(propertyValueOfCurrentElement, propertyValue)) newFilteredIterable.add(element);
}
this.filteredIterable = newFilteredIterable;
return areEqual(propertyValueOfCurrentElement, propertyValue);
}).collect(toList());
return this;
}

Expand All @@ -300,12 +291,10 @@ public Filters<E> equalsTo(Object propertyValue) {
*/
public Filters<E> notEqualsTo(Object propertyValue) {
checkPropertyNameToFilterOnIsNotNull();
List<E> newFilteredIterable = new ArrayList<>();
for (E element : filteredIterable) {
this.filteredIterable = filteredIterable.stream().filter(element -> {
Object propertyValueOfCurrentElement = propertyOrFieldSupport.getValueOf(propertyOrFieldNameToFilterOn, element);
if (!areEqual(propertyValueOfCurrentElement, propertyValue)) newFilteredIterable.add(element);
}
this.filteredIterable = newFilteredIterable;
return !areEqual(propertyValueOfCurrentElement, propertyValue);
}).collect(toList());
return this;
}

Expand All @@ -316,8 +305,7 @@ private void checkPropertyNameToFilterOnIsNotNull() {

/**
* Filters the underlying iterable to keep object with property (specified by {@link #with(String)}) <b>equals to</b>
* one of the
* given values.
* one of the given values.
* <p>
* Typical usage :
* <pre><code class='java'>filter(players).with("team").in("Bulls", "Lakers").get();</code></pre>
Expand All @@ -328,19 +316,16 @@ private void checkPropertyNameToFilterOnIsNotNull() {
*/
public Filters<E> in(Object... propertyValues) {
checkPropertyNameToFilterOnIsNotNull();
List<E> newFilteredIterable = new ArrayList<>();
for (E element : filteredIterable) {
this.filteredIterable = filteredIterable.stream().filter(element -> {
Object propertyValueOfCurrentElement = propertyOrFieldSupport.getValueOf(propertyOrFieldNameToFilterOn, element);
if (isItemInArray(propertyValueOfCurrentElement, propertyValues)) newFilteredIterable.add(element);
}
this.filteredIterable = newFilteredIterable;
return isItemInArray(propertyValueOfCurrentElement, propertyValues);
}).collect(toList());
return this;
}

/**
* Filters the underlying iterable to keep object with property (specified by {@link #with(String)}) <b>not in</b> the
* given
* values.
* given values.
* <p>
* Typical usage :
* <pre><code class='java'> filter(players).with("team").notIn("Heat", "Lakers").get();</code></pre>
Expand All @@ -351,12 +336,10 @@ public Filters<E> in(Object... propertyValues) {
*/
public Filters<E> notIn(Object... propertyValues) {
checkPropertyNameToFilterOnIsNotNull();
List<E> newFilteredIterable = new ArrayList<>();
for (E element : filteredIterable) {
this.filteredIterable = filteredIterable.stream().filter(element -> {
Object propertyValueOfCurrentElement = propertyOrFieldSupport.getValueOf(propertyOrFieldNameToFilterOn, element);
if (!isItemInArray(propertyValueOfCurrentElement, propertyValues)) newFilteredIterable.add(element);
}
this.filteredIterable = newFilteredIterable;
return !isItemInArray(propertyValueOfCurrentElement, propertyValues);
}).collect(toList());
return this;
}

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/assertj/core/condition/AllOf.java
Expand Up @@ -60,9 +60,7 @@ private AllOf(Iterable<? extends Condition<? super T>> conditions) {
/** {@inheritDoc} */
@Override
public boolean matches(T value) {
for (Condition<? super T> condition : conditions)
if (!condition.matches(value)) return false;
return true;
return conditions.stream().allMatch(condition -> condition.matches(value));
}

@Override
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/assertj/core/condition/AnyOf.java
Expand Up @@ -61,9 +61,7 @@ private AnyOf(Iterable<? extends Condition<? super T>> conditions) {
/** {@inheritDoc} */
@Override
public boolean matches(T value) {
for (Condition<? super T> condition : conditions)
if (condition.matches(value)) return true;
return false;
return conditions.stream().anyMatch(condition -> condition.matches(value));
}

@Override
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/assertj/core/condition/Join.java
Expand Up @@ -13,7 +13,7 @@
package org.assertj.core.condition;

import static java.util.Collections.unmodifiableCollection;

import static java.util.stream.Collectors.toList;
import static org.assertj.core.util.Preconditions.checkNotNull;

import java.util.*;
Expand Down Expand Up @@ -43,9 +43,7 @@ public abstract class Join<T> extends Condition<T> {
@SafeVarargs
protected Join(Condition<? super T>... conditions) {
if (conditions == null) throw conditionsIsNull();
this.conditions = new ArrayList<>();
for (Condition<? super T> condition : conditions)
this.conditions.add(notNull(condition));
this.conditions = Arrays.stream(conditions).map(Join::notNull).collect(toList());
}

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

import static java.util.stream.Collectors.joining;

import java.util.List;

import org.assertj.core.description.Description;
Expand Down Expand Up @@ -49,10 +51,7 @@ public String create(Description d, Representation representation) {
}

protected static String diffsAsString(List<Delta<String>> diffsList) {
StringBuilder stringBuilder = new StringBuilder();
for (Delta<String> diff : diffsList)
stringBuilder.append(System.lineSeparator()).append(diff);
return stringBuilder.toString();
return diffsList.stream().map(Delta::toString).collect(joining(System.lineSeparator()));
}

}

0 comments on commit 95ab1a0

Please sign in to comment.