com.univocity.api.common
Class ParameterizedString

java.lang.Object
  extended by com.univocity.api.common.ParameterizedString
All Implemented Interfaces:
Cloneable

public class ParameterizedString
extends Object
implements Cloneable

Utility class for handling Strings with parameters. Use set(String, Object) to set a parameter value, and applyParameterValues() to obtain a result String with the values of all known parameters replaced. Parameters without values provided will not be replaced, therefore the string "zero/{one}/{two}/{one}", with parameter "one" set to 27 will evaluate to "zero/27/{two}/27"

Author:
uniVocity Software Pty Ltd - dev@univocity.com

Constructor Summary
ParameterizedString(String string)
          Creates a new parameterized string with parameter names enclosed within { and }
ParameterizedString(String string, String openBracket, String closeBracket)
          Creates a new parameterized string with custom open and closing brackets
 
Method Summary
 String applyParameterValues()
          Applies the parameter values provided using set(String, Object) and returns the resulting String Unless a default value is provided via getDefaultValue(), parameters without values provided will not be replaced.
 void applyParameterValues(StringBuilder out)
          Applies the parameter values provided using set(String, Object) and appends the resulting String to a given StringBuilder Unless a default value is provided via getDefaultValue(), parameters without values provided will not be replaced.
 void clearValues()
          Clears the values of all parameters.
 ParameterizedString clone()
          Clones this parameterzied string object.
 boolean contains(String parameterName)
          Tests whether a given parameter name exists in this parameterized string.
 boolean equals(Object o)
           
 Object get(String parameter)
          Returns the value of a given parameter.
 String getContentAfterLastParameter()
          Returns the content after the last parameter in this parameterized string.
 String getContentBeforeFirstParameter()
          Returns the content before the first parameter in this parameterized string.
 boolean getConvertDefaultValueToNull()
          Flag indicating that values parsed from this ParameterizedString will be converted to null, when equal to the String representation of the value returned by getDefaultValue().
 Object getDefaultValue()
          Returns the default value to be used for parameters that have no value associated.
 String getFormat(String parameterName)
          Returns the format associated with a given parameter
 int getIndexAfterLastParameter()
          Returns the index after the last parameter in this parameterized string.
 int getIndexBeforeFirstParameter()
          Returns the index before the first parameter in this parameterized string.
 Set<String> getParameters()
          Returns a set of all parameter names found in the input string given in the constructor of this class.
 Map<String,Object> getParameterValues()
          Returns the current parameter names and their values.
 int hashCode()
           
 Map<String,Object> parse(String input)
          Parses the String input and extracts the parameter values storing them as regular parameters.
 void set(Map<String,?> parametersAndValues)
          Sets multiple parameter values
 void set(String parameter, Object value)
          Sets a parameter value
 void setConvertDefaultValueToNull(boolean convertDefaultValueToNull)
          Defines whether values parsed from this ParameterizedString should be converted to null, when equal to the String representation of the value returned by getDefaultValue().
 void setDefaultValue(Object defaultValue)
          Defines a default value to be used for parameters that have no value associated.
 String toString()
          Returns the original String provided in the constructor of this class, no parameters are replaced
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ParameterizedString

public ParameterizedString(String string,
                           String openBracket,
                           String closeBracket)
Creates a new parameterized string with custom open and closing brackets

Parameters:
string - the string with parameters
openBracket - the open bracket (before a parameter name)
closeBracket - the close bracket (after a paramter name)

ParameterizedString

public ParameterizedString(String string)
Creates a new parameterized string with parameter names enclosed within { and }

Parameters:
string - the string with parameters
Method Detail

set

public final void set(Map<String,?> parametersAndValues)
               throws IllegalArgumentException
Sets multiple parameter values

Parameters:
parametersAndValues - map of parameter names and thier corresponding values.
Throws:
IllegalArgumentException - if a parameter name in found in the given map does not exist

set

public final void set(String parameter,
                      Object value)
               throws IllegalArgumentException
Sets a parameter value

Parameters:
parameter - the parameter name
value - the parameter value
Throws:
IllegalArgumentException - if the parameter name does not exist

get

public final Object get(String parameter)
                 throws IllegalArgumentException
Returns the value of a given parameter.

Parameters:
parameter - the parameter name
Returns:
the parameter value
Throws:
IllegalArgumentException - if the parameter name does not exist

parse

public final Map<String,Object> parse(String input)

Parses the String input and extracts the parameter values storing them as regular parameters.

The Map of parameters is returned as a convenience, but parameter values can also be retrieved using:

Parameters:
input - the input String to be parsed
Returns:
the Map of parameters to their assigned values

toString

public final String toString()
Returns the original String provided in the constructor of this class, no parameters are replaced

Overrides:
toString in class Object
Returns:
the String with parameters

applyParameterValues

public final String applyParameterValues()
Applies the parameter values provided using set(String, Object) and returns the resulting String Unless a default value is provided via getDefaultValue(), parameters without values provided will not be replaced. Therefore the String "zero/{one}/{two}/{one}", with parameter "one" set to 27 will evaluate to "zero/27/{two}/27"

Returns:
the resulting String with all parameters replaced by their values.

applyParameterValues

public final void applyParameterValues(StringBuilder out)
Applies the parameter values provided using set(String, Object) and appends the resulting String to a given StringBuilder Unless a default value is provided via getDefaultValue(), parameters without values provided will not be replaced. Therefore the String "zero/{one}/{two}/{one}", with parameter "one" set to 27 will evaluate to "zero/27/{two}/27"

Parameters:
out - the StringBuilder that will be appended with the result of all parameters replaced by their values.

getParameters

public final Set<String> getParameters()
Returns a set of all parameter names found in the input string given in the constructor of this class.

Returns:
the unmodifiable set of available parameter names.

clearValues

public final void clearValues()
Clears the values of all parameters.


contains

public final boolean contains(String parameterName)
Tests whether a given parameter name exists in this parameterized string.

Parameters:
parameterName - the name of the parameter
Returns:
true if the parameter name exists in this parameterized string, otherwise false

getFormat

public final String getFormat(String parameterName)
                       throws IllegalArgumentException
Returns the format associated with a given parameter

Parameters:
parameterName - the name of the parameter
Returns:
the format of parameter as String. Returns null if the parameter does not exist or format was not set.
Throws:
IllegalArgumentException - if the parameter name does not exist

clone

public final ParameterizedString clone()
Clones this parameterzied string object. Current parameter values are copied as well.

Overrides:
clone in class Object
Returns:
a clone of this object.

getParameterValues

public final Map<String,Object> getParameterValues()
Returns the current parameter names and their values.

Returns:
an unmodifiable copy of the map of parameter names and their values.

setDefaultValue

public final void setDefaultValue(Object defaultValue)
Defines a default value to be used for parameters that have no value associated. Defaults to null, in which case the original parameter will appear in the result of applyParameterValues().

Parameters:
defaultValue - the default value to be used when one or more parameters have no value associated.

getDefaultValue

public final Object getDefaultValue()
Returns the default value to be used for parameters that have no value associated. Defaults to null, in which case the original parameter will appear in the result of applyParameterValues().

Returns:
the default value to be used when one or more parameters have no value associated.

getIndexBeforeFirstParameter

public final int getIndexBeforeFirstParameter()
Returns the index before the first parameter in this parameterized string.

Returns:
index before the first parameter, or -1 if no parameters exist.

getIndexAfterLastParameter

public final int getIndexAfterLastParameter()
Returns the index after the last parameter in this parameterized string.

Returns:
index after the last parameter, or -1 if no parameters exist.

getContentBeforeFirstParameter

public final String getContentBeforeFirstParameter()
Returns the content before the first parameter in this parameterized string.

Returns:
text content before the first parameter, or the entire String if no parameters exist.

getContentAfterLastParameter

public final String getContentAfterLastParameter()
Returns the content after the last parameter in this parameterized string.

Returns:
text content after the last parameter, or the entire String if no parameters exist.

equals

public final boolean equals(Object o)
Overrides:
equals in class Object

hashCode

public final int hashCode()
Overrides:
hashCode in class Object

getConvertDefaultValueToNull

public final boolean getConvertDefaultValueToNull()
Flag indicating that values parsed from this ParameterizedString will be converted to null, when equal to the String representation of the value returned by getDefaultValue().

Returns:
true if default values should be converted to null when reading parameter values, otherwise false

setConvertDefaultValueToNull

public final void setConvertDefaultValueToNull(boolean convertDefaultValueToNull)
Defines whether values parsed from this ParameterizedString should be converted to null, when equal to the String representation of the value returned by getDefaultValue().

Parameters:
convertDefaultValueToNull - flag indicating whether default values should be converted to null when reading parameter values


Copyright © 2018 uniVocity Software Pty Ltd. All rights reserved.