Skip to content

Commit

Permalink
Implement Traversable#reject (#2157)
Browse files Browse the repository at this point in the history
* Implement Traversable#reject

* Replace Seq#removeAll(Predicate) with Traversable#reject(Predicate)

* Remove `reject` implementation from Queue and PriorityQueue
  • Loading branch information
valery1707 authored and danieldietrich committed Jan 13, 2019
1 parent 955cc43 commit 6b57bcf
Show file tree
Hide file tree
Showing 37 changed files with 500 additions and 29 deletions.
33 changes: 30 additions & 3 deletions vavr/src/main/java/io/vavr/collection/AbstractMultimap.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,40 +253,67 @@ public M filter(Predicate<? super Tuple2<K, V>> predicate) {
}
}

@Override
public M reject(Predicate<? super Tuple2<K, V>> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return filter(predicate.negate());
}

@Override
public M filter(BiPredicate<? super K, ? super V> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return filter(t -> predicate.test(t._1, t._2));
}

@Override
public M reject(BiPredicate<? super K, ? super V> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return reject(t -> predicate.test(t._1, t._2));
}

@Override
public M filterKeys(Predicate<? super K> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return filter(t -> predicate.test(t._1));
}

@Override
public M rejectKeys(Predicate<? super K> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return reject(t -> predicate.test(t._1));
}

@Override
public M filterValues(Predicate<? super V> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return filter(t -> predicate.test(t._2));
}

@Override
public M rejectValues(Predicate<? super V> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return reject(t -> predicate.test(t._2));
}

@Override
@Deprecated
public M removeAll(BiPredicate<? super K, ? super V> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return filter(predicate.negate());
return reject(predicate);
}

@Override
@Deprecated
public M removeKeys(Predicate<? super K> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return filterKeys(predicate.negate());
return rejectKeys(predicate);
}

@Override
@Deprecated
public M removeValues(Predicate<? super V> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return filterValues(predicate.negate());
return rejectValues(predicate);
}

@SuppressWarnings("unchecked")
Expand Down
11 changes: 9 additions & 2 deletions vavr/src/main/java/io/vavr/collection/AbstractQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,16 @@ public Q removeAll(Iterable<? extends T> elements) {
return Collections.removeAll((Q) this, elements);
}

@SuppressWarnings("unchecked")
@Deprecated
public Q removeAll(Predicate<? super T> predicate) {
return Collections.removeAll((Q) this, predicate);
Objects.requireNonNull(predicate, "predicate is null");
return reject(predicate);
}

@SuppressWarnings("unchecked")
@Override
public Q reject(Predicate<? super T> predicate) {
return Collections.reject((Q) this, predicate);
}

@Override
Expand Down
10 changes: 9 additions & 1 deletion vavr/src/main/java/io/vavr/collection/Array.java
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,12 @@ public Array<T> filter(Predicate<? super T> predicate) {
}
}

@Override
public Array<T> reject(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return Collections.reject(this, predicate);
}

@Override
public <U> Array<U> flatMap(Function<? super T, ? extends Iterable<? extends U>> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
Expand Down Expand Up @@ -1098,8 +1104,10 @@ public Array<T> removeAll(Iterable<? extends T> elements) {
}

@Override
@Deprecated
public Array<T> removeAll(Predicate<? super T> predicate) {
return io.vavr.collection.Collections.removeAll(this, predicate);
Objects.requireNonNull(predicate, "predicate is null");
return reject(predicate);
}

@Override
Expand Down
10 changes: 10 additions & 0 deletions vavr/src/main/java/io/vavr/collection/BitSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,9 @@ default BitSet<T> dropUntil(Predicate<? super T> predicate) {
@Override
BitSet<T> filter(Predicate<? super T> predicate);

@Override
BitSet<T> reject(Predicate<? super T> predicate);

@Override
default <U> SortedSet<U> flatMap(Comparator<? super U> comparator, Function<? super T, ? extends Iterable<? extends U>> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
Expand Down Expand Up @@ -823,6 +826,13 @@ public BitSet<T> filter(Predicate<? super T> predicate) {
return (bitSet.length() == length()) ? this : bitSet;
}

@Override
public BitSet<T> reject(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
final BitSet<T> bitSet = createFromAll(iterator().reject(predicate));
return (bitSet.length() == length()) ? this : bitSet;
}

@Override
public <C> Map<C, BitSet<T>> groupBy(Function<? super T, ? extends C> classifier) {
return Collections.groupBy(this, classifier, this::createFromAll);
Expand Down
10 changes: 9 additions & 1 deletion vavr/src/main/java/io/vavr/collection/CharSeq.java
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,12 @@ public CharSeq filter(Predicate<? super Character> predicate) {
}
}

@Override
public CharSeq reject(Predicate<? super Character> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return Collections.reject(this, predicate);
}

@Override
public <U> IndexedSeq<U> flatMap(Function<? super Character, ? extends Iterable<? extends U>> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
Expand Down Expand Up @@ -848,8 +854,10 @@ public CharSeq removeAll(Iterable<? extends Character> elements) {
}

@Override
@Deprecated
public CharSeq removeAll(Predicate<? super Character> predicate) {
return io.vavr.collection.Collections.removeAll(this, predicate);
Objects.requireNonNull(predicate, "predicate is null");
return reject(predicate);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion vavr/src/main/java/io/vavr/collection/Collections.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ static <C extends Traversable<T>, T> C removeAll(C source, Iterable<? extends T>
}

@SuppressWarnings("unchecked")
static <C extends Traversable<T>, T> C removeAll(C source, Predicate<? super T> predicate) {
static <C extends Traversable<T>, T> C reject(C source, Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
if (source.isEmpty()) {
return source;
Expand Down
32 changes: 29 additions & 3 deletions vavr/src/main/java/io/vavr/collection/HashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -564,21 +564,41 @@ public HashMap<K, V> filter(BiPredicate<? super K, ? super V> predicate) {
return Maps.filter(this, this::createFromEntries, predicate);
}

@Override
public HashMap<K, V> reject(BiPredicate<? super K, ? super V> predicate) {
return Maps.reject(this, this::createFromEntries, predicate);
}

@Override
public HashMap<K, V> filter(Predicate<? super Tuple2<K, V>> predicate) {
return Maps.filter(this, this::createFromEntries, predicate);
}

@Override
public HashMap<K, V> reject(Predicate<? super Tuple2<K, V>> predicate) {
return Maps.reject(this, this::createFromEntries, predicate);
}

@Override
public HashMap<K, V> filterKeys(Predicate<? super K> predicate) {
return Maps.filterKeys(this, this::createFromEntries, predicate);
}

@Override
public HashMap<K, V> rejectKeys(Predicate<? super K> predicate) {
return Maps.rejectKeys(this, this::createFromEntries, predicate);
}

@Override
public HashMap<K, V> filterValues(Predicate<? super V> predicate) {
return Maps.filterValues(this, this::createFromEntries, predicate);
}

@Override
public HashMap<K, V> rejectValues(Predicate<? super V> predicate) {
return Maps.rejectValues(this, this::createFromEntries, predicate);
}

@Override
public <K2, V2> HashMap<K2, V2> flatMap(BiFunction<? super K, ? super V, ? extends Iterable<Tuple2<K2, V2>>> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
Expand Down Expand Up @@ -755,8 +775,10 @@ public HashMap<K, V> remove(K key) {
}

@Override
@Deprecated
public HashMap<K, V> removeAll(BiPredicate<? super K, ? super V> predicate) {
return Maps.removeAll(this, this::createFromEntries, predicate);
Objects.requireNonNull(predicate, "predicate is null");
return reject(predicate);
}

@Override
Expand All @@ -777,13 +799,17 @@ public HashMap<K, V> removeAll(Iterable<? extends K> keys) {
}

@Override
@Deprecated
public HashMap<K, V> removeKeys(Predicate<? super K> predicate) {
return Maps.removeKeys(this, this::createFromEntries, predicate);
Objects.requireNonNull(predicate, "predicate is null");
return rejectKeys(predicate);
}

@Override
@Deprecated
public HashMap<K, V> removeValues(Predicate<? super V> predicate) {
return Maps.removeValues(this, this::createFromEntries, predicate);
Objects.requireNonNull(predicate, "predicate is null");
return rejectValues(predicate);
}

@Override
Expand Down
6 changes: 6 additions & 0 deletions vavr/src/main/java/io/vavr/collection/HashSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,12 @@ public HashSet<T> filter(Predicate<? super T> predicate) {
}
}

@Override
public HashSet<T> reject(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return filter(predicate.negate());
}

@Override
public <U> HashSet<U> flatMap(Function<? super T, ? extends Iterable<? extends U>> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
Expand Down
4 changes: 4 additions & 0 deletions vavr/src/main/java/io/vavr/collection/IndexedSeq.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ default boolean endsWith(Seq<? extends T> that) {
@Override
IndexedSeq<T> filter(Predicate<? super T> predicate);

@Override
IndexedSeq<T> reject(Predicate<? super T> predicate);

@Override
<U> IndexedSeq<U> flatMap(Function<? super T, ? extends Iterable<? extends U>> mapper);

Expand Down Expand Up @@ -253,6 +256,7 @@ default int lastIndexWhere(Predicate<? super T> predicate, int end) {
IndexedSeq<T> removeAll(Iterable<? extends T> elements);

@Override
@Deprecated
IndexedSeq<T> removeAll(Predicate<? super T> predicate);

@Override
Expand Down
6 changes: 6 additions & 0 deletions vavr/src/main/java/io/vavr/collection/Iterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,12 @@ public T getNext() {
}
}

@Override
default Iterator<T> reject(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return filter(predicate.negate());
}

@Override
default Option<T> findLast(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
Expand Down
4 changes: 4 additions & 0 deletions vavr/src/main/java/io/vavr/collection/LinearSeq.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ static <T> LinearSeq<T> narrow(LinearSeq<? extends T> linearSeq) {
@Override
LinearSeq<T> filter(Predicate<? super T> predicate);

@Override
LinearSeq<T> reject(Predicate<? super T> predicate);

@Override
<U> LinearSeq<U> flatMap(Function<? super T, ? extends Iterable<? extends U>> mapper);

Expand Down Expand Up @@ -234,6 +237,7 @@ default int lastIndexWhere(Predicate<? super T> predicate, int end) {
LinearSeq<T> removeAll(Iterable<? extends T> elements);

@Override
@Deprecated
LinearSeq<T> removeAll(Predicate<? super T> predicate);

@Override
Expand Down
32 changes: 29 additions & 3 deletions vavr/src/main/java/io/vavr/collection/LinkedHashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -588,21 +588,41 @@ public LinkedHashMap<K, V> filter(BiPredicate<? super K, ? super V> predicate) {
return Maps.filter(this, this::createFromEntries, predicate);
}

@Override
public LinkedHashMap<K, V> reject(BiPredicate<? super K, ? super V> predicate) {
return Maps.reject(this, this::createFromEntries, predicate);
}

@Override
public LinkedHashMap<K, V> filter(Predicate<? super Tuple2<K, V>> predicate) {
return Maps.filter(this, this::createFromEntries, predicate);
}

@Override
public LinkedHashMap<K, V> reject(Predicate<? super Tuple2<K, V>> predicate) {
return Maps.reject(this, this::createFromEntries, predicate);
}

@Override
public LinkedHashMap<K, V> filterKeys(Predicate<? super K> predicate) {
return Maps.filterKeys(this, this::createFromEntries, predicate);
}

@Override
public LinkedHashMap<K, V> rejectKeys(Predicate<? super K> predicate) {
return Maps.rejectKeys(this, this::createFromEntries, predicate);
}

@Override
public LinkedHashMap<K, V> filterValues(Predicate<? super V> predicate) {
return Maps.filterValues(this, this::createFromEntries, predicate);
}

@Override
public LinkedHashMap<K, V> rejectValues(Predicate<? super V> predicate) {
return Maps.rejectValues(this, this::createFromEntries, predicate);
}

@Override
public <K2, V2> LinkedHashMap<K2, V2> flatMap(BiFunction<? super K, ? super V, ? extends Iterable<Tuple2<K2, V2>>> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
Expand Down Expand Up @@ -794,8 +814,10 @@ public LinkedHashMap<K, V> remove(K key) {
}

@Override
@Deprecated
public LinkedHashMap<K, V> removeAll(BiPredicate<? super K, ? super V> predicate) {
return Maps.removeAll(this, this::createFromEntries, predicate);
Objects.requireNonNull(predicate, "predicate is null");
return reject(predicate);
}

@Override
Expand All @@ -808,13 +830,17 @@ public LinkedHashMap<K, V> removeAll(Iterable<? extends K> keys) {
}

@Override
@Deprecated
public LinkedHashMap<K, V> removeKeys(Predicate<? super K> predicate) {
return Maps.removeKeys(this, this::createFromEntries, predicate);
Objects.requireNonNull(predicate, "predicate is null");
return rejectKeys(predicate);
}

@Override
@Deprecated
public LinkedHashMap<K, V> removeValues(Predicate<? super V> predicate) {
return Maps.removeValues(this, this::createFromEntries, predicate);
Objects.requireNonNull(predicate, "predicate is null");
return rejectValues(predicate);
}

@Override
Expand Down
6 changes: 6 additions & 0 deletions vavr/src/main/java/io/vavr/collection/LinkedHashSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,12 @@ public LinkedHashSet<T> filter(Predicate<? super T> predicate) {
return filtered.length() == length() ? this : filtered;
}

@Override
public LinkedHashSet<T> reject(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return filter(predicate.negate());
}

@Override
public <U> LinkedHashSet<U> flatMap(Function<? super T, ? extends Iterable<? extends U>> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
Expand Down
Loading

0 comments on commit 6b57bcf

Please sign in to comment.