Class FormField.Builder<I,O>
- Type Parameters:
I
- input value typeO
- output value type
FormField
.-
Method Summary
Modifier and TypeMethodDescription<C extends Enum<C>>
FormField.Builder<I, C> Uppercases value and converts to an enum field ofenumClass
.FormField.Builder
<List<I>, List<O>> asList()
Turns this form field into something that processes lists.asList
(com.google.common.base.Splitter splitter) Turns this form field into a split string list that applies itself to each item.FormField.Builder
<List<I>, Set<O>> asSet()
Same asasList()
but outputs anImmutableSet
instead.asSet
(com.google.common.base.Splitter splitter) Same asasList(Splitter)
but outputs anImmutableSet
instead.build()
Creates a newFormField
instance.Transform empty values intonull
.Enforce value be a member ofvalues
.ModifyString
input to be lowercase.matches
(com.google.re2j.Pattern pattern) Alias formatches(pattern, null)
Ensure input matchespattern
.Enforce value length/size/value is withinrange
.required()
Ensure value is notnull
.retains
(com.google.common.base.CharMatcher matcher) Removes all characters not inmatcher
.<T> FormField.Builder
<I, T> Performs arbitrary type transformation fromO
toT
.Manipulates values without changing type.trimmed()
ModifyString
input to remove whitespace around the sides.ModifyString
input to be uppercase.withDefault
(O defaultValue) CausesdefaultValue
to be substituted if value isnull
.
-
Method Details
-
withDefault
CausesdefaultValue
to be substituted if value isnull
. -
required
Ensure value is notnull
. -
emptyToNull
Transform empty values intonull
.- Throws:
IllegalStateException
- if current output type is not aCharSequence
orCollection
.
-
trimmed
ModifyString
input to remove whitespace around the sides.null
values are passed through.- Throws:
IllegalStateException
- if current output type is not a String.
-
uppercased
ModifyString
input to be uppercase.null
values are passed through.- Throws:
IllegalStateException
- if current output type is not a String.
-
lowercased
ModifyString
input to be lowercase.null
values are passed through.- Throws:
IllegalStateException
- if current output type is not a String.
-
matches
public FormField.Builder<I,O> matches(com.google.re2j.Pattern pattern, @Nullable String errorMessage) Ensure input matchespattern
.null
values are passed through.- Parameters:
pattern
- is used to validate the user input. It matches against the whole string, so you don't need to use the ^$ characters.errorMessage
- is a helpful error message, which should include an example. If this is not provided, a default error message will be shown that includes the regexp pattern.- Throws:
IllegalStateException
- if current output type is not aCharSequence
.- See Also:
-
matches
Alias formatches(pattern, null)
-
retains
Removes all characters not inmatcher
.null
values are passed through.- Parameters:
matcher
- indicates which characters are to be retained- Throws:
IllegalStateException
- if current output type is not aCharSequence
-
range
Enforce value length/size/value is withinrange
.The following input value types are supported:
CharSequence
: Length must be withinrange
.Collection
: Size must be withinrange
.Number
: Value must be withinrange
.
null
values are passed through. Please note that setting a lower bound on your range does not implyrequired()
, as range checking only applies to non-null
values.- Throws:
IllegalStateException
- if current output type is not one of the above types.
-
in
Enforce value be a member ofvalues
.null
values are passed through.- Throws:
IllegalArgumentException
- ifvalues
is empty.
-
transform
Performs arbitrary type transformation fromO
toT
.Your
transform
function is expected to pass-throughnull
values as a no-op, since it's up torequired()
to block them. You might also want to consider using a try block that rethrows exceptions asFormFieldException
.Here's an example of how you'd convert from String to Integer:
FormField.named("foo", String.class) .transform(Integer.class, new Function<String, Integer>() { @Nullable @Override public Integer apply(@Nullable String input) { try { return input != null ? Integer.parseInt(input) : null; } catch (IllegalArgumentException e) { throw new FormFieldException("Invalid number.", e); } }}) .build();
- See Also:
-
transform
Manipulates values without changing type.Please see
transform(Class, Function)
for information about the contract to whichtransform
is expected to conform. -
asEnum
Uppercases value and converts to an enum field ofenumClass
.null
values are passed through.- Throws:
IllegalArgumentException
- ifenumClass
is not an enum class.IllegalStateException
- if current output type is not a String.
-
asList
Turns this form field into something that processes lists.The current object definition will be applied to each item in the list. If a
FormFieldException
is thrown when processing an item, then itsfieldName
will be rewritten to include the index, e.g.name
becomesname[0]
.The outputted list will be an
ImmutableList
. This is not reflected in the generic typing for the sake of brevity.A
null
value for list will be passed through. List items that convert tonull
will be discarded (sinceImmutableList
does not permitnull
values). -
asList
Turns this form field into a split string list that applies itself to each item.The behavior of this method is counter-intuitive. It behaves similar to
asList()
in the sense that all transforms specified before this method will be applied to the individual resulting list items.For example, to turn a comma-delimited string into an enum list:
private static final FormField<String, List<State>> STATES_FIELD = FormField.named("states") .uppercased() .asEnum(State.class) .asList(Splitter.on(',').omitEmptyStrings().trimResults()) .build();
You'll notice that the transforms specified before this method are applied to each list item. However unlike
asList()
, if an error is thrown on an individual item, thenFormFieldException.getFieldName()
will not contain the index.- Throws:
IllegalStateException
- If either the current input type isn't String.
-
asSet
Same asasList()
but outputs anImmutableSet
instead.- Throws:
IllegalStateException
- if you called asList() before calling this method.
-
asSet
Same asasList(Splitter)
but outputs anImmutableSet
instead.- Throws:
IllegalStateException
- If the current input type isn't String.
-
build
Creates a newFormField
instance.
-