mirror of
https://github.com/google/nomulus.git
synced 2025-05-13 16:07:15 +02:00
Use java-8 functionals in FormFields
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=193407158
This commit is contained in:
parent
0f2a1bfccd
commit
b885d01c0c
1 changed files with 13 additions and 21 deletions
|
@ -266,9 +266,7 @@ public final class FormField<I, O> {
|
||||||
|
|
||||||
/** Ensure value is not {@code null}. */
|
/** Ensure value is not {@code null}. */
|
||||||
public Builder<I, O> required() {
|
public Builder<I, O> required() {
|
||||||
@SuppressWarnings("unchecked")
|
return transform(Builder::checkNotNullTransform);
|
||||||
Function<O, O> requiredFunction = (Function<O, O>) REQUIRED_FUNCTION;
|
|
||||||
return transform(requiredFunction);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -280,17 +278,12 @@ public final class FormField<I, O> {
|
||||||
public Builder<I, O> emptyToNull() {
|
public Builder<I, O> emptyToNull() {
|
||||||
checkState(CharSequence.class.isAssignableFrom(typeOut)
|
checkState(CharSequence.class.isAssignableFrom(typeOut)
|
||||||
|| Collection.class.isAssignableFrom(typeOut));
|
|| Collection.class.isAssignableFrom(typeOut));
|
||||||
@SuppressWarnings("unchecked")
|
return transform(
|
||||||
Function<O, O> emptyToNullFunction =
|
input ->
|
||||||
(Function<O, O>)
|
((input instanceof CharSequence) && (((CharSequence) input).length() == 0))
|
||||||
((Function<Object, Object>)
|
|| ((input instanceof Collection) && ((Collection<?>) input).isEmpty())
|
||||||
input ->
|
? null
|
||||||
((input instanceof CharSequence) && (((CharSequence) input).length() == 0))
|
: input);
|
||||||
|| ((input instanceof Collection)
|
|
||||||
&& ((Collection<?>) input).isEmpty())
|
|
||||||
? null
|
|
||||||
: input);
|
|
||||||
return transform(emptyToNullFunction);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -560,13 +553,12 @@ public final class FormField<I, O> {
|
||||||
return new FormField<>(name, typeIn, typeOut, converter);
|
return new FormField<>(name, typeIn, typeOut, converter);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Function<Object, Object> REQUIRED_FUNCTION =
|
private static <O> O checkNotNullTransform(@Nullable O input) {
|
||||||
input -> {
|
if (input == null) {
|
||||||
if (input == null) {
|
throw new FormFieldException("This field is required.");
|
||||||
throw new FormFieldException("This field is required.");
|
}
|
||||||
}
|
return input;
|
||||||
return input;
|
}
|
||||||
};
|
|
||||||
|
|
||||||
private static final class DefaultFunction<O> implements Function<O, O> {
|
private static final class DefaultFunction<O> implements Function<O, O> {
|
||||||
private final O defaultValue;
|
private final O defaultValue;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue