| 1 |
/* |
| 2 |
* @(#) ArrayConverter.java |
| 3 |
* |
| 4 |
* Copyright 2002 - 2003 JIDE Software. All rights reserved. |
| 5 |
*/ |
| 6 |
package com.jidesoft.converter; |
| 7 |
|
| 8 |
import java.util.StringTokenizer; |
| 9 |
|
| 10 |
/** |
| 11 |
* An abstract class that is extended by any converters that convert to/from an array-like format, such as 1, 2, 3. |
| 12 |
* Examples are Point. Point(100, 200) can convert to/from "100, 200" <br> You have the choice of what the separator is; |
| 13 |
* separator is the ", " in the Point example above. |
| 14 |
*/ |
| 15 |
abstract public class ArrayConverter implements ObjectConverter { |
| 16 |
|
| 17 |
private String _separator; |
| 18 |
|
| 19 |
private int _size; |
| 20 |
|
| 21 |
private Class<?> _elementClass; |
| 22 |
|
| 23 |
private Class<?>[] _elementClasses; |
| 24 |
|
| 25 |
private boolean _useTokenizer; |
| 26 |
|
| 27 |
/** |
| 28 |
* Creates an ArrayConverter. |
| 29 |
* |
| 30 |
* @param separator separator to separate values. It should contain at least non-empty character. |
| 31 |
* @param size size of the array |
| 32 |
* @param elementClass class of the array element. Assume all elements have the same class type. If not, use the |
| 33 |
* constructor which takes Class<?>[] as parameter. |
| 34 |
*/ |
| 35 |
public ArrayConverter(String separator, int size, Class<?> elementClass) { |
| 36 |
_separator = separator; |
| 37 |
_size = size; |
| 38 |
_elementClass = elementClass; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Creates an ArrayConverter. |
| 43 |
* |
| 44 |
* @param separator separator to separate values. It should contain at least non-empty character. |
| 45 |
* @param size size of the array |
| 46 |
* @param elementClasses classes of the array element. The length must be the same as size. If not, |
| 47 |
* IllegalArgumentException will be thrown. |
| 48 |
*/ |
| 49 |
public ArrayConverter(String separator, int size, Class<?>[] elementClasses) { |
| 50 |
if (separator == null || separator.trim().length() == 0) { |
| 51 |
throw new IllegalArgumentException("separator cannot be empty."); |
| 52 |
} |
| 53 |
if (elementClasses == null) { |
| 54 |
throw new IllegalArgumentException("elementClasses cannot be null."); |
| 55 |
} |
| 56 |
if (size != elementClasses.length) { |
| 57 |
throw new IllegalArgumentException("size must be equal to the length of elementClasses."); |
| 58 |
} |
| 59 |
_separator = separator; |
| 60 |
_size = size; |
| 61 |
_elementClasses = elementClasses; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Converts from array to string by concating them with separators. |
| 66 |
* |
| 67 |
* @param objects an array of objects |
| 68 |
* @param context converter context |
| 69 |
* @return string all objects concatenated with separators |
| 70 |
*/ |
| 71 |
public String arrayToString(Object[] objects, ConverterContext context) { |
| 72 |
StringBuffer buffer = new StringBuffer(); |
| 73 |
for (int i = 0; i < objects.length; i++) { |
| 74 |
Object o = objects[i]; |
| 75 |
buffer.append(toString(i, o, context)); |
| 76 |
if (i != objects.length - 1) { |
| 77 |
buffer.append(_separator); |
| 78 |
} |
| 79 |
} |
| 80 |
return new String(buffer); |
| 81 |
} |
| 82 |
|
| 83 |
protected String toString(int i, Object o, ConverterContext context) { |
| 84 |
return _elementClass != null ? ObjectConverterManager.toString(o, _elementClass, context) : ObjectConverterManager.toString(o, _elementClasses[i], context); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Converts from string to an array of objects, using separator to separate the string. |
| 89 |
* |
| 90 |
* @param string string to be converted |
| 91 |
* @param context converter context |
| 92 |
* @return the array |
| 93 |
*/ |
| 94 |
public Object[] arrayFromString(String string, ConverterContext context) { |
| 95 |
if (string == null || string.trim().length() == 0) { |
| 96 |
return null; |
| 97 |
} |
| 98 |
if (isUseTokenizer()) { |
| 99 |
StringTokenizer token = new StringTokenizer(string, _separator); |
| 100 |
Object[] objects = new Object[_size != -1 ? _size : token.countTokens()]; |
| 101 |
for (int i = 0; i < objects.length && token.hasMoreTokens(); i++) { |
| 102 |
String s = token.nextToken().trim(); |
| 103 |
objects[i] = fromString(i, s, context); |
| 104 |
} |
| 105 |
return objects; |
| 106 |
} |
| 107 |
String[] ss = string.split(_separator); |
| 108 |
Object[] objects = new Object[_size != -1 ? _size : ss.length]; |
| 109 |
for (int i = 0; i < objects.length && i < ss.length; i++) { |
| 110 |
String s = ss[i].trim(); |
| 111 |
objects[i] = fromString(i, s, context); |
| 112 |
} |
| 113 |
return objects; |
| 114 |
} |
| 115 |
|
| 116 |
protected Object fromString(int i, String s, ConverterContext context) { |
| 117 |
return _elementClass != null ? ObjectConverterManager.fromString(s, _elementClass, context) : ObjectConverterManager.fromString(s, _elementClasses[i], context); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Gets the element class for the array. |
| 122 |
* |
| 123 |
* @return the element class for the array. |
| 124 |
*/ |
| 125 |
public Class<?> getElementClass() { |
| 126 |
return _elementClass; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Gets the flag indicating if the converter will utilize Tokenizer to split the string or just utilize String#split(). |
| 131 |
* |
| 132 |
* @return true if tokenizer is to be used. Otherwise false. |
| 133 |
* @see #setUseTokenizer(boolean) |
| 134 |
* @since 3.5.0 |
| 135 |
*/ |
| 136 |
public boolean isUseTokenizer() { |
| 137 |
return _useTokenizer; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Sets the flag indicating if the converter will utilize Tokenizer to split the string or just utilize String#split(). |
| 142 |
* <p/> |
| 143 |
* By default, the flag is false to keep backward compatibility. |
| 144 |
* |
| 145 |
* @param useTokenizer the flag |
| 146 |
* @since 3.5.0 |
| 147 |
*/ |
| 148 |
public void setUseTokenizer(boolean useTokenizer) { |
| 149 |
_useTokenizer = useTokenizer; |
| 150 |
} |
| 151 |
} |