
de.is24.guava.NonNullFunctions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of deadcode4j-maven-plugin Show documentation
Show all versions of deadcode4j-maven-plugin Show documentation
Finds unused classes of a project
package de.is24.guava;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import de.is24.deadcode4j.Utils;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* Provides convenience methods for {@link de.is24.guava.NonNullFunction}.
*
* @since 2.0.0
*/
public final class NonNullFunctions {
private NonNullFunctions() {}
/**
* Returns a NonNullFunction
that will call the specified functions one by one until a return value is
* present or the end of the call chain is reached.
*
* @since 2.0.0
*/
@Nonnull
public static NonNullFunction> or(@Nonnull final NonNullFunction>... functions) {
return new NonNullFunction>() {
@Nonnull
@Override
public Optional apply(@Nonnull F input) {
for (int i = 0; ; ) {
Optional result = functions[i++].apply(input);
if (result.isPresent() || i == functions.length) {
return result;
}
}
}
};
}
/**
* Transforms a NonNullFunction
into a Function
.
* If a Function
's input is null
, a NullPointerException
is thrown.
*
* @since 2.0.0
*/
@Nonnull
public static Function toFunction(@Nonnull final NonNullFunction nonNullFunction) {
return new Function() {
@Nullable
@Override
@SuppressWarnings("ConstantConditions")
public T apply(@Nullable F input) {
return nonNullFunction.apply(Utils.checkNotNull(input));
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy