Skip to content

Commit

Permalink
Add nullability annotations and null checks to function package
Browse files Browse the repository at this point in the history
  • Loading branch information
aNNiMON committed Mar 22, 2019
1 parent 6f2a806 commit 8d18ee2
Show file tree
Hide file tree
Showing 37 changed files with 324 additions and 111 deletions.
1 change: 1 addition & 0 deletions stream/src/main/java/com/annimon/stream/Exceptional.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class Exceptional<T> {
* @param <T> the type of value
* @param supplier a supplier function
* @return an {@code Exceptional}
* @throws NullPointerException if {@code supplier} is null
*/
@NotNull
public static <T> Exceptional<T> of(@NotNull ThrowableSupplier<T, Throwable> supplier) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.annimon.stream.function;

import org.jetbrains.annotations.NotNull;

/**
* Represents an operation on two input arguments.
*
Expand Down Expand Up @@ -34,8 +36,8 @@ private Util() { }
* @throws NullPointerException if {@code c1} or {@code c2} is null
*/
public static <T, U> BiConsumer<T, U> andThen(
final BiConsumer<? super T, ? super U> c1,
final BiConsumer<? super T, ? super U> c2) {
@NotNull final BiConsumer<? super T, ? super U> c1,
@NotNull final BiConsumer<? super T, ? super U> c2) {
return new BiConsumer<T, U>() {
@Override
public void accept(T t, U u) {
Expand Down
11 changes: 7 additions & 4 deletions stream/src/main/java/com/annimon/stream/function/BiFunction.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.annimon.stream.function;

import com.annimon.stream.Objects;
import org.jetbrains.annotations.NotNull;

/**
* Represents a function which produces result from two input arguments.
Expand Down Expand Up @@ -36,11 +37,13 @@ private Util() { }
* @param f1 the {@code BiFunction} which is called first
* @param f2 the function for transform {@code BiFunction f1} result to the type {@code V}
* @return the result of composed function
* @throws NullPointerException if {@code f1} or {@code f2} or result of {@code BiFunction f1} is null
* @throws NullPointerException if {@code f1} or {@code f2} is null
*/
public static <T, U, R, V> BiFunction<T, U, V> andThen(
final BiFunction<? super T, ? super U, ? extends R> f1,
final Function<? super R, ? extends V> f2) {
@NotNull final BiFunction<? super T, ? super U, ? extends R> f1,
@NotNull final Function<? super R, ? extends V> f2) {
Objects.requireNonNull(f1, "f1");
Objects.requireNonNull(f2, "f2");
return new BiFunction<T, U, V>() {

@Override
Expand All @@ -66,7 +69,7 @@ public V apply(T t, U u) {
* @since 1.1.6
*/
public static <T, U, R> BiFunction<U, T, R> reverse(
final BiFunction<? super T, ? super U, ? extends R> function) {
@NotNull final BiFunction<? super T, ? super U, ? extends R> function) {
Objects.requireNonNull(function);
return new BiFunction<U, T, R>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.annimon.stream.Objects;
import java.util.Comparator;
import org.jetbrains.annotations.NotNull;

/**
* Represents an operation on two operands that produces a result of the
Expand All @@ -24,7 +25,8 @@ private Util() { }
* according to the supplied {@code Comparator}
* @throws NullPointerException if the argument is null
*/
public static <T> BinaryOperator<T> minBy(final Comparator<? super T> comparator) {
public static <T> BinaryOperator<T> minBy(
@NotNull final Comparator<? super T> comparator) {
Objects.requireNonNull(comparator);
return new BinaryOperator<T>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.annimon.stream.function;

import com.annimon.stream.Objects;
import org.jetbrains.annotations.NotNull;

/**
* Represents an operation on a {@code boolean}-valued input argument.
*
Expand Down Expand Up @@ -29,7 +32,11 @@ private Util() { }
* @return a composed {@code BooleanConsumer}
* @throws NullPointerException if {@code c1} or {@code c2} is null
*/
public static BooleanConsumer andThen(final BooleanConsumer c1, final BooleanConsumer c2) {
public static BooleanConsumer andThen(
@NotNull final BooleanConsumer c1,
@NotNull final BooleanConsumer c2) {
Objects.requireNonNull(c1, "c1");
Objects.requireNonNull(c2, "c2");
return new BooleanConsumer() {
@Override
public void accept(boolean value) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.annimon.stream.function;

import com.annimon.stream.Objects;
import org.jetbrains.annotations.NotNull;

/**
* Represents a {@code boolean}-valued predicate (function with boolean type result).
*
Expand Down Expand Up @@ -43,7 +46,11 @@ public boolean test(boolean operand) {
* @return a composed {@code BooleanPredicate}
* @throws NullPointerException if {@code p1} or {@code p2} is null
*/
public static BooleanPredicate and(final BooleanPredicate p1, final BooleanPredicate p2) {
public static BooleanPredicate and(
@NotNull final BooleanPredicate p1,
@NotNull final BooleanPredicate p2) {
Objects.requireNonNull(p1, "predicate1");
Objects.requireNonNull(p2, "predicate2");
return new BooleanPredicate() {
@Override
public boolean test(boolean value) {
Expand All @@ -60,7 +67,11 @@ public boolean test(boolean value) {
* @return a composed {@code BooleanPredicate}
* @throws NullPointerException if {@code p1} or {@code p2} is null
*/
public static BooleanPredicate or(final BooleanPredicate p1, final BooleanPredicate p2) {
public static BooleanPredicate or(
@NotNull final BooleanPredicate p1,
@NotNull final BooleanPredicate p2) {
Objects.requireNonNull(p1, "predicate1");
Objects.requireNonNull(p2, "predicate2");
return new BooleanPredicate() {
@Override
public boolean test(boolean value) {
Expand All @@ -77,7 +88,11 @@ public boolean test(boolean value) {
* @return a composed {@code BooleanPredicate}
* @throws NullPointerException if {@code p1} or {@code p2} is null
*/
public static BooleanPredicate xor(final BooleanPredicate p1, final BooleanPredicate p2) {
public static BooleanPredicate xor(
@NotNull final BooleanPredicate p1,
@NotNull final BooleanPredicate p2) {
Objects.requireNonNull(p1, "predicate1");
Objects.requireNonNull(p2, "predicate2");
return new BooleanPredicate() {
@Override
public boolean test(boolean value) {
Expand All @@ -93,7 +108,8 @@ public boolean test(boolean value) {
* @return a composed {@code BooleanPredicate}
* @throws NullPointerException if {@code p1} is null
*/
public static BooleanPredicate negate(final BooleanPredicate p1) {
public static BooleanPredicate negate(@NotNull final BooleanPredicate p1) {
Objects.requireNonNull(p1);
return new BooleanPredicate() {
@Override
public boolean test(boolean value) {
Expand Down
16 changes: 12 additions & 4 deletions stream/src/main/java/com/annimon/stream/function/Consumer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.annimon.stream.function;

import com.annimon.stream.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Represents an operation on input argument.
Expand Down Expand Up @@ -32,7 +34,11 @@ private Util() { }
* @return a composed {@code Consumer}
* @throws NullPointerException if {@code c1} or {@code c2} is null
*/
public static <T> Consumer<T> andThen(final Consumer<? super T> c1, final Consumer<? super T> c2) {
public static <T> Consumer<T> andThen(
@NotNull final Consumer<? super T> c1,
@NotNull final Consumer<? super T> c2) {
Objects.requireNonNull(c1, "c1");
Objects.requireNonNull(c2, "c2");
return new Consumer<T>() {
@Override
public void accept(T value) {
Expand All @@ -51,7 +57,8 @@ public void accept(T value) {
* @throws NullPointerException if {@code throwableConsumer} is null
* @see #safe(com.annimon.stream.function.ThrowableConsumer, com.annimon.stream.function.Consumer)
*/
public static <T> Consumer<T> safe(ThrowableConsumer<? super T, Throwable> throwableConsumer) {
public static <T> Consumer<T> safe(
@NotNull ThrowableConsumer<? super T, Throwable> throwableConsumer) {
return safe(throwableConsumer, null);
}

Expand All @@ -66,8 +73,9 @@ public static <T> Consumer<T> safe(ThrowableConsumer<? super T, Throwable> throw
* @see #safe(com.annimon.stream.function.ThrowableConsumer)
*/
public static <T> Consumer<T> safe(
final ThrowableConsumer<? super T, Throwable> throwableConsumer,
final Consumer<? super T> onFailedConsumer) {
@NotNull final ThrowableConsumer<? super T, Throwable> throwableConsumer,
@Nullable final Consumer<? super T> onFailedConsumer) {
Objects.requireNonNull(throwableConsumer);
return new Consumer<T>() {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.annimon.stream.function;

import com.annimon.stream.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Represents an operation on a {@code double}-valued input argument.
*
Expand Down Expand Up @@ -29,7 +33,11 @@ private Util() { }
* @return a composed {@code DoubleConsumer}
* @throws NullPointerException if {@code c1} or {@code c2} is null
*/
public static DoubleConsumer andThen(final DoubleConsumer c1, final DoubleConsumer c2) {
public static DoubleConsumer andThen(
@NotNull final DoubleConsumer c1,
@NotNull final DoubleConsumer c2) {
Objects.requireNonNull(c1, "c1");
Objects.requireNonNull(c2, "c2");
return new DoubleConsumer() {
@Override
public void accept(double value) {
Expand All @@ -48,7 +56,8 @@ public void accept(double value) {
* @since 1.1.7
* @see #safe(com.annimon.stream.function.ThrowableDoubleConsumer, com.annimon.stream.function.DoubleConsumer)
*/
public static DoubleConsumer safe(ThrowableDoubleConsumer<Throwable> throwableConsumer) {
public static DoubleConsumer safe(
@NotNull ThrowableDoubleConsumer<Throwable> throwableConsumer) {
return safe(throwableConsumer, null);
}

Expand All @@ -63,8 +72,9 @@ public static DoubleConsumer safe(ThrowableDoubleConsumer<Throwable> throwableCo
* @see #safe(com.annimon.stream.function.ThrowableDoubleConsumer)
*/
public static DoubleConsumer safe(
final ThrowableDoubleConsumer<Throwable> throwableConsumer,
final DoubleConsumer onFailedConsumer) {
@NotNull final ThrowableDoubleConsumer<Throwable> throwableConsumer,
@Nullable final DoubleConsumer onFailedConsumer) {
Objects.requireNonNull(throwableConsumer);
return new DoubleConsumer() {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.annimon.stream.function;

import com.annimon.stream.Objects;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Represents a function which produces result from {@code double}-valued input argument.
*
Expand Down Expand Up @@ -33,7 +37,7 @@ private Util() { }
* @see #safe(com.annimon.stream.function.ThrowableDoubleFunction, java.lang.Object)
*/
public static <R> DoubleFunction<R> safe(
ThrowableDoubleFunction<? extends R, Throwable> throwableFunction) {
@NotNull ThrowableDoubleFunction<? extends R, Throwable> throwableFunction) {
return Util.<R>safe(throwableFunction, null);
}

Expand All @@ -48,8 +52,9 @@ public static <R> DoubleFunction<R> safe(
* @since 1.1.7
*/
public static <R> DoubleFunction<R> safe(
final ThrowableDoubleFunction<? extends R, Throwable> throwableFunction,
final R resultIfFailed) {
@NotNull final ThrowableDoubleFunction<? extends R, Throwable> throwableFunction,
@Nullable final R resultIfFailed) {
Objects.requireNonNull(throwableFunction);
return new DoubleFunction<R>() {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.annimon.stream.function;

import com.annimon.stream.Objects;
import org.jetbrains.annotations.NotNull;

/**
* Represents a {@code double}-valued predicate (function with boolean type result).
*
Expand Down Expand Up @@ -28,7 +31,11 @@ private Util() { }
* @return a composed {@code DoublePredicate}
* @throws NullPointerException if {@code p1} or {@code p2} is null
*/
public static DoublePredicate and(final DoublePredicate p1, final DoublePredicate p2) {
public static DoublePredicate and(
@NotNull final DoublePredicate p1,
@NotNull final DoublePredicate p2) {
Objects.requireNonNull(p1, "predicate1");
Objects.requireNonNull(p2, "predicate2");
return new DoublePredicate() {
@Override
public boolean test(double value) {
Expand All @@ -45,7 +52,11 @@ public boolean test(double value) {
* @return a composed {@code DoublePredicate}
* @throws NullPointerException if {@code p1} or {@code p2} is null
*/
public static DoublePredicate or(final DoublePredicate p1, final DoublePredicate p2) {
public static DoublePredicate or(
@NotNull final DoublePredicate p1,
@NotNull final DoublePredicate p2) {
Objects.requireNonNull(p1, "predicate1");
Objects.requireNonNull(p2, "predicate2");
return new DoublePredicate() {
@Override
public boolean test(double value) {
Expand All @@ -62,7 +73,11 @@ public boolean test(double value) {
* @return a composed {@code DoublePredicate}
* @throws NullPointerException if {@code p1} or {@code p2} is null
*/
public static DoublePredicate xor(final DoublePredicate p1, final DoublePredicate p2) {
public static DoublePredicate xor(
@NotNull final DoublePredicate p1,
@NotNull final DoublePredicate p2) {
Objects.requireNonNull(p1, "predicate1");
Objects.requireNonNull(p2, "predicate2");
return new DoublePredicate() {
@Override
public boolean test(double value) {
Expand All @@ -78,7 +93,8 @@ public boolean test(double value) {
* @return a composed {@code DoublePredicate}
* @throws NullPointerException if {@code p1} is null
*/
public static DoublePredicate negate(final DoublePredicate p1) {
public static DoublePredicate negate(@NotNull final DoublePredicate p1) {
Objects.requireNonNull(p1);
return new DoublePredicate() {
@Override
public boolean test(double value) {
Expand All @@ -95,7 +111,7 @@ public boolean test(double value) {
* @since 1.1.7
* @see #safe(com.annimon.stream.function.ThrowableDoublePredicate, boolean)
*/
public static DoublePredicate safe(ThrowableDoublePredicate<Throwable> throwablePredicate) {
public static DoublePredicate safe(@NotNull ThrowableDoublePredicate<Throwable> throwablePredicate) {
return safe(throwablePredicate, false);
}

Expand All @@ -109,8 +125,9 @@ public static DoublePredicate safe(ThrowableDoublePredicate<Throwable> throwable
* @since 1.1.7
*/
public static DoublePredicate safe(
final ThrowableDoublePredicate<Throwable> throwablePredicate,
@NotNull final ThrowableDoublePredicate<Throwable> throwablePredicate,
final boolean resultIfFailed) {
Objects.requireNonNull(throwablePredicate);
return new DoublePredicate() {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.annimon.stream.function;

import com.annimon.stream.Objects;
import org.jetbrains.annotations.NotNull;

/**
* Represents a supplier of {@code double}-valued results.
*
Expand Down Expand Up @@ -28,7 +31,8 @@ private Util() { }
* @since 1.1.7
* @see #safe(com.annimon.stream.function.ThrowableDoubleSupplier, double)
*/
public static DoubleSupplier safe(ThrowableDoubleSupplier<Throwable> throwableSupplier) {
public static DoubleSupplier safe(
@NotNull ThrowableDoubleSupplier<Throwable> throwableSupplier) {
return safe(throwableSupplier, 0.0);
}

Expand All @@ -42,8 +46,9 @@ public static DoubleSupplier safe(ThrowableDoubleSupplier<Throwable> throwableSu
* @since 1.1.7
*/
public static DoubleSupplier safe(
final ThrowableDoubleSupplier<Throwable> throwableSupplier,
@NotNull final ThrowableDoubleSupplier<Throwable> throwableSupplier,
final double resultIfFailed) {
Objects.requireNonNull(throwableSupplier);
return new DoubleSupplier() {

@Override
Expand Down
Loading

0 comments on commit 8d18ee2

Please sign in to comment.