Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
martinsawicki committed Nov 28, 2020
2 parents 025202b + a7cd1be commit e7aa1fb
Show file tree
Hide file tree
Showing 5 changed files with 346 additions and 184 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.github.chainables</groupId>
<artifactId>chainable</artifactId>
<version>0.6.0</version>
<version>0.7.0</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
110 changes: 79 additions & 31 deletions src/main/java/com/github/chainables/chainable/Chainable.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* It is intended to be a rich, `Iterable`-based alternative to Java's `Stream` and Google's *guava*.
* <p>
* See the project's home site at http://www.github.com/chainables/chainable for more information.
*
*
* @author Martin Sawicki
*
* @param <T> the type of items in the chain
Expand Down Expand Up @@ -152,7 +152,7 @@ public T next() {
cache.add(next);
this.nextCacheIndex++;
return next;
}
}
}
};
}
Expand Down Expand Up @@ -484,7 +484,7 @@ default Chainable<T> beforeValue(T item) {
* @see #breadthFirstAsLongAs(Function, Predicate)
* @see #depthFirst(Function)
*/
default Chainable<T> breadthFirst(Function<? super T, Iterable<T>> childExtractor) {
default Chainable<T> breadthFirst(Function<? super T, Iterable<? extends T>> childExtractor) {
return Chainables.breadthFirst(this, childExtractor);
}

Expand All @@ -506,7 +506,7 @@ default Chainable<T> breadthFirst(Function<? super T, Iterable<T>> childExtracto
* @see #breadthFirst(Function)
* @see #depthFirst(Function)
*/
default Chainable<T> breadthFirstNotBelow(Function<? super T, Iterable<T>> childExtractor, Predicate<? super T> condition) {
default Chainable<T> breadthFirstNotBelow(Function<? super T, Iterable<? extends T>> childExtractor, Predicate<? super T> condition) {
return Chainables.breadthFirstNotBelow(this, childExtractor, condition);
}

Expand Down Expand Up @@ -603,7 +603,7 @@ default Chainable<T> chain(BinaryOperator<T> nextItemExtractorFromLastTwo) {
* startint with 0
* @return the resulting chain
*/
default Chainable<T> chainIndexed(BiFunction<T, Long, T> nextItemExtractor) {
default Chainable<T> chainIndexed(BiFunction<? super T, Long, T> nextItemExtractor) {
return Chainables.chainIndexed(this, nextItemExtractor);
}

Expand All @@ -615,7 +615,7 @@ default Chainable<T> chainIndexed(BiFunction<T, Long, T> nextItemExtractor) {
* @return resulting {@link Chainable}
* @see #chain(UnaryOperator)
*/
default Chainable<T> chainIf(Predicate<T> condition, UnaryOperator<T> nextItemExtractor) {
default Chainable<T> chainIf(Predicate<? super T> condition, UnaryOperator<T> nextItemExtractor) {
return Chainables.chainIf(this, condition, nextItemExtractor);
}

Expand All @@ -629,7 +629,7 @@ default Chainable<T> chainIf(Predicate<T> condition, UnaryOperator<T> nextItemEx
* <tr><td><i>C#:</i></td><td>{@code Enumerable.ToList()} and the like</td></tr>
* </table>
*/
default Chainable<T> collectInto(Collection<T> targetCollection) {
default Chainable<T> collectInto(Collection<? super T> targetCollection) {
return Chainables.collectInto(this, targetCollection);
}

Expand All @@ -645,7 +645,7 @@ default Chainable<T> collectInto(Collection<T> targetCollection) {
* </table>
* @see #concat(Object)
*/
default Chainable<T> concat(Iterable<T> items) {
default Chainable<T> concat(Iterable<? extends T> items) {
return Chainables.concat(this, items);
}

Expand All @@ -656,7 +656,7 @@ default Chainable<T> concat(Iterable<T> items) {
* @see #concat(Iterable)
*/
@SuppressWarnings("unchecked")
default Chainable<T> concat(Iterable<T>...iterables) {
default Chainable<T> concat(Iterable<? extends T>...iterables) {
return this.concat(Chainables.concat(iterables));
}

Expand All @@ -680,7 +680,7 @@ default Chainable<T> concat(T item) {
* @return the resulting chain
* @see #concat(Iterable)
*/
default Chainable<T> concat(Function<? super T, Iterable<T>> lister) {
default Chainable<T> concat(Function<? super T, Iterable<? extends T>> lister) {
return Chainables.concat(this, lister);
}

Expand Down Expand Up @@ -724,6 +724,18 @@ default boolean containsAny(T...items) {
return Chainables.containsAny(this, items);
}

/**
* Determines whether this chain contains no items other than those that equal one of the specified {@code contents}.
* <p>
* If the chain is empty, the checked condition is not violated, so {@code true} is returned.
* @param contents the contents for the items of this chain to match
* @return {@code true} iff this chain contains no items other than those that equal one of the specified {@code contents}
*/
@SuppressWarnings("unchecked")
default boolean containsOnly(T...contents) {
return Chainables.containsOnly(this, contents);
}

/**
* Determines whether this chain contains items in the specified {@code subarray} in that exact order.
* @param subarray
Expand All @@ -733,7 +745,7 @@ default boolean containsAny(T...items) {
* @see #containsAll(Object...)
* @see #containsAny(Object...)
*/
default boolean containsSubarray(Iterable<T> subarray) {
default boolean containsSubarray(Iterable<? extends T> subarray) {
return Chainables.containsSubarray(this, subarray);
}

Expand Down Expand Up @@ -768,7 +780,7 @@ default long count() {
* @return resulting chain
* @see #breadthFirst(Function)
*/
default Chainable<T> depthFirst(Function<T, Iterable<T>> childExtractor) {
default Chainable<T> depthFirst(Function<? super T, Iterable<? extends T>> childExtractor) {
return Chainables.depthFirst(this, childExtractor);
}

Expand All @@ -788,7 +800,7 @@ default Chainable<T> depthFirst(Function<T, Iterable<T>> childExtractor) {
* @param condition
* @return resulting chain
*/
default Chainable<T> depthFirstNotBelow(Function<? super T, Iterable<T>> childExtractor, Predicate<? super T> condition) {
default Chainable<T> depthFirstNotBelow(Function<? super T, Iterable<? extends T>> childExtractor, Predicate<? super T> condition) {
return Chainables.depthFirstNotBelow(this, childExtractor, condition);
}

Expand Down Expand Up @@ -897,7 +909,7 @@ default <V> Chainable<T> distinct(Function<? super T, V> keyExtractor) {
* @see #endsWithEither(Iterable...)
* @see #startsWith(Iterable)
*/
default boolean endsWith(Iterable<T> suffix) {
default boolean endsWith(Iterable<? extends T> suffix) {
return Chainables.endsWith(this, suffix);
}

Expand All @@ -910,7 +922,7 @@ default boolean endsWith(Iterable<T> suffix) {
* @see #endsWith(Iterable)
*/
@SuppressWarnings("unchecked")
default boolean endsWithEither(Iterable<T>...suffixes) {
default boolean endsWithEither(Iterable<? extends T>...suffixes) {
return Chainables.endsWithEither(this, suffixes);
}

Expand All @@ -924,7 +936,7 @@ default boolean endsWithEither(Iterable<T>...suffixes) {
* </table>
* @see #equalsEither(Iterable...)
*/
default boolean equals(Iterable<T> items) {
default boolean equals(Iterable<? extends T> items) {
return Chainables.equal(this, items);
}

Expand All @@ -938,11 +950,11 @@ default boolean equals(Iterable<T> items) {
* @see #equalsEither(Iterable...)
*/
@SuppressWarnings("unchecked")
default boolean equalsEither(Iterable<T>...iterables) {
default boolean equalsEither(Iterable<? extends T>...iterables) {
if (iterables == null) {
return false;
} else {
for (Iterable<T> iterable : iterables) {
for (Iterable<? extends T> iterable : iterables) {
if (Chainables.equal(this, iterable)) {
return true;
}
Expand Down Expand Up @@ -980,9 +992,9 @@ default Chainable<T> first(long count) {
}

/**
* Finds the first item satisfying the specified {@code condition}.
* @param condition
* @return the first item satisfying the specified {@code condition}
* Returns the first item satisfying the specified {@code condition}, or {@code null} if none.
* @param condition the condition for the returned item to satisfy
* @return the first item satisfying the specified {@code condition}, or {@code null} if none.
* @chainables.similar
* <table summary="Similar to:">
* <tr><td><i>Java:</i></td><td>a combination of {@link java.util.stream.Stream#filter(Predicate)} and {@link java.util.stream.Stream#findFirst()}</td></tr>
Expand All @@ -991,20 +1003,41 @@ default Chainable<T> first(long count) {
* @see #firstWhereEither(Predicate...)
*/
default T firstWhere(Predicate<? super T> condition) {
return Chainables.firstWhereEither(this, condition);
return Chainables.firstWhereEither(this, (T)null, condition);
}

/**
* Finds the first item satisying any of the specified {@code conditions}.
* @param conditions
* @return the first item satisfying any of the specified {@code conditions}
* Returns the first item satisfying the specified {@code condition}, or {@code defaultValue} if none.
* @param condition the condition for the returned item to satisfy
* @param defaultValue the value to return if no item satisfying the specified {@code condition} was found.
* @return the first item satisfying the specified {@code condition}, or {@code defaultValue} if none.
*/
default T firstWhere(Predicate<? super T> condition, T defaultValue) {
return Chainables.firstWhereEither(this, defaultValue, condition);
}

/**
* Returns the first item satisfying any of the specified {@code conditions} or {@code null} if none found.
* @param conditions the conditions any of which the returned item must satisfy
* @return the first item satisfying any of the specified {@code conditions} or {@code null} if none
* @see #firstWhere(Predicate)
*/
@SuppressWarnings("unchecked")
default T firstWhereEither(Predicate<? super T>... conditions) {
return Chainables.firstWhereEither(this, conditions);
}

/**
* Returns the first item satisfying any of the specified {@code conditions} or {@code defaultValue} if none found.
* @param defaultValue the value to return if no item satisfying any of the specified {@code conditions} has been found
* @param conditions the conditions any of which the returned item must satisfy
* @return the first item satisfying any of the specified {@code conditions} or {@code defaultValue} if none
*/
@SuppressWarnings("unchecked")
default T firstWhereEither(T defaultValue, Predicate<? super T>... conditions) {
return Chainables.firstWhereEither(this, defaultValue, conditions);
}

/**
* Fetches the item in the chain at the specified {@code index}, traversing/evaluating the chain as needed until that index is reached.
* @param index the index of the item to retrieve from this chain
Expand All @@ -1022,7 +1055,7 @@ default T get(long index) {
* <tr><td>{@code items2}:</td><td>2, 4, 6</td></tr>
* <tr><td><i>result:</i></td><td>1, 2, 3, 4, 5, 6</td></tr>
* </table>
* @param iterables to merge by interleaving
* @param iterables iterables to merge by interleaving
* @return items from the interleaved merger of the specified {@code iterables}
*/
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -1308,7 +1341,7 @@ default Chainable<T> notWhere(Predicate<? super T> condition) {
* </table>
* @see #transformAndFlatten(Function)
*/
default Chainable<T> replace(Function<? super T, Iterable<T>> replacer) {
default Chainable<T> replace(Function<? super T, Iterable<? extends T>> replacer) {
return Chainables.replace(this, replacer);
}

Expand All @@ -1333,7 +1366,7 @@ default Chainable<T> reverse() {
* @see #endsWith(Iterable)
* @see #startsWithEither(Iterable...)
*/
default boolean startsWith(Iterable<T> prefix) {
default boolean startsWith(Iterable<? extends T> prefix) {
return Chainables.startsWithEither(this, prefix);
}

Expand All @@ -1344,7 +1377,7 @@ default boolean startsWith(Iterable<T> prefix) {
* @see #startsWith(Iterable)
*/
@SuppressWarnings("unchecked")
default boolean startsWithEither(Iterable<T>... prefixes) {
default boolean startsWithEither(Iterable<? extends T>... prefixes) {
return Chainables.startsWithEither(this, prefixes);
}

Expand Down Expand Up @@ -1423,7 +1456,22 @@ default <O> Chainable<O> transform(Function<? super T, O> transformer) {

/**
* Transforms each item into several other items, possibly of a different type, using the specified {@code transformer}.
* @param transformer
* @param transformer the function to apply to each item whose output items will become part of the chain
* @return the resulting items from the transformation
* @chainables.similar
* <table summary="Similar to:">
* <tr><td><i>Java:</i></td><td>{@link java.util.stream.Stream#flatMap(Function)}</td></tr>
* <tr><td><i>C#:</i></td><td>{@code Enumerable.SelectMany()}</td></tr>
* </table>
* @see #transform(Function)
*/
default <O> Chainable<O> transformAndFlattenArray(Function<? super T, O[]> transformer) {
return Chainables.transformAndFlattenArray(this, transformer);
}

/**
* Transforms each item into several other items, possibly of a different type, using the specified {@code transformer}.
* @param transformer the function to apply to each item whose output items will become part of the chain
* @return the resulting items from the transformation
* @chainables.similar
* <table summary="Similar to:">
Expand All @@ -1432,7 +1480,7 @@ default <O> Chainable<O> transform(Function<? super T, O> transformer) {
* </table>
* @see #transform(Function)
*/
default <O> Chainable<O> transformAndFlatten(Function<? super T, Iterable<O>> transformer) {
default <O> Chainable<O> transformAndFlatten(Function<? super T, Iterable<? extends O>> transformer) {
return Chainables.transformAndFlatten(this, transformer);
}

Expand Down
Loading

0 comments on commit e7aa1fb

Please sign in to comment.