org.apache.velocity.tools.generic

## Class ConversionTool

- java.lang.Object
- - org.apache.velocity.tools.generic.SafeConfig
  - - org.apache.velocity.tools.generic.LocaleConfig
    - - org.apache.velocity.tools.generic.ConversionTool

All Implemented Interfaces:
  Serializable

Deprecated.
use NumberTool for numbers formatting/parsing, DateTool for date/time formatting/parsing, or CollectionTool for toStrings().

    @DefaultKey(value="convert")
     @SkipSetters
     @Deprecated
    public class ConversionTool
    extends LocaleConfig
    implements Serializable

Utility class for easy conversion of String values to richer types.

Template example(s):

       $convert.toNumber('12.6')   ->  12.6
       $convert.toInt('12.6')      ->  12
       $convert.toNumbers('12.6,42')  ->  [12.6, 42]

     Toolbox configuration:
     <tools>
       <toolbox scope="application">
         <tool class="org.apache.velocity.tools.generic.ConversionTool"
                  dateFormat="yyyy-MM-dd"/>
       </toolbox>
     </tools>

This comes in very handy when parsing anything.

Since:
  VelocityTools 2.0
Version:
  $Revision$ $Date: 2007-02-26 11:24:39 -0800 (Mon, 26 Feb 2007) $
Author:
  Nathan Bubna
See Also:
  Serialized Form

### Field Summary

Fields
`static String` `DATE_FORMAT_KEY` Deprecated.
`static String` `DEFAULT_DATE_FORMAT` Deprecated.
`static String` `DEFAULT_NUMBER_FORMAT` Deprecated.
`static String` `DEFAULT_STRINGS_DELIMITER` Deprecated.
`static boolean` `DEFAULT_STRINGS_TRIM` Deprecated.
`static String` `NUMBER_FORMAT_KEY` Deprecated.
`static String` `STRINGS_DELIMITER_FORMAT_KEY` Deprecated.

### Fields inherited from class org.apache.velocity.tools.generic.LocaleConfig

`DEFAULT_LOCALE`

### Fields inherited from class org.apache.velocity.tools.generic.SafeConfig

`LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY`

### Constructor Summary

Constructors
Constructor and Description
`ConversionTool()` Deprecated.

### Method Summary

All Methods Instance Methods Concrete Methods Deprecated Methods
`protected void` `configure(ValueParser values)` Deprecated. Does the actual configuration.
`String` `getDateFormat()` Deprecated. use `DateTool` getDateFormat() methods
`String` `getNumberFormat()` Deprecated. use `NumberTool` format
`String` `getStringsDelimiter()` Deprecated. Gets the delimiter used for separating values in a single String value.
`boolean` `getStringsTrim()` Deprecated. use `CollectionTool.getStringsTrim()`
`protected Boolean` `parseBoolean(String value)` Deprecated. Converts a parameter value into a `Boolean` Sub-classes can override to allow for customized boolean parsing.
`Date` `parseDate(String value)` Deprecated. use `DateTool.toDate(Object)`
`Date` `parseDate(String value, Object locale)` Deprecated. use `DateTool.toDate(String, Object, Locale)`}
`Date` `parseDate(String value, String format)` Deprecated. use `DateTool.toDate(String, Object)`
`Date` `parseDate(String value, String format, Object locale)` Deprecated. use `DateTool.toDate(String, Object, Locale)`}
`Date` `parseDate(String value, String format, Object locale, TimeZone timezone)` Deprecated. use `DateTool.toDate(String, Object, Locale, TimeZone)`}
`protected Locale` `parseLocale(String value)` Deprecated. Converts a String value into a Locale.
`Number` `parseNumber(String value)` Deprecated. use `NumberTool.toNumber(Object)`
`Number` `parseNumber(String value, Object locale)` Deprecated. use `NumberTool.toNumber(String, Object, Locale)`
`Number` `parseNumber(String value, String format)` Deprecated. use `NumberTool.toNumber(String, Object)`
`Number` `parseNumber(String value, String format, Object locale)` Deprecated. use `NumberTool.toNumber(String, Object, Locale)`
`protected String[]` `parseStringList(String value)` Deprecated. Converts a single String value into an array of Strings by splitting it on the tool's set stringsDelimiter.
`protected void` `setDateFormat(String format)` Deprecated. use `DateTool` setDateFormat() methods
`protected void` `setNumberFormat(String format)` Deprecated. use `NumberTool` format
`protected void` `setStringsDelimiter(String stringsDelimiter)` Deprecated. use `CollectionTool.setStringsDelimiter(String)`
`protected void` `setStringsTrim(boolean stringsTrim)` Deprecated. use `CollectionTool.setStringsTrim(boolean)`
`Boolean` `toBoolean(Object value)` Deprecated.
`Boolean[]` `toBooleans(Collection values)` Deprecated.
`Boolean[]` `toBooleans(Object value)` Deprecated.
`Calendar` `toCalendar(Object value)` Deprecated. use `DateTool.toCalendar(Object)`
`Calendar[]` `toCalendars(Collection values)` Deprecated.
`Calendar[]` `toCalendars(Object value)` Deprecated.
`Date` `toDate(Object value)` Deprecated. use `DateTool.toDate(Object)`
`Date[]` `toDates(Collection values)` Deprecated.
`Date[]` `toDates(Object value)` Deprecated.
`Double` `toDouble(Object value)` Deprecated.
`double[]` `toDoubles(Object value)` Deprecated.
`Integer` `toInteger(Object value)` Deprecated.
`int[]` `toIntegers(Object value)` Deprecated.
`int[]` `toInts(Object value)` Deprecated.
`List` `toList(Object value)` Deprecated. Convert a singleton or an array as List
`Locale` `toLocale(Object value)` Deprecated. use `DateTool`.toLocale(Object)
`Locale[]` `toLocales(Collection values)` Deprecated.
`Locale[]` `toLocales(Object value)` Deprecated.
`Number` `toNumber(Object value)` Deprecated.
`Number[]` `toNumbers(Collection values)` Deprecated.
`Number[]` `toNumbers(Object value)` Deprecated.
`String` `toString(Object value)` Deprecated. Converts objects to String in a more Tools-ish way than String.valueOf(Object), especially with nulls, Arrays and Collections.
`String[]` `toStrings(Object value)` Deprecated. use `CollectionTool.split(String)`

### Methods inherited from class org.apache.velocity.tools.generic.LocaleConfig

`getLocale, setLocale`

### Methods inherited from class org.apache.velocity.tools.generic.SafeConfig

`configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### STRINGS_DELIMITER_FORMAT_KEY

    public static final String STRINGS_DELIMITER_FORMAT_KEY

Deprecated.

See Also:
  Constant Field Values

#### DATE_FORMAT_KEY

    public static final String DATE_FORMAT_KEY

Deprecated.

See Also:
  Constant Field Values

#### NUMBER_FORMAT_KEY

    public static final String NUMBER_FORMAT_KEY

Deprecated.

See Also:
  Constant Field Values

#### DEFAULT_STRINGS_DELIMITER

    public static final String DEFAULT_STRINGS_DELIMITER

Deprecated.

See Also:
  Constant Field Values

#### DEFAULT_STRINGS_TRIM

    public static final boolean DEFAULT_STRINGS_TRIM

Deprecated.

See Also:
  Constant Field Values

#### DEFAULT_NUMBER_FORMAT

    public static final String DEFAULT_NUMBER_FORMAT

Deprecated.

See Also:
  Constant Field Values

#### DEFAULT_DATE_FORMAT

    public static final String DEFAULT_DATE_FORMAT

Deprecated.

See Also:
  Constant Field Values

### Constructor Detail

#### ConversionTool

    public ConversionTool()

Deprecated.

### Method Detail

#### configure

    protected void configure(ValueParser values)

Deprecated.
Does the actual configuration. This is protected, so subclasses may share the same ValueParser and call configure at any time, while preventing templates from doing so when configure(Map) is locked.

Overrides:
  `configure` in class `LocaleConfig`
Parameters:
  `values` - configuration values

#### setStringsDelimiter

    protected final void setStringsDelimiter(String stringsDelimiter)

Deprecated. use `CollectionTool.setStringsDelimiter(String)`
Sets the delimiter used for separating values in a single String value. The default string delimiter is a comma.

Parameters:
  `stringsDelimiter` - strings delimiter to use
See Also:
  `parseStringList(java.lang.String)`

#### getStringsDelimiter

    public final String getStringsDelimiter()

Deprecated.
Gets the delimiter used for separating values in a single String value.

Returns:
  string delimiter

#### setStringsTrim

    protected final void setStringsTrim(boolean stringsTrim)

Deprecated. use `CollectionTool.setStringsTrim(boolean)`
Sets whether strings should be trimmed when separated from a delimited string value. The default is true.

Parameters:
  `stringsTrim` - flag value
See Also:
  `parseStringList(java.lang.String)`

#### getStringsTrim

    public final boolean getStringsTrim()

Deprecated. use `CollectionTool.getStringsTrim()`

Returns:
  strings trim flag

#### setNumberFormat

    protected final void setNumberFormat(String format)

Deprecated. use `NumberTool` format
Sets numbers format

Parameters:
  `format` - numbers format

#### getNumberFormat

    public final String getNumberFormat()

Deprecated. use `NumberTool` format

Returns:
  number format

#### setDateFormat

    protected final void setDateFormat(String format)

Deprecated. use `DateTool` setDateFormat() methods
Sets date format

Parameters:
  `format` - date format

#### getDateFormat

    public final String getDateFormat()

Deprecated. use `DateTool` getDateFormat() methods

Returns:
  date format

#### toString

    public String toString(Object value)

Deprecated.
Converts objects to String in a more Tools-ish way than String.valueOf(Object), especially with nulls, Arrays and Collections. Null returns null, Arrays and Collections return the toString(Object) of their first value, or null if they have no values.

Parameters:
  `value` - the object to be turned into a String
Returns:
  the string value of the object or null if the value is null or it is an array or collection whose first value is null

#### toBoolean

    public Boolean toBoolean(Object value)

Deprecated.

Parameters:
  `value` - the object to be converted
Returns:
  a `Boolean` object for the specified value or `null` if the value is null or the conversion failed

#### toInteger

    public Integer toInteger(Object value)

Deprecated.

Parameters:
  `value` - the object to be converted
Returns:
  a `Integer` for the specified value or `null` if the value is null or the conversion failed

#### toDouble

    public Double toDouble(Object value)

Deprecated.

Parameters:
  `value` - the object to be converted
Returns:
  a `Double` for the specified value or `null` if the value is null or the conversion failed

#### toNumber

    public Number toNumber(Object value)

Deprecated.

Parameters:
  `value` - the object to be converted
Returns:
  a `Number` for the specified value or `null` if the value is null or the conversion failed

#### toLocale

    public Locale toLocale(Object value)

Deprecated. use `DateTool`.toLocale(Object)

Overrides:
  `toLocale` in class `LocaleConfig`
Parameters:
  `value` - the object to be converted
Returns:
  a `Locale` for the specified value or `null` if the value is null or the conversion failed

#### toDate

    public Date toDate(Object value)

Deprecated. use `DateTool.toDate(Object)`
Converts an object to an instance of `Date`, when necessary using the configured date parsing format, the configured default `Locale`, and the system's default `TimeZone` to parse the string value of the specified object.

Parameters:
  `value` - the date to convert
Returns:
  the object as a `Date` or `null` if no conversion is possible

#### toCalendar

    public Calendar toCalendar(Object value)

Deprecated. use `DateTool.toCalendar(Object)`

Parameters:
  `value` - target object
Returns:
  calendar

#### toStrings

    public String[] toStrings(Object value)

Deprecated. use `CollectionTool.split(String)`

Parameters:
  `value` - the value to be converted
Returns:
  an array of String objects containing all of the values derived from the specified array, Collection, or delimited String

#### toBooleans

    public Boolean[] toBooleans(Object value)

Deprecated.

Parameters:
  `value` - the value to be converted
Returns:
  an array of Boolean objects derived from the specified value, or `null`.

#### toBooleans

    public Boolean[] toBooleans(Collection values)

Deprecated.

Parameters:
  `values` - the collection of values to be converted
Returns:
  an array of Boolean objects derived from the specified values, or `null`.

#### toNumbers

    public Number[] toNumbers(Object value)

Deprecated.

Parameters:
  `value` - the value to be converted
Returns:
  an array of Number objects derived from the specified value, or `null`.

#### toNumbers

    public Number[] toNumbers(Collection values)

Deprecated.

Parameters:
  `values` - the collection of values to be converted
Returns:
  an array of Number objects derived from the specified values, or `null`.

#### toInts

    public int[] toInts(Object value)

Deprecated.

Parameters:
  `value` - the value to be converted
Returns:
  an array of int values derived from the specified value, or `null`.

#### toIntegers

    public int[] toIntegers(Object value)

Deprecated.

Parameters:
  `value` - the value to be converted
Returns:
  an array of int values derived from the specified value, or `null`.

#### toDoubles

    public double[] toDoubles(Object value)

Deprecated.

Parameters:
  `value` - the value to be converted
Returns:
  an array of double values derived from the specified value, or `null`.

#### toLocales

    public Locale[] toLocales(Object value)

Deprecated.

Parameters:
  `value` - the value to be converted
Returns:
  an array of Locale objects derived from the specified value, or `null`.

#### toLocales

    public Locale[] toLocales(Collection values)

Deprecated.

Parameters:
  `values` - the collection of values to be converted
Returns:
  an array of Locale objects derived from the specified values, or `null`.

#### toDates

    public Date[] toDates(Object value)

Deprecated.

Parameters:
  `value` - the value to be converted
Returns:
  an array of Date objects derived from the specified value, or `null`.

#### toDates

    public Date[] toDates(Collection values)

Deprecated.

Parameters:
  `values` - the collection of values to be converted
Returns:
  an array of Date objects derived from the specified values, or `null`.

#### toCalendars

    public Calendar[] toCalendars(Object value)

Deprecated.

Parameters:
  `value` - the value to be converted
Returns:
  an array of Calendar objects derived from the specified value, or `null`.

#### toCalendars

    public Calendar[] toCalendars(Collection values)

Deprecated.

Parameters:
  `values` - the collection of values to be converted
Returns:
  an array of Calendar objects derived from the specified values, or `null`.

#### toList

    public List toList(Object value)

Deprecated.
Convert a singleton or an array as List

Parameters:
  `value` - Object to be converted
Returns:
  either the object itself if it's already a list, the object converted to a list if it's an array, or a new singleton list.

#### parseBoolean

    protected Boolean parseBoolean(String value)

Deprecated.
Converts a parameter value into a `Boolean` Sub-classes can override to allow for customized boolean parsing. (e.g. to handle "Yes/No" or "T/F")

Parameters:
  `value` - the string to be parsed
Returns:
  the value as a `Boolean`

#### parseStringList

    protected String[] parseStringList(String value)

Deprecated.
Converts a single String value into an array of Strings by splitting it on the tool's set stringsDelimiter. The default stringsDelimiter is a comma, and by default, all strings parsed out are trimmed before returning.

Parameters:
  `value` - string list to parse
Returns:
  array of strings

#### parseLocale

    protected Locale parseLocale(String value)

Deprecated.
Converts a String value into a Locale.

Parameters:
  `value` - string of locale to parse
Returns:
  parsed locale, or null

#### parseNumber

    public Number parseNumber(String value)

Deprecated. use `NumberTool.toNumber(Object)`
Converts an object to an instance of `Number` using the format returned by `getNumberFormat()` and the default `Locale` if the object is not already an instance of Number.

Parameters:
  `value` - the string to parse
Returns:
  the string as a `Number` or `null` if no conversion is possible

#### parseNumber

    public Number parseNumber(String value,
                              String format)

Deprecated. use `NumberTool.toNumber(String, Object)`
Converts an object to an instance of `Number` using the specified format and the `Locale` returned by `LocaleConfig.getLocale()`.

Parameters:
  `value` - - the string to parse
  `format` - - the format the number is in
Returns:
  the string as a `Number` or `null` if no conversion is possible
See Also:
  `parseNumber(String value, String format, Object locale)`

#### parseNumber

    public Number parseNumber(String value,
                              Object locale)

Deprecated. use `NumberTool.toNumber(String, Object, Locale)`
Converts an object to an instance of `Number` using the configured number format and the specified `Locale`.

Parameters:
  `value` - - the string to parse
  `locale` - - the Locale to use
Returns:
  the string as a `Number` or `null` if no conversion is possible
See Also:
  `NumberFormat.parse(java.lang.String, java.text.ParsePosition)`

#### parseNumber

    public Number parseNumber(String value,
                              String format,
                              Object locale)

Deprecated. use `NumberTool.toNumber(String, Object, Locale)`
Converts an object to an instance of `Number` using the specified format and `Locale`.

Parameters:
  `value` - - the string to parse
  `format` - - the format the number is in
  `locale` - - the Locale to use
Returns:
  the string as a `Number` or `null` if no conversion is possible
See Also:
  `NumberFormat.parse(java.lang.String, java.text.ParsePosition)`

#### parseDate

    public Date parseDate(String value)

Deprecated. use `DateTool.toDate(Object)`
Converts a string to an instance of `Date`, using the configured date parsing format, the configured default `Locale`, and the system's default `TimeZone` to parse it.

Parameters:
  `value` - the date to convert
Returns:
  the object as a `Date` or `null` if no conversion is possible

#### parseDate

    public Date parseDate(String value,
                          String format)

Deprecated. use `DateTool.toDate(String, Object)`
Converts a string to an instance of `Date` using the specified format,the configured default `Locale`, and the system's default `TimeZone` to parse it.

Parameters:
  `value` - - the date to convert
  `format` - - the format the date is in
Returns:
  the string as a `Date` or `null` if no conversion is possible
See Also:
  `ConversionUtils.toDate(String str, String format, Locale locale, TimeZone timezone)`

#### parseDate

    public Date parseDate(String value,
                          Object locale)

Deprecated. use `DateTool.toDate(String, Object, Locale)`}
Converts a string to an instance of `Date` using the configured date format and specified `Locale` to parse it.

Parameters:
  `value` - - the date to convert
  `locale` - - the Locale to use
Returns:
  the string as a `Date` or `null` if no conversion is possible
See Also:
  `SimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)`

#### parseDate

    public Date parseDate(String value,
                          String format,
                          Object locale)

Deprecated. use `DateTool.toDate(String, Object, Locale)`}
Converts a string to an instance of `Date` using the specified format and `Locale` to parse it.

Parameters:
  `value` - - the date to convert
  `format` - - the format the date is in
  `locale` - - the Locale to use
Returns:
  the string as a `Date` or `null` if no conversion is possible
See Also:
  `SimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)`

#### parseDate

    public Date parseDate(String value,
                          String format,
                          Object locale,
                          TimeZone timezone)

Deprecated. use `DateTool.toDate(String, Object, Locale, TimeZone)`}
Converts a string to an instance of `Date` using the specified format, `Locale`, and `TimeZone`.

Parameters:
  `value` - - the date to convert
  `format` - - the format the date is in
  `locale` - - the Locale to use
  `timezone` - - the `TimeZone`
Returns:
  the string as a `Date` or `null` if no conversion is possible
See Also:
  `getDateFormat()`, `SimpleDateFormat.parse(java.lang.String, java.text.ParsePosition)`