java.lang.Object | ||
↳ | org.apache.commons.beanutils.converters.AbstractConverter | |
↳ | org.apache.commons.beanutils.converters.ArrayConverter |
Generic Converter
implementaion that handles conversion
to and from array objects.
Can be configured to either return a default value or throw a
ConversionException
if a conversion error occurs.
The main features of this implementation are:
Converter
,
appropriate for the type, to convert individual elements
of the array. This leverages the power of existing converters
without having to replicate their functionality for converting
to the element type and removes the need to create a specifc
array type converters.Converter
.String
in one of two ways: as a delimited list
or by converting the first element in the array to a String - this
is controlled by the setOnlyFirstToString(boolean)
parameter.String
to a multi-dimensional arrays, by embedding ArrayConverter
within each other - see example below.ArrayConverter(Class, Converter)
constructor to create a converter which throws a
ConversionException
if the value is missing or
invalid.ArrayConverter(Class, Converter, int)
constructor to create a converter which returns a default
value. The defaultSize parameter controls the
default value in the following way:null
defaultSize
(N.B. elements
in the array will be null
)String
format
into an array of the appropriate type. By default, it uses a comma as the delimiter
but the following methods can be used to configure parsing:
setDelimiter(char)
- allows the character used as
the delimiter to be configured [default is a comma].setAllowedChars(char[])
- adds additional characters
(to the default alphabetic/numeric) to those considered to be
valid token characters.
String
to mulit-dimensional arrays by using
ArrayConverter
as the element Converter
within another ArrayConverter
.
For example, the following code demonstrates how to construct a Converter
to convert a delimited String
into a two dimensional integer array:
// Construct an Integer Converter IntegerConverter integerConverter = new IntegerConverter(); // Construct an array Converter for an integer array (i.e. int[]) using // an IntegerConverter as the element converter. // N.B. Uses the default comma (i.e. ",") as the delimiter between individual numbers ArrayConverter arrayConverter = new ArrayConverter(int[].class, integerConverter); // Construct a "Matrix" Converter which converts arrays of integer arrays using // the pre-ceeding ArrayConverter as the element Converter. // N.B. Uses a semi-colon (i.e. ";") as the delimiter to separate the different sets of numbers. // Also the delimiter used by the first ArrayConverter needs to be added to the // "allowed characters" for this one. ArrayConverter matrixConverter = new ArrayConverter(int[][].class, arrayConverter); matrixConverter.setDelimiter(';'); matrixConverter.setAllowedChars(new char[] {','}); // Do the Conversion String matrixString = "11,12,13 ; 21,22,23 ; 31,32,33 ; 41,42,43"; int[][] result = (int[][])matrixConverter.convert(int[][].class, matrixString);
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Construct an array
Converter with the specified
component Converter that throws a
ConversionException if an error occurs. | |||||||||||
Construct an array
Converter with the specified
component Converter that returns a default
array of the specified size (or null ) if an error occurs. |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Set the allowed characters to be used for parsing a delimited String.
| |||||||||||
Set the delimiter to be used for parsing a delimited String.
| |||||||||||
Indicates whether converting to a String should create
a delimited list or just convert the first value.
| |||||||||||
Provide a String representation of this array converter.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns the value unchanged.
| |||||||||||
Converts non-array values to a Collection prior
to being converted either to an array or a String.
| |||||||||||
Handles conversion to a String.
| |||||||||||
Handles conversion to an array of the specified type.
| |||||||||||
Return the default value for conversions to the specified
type.
| |||||||||||
Return the default type this
Converter handles. |
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
org.apache.commons.beanutils.converters.AbstractConverter
| |||||||||||
From class
java.lang.Object
| |||||||||||
From interface
org.apache.commons.beanutils.Converter
|
Construct an array Converter
with the specified
component Converter
that throws a
ConversionException
if an error occurs.
defaultType | The default array type this
Converter handles |
---|---|
elementConverter | Converter used to convert individual array elements. |
Construct an array Converter
with the specified
component Converter
that returns a default
array of the specified size (or null
) if an error occurs.
defaultType | The default array type this
Converter handles |
---|---|
elementConverter | Converter used to convert individual array elements. |
defaultSize | Specifies the size of the default array value or if less
than zero indicates that a null default value should be used.
|
Set the allowed characters to be used for parsing a delimited String.
allowedChars | Characters which are to be considered as part of the tokens when parsing a delimited String [default is '.' and '-'] |
---|
Set the delimiter to be used for parsing a delimited String.
delimiter | The delimiter [default ','] |
---|
Indicates whether converting to a String should create a delimited list or just convert the first value.
onlyFirstToString | true converts only
the first value in the array to a String, false
converts all values in the array into a delimited list (default
is true
|
---|
Provide a String representation of this array converter.
Returns the value unchanged.
value | The value to convert |
---|
Converts non-array values to a Collection prior to being converted either to an array or a String.
Collection
values are returned unchangedconvertToType(Class, Object)
and
convertToString(Object)
methods for
non-array types.type | The type to convert the value to |
---|---|
value | value to be converted |
Handles conversion to a String.
value | The value to be converted. |
---|
Throwable | if an error occurs converting to a String |
---|
Handles conversion to an array of the specified type.
type | The type to which this value should be converted. |
---|---|
value | The input value to be converted. |
Throwable | if an error occurs converting to the specified type |
---|
Return the default value for conversions to the specified type.
type | Data type to which this value should be converted. |
---|
Return the default type this Converter
handles.
Converter
handles.