View Javadoc
1 /* ------------------------------------------------------------------- 2 * Java source file for the class StringArray 3 * 4 * Copyright (c), 2002, Masahiro Takatsuka. 5 * All Rights Researved. 6 * 7 * Original Author: Masahiro Takatsuka (masa@jbeans.net) 8 * $Author: takatsukam $ 9 * 10 * $Date: 2003/07/25 04:51:47 $ 11 * 12 * $Id: StringArray.java,v 1.1.1.1 2003/07/25 04:51:47 takatsukam Exp $ 13 * 14 * Reference: Document no: 15 * ___ ___ 16 * 17 * To Do: 18 * ___ 19 * 20 ------------------------------------------------------------------- */ 21 22 /* --------------------------- Package ---------------------------- */ 23 package net.jbeans.util.text; 24 25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">color="#329900"> ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900"> ------------------ Import classes (packages) ------------------- */ 26 import java.util.*; 27 28 import net.jbeans.io.*; 29 30 /*==================================================================== 31 Implementation of class StringArray 32 ====================================================================*/ 33 /*** 34 * generally describe StringArray in here 35 * 36 * @version $Revision: 1.1.1.1 $ 37 * @author Masahiro Takatsuka (masa@jbeans.net) 38 */ 39 40 public final class StringArray { 41 42 private StringArray() { 43 super(); 44 } 45 46 /*** 47 * Parses the string into an array of strings 48 * <p> 49 * @param value String to parse 50 * @param delimiters Delimiters to use. If this is null or empty then 51 * then the set [ ' ', '\t', '\n', '\r', '\f' ] is used. 52 */ 53 public final static String[] toArray(String value, String delimiters) { 54 if (value == null) { 55 return null; 56 } 57 58 StringReader sr = new StringReader(value, delimiters); 59 ArrayList list = new ArrayList(); 60 while (sr.hasMoreTokens()) { 61 list.add(sr.nextToken()); 62 } 63 String[] result = new String[0]; 64 return (String[]) list.toArray(result); 65 } 66 67 public final static String[] toArray(String value) { 68 return toArray(value, null); 69 } 70 71 public final static String[] toArray(String value, String delimiters, String[] a) { 72 String[] result = toArray(value, delimiters); 73 if (result == null) { 74 return null; 75 } 76 int len = Math.min(a.length, result.length); 77 System.arraycopy(result, 0, a, 0, len); 78 return a; 79 } 80 81 public final static byte[] toByteArray(String value, String delimiters) { 82 String[] strs = toArray(value, delimiters); 83 if (strs == null) { 84 return null; 85 } 86 87 byte[] result = new byte[strs.length]; 88 89 for (int i = 0; i < strs.length; i++) { 90 result[i] = Byte.parseByte(strs[i]); 91 } 92 return result; 93 } 94 95 public final static byte[] toByteArray(String value, String delimiters, byte a[]) { 96 byte[] result = toByteArray(value, delimiters); 97 int len = Math.min(a.length, result.length); 98 System.arraycopy(result, 0, a, 0, len); 99 return a; 100 } 101 102 public final static short[] toShortArray(String value, String delimiters) { 103 String[] strs = toArray(value, delimiters); 104 if (strs == null) { 105 return null; 106 } 107 108 short[] result = new short[strs.length]; 109 110 for (int i = 0; i < strs.length; i++) { 111 result[i] = Short.parseShort(strs[i]); 112 } 113 return result; 114 } 115 116 public final static short[] toShortArray(String value, String delimiters, short a[]) { 117 short[] result = toShortArray(value, delimiters); 118 int len = Math.min(a.length, result.length); 119 System.arraycopy(result, 0, a, 0, len); 120 return a; 121 } 122 123 public final static int[] toIntArray(String value, String delimiters) { 124 String[] strs = toArray(value, delimiters); 125 if (strs == null) { 126 return null; 127 } 128 129 int[] result = new int[strs.length]; 130 131 for (int i = 0; i < strs.length; i++) { 132 result[i] = Integer.parseInt(strs[i]); 133 } 134 return result; 135 } 136 137 public final static synchronized int[] toIntArray(String value, String delimiters, int a[]) { 138 int[] result = toIntArray(value, delimiters); 139 int len = Math.min(a.length, result.length); 140 System.arraycopy(result, 0, a, 0, len); 141 return a; 142 } 143 144 public final static synchronized long[] toLongArray(String value, String delimiters) { 145 String[] strs = toArray(value, delimiters); 146 if (strs == null) { 147 return null; 148 } 149 150 long[] result = new long[strs.length]; 151 152 for (int i = 0; i < strs.length; i++) { 153 result[i] = Long.parseLong(strs[i]); 154 } 155 return result; 156 } 157 158 public final static synchronized long[] toLongArray(String value, String delimiters, long a[]) { 159 long[] result = toLongArray(value, delimiters); 160 int len = Math.min(a.length, result.length); 161 System.arraycopy(result, 0, a, 0, len); 162 return a; 163 } 164 165 public final static synchronized float[] toFloatArray(String value, String delimiters) { 166 String[] strs = toArray(value, delimiters); 167 if (strs == null) { 168 return null; 169 } 170 171 float[] result = new float[strs.length]; 172 173 for (int i = 0; i < strs.length; i++) { 174 result[i] = Float.parseFloat(strs[i]); 175 } 176 return result; 177 } 178 179 public final static synchronized float[] toFloatArray(String value, String delimiters, float a[]) { 180 float[] result = toFloatArray(value, delimiters); 181 int len = Math.min(a.length, result.length); 182 System.arraycopy(result, 0, a, 0, len); 183 return a; 184 } 185 186 public final static synchronized double[] toDoubleArray(String value, String delimiters) { 187 String[] strs = toArray(value, delimiters); 188 if (strs == null) { 189 return null; 190 } 191 192 double[] result = new double[strs.length]; 193 194 for (int i = 0; i < strs.length; i++) { 195 result[i] = Double.parseDouble(strs[i]); 196 } 197 return result; 198 } 199 200 public final static synchronized double[] toDoubleArray(String value, String delimiters, double a[]) { 201 double[] result = toDoubleArray(value, delimiters); 202 int len = Math.min(a.length, result.length); 203 System.arraycopy(result, 0, a, 0, len); 204 return a; 205 } 206 207 public final static synchronized boolean[] toBooleanArray(String value, String delimiters) { 208 String[] strs = toArray(value, delimiters); 209 if (strs == null) { 210 return null; 211 } 212 213 boolean[] result = new boolean[strs.length]; 214 215 for (int i = 0; i < strs.length; i++) { 216 result[i] = Boolean.valueOf(strs[i]).booleanValue(); 217 } 218 return result; 219 } 220 221 public final static synchronized boolean[] toBooleanArray(String value, String delimiters, boolean a[]) { 222 boolean[] result = toBooleanArray(value, delimiters); 223 int len = Math.min(a.length, result.length); 224 System.arraycopy(result, 0, a, 0, len); 225 return a; 226 } 227 228 /*** 229 * Creates a string from an array of strings. 230 * 231 * @param strings - Array to concatenate 232 * @param delimiter - Delimiter to use. If this is null or empty then 233 * then a space will be used 234 */ 235 public final static String toString(String[] strings, String delim) { 236 StringBuffer strbuf = new StringBuffer(""); 237 String separator; 238 239 if (delim == null || delim.equals("")) { 240 separator = " "; 241 } else { 242 separator = delim; 243 } 244 245 for (int i = 0; i < strings.length; i++) { 246 if (i > 0) { 247 strbuf.append(separator); 248 } 249 strbuf.append(strings[i]); 250 } 251 252 return strbuf.toString(); 253 } 254 255 public final static String toString(String[] strings) { 256 return toString(strings, ""); 257 } 258 }

This page was automatically generated by Maven