Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove explicit null checks #2744

Open
wants to merge 25 commits into
base: version/1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
6445c85
[failing] Removed Objects.requireNonNull() checks throughout.
jbrains Apr 22, 2022
a62f55b
Shifted the failure from composing f.andThen(null) to invoking the re…
jbrains Apr 29, 2022
5c207d3
Removed an obsolete test.
jbrains Apr 29, 2022
8cd8fbb
Weakened an expected exception type, avoiding overspecifying.
jbrains Apr 29, 2022
d7525fe
Traversable can tolerate a null _combine_ operator when folding right…
jbrains Apr 29, 2022
d4e650f
Traversable can now tolerate a null Comparator if minBy() doesn't nee…
jbrains Apr 29, 2022
5f3fe30
Traversable now tolerates a null combine operator, as long as it does…
jbrains Apr 29, 2022
c093e16
Traversable now ignores the combine operator when trying to reduce() …
jbrains Apr 29, 2022
5fa7bf4
Traversables now ignore a null combine operator when reduce*() doesn'…
jbrains Apr 29, 2022
eae1d55
Fixed Traversable tests for reduce*Option(null) over an empty collect…
jbrains Apr 29, 2022
c76fdf7
Traversable.zip() now ignores a null argument when there's nothing to…
jbrains Apr 29, 2022
865ffea
Traversable can now tolerate a null Comparator if maxBy() doesn't nee…
jbrains Apr 30, 2022
fe0de3c
Traversable now ignores a null combine operator when it doesn't need …
jbrains Apr 30, 2022
3969840
Traversable now tolerates a null predicate when there are no elements…
jbrains Apr 30, 2022
a67ae71
Traversable now ignores a null partial function when it doesn't need …
jbrains Apr 30, 2022
95fcd33
iterator returns itself when empty
patrickguenther May 6, 2023
d4829d7
fixed CharSeqTest
patrickguenther May 6, 2023
819a84f
Stream::partition no longer throws when null predicate and empty
patrickguenther May 6, 2023
f36b463
Fixes EitherTest
patrickguenther May 6, 2023
9051824
fixed OptionTest
patrickguenther May 6, 2023
8c6732e
fixed TryTest
patrickguenther May 6, 2023
665a2c6
fixed ValidationTest
patrickguenther May 6, 2023
11f66ef
fixed JavaConvertersTest
patrickguenther May 6, 2023
1b6c25b
fixed FutureTest
patrickguenther May 6, 2023
28aa943
fixes erroneous javadoc comment
patrickguenther May 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/main/java/io/vavr/CheckedConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ static <T> CheckedConsumer<T> of(CheckedConsumer<T> methodReference) {
* @throws NullPointerException if {@code after} is null
*/
default CheckedConsumer<T> andThen(CheckedConsumer<? super T> after) {
Objects.requireNonNull(after, "after is null");
return (T t) -> { accept(t); after.accept(t); };
}

Expand Down
7 changes: 0 additions & 7 deletions src/main/java/io/vavr/Lazy.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ public static <T> Lazy<T> narrow(Lazy<? extends T> lazy) {
*/
@SuppressWarnings("unchecked")
public static <T> Lazy<T> of(Supplier<? extends T> supplier) {
Objects.requireNonNull(supplier, "supplier is null");
if (supplier instanceof Lazy) {
return (Lazy<T>) supplier;
} else {
Expand All @@ -122,7 +121,6 @@ public static <T> Lazy<T> of(Supplier<? extends T> supplier) {
*/
@SuppressWarnings("Convert2MethodRef") // TODO should be fixed in JDK 9 and Idea
public static <T> Lazy<Seq<T>> sequence(Iterable<? extends Lazy<? extends T>> values) {
Objects.requireNonNull(values, "values is null");
return Lazy.of(() -> Vector.ofAll(values).map(lazy -> lazy.get()));
}

Expand All @@ -137,8 +135,6 @@ public static <T> Lazy<Seq<T>> sequence(Iterable<? extends Lazy<? extends T>> va
*/
@SuppressWarnings("unchecked")
public static <T> T val(Supplier<? extends T> supplier, Class<T> type) {
Objects.requireNonNull(supplier, "supplier is null");
Objects.requireNonNull(type, "type is null");
if (!type.isInterface()) {
throw new IllegalArgumentException("type has to be an interface");
}
Expand All @@ -148,13 +144,11 @@ public static <T> T val(Supplier<? extends T> supplier, Class<T> type) {
}

public Option<T> filter(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
final T v = get();
return predicate.test(v) ? Option.some(v) : Option.none();
}

public Option<T> filterNot(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return filter(predicate.negate());
}

Expand Down Expand Up @@ -245,7 +239,6 @@ public Lazy<T> peek(Consumer<? super T> action) {
* @throws NullPointerException if {@code f} is null
*/
public <U> U transform(Function<? super Lazy<T>, ? extends U> f) {
Objects.requireNonNull(f, "f is null");
return f.apply(this);
}

Expand Down
8 changes: 0 additions & 8 deletions src/main/java/io/vavr/Predicates.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ private Predicates() {
@SuppressWarnings({ "unchecked", "varargs" })
@SafeVarargs
public static <T> Predicate<T> allOf(Predicate<T>... predicates) {
Objects.requireNonNull(predicates, "predicates is null");
return t -> List.of(predicates).foldLeft(true, (bool, pred) -> bool && pred.test(t));
}

Expand All @@ -88,7 +87,6 @@ public static <T> Predicate<T> allOf(Predicate<T>... predicates) {
@SuppressWarnings({ "unchecked", "varargs" })
@SafeVarargs
public static <T> Predicate<T> anyOf(Predicate<T>... predicates) {
Objects.requireNonNull(predicates, "predicates is null");
return t -> List.of(predicates).find(pred -> pred.test(t)).isDefined();
}

Expand All @@ -108,7 +106,6 @@ public static <T> Predicate<T> anyOf(Predicate<T>... predicates) {
* @throws NullPointerException if {@code predicate} is null
*/
public static <T> Predicate<Iterable<T>> exists(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return iterable -> Iterator.ofAll(iterable).exists(predicate);
}

Expand All @@ -128,7 +125,6 @@ public static <T> Predicate<Iterable<T>> exists(Predicate<? super T> predicate)
* @throws NullPointerException if {@code predicate} is null
*/
public static <T> Predicate<Iterable<T>> forAll(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return iterable -> Iterator.ofAll(iterable).forAll(predicate);
}

Expand All @@ -148,7 +144,6 @@ public static <T> Predicate<Iterable<T>> forAll(Predicate<? super T> predicate)
*/
// DEV-NOTE: We need Class<? extends T> instead of Class<T>, see {@link TryTest#shouldRecoverSuccessUsingCase()}
public static <T> Predicate<T> instanceOf(Class<? extends T> type) {
Objects.requireNonNull(type, "type is null");
return obj -> obj != null && type.isAssignableFrom(obj.getClass());
}

Expand Down Expand Up @@ -188,7 +183,6 @@ public static <T> Predicate<T> is(T value) {
@SuppressWarnings({ "unchecked", "varargs" })
@SafeVarargs
public static <T> Predicate<T> isIn(T... values) {
Objects.requireNonNull(values, "values is null");
return obj -> List.of(values).find(value -> Objects.equals(value, obj)).isDefined();
}

Expand Down Expand Up @@ -245,7 +239,6 @@ public static <T> Predicate<T> isNull() {
@SuppressWarnings({ "unchecked", "varargs" })
@SafeVarargs
public static <T> Predicate<T> noneOf(Predicate<T>... predicates) {
Objects.requireNonNull(predicates, "predicates is null");
return anyOf(predicates).negate();
}

Expand All @@ -271,7 +264,6 @@ public static <T> Predicate<T> noneOf(Predicate<T>... predicates) {
*/
@SuppressWarnings("unchecked")
public static <T> Predicate<T> not(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return (Predicate<T>) predicate.negate();
}

Expand Down
33 changes: 0 additions & 33 deletions src/main/java/io/vavr/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ default boolean eq(Object o) {
* @throws NullPointerException if {@code predicate} is null
*/
default boolean exists(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
for (T t : this) {
if (predicate.test(t)) {
return true;
Expand All @@ -330,7 +329,6 @@ default boolean exists(Predicate<? super T> predicate) {
* @throws NullPointerException if {@code predicate} is null
*/
default boolean forAll(Predicate<? super T> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return !exists(predicate.negate());
}

Expand All @@ -342,7 +340,6 @@ default boolean forAll(Predicate<? super T> predicate) {
*/
@Override
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action, "action is null");
for (T t : this) {
action.accept(t);
}
Expand Down Expand Up @@ -383,7 +380,6 @@ default T getOrElse(T other) {
* @throws NullPointerException if supplier is null
*/
default T getOrElse(Supplier<? extends T> supplier) {
Objects.requireNonNull(supplier, "supplier is null");
return isEmpty() ? supplier.get() : get();
}

Expand All @@ -397,7 +393,6 @@ default T getOrElse(Supplier<? extends T> supplier) {
* @throws X if no value is present
*/
default <X extends Throwable> T getOrElseThrow(Supplier<X> supplier) throws X {
Objects.requireNonNull(supplier, "supplier is null");
if (isEmpty()) {
throw supplier.get();
} else {
Expand All @@ -413,7 +408,6 @@ default <X extends Throwable> T getOrElseThrow(Supplier<X> supplier) throws X {
* @throws NullPointerException if supplier is null
*/
default T getOrElseTry(CheckedFunction0<? extends T> supplier) {
Objects.requireNonNull(supplier, "supplier is null");
return isEmpty() ? Try.of(supplier).get() : get();
}

Expand Down Expand Up @@ -612,7 +606,6 @@ default <U> Validation<T, U> toInvalid(U value) {
*/
@Deprecated
default <U> Validation<T, U> toInvalid(Supplier<? extends U> valueSupplier) {
Objects.requireNonNull(valueSupplier, "valueSupplier is null");
return isEmpty() ? Validation.valid(valueSupplier.get()) : Validation.invalid(get());
}

Expand Down Expand Up @@ -672,7 +665,6 @@ default Object[] toJavaArray() {
@Deprecated
@SuppressWarnings("unchecked")
default T[] toJavaArray(Class<T> componentType) {
Objects.requireNonNull(componentType, "componentType is null");
if (componentType.isPrimitive()) {
final Class<?> boxedType =
componentType == boolean.class ? Boolean.class :
Expand Down Expand Up @@ -852,8 +844,6 @@ default <K, V> java.util.Map<K, V> toJavaMap(Function<? super T, ? extends Tuple
* @return a new {@code java.util.Map} of type {@code MAP}
*/
default <K, V, MAP extends java.util.Map<K, V>> MAP toJavaMap(Supplier<MAP> factory, Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) {
Objects.requireNonNull(keyMapper, "keyMapper is null");
Objects.requireNonNull(valueMapper, "valueMapper is null");
return toJavaMap(factory, t -> Tuple.of(keyMapper.apply(t), valueMapper.apply(t)));
}

Expand Down Expand Up @@ -883,7 +873,6 @@ default <K, V, MAP extends java.util.Map<K, V>> MAP toJavaMap(Supplier<MAP> fact
* @return a new {@code java.util.Map} of type {@code MAP}
*/
default <K, V, MAP extends java.util.Map<K, V>> MAP toJavaMap(Supplier<MAP> factory, Function<? super T, ? extends Tuple2<? extends K, ? extends V>> f) {
Objects.requireNonNull(f, "f is null");
final MAP map = factory.get();
if (!isEmpty()) {
if (isSingleValued()) {
Expand Down Expand Up @@ -1048,7 +1037,6 @@ default <R> Either<T, R> toLeft(R right) {
*/
@Deprecated
default <R> Either<T, R> toLeft(Supplier<? extends R> right) {
Objects.requireNonNull(right, "right is null");
return isEmpty() ? Either.right(right.get()) : Either.left(get());
}

Expand All @@ -1071,8 +1059,6 @@ default List<T> toList() {
* @return A new {@link HashMap}.
*/
default <K, V> Map<K, V> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) {
Objects.requireNonNull(keyMapper, "keyMapper is null");
Objects.requireNonNull(valueMapper, "valueMapper is null");
return toMap(t -> Tuple.of(keyMapper.apply(t), valueMapper.apply(t)));
}

Expand All @@ -1085,7 +1071,6 @@ default <K, V> Map<K, V> toMap(Function<? super T, ? extends K> keyMapper, Funct
* @return A new {@link HashMap}.
*/
default <K, V> Map<K, V> toMap(Function<? super T, ? extends Tuple2<? extends K, ? extends V>> f) {
Objects.requireNonNull(f, "f is null");
final Function<Tuple2<? extends K, ? extends V>, Map<K, V>> ofElement = HashMap::of;
final Function<Iterable<Tuple2<? extends K, ? extends V>>, Map<K, V>> ofAll = HashMap::ofEntries;
return ValueModule.toMap(this, HashMap.empty(), ofElement, ofAll, f);
Expand All @@ -1101,8 +1086,6 @@ default <K, V> Map<K, V> toMap(Function<? super T, ? extends Tuple2<? extends K,
* @return A new {@link LinkedHashMap}.
*/
default <K, V> Map<K, V> toLinkedMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) {
Objects.requireNonNull(keyMapper, "keyMapper is null");
Objects.requireNonNull(valueMapper, "valueMapper is null");
return toLinkedMap(t -> Tuple.of(keyMapper.apply(t), valueMapper.apply(t)));
}

Expand All @@ -1115,7 +1098,6 @@ default <K, V> Map<K, V> toLinkedMap(Function<? super T, ? extends K> keyMapper,
* @return A new {@link LinkedHashMap}.
*/
default <K, V> Map<K, V> toLinkedMap(Function<? super T, ? extends Tuple2<? extends K, ? extends V>> f) {
Objects.requireNonNull(f, "f is null");
final Function<Tuple2<? extends K, ? extends V>, Map<K, V>> ofElement = LinkedHashMap::of;
final Function<Iterable<Tuple2<? extends K, ? extends V>>, Map<K, V>> ofAll = LinkedHashMap::ofEntries;
return ValueModule.toMap(this, LinkedHashMap.empty(), ofElement, ofAll, f);
Expand All @@ -1131,8 +1113,6 @@ default <K, V> Map<K, V> toLinkedMap(Function<? super T, ? extends Tuple2<? exte
* @return A new {@link TreeMap}.
*/
default <K extends Comparable<? super K>, V> SortedMap<K, V> toSortedMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) {
Objects.requireNonNull(keyMapper, "keyMapper is null");
Objects.requireNonNull(valueMapper, "valueMapper is null");
return toSortedMap(t -> Tuple.of(keyMapper.apply(t), valueMapper.apply(t)));
}

Expand All @@ -1145,7 +1125,6 @@ default <K extends Comparable<? super K>, V> SortedMap<K, V> toSortedMap(Functio
* @return A new {@link TreeMap}.
*/
default <K extends Comparable<? super K>, V> SortedMap<K, V> toSortedMap(Function<? super T, ? extends Tuple2<? extends K, ? extends V>> f) {
Objects.requireNonNull(f, "f is null");
return toSortedMap(Comparator.naturalOrder(), f);
}

Expand All @@ -1160,9 +1139,6 @@ default <K extends Comparable<? super K>, V> SortedMap<K, V> toSortedMap(Functio
* @return A new {@link TreeMap}.
*/
default <K, V> SortedMap<K, V> toSortedMap(Comparator<? super K> comparator, Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) {
Objects.requireNonNull(comparator, "comparator is null");
Objects.requireNonNull(keyMapper, "keyMapper is null");
Objects.requireNonNull(valueMapper, "valueMapper is null");
return toSortedMap(comparator, t -> Tuple.of(keyMapper.apply(t), valueMapper.apply(t)));
}

Expand All @@ -1176,8 +1152,6 @@ default <K, V> SortedMap<K, V> toSortedMap(Comparator<? super K> comparator, Fun
* @return A new {@link TreeMap}.
*/
default <K, V> SortedMap<K, V> toSortedMap(Comparator<? super K> comparator, Function<? super T, ? extends Tuple2<? extends K, ? extends V>> f) {
Objects.requireNonNull(comparator, "comparator is null");
Objects.requireNonNull(f, "f is null");
final Function<Tuple2<? extends K, ? extends V>, SortedMap<K, V>> ofElement = t -> TreeMap.of(comparator, t);
final Function<Iterable<Tuple2<? extends K, ? extends V>>, SortedMap<K, V>> ofAll = t -> TreeMap.ofEntries(comparator, t);
return ValueModule.toMap(this, TreeMap.empty(comparator), ofElement, ofAll, f);
Expand Down Expand Up @@ -1219,7 +1193,6 @@ default <L> Either<L, T> toEither(L left) {
* @return A new {@link Either}.
*/
default <L> Either<L, T> toEither(Supplier<? extends L> leftSupplier) {
Objects.requireNonNull(leftSupplier, "leftSupplier is null");
if (this instanceof Either) {
return ((Either<?, T>) this).mapLeft(ignored -> leftSupplier.get());
} else {
Expand Down Expand Up @@ -1250,7 +1223,6 @@ default <E> Validation<E, T> toValidation(E invalid) {
* @return A new {@link Validation}.
*/
default <E> Validation<E, T> toValidation(Supplier<? extends E> invalidSupplier) {
Objects.requireNonNull(invalidSupplier, "invalidSupplier is null");
if (this instanceof Validation) {
return ((Validation<?, T>) this).mapError(ignored -> invalidSupplier.get());
} else {
Expand Down Expand Up @@ -1291,7 +1263,6 @@ default PriorityQueue<T> toPriorityQueue() {
* @return A new {@link PriorityQueue}.
*/
default PriorityQueue<T> toPriorityQueue(Comparator<? super T> comparator) {
Objects.requireNonNull(comparator, "comparator is null");
final PriorityQueue<T> empty = PriorityQueue.empty(comparator);
final Function<T, PriorityQueue<T>> of = value -> PriorityQueue.of(comparator, value);
final Function<Iterable<T>, PriorityQueue<T>> ofAll = values -> PriorityQueue.ofAll(comparator, values);
Expand Down Expand Up @@ -1324,7 +1295,6 @@ default <L> Either<L, T> toRight(L left) {
*/
@Deprecated
default <L> Either<L, T> toRight(Supplier<? extends L> left) {
Objects.requireNonNull(left, "left is null");
return isEmpty() ? Either.left(left.get()) : Either.right(get());
}

Expand Down Expand Up @@ -1372,7 +1342,6 @@ default SortedSet<T> toSortedSet() throws ClassCastException {
* @return A new {@link TreeSet}.
*/
default SortedSet<T> toSortedSet(Comparator<? super T> comparator) {
Objects.requireNonNull(comparator, "comparator is null");
return ValueModule.toTraversable(this, TreeSet.empty(comparator), value -> TreeSet.of(comparator, value), values -> TreeSet.ofAll(comparator, values));
}

Expand Down Expand Up @@ -1411,7 +1380,6 @@ default Try<T> toTry() {
* @return A new {@link Try}.
*/
default Try<T> toTry(Supplier<? extends Throwable> ifEmpty) {
Objects.requireNonNull(ifEmpty, "ifEmpty is null");
return isEmpty() ? Try.failure(ifEmpty.get()) : toTry();
}

Expand Down Expand Up @@ -1463,7 +1431,6 @@ default <E> Validation<E, T> toValid(E error) {
*/
@Deprecated
default <E> Validation<E, T> toValid(Supplier<? extends E> errorSupplier) {
Objects.requireNonNull(errorSupplier, "errorSupplier is null");
return isEmpty() ? Validation.invalid(errorSupplier.get()) : Validation.valid(get());
}

Expand Down
Loading