public class MatcherMapConverter<I,O>
extends java.lang.Object
implements org.srplib.conversion.Converter<I,O>, java.io.Serializable
Useful as a replacement for switch...case...default and if...else chains.
Internally this implementation leverages Hamcrest Matcher. First matcher wins.
Caution! If matchers are order sensitive, use LinkedHashMap implementation.
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
DEFAULT_NO_MATCH_MESSAGE_PATTERN |
| Modifier | Constructor and Description |
|---|---|
protected |
MatcherMapConverter() |
|
MatcherMapConverter(java.util.Map<org.hamcrest.Matcher<I>,O> matcherMap)
Creates converter using
Matcher to object map. |
| Modifier and Type | Method and Description |
|---|---|
O |
convert(I input)
Tries to find target value for specified input value.
|
O |
getDefaultValue()
Return an object to be returned if no matcher found for specified value.
|
protected void |
put(org.hamcrest.Matcher<I> key,
O value) |
void |
setDefaultValue(O defaultValue)
Set an object to be returned if no matcher found for specified value.
|
void |
setNoMatchMessagePattern(java.lang.String pattern)
Set message pattern to be used to construct error message when no matches is found.
|
void |
setThrowExceptionOnNoMatch(boolean throwExceptionOnNoMatch)
Specifies whether to throw an exception if no match is found and no default value is specified.
|
public static final java.lang.String DEFAULT_NO_MATCH_MESSAGE_PATTERN
public MatcherMapConverter(java.util.Map<org.hamcrest.Matcher<I>,O> matcherMap)
Matcher to object map.matcherMap - Map matcher to object map (non-null). Use LinkedHashMap if matchers are ordered)java.lang.IllegalArgumentException - if matcherMap is nullprotected MatcherMapConverter()
public O getDefaultValue()
public void setThrowExceptionOnNoMatch(boolean throwExceptionOnNoMatch)
throwExceptionOnNoMatch - boolean a flag. Default value is true.public void setDefaultValue(O defaultValue)
defaultValue - Object a default value. If not specified then an exception will be thrown/public void setNoMatchMessagePattern(java.lang.String pattern)
pattern - String message pattern with single string placeholder '%s'public O convert(I input)
convert in interface org.srplib.conversion.Converter<I,O>input - input valuenull depending on
throwExceptionOnNoMatch value otherwise throws an exception.java.lang.IllegalArgumentException - if mapping isn't found and default value isn't defined and
throwExceptionOnNoMatch == true.