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}. */
|
||||
public Builder<I, O> required() {
|
||||
@SuppressWarnings("unchecked")
|
||||
Function<O, O> requiredFunction = (Function<O, O>) REQUIRED_FUNCTION;
|
||||
return transform(requiredFunction);
|
||||
return transform(Builder::checkNotNullTransform);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -280,17 +278,12 @@ public final class FormField<I, O> {
|
|||
public Builder<I, O> emptyToNull() {
|
||||
checkState(CharSequence.class.isAssignableFrom(typeOut)
|
||||
|| Collection.class.isAssignableFrom(typeOut));
|
||||
@SuppressWarnings("unchecked")
|
||||
Function<O, O> emptyToNullFunction =
|
||||
(Function<O, O>)
|
||||
((Function<Object, Object>)
|
||||
input ->
|
||||
((input instanceof CharSequence) && (((CharSequence) input).length() == 0))
|
||||
|| ((input instanceof Collection)
|
||||
&& ((Collection<?>) input).isEmpty())
|
||||
? null
|
||||
: input);
|
||||
return transform(emptyToNullFunction);
|
||||
return transform(
|
||||
input ->
|
||||
((input instanceof CharSequence) && (((CharSequence) input).length() == 0))
|
||||
|| ((input instanceof Collection) && ((Collection<?>) input).isEmpty())
|
||||
? null
|
||||
: input);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -560,13 +553,12 @@ public final class FormField<I, O> {
|
|||
return new FormField<>(name, typeIn, typeOut, converter);
|
||||
}
|
||||
|
||||
private static final Function<Object, Object> REQUIRED_FUNCTION =
|
||||
input -> {
|
||||
if (input == null) {
|
||||
throw new FormFieldException("This field is required.");
|
||||
}
|
||||
return input;
|
||||
};
|
||||
private static <O> O checkNotNullTransform(@Nullable O input) {
|
||||
if (input == null) {
|
||||
throw new FormFieldException("This field is required.");
|
||||
}
|
||||
return input;
|
||||
}
|
||||
|
||||
private static final class DefaultFunction<O> implements Function<O, O> {
|
||||
private final O defaultValue;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue