View Javadoc

1   package net.sourceforge.argval.collection;
2   
3   
4   import java.util.ArrayList;
5   import java.util.Collection;
6   import java.util.Iterator;
7   import java.util.List;
8   import java.util.Map;
9   import java.util.Set;
10  import java.util.StringTokenizer;
11  import java.util.TreeSet;
12  
13  
14  public class CollectionUtil {
15      private static CollectionUtilConfig defaultConfig = new CollectionUtilConfig();
16      
17      
18      /**
19       * Puts all the instance of the collection in a String array. Calls on each instance
20       * of the given Set the <code>toString()</code>.
21       * 
22       * @param  set  The input collection.
23       * @return  The array in which each instance of the Set is represented as a String.
24       */
25      public static String[] toStringArray(Set<?> set) {
26          if (set == null || set.size() == 0) {
27              return new String[0];
28          }
29          
30          String[] strArray = new String[set.size()];
31          int index = 0;
32          for (Iterator<?> iter = set.iterator(); iter.hasNext();) {
33              strArray[index++] = iter.next().toString();
34          }
35          return strArray;
36      }
37  
38  
39      // TODO Test + Doc + split into interface + impl?
40      // 
41      public static String[] toArray(List<?> list)
42      {
43  //        if (list == null || list.size() == 0) {
44  //            return new String[0];
45  //        }
46          
47          String[] strArray = new String[list.size()];
48          for (int index = 0; index < strArray.length; index++)
49          {
50              strArray[index] = list.get(index).toString();
51          }
52          return strArray;
53      }
54  
55      
56      public static String toString(Object[] nameArray, Object[] valueArray) {
57          return toString(nameArray, valueArray, defaultConfig);
58      }
59      public static String toString(Object[] nameArray, Object[] valueArray, CollectionUtilConfig config) {
60          return toString(new StringBuffer(), nameArray, valueArray, config);
61      }
62      public static String toString(StringBuffer strBuf, Object[] nameArray, Object[] valueArray, CollectionUtilConfig config) {
63          if (valueArray == null || valueArray.length == 0) {
64              return "";
65          }
66          if (nameArray == null || nameArray.length == 0) {
67              return toString(strBuf, valueArray, config);
68          }
69          if (nameArray.length != valueArray.length) {
70              //log.warn();
71              return toString(strBuf, valueArray, config);
72          }
73          
74          strBuf.append(config.getArrayPrefix());
75          boolean isFirst = true;
76          for (int index = 0; index < valueArray.length; index++) {
77              if (! isFirst) {
78                  strBuf.append(config.getArrayValueSeparator());
79              }
80              strBuf.append(config.getArrayKeyPrefix()).append(nameArray[index]).append(config.getArrayKeyPostfix());
81              strBuf.append(config.getKeyValueSeparator()).append(config.getArrayValuePrefix());
82              strBuf.append(valueArray[index]).append(config.getArrayValuePostfix());
83              //strBuf.append(nameArray[index]).append(config.getArrayValuePostfix());
84              isFirst = false;
85          }
86          strBuf.append(config.getArrayPostfix());
87          
88          return strBuf.toString();
89      }
90      
91      public static String toString(Object[] input) {
92          return toString(input, defaultConfig);
93      }
94      public static String toString(Object[] input, CollectionUtilConfig config) {
95          return toString(new StringBuffer(), input, config);
96      }
97      public static String toString(StringBuffer strBuf, Object[] input, CollectionUtilConfig config) {
98          if (input == null || input.length == 0) {
99              return null;
100         }
101         
102         strBuf.append(config.getArrayPrefix());
103         boolean isFirst = true;
104         for (int index = 0; index < input.length; index++) {
105             if (! isFirst) {
106                 strBuf.append(config.getArrayValueSeparator());
107             }
108             if (input[index] != null) {
109                 strBuf.append(config.getArrayValuePrefix());
110                 strBuf.append(input[index]);
111                 strBuf.append(config.getArrayValuePostfix());
112             }
113             else {
114                 strBuf.append(config.getNullValue());
115             }
116             isFirst = false;
117         }
118         strBuf.append(config.getArrayPostfix());
119         
120         return strBuf.toString();
121     }
122 
123     
124     public static Object[] toArray(Set<?> set) {
125         // FIXME [20071210 tv]: Remove circular dependency.
126 //        ArgumentValidation argVal = new ArgumentValidationImpl();
127 //        argVal.isValidWhenNotNull("set", set);
128 //        if (argVal.containsIllegalArgument()) throw argVal.createIllegalArgumentException();
129         
130         Object[] objArray = new Object[set.size()];
131         int index = 0;
132         for (Iterator<?> iter = set.iterator(); iter.hasNext();) {
133             objArray[index++] = iter.next();
134         }
135         return objArray;
136     }
137     
138 
139     public static String toString(String name, String[] nameArray, Object[] valueArray) {
140         return toString(new StringBuffer(), name, nameArray, valueArray).toString();
141     }
142     public static StringBuffer toString(StringBuffer strBuf, String name, String[] nameArray, Object[] valueArray) {
143     	return toString(strBuf, name, nameArray, valueArray, defaultConfig);
144     }
145     public static StringBuffer toString(StringBuffer strBuf, String name, String[] nameArray, Object[] valueArray, CollectionUtilConfig config) {
146         strBuf.append(name);
147         toString(strBuf, nameArray, valueArray, config);
148         return strBuf;
149     }
150 
151 
152     public static String toString(Map<?, ?> patternMap) {
153         return toString(patternMap, defaultConfig);
154     }
155     public static String toString(Map<?, ?> patternMap, CollectionUtilConfig config) {
156         return toString(new StringBuffer(), patternMap, config).toString();
157     }
158     public static StringBuffer toString(StringBuffer strBuf, Map<?, ?> patternMap, CollectionUtilConfig config) {
159         if (patternMap == null || patternMap.size() == 0) {
160             return null;
161         }
162         
163         strBuf.append(config.getArrayPrefix());
164         boolean isFirst = true;
165         for (Iterator<Object> iter = new TreeSet<Object>(patternMap.keySet()).iterator(); iter.hasNext();) {
166 			Object key = iter.next();
167             if (! isFirst) {
168                 strBuf.append(config.getArrayValueSeparator());
169             }
170             strBuf.append(config.getArrayKeyPrefix());
171             strBuf.append(key.toString());
172             strBuf.append(config.getArrayKeyPostfix());
173             strBuf.append(config.getKeyValueSeparator());
174             if (patternMap.get(key) != null) {
175                 strBuf.append(config.getArrayValuePrefix());
176                 strBuf.append(patternMap.get(key));
177                 strBuf.append(config.getArrayValuePostfix());
178             }
179             else {
180                 strBuf.append(config.getNullValue());
181             }
182             isFirst = false;
183         }
184         strBuf.append(config.getArrayPostfix());
185         
186         return strBuf;
187     }
188 
189     
190     public static String toString(byte[] byteArray) {
191         String[] byteStrArray = new String[byteArray.length];
192         for (int index = 0; index < byteArray.length; index++) {
193             int xx = new Byte(byteArray[index]).intValue();
194             xx = (xx < 0) ? xx + 256: xx;
195             byteStrArray[index] = String.valueOf(xx);
196         }
197         return toString(byteStrArray);
198     }
199 
200     
201     public static List<String> stringToList(String multipleValues, String separator) {
202         return (List<String>)stringToCollection(new ArrayList<String>(), multipleValues, separator);
203     }
204     public static Set<String> stringToSet(String multipleValues, String separator) {
205         return (Set<String>)stringToCollection(new TreeSet<String>(), multipleValues, separator);
206     }
207     public static Collection<String> stringToCollection(Collection<String> coll, String multipleValues, String separator) {
208         if (multipleValues != null) {
209             StringTokenizer tokenizer = (separator == null) 
210                     ? new StringTokenizer(multipleValues)
211                     : new StringTokenizer(multipleValues, separator);
212             while (tokenizer.hasMoreTokens()) {
213                 coll.add(tokenizer.nextToken().trim());
214             }
215         }
216         return coll;
217     }
218 
219 
220 	public static String toString(Set<?> messageSet) {
221 		return toString(new StringBuffer(), new ArrayList<Object>(messageSet), defaultConfig);
222 	}
223 	public static String toString(List<?> messageList) {
224 		return toString(new StringBuffer(), messageList, defaultConfig);
225 	}
226     public static String toString(List<?> messageList, CollectionUtilConfig config) {
227         return toString(new StringBuffer(), messageList, config);
228     }
229 	public static String toString(StringBuffer strBuf, List<?> inputList, CollectionUtilConfig config) {
230         if (inputList == null || inputList.size() == 0) {
231             return null;
232         }
233         
234         strBuf.append(config.getArrayPrefix());
235         boolean isFirst = true;
236         for (int index = 0; index < inputList.size(); index++) {
237             if (! isFirst) {
238                 strBuf.append(config.getArrayValueSeparator());
239             }
240             if (inputList.get(index) != null) {
241                 strBuf.append(config.getArrayValuePrefix());
242                 strBuf.append(inputList.get(index));
243                 strBuf.append(config.getArrayValuePostfix());
244             }
245             else {
246                 strBuf.append(config.getNullValue());
247             }
248             isFirst = false;
249         }
250         strBuf.append(config.getArrayPostfix());
251         
252         return strBuf.toString();
253 	}
254 
255 
256 	public static List<String> toList(String[] stringArray) {
257 		List<String> list = new ArrayList<String>();
258 		for (int index = 0; index < stringArray.length; index++) {
259 			list.add(stringArray[index]);
260 		}
261 		return list;
262 	}
263 
264 }