Java Doc for ArrayConverter.java in  » Library » Apache-commons-beanutils-1.8.0-BETA-src » org » apache » commons » beanutils » converters » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache commons beanutils 1.8.0 BETA src » org.apache.commons.beanutils.converters 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   org.apache.commons.beanutils.converters.AbstractConverter
      org.apache.commons.beanutils.converters.ArrayConverter

ArrayConverter
public class ArrayConverter extends AbstractConverter (Code)
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:

  • Element Conversion - delegates to a 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.
  • Arrays or Collections - can convert from either arrays or Collections to an array, limited only by the capability of the delegate Converter .
  • Delimited Lists - can Convert to and from a delimited list in String format.
  • Conversion to String - converts an array to a 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 ArrayConverter.setOnlyFirstToString(boolean) parameter.
  • Multi Dimensional Arrays - its possible to convert a String to a multi-dimensional arrays, by embedding ArrayConverter within each other - see example below.
  • Default Value
    • No Default - use the ArrayConverter.ArrayConverter(ClassConverter) constructor to create a converter which throws a ConversionException if the value is missing or invalid.
    • Default values - use the ArrayConverter.ArrayConverter(ClassConverterint) constructor to create a converter which returns a default value. The defaultSize parameter controls the default value in the following way:
      • defaultSize < 0 - default is null
      • defaultSize = 0 - default is an array of length zero
      • defaultSize > 0 - default is an array with a length specified by defaultSize (N.B. elements in the array will be null)

Parsing Delimited Lists

This implementation can convert a delimited list in 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.

Multi Dimensional Arrays

It is possible to convert a 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);
 

version:
   $Revision: 555824 $ $Date: 2007-07-13 01:27:15 +0100 (Fri, 13 Jul 2007) $
since:
   1.8.0



Constructor Summary
public  ArrayConverter(Class defaultType, Converter elementConverter)
     Construct an array Converter with the specified component Converter that throws a ConversionException if an error occurs.
public  ArrayConverter(Class defaultType, Converter elementConverter, int defaultSize)
     Construct an array Converter with the specified component Converter that returns a default array of the specified size (or null) if an error occurs.

Method Summary
protected  ObjectconvertArray(Object value)
     Returns the value unchanged.
protected  CollectionconvertToCollection(Class type, Object value)
     Converts non-array values to a Collection prior to being converted either to an array or a String.
protected  StringconvertToString(Object value)
     Handles conversion to a String.
Parameters:
  value - The value to be converted.
protected  ObjectconvertToType(Class type, Object value)
     Handles conversion to an array of the specified type.
Parameters:
  type - The type to which this value should be converted.
Parameters:
  value - The input value to be converted.
protected  ObjectgetDefault(Class type)
     Return the default value for conversions to the specified type.
Parameters:
  type - Data type to which this value should be converted.
public  voidsetAllowedChars(char[] allowedChars)
     Set the allowed characters to be used for parsing a delimited String.
public  voidsetDelimiter(char delimiter)
     Set the delimiter to be used for parsing a delimited String.
public  voidsetOnlyFirstToString(boolean onlyFirstToString)
     Indicates whether converting to a String should create a delimited list or just convert the first value.
public  StringtoString()
     Provide a String representation of this array converter.


Constructor Detail
ArrayConverter
public ArrayConverter(Class defaultType, Converter elementConverter)(Code)
Construct an array Converter with the specified component Converter that throws a ConversionException if an error occurs.
Parameters:
  defaultType - The default array type thisConverter handles
Parameters:
  elementConverter - Converter used to convertindividual array elements.



ArrayConverter
public ArrayConverter(Class defaultType, Converter elementConverter, int defaultSize)(Code)
Construct an array Converter with the specified component Converter that returns a default array of the specified size (or null) if an error occurs.
Parameters:
  defaultType - The default array type thisConverter handles
Parameters:
  elementConverter - Converter used to convertindividual array elements.
Parameters:
  defaultSize - Specifies the size of the default array value or if lessthan zero indicates that a null default value should be used.




Method Detail
convertArray
protected Object convertArray(Object value)(Code)
Returns the value unchanged.
Parameters:
  value - The value to convert The value unchanged



convertToCollection
protected Collection convertToCollection(Class type, Object value)(Code)
Converts non-array values to a Collection prior to being converted either to an array or a String.

  • Collection values are returned unchanged
  • Number , Boolean and java.util.Date values returned as a the only element in a List.
  • All other types are converted to a String and parsed as a delimited list.
N.B. The method is called by both the ArrayConverter.convertToType(ClassObject) and ArrayConverter.convertToString(Object) methods for non-array types.
Parameters:
  type - The type to convert the value to
Parameters:
  value - value to be converted Collection elements.



convertToString
protected String convertToString(Object value) throws Throwable(Code)
Handles conversion to a String.
Parameters:
  value - The value to be converted. the converted String value.
throws:
  Throwable - if an error occurs converting to a String



convertToType
protected Object convertToType(Class type, Object value) throws Throwable(Code)
Handles conversion to an array of the specified type.
Parameters:
  type - The type to which this value should be converted.
Parameters:
  value - The input value to be converted. The converted value.
throws:
  Throwable - if an error occurs converting to the specified type



getDefault
protected Object getDefault(Class type)(Code)
Return the default value for conversions to the specified type.
Parameters:
  type - Data type to which this value should be converted. The default value for the specified type.



setAllowedChars
public void setAllowedChars(char[] allowedChars)(Code)
Set the allowed characters to be used for parsing a delimited String.
Parameters:
  allowedChars - Characters which are to be considered as part ofthe tokens when parsing a delimited String [default is '.' and '-']



setDelimiter
public void setDelimiter(char delimiter)(Code)
Set the delimiter to be used for parsing a delimited String.
Parameters:
  delimiter - The delimiter [default ',']



setOnlyFirstToString
public void setOnlyFirstToString(boolean onlyFirstToString)(Code)
Indicates whether converting to a String should create a delimited list or just convert the first value.
Parameters:
  onlyFirstToString - true converts onlythe first value in the array to a String, falseconverts all values in the array into a delimited list (defaultis true



toString
public String toString()(Code)
Provide a String representation of this array converter. A String representation of this array converter



Methods inherited from org.apache.commons.beanutils.converters.AbstractConverter
public Object convert(Class type, Object value)(Code)(Java Doc)
protected Object convertArray(Object value)(Code)(Java Doc)
protected String convertToString(Object value) throws Throwable(Code)(Java Doc)
abstract protected Object convertToType(Class type, Object value) throws Throwable(Code)(Java Doc)
protected Object getDefault(Class type)(Code)(Java Doc)
protected Class getDefaultType()(Code)(Java Doc)
protected Object handleError(Class type, Object value, Throwable cause)(Code)(Java Doc)
protected Object handleMissing(Class type)(Code)(Java Doc)
public boolean isUseDefault()(Code)(Java Doc)
Log log()(Code)(Java Doc)
Class primitive(Class type)(Code)(Java Doc)
protected void setDefaultValue(Object defaultValue)(Code)(Java Doc)
public String toString()(Code)(Java Doc)
String toString(Class type)(Code)(Java Doc)

Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.