1 9 package org.jboss.portal.common.value; 10 11 12 16 public class Helper 17 { 18 19 public static final int[] EMPTY_INT_ARRAY = new int[0]; 20 public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0]; 21 public static final Object [] EMPTY_OBJECT_ARRAY = new Object [0]; 22 public static final String [] EMPTY_STRING_ARRAY = new String [0]; 23 24 public static final Converter INTEGER_CONVERTER = new Converter() 25 { 26 public boolean accept(Class clazz) 27 { 28 return Integer .class.equals(clazz); 29 } 30 public Object toObject(String value) throws NullConversionException, FormatConversionException 31 { 32 if (value == null) 33 { 34 return null; 35 } 36 try 37 { 38 return Integer.decode(value); 39 } 40 catch (NumberFormatException e) 41 { 42 throw new FormatConversionException(); 43 } 44 } 45 public String toString(Object value) throws NullConversionException, FormatConversionException 46 { 47 if (value == null) 48 { 49 return null; 50 } 51 if (!(value instanceof Integer )) 52 { 53 throw new FormatConversionException(); 54 } 55 return value.toString(); 56 } 57 }; 58 59 public static final Converter BOOLEAN_CONVERTER = new Converter() 60 { 61 public boolean accept(Class clazz) 62 { 63 return Boolean .class.equals(clazz); 64 } 65 public Object toObject(String value) throws NullConversionException, FormatConversionException 66 { 67 if (value == null) 68 { 69 return null; 70 } 71 return Boolean.valueOf(Helper.toBoolean(value)); 72 } 73 public String toString(Object value) throws NullConversionException, FormatConversionException 74 { 75 if (value == null) 76 { 77 return null; 78 } 79 if (!(value instanceof Boolean )) 80 { 81 throw new FormatConversionException(); 82 } 83 return value.toString(); 84 } 85 }; 86 87 public static int toInt(String value) throws NullConversionException, FormatConversionException 88 { 89 try 90 { 91 if (value == null) 92 { 93 throw new NullConversionException(); 94 } 95 return Integer.parseInt(value); 96 } 97 catch (NumberFormatException e) 98 { 99 throw new FormatConversionException(); 100 } 101 } 102 103 public static boolean toBoolean(String value) throws NullConversionException, FormatConversionException 104 { 105 if ("true".equals(value)) 106 { 107 return true; 108 } 109 else if ("false".equals(value)) 110 { 111 return false; 112 } 113 else if (value == null) 114 { 115 throw new NullConversionException(); 116 } 117 else 118 { 119 throw new FormatConversionException(); 120 } 121 } 122 123 public static String toString(int value) 124 { 125 return Integer.toString(value); 126 } 127 128 public static String toString(boolean value) 129 { 130 return Boolean.toString(value); 131 } 132 133 public static String toString(Object value, Converter converter) throws NullConversionException, FormatConversionException, IllegalArgumentException 134 { 135 if (converter == null) 136 { 137 throw new IllegalArgumentException (); 138 } 139 return converter.toString(value); 140 } 141 142 public static String [] toStringArray(int[] values) throws IllegalArgumentException 143 { 144 if (values == null) 145 { 146 throw new IllegalArgumentException (); 147 } 148 String [] strings = new String [values.length]; 149 for (int i = 0;i < strings.length;i++) 150 { 151 strings[i] = Integer.toString(values[i]); 152 } 153 return strings; 154 } 155 156 public static String [] toStringArray(boolean[] values) throws IllegalArgumentException 157 { 158 if (values == null) 159 { 160 throw new IllegalArgumentException (); 161 } 162 String [] strings = new String [values.length]; 163 for (int i = 0;i < strings.length;i++) 164 { 165 strings[i] = Boolean.toString(values[i]); 166 } 167 return strings; 168 } 169 170 public static String [] toStringArray(Object [] values, Converter converter) throws NullConversionException, FormatConversionException, IllegalArgumentException 171 { 172 if (values == null) 173 { 174 throw new IllegalArgumentException (); 175 } 176 if (converter == null) 177 { 178 throw new IllegalArgumentException (); 179 } 180 String [] strings = new String [values.length]; 181 for (int i = 0;i < strings.length;i++) 182 { 183 Object value = values[i]; 184 if (value != null) 185 { 186 strings[i] = converter.toString(values[i]); 187 } 188 } 189 return strings; 190 } 191 } 192 | Popular Tags |