001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.migration.model22.other;
023:
024: /**
025: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
026: * @version $Revision: 8784 $
027: */
028: public class Helper {
029:
030: public static final int[] EMPTY_INT_ARRAY = new int[0];
031: public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];
032: public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
033: public static final String[] EMPTY_STRING_ARRAY = new String[0];
034:
035: public static final Converter FLOAT_CONVERTER = new Converter() {
036: public boolean accept(Class clazz) {
037: return Float.class.equals(clazz);
038: }
039:
040: public Object toObject(String value)
041: throws NullConversionException,
042: FormatConversionException {
043: if (value == null) {
044: return null;
045: }
046: try {
047: return Float.valueOf(value);
048: } catch (NumberFormatException e) {
049: throw new FormatConversionException();
050: }
051: }
052:
053: public String toString(Object value)
054: throws NullConversionException,
055: FormatConversionException {
056: if (value == null) {
057: return null;
058: }
059: if (!(value instanceof Float)) {
060: throw new FormatConversionException();
061: }
062: return value.toString();
063: }
064: };
065:
066: public static final Converter INTEGER_CONVERTER = new Converter() {
067: public boolean accept(Class clazz) {
068: return Integer.class.equals(clazz);
069: }
070:
071: public Object toObject(String value)
072: throws NullConversionException,
073: FormatConversionException {
074: if (value == null) {
075: return null;
076: }
077: try {
078: return Integer.valueOf(value);
079: } catch (NumberFormatException e) {
080: throw new FormatConversionException();
081: }
082: }
083:
084: public String toString(Object value)
085: throws NullConversionException,
086: FormatConversionException {
087: if (value == null) {
088: return null;
089: }
090: if (!(value instanceof Integer)) {
091: throw new FormatConversionException();
092: }
093: return value.toString();
094: }
095: };
096:
097: public static final Converter BOOLEAN_CONVERTER = new Converter() {
098: public boolean accept(Class clazz) {
099: return Boolean.class.equals(clazz);
100: }
101:
102: public Object toObject(String value)
103: throws NullConversionException,
104: FormatConversionException {
105: if (value == null) {
106: return null;
107: }
108: return Boolean.valueOf(Helper.toBoolean(value));
109: }
110:
111: public String toString(Object value)
112: throws NullConversionException,
113: FormatConversionException {
114: if (value == null) {
115: return null;
116: }
117: if (!(value instanceof Boolean)) {
118: throw new FormatConversionException();
119: }
120: return value.toString();
121: }
122: };
123:
124: public static int toInt(String value)
125: throws NullConversionException, FormatConversionException {
126: try {
127: if (value == null) {
128: throw new NullConversionException();
129: }
130: return Integer.parseInt(value);
131: } catch (NumberFormatException e) {
132: throw new FormatConversionException();
133: }
134: }
135:
136: public static boolean toBoolean(String value)
137: throws NullConversionException, FormatConversionException {
138: if ("true".equals(value)) {
139: return true;
140: } else if ("false".equals(value)) {
141: return false;
142: } else if (value == null) {
143: throw new NullConversionException();
144: } else {
145: throw new FormatConversionException();
146: }
147: }
148:
149: public static String toString(int value) {
150: return Integer.toString(value);
151: }
152:
153: public static String toString(boolean value) {
154: return Boolean.toString(value);
155: }
156:
157: public static String toString(Object value, Converter converter)
158: throws NullConversionException, FormatConversionException,
159: IllegalArgumentException {
160: if (converter == null) {
161: throw new IllegalArgumentException();
162: }
163: return converter.toString(value);
164: }
165:
166: public static String[] toStringArray(int[] values)
167: throws IllegalArgumentException {
168: if (values == null) {
169: throw new IllegalArgumentException();
170: }
171: String[] strings = new String[values.length];
172: for (int i = 0; i < strings.length; i++) {
173: strings[i] = Integer.toString(values[i]);
174: }
175: return strings;
176: }
177:
178: public static String[] toStringArray(boolean[] values)
179: throws IllegalArgumentException {
180: if (values == null) {
181: throw new IllegalArgumentException();
182: }
183: String[] strings = new String[values.length];
184: for (int i = 0; i < strings.length; i++) {
185: strings[i] = Boolean.toString(values[i]);
186: }
187: return strings;
188: }
189:
190: public static String[] toStringArray(Object[] values,
191: Converter converter) throws NullConversionException,
192: FormatConversionException, IllegalArgumentException {
193: if (values == null) {
194: throw new IllegalArgumentException();
195: }
196: if (converter == null) {
197: throw new IllegalArgumentException();
198: }
199: String[] strings = new String[values.length];
200: for (int i = 0; i < strings.length; i++) {
201: Object value = values[i];
202: if (value != null) {
203: strings[i] = converter.toString(values[i]);
204: }
205: }
206: return strings;
207: }
208: }
|