1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 package com.ivata.mask.util;
86 import java.util.Collection;
87 import java.util.Enumeration;
88 import java.util.Iterator;
89 import java.util.List;
90 import java.util.Properties;
91 import java.util.StringTokenizer;
92 import java.util.Vector;
93 /***
94 * <p>
95 * This class contains extra routines for combining collection objects.
96 * </p>
97 *
98 * <p>
99 * Don't create an instance of this class; use the static final methods.
100 * </p>
101 *
102 * @since ivata masks 0.4 (2002-05-16)
103 * @author Colin MacLeod
104 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
105 * @version $Revision: 1.3 $
106 */
107 public final class CollectionHandling {
108 /***
109 * <p>
110 * Convert a string of strings separated by new lines into a collection of
111 * strings.
112 * </p>
113 *
114 * @param lines
115 * a single text containing many lines to be split.
116 * @return a <code>Collection</code> of <code>String</code> instances
117 * representing each line from the input.
118 */
119 public static List convertFromLines(final String lines) {
120 return convertFromLines(lines, "\n\r");
121 }
122 /***
123 * <p>
124 * Convert a string of strings separated by deliminators into a collection
125 * of strings.
126 * </p>
127 *
128 * @param lines
129 * a single text containing many lines to be split.
130 * @param deliminators
131 * a <code>String</code> containing all possible deliminators
132 * to search for.
133 * @return a <code>Collection</code> of <code>String</code> instances
134 * representing each line from the input.
135 */
136 public static java.util.List convertFromLines(final String lines,
137 final String deliminators) {
138
139 if (lines == null) {
140 return null;
141 }
142 Vector list = new Vector();
143 StringTokenizer tokenizer = new StringTokenizer(lines, deliminators);
144 while (tokenizer.hasMoreTokens()) {
145 list.add(tokenizer.nextToken());
146 }
147 return list;
148 }
149 /***
150 * <p>
151 * Convert a collection of strings to one long string, separated by new
152 * lines.
153 * </p>
154 *
155 * @param convertToLines
156 * a <code>Collection</code> of <code>String</code> instances
157 * representing each line of the output.
158 * @return a single text containing the elements of the input
159 * <code>Collection</code> separated by new line characters.
160 */
161 public static String convertToLines(final Collection convertToLines) {
162 return convertToLines(convertToLines, '\n');
163 }
164 /***
165 * <p>
166 * Convert a collection of strings to one long string, separated by
167 * deliminators.
168 * </p>
169 *
170 * @param convertToLines
171 * a <code>Collection</code> of <code>String</code> instances
172 * representing each line of the output.
173 * @param deliminator
174 * a <code>char</code> containing deliminator to add after each
175 * element.
176 * @return a single text containing the elements of the input
177 * <code>Collection</code> separated by new line characters.
178 */
179 public static String convertToLines(final Collection convertToLines,
180 final char deliminator) {
181
182 if (convertToLines == null) {
183 return null;
184 }
185 StringBuffer lines = new StringBuffer();
186 for (Iterator i = convertToLines.iterator(); i.hasNext();) {
187 lines.append((String) i.next());
188
189 if (i.hasNext()) {
190 lines.append(deliminator);
191 }
192 }
193 return lines.toString();
194 }
195 /***
196 * <p>
197 * Copy elements from one collection to another. Adds all of the elements in
198 * <code>from</code> to those in <code>to</code>, if that element is
199 * not already present in <code>to</code>.
200 * </p>
201 *
202 * @param from
203 * the collection to copy elements from
204 * @param to
205 * the collection to copy elements to. This collection will be
206 * changed and all the elements of <code>from</code> which
207 * can't be found will be replaced.
208 */
209 public static void merge(final Collection from, final Collection to) {
210 for (Iterator i = from.iterator(); i.hasNext();) {
211 Object item = i.next();
212 if (!to.contains(item)) {
213 to.add(item);
214 }
215 }
216 }
217 /***
218 * <p>
219 * Creates a new properties instance which is a mixture of the two. Adds all
220 * of the properties in <code>fromProperties</code> to those in
221 * <code>toProperties</code>, overwriting any which exist already.
222 * </p>
223 *
224 * @param fromProperties
225 * the properties to base the new object on
226 * @param toProperties
227 * the properties to include if not already set
228 * @return a new <code>Properties</code> instance which contains all of
229 * the properties in <code>fromProperties</code> added to those in
230 * <code>toProperties</code>.
231 */
232 public static Properties splice(final Properties fromProperties,
233 final Properties toProperties) {
234 Properties returnProperties = new java.util.Properties();
235 Enumeration enumeration;
236
237
238 if (toProperties != null) {
239 enumeration = toProperties.keys();
240 while (enumeration.hasMoreElements()) {
241 Object key = enumeration.nextElement();
242 Object value = toProperties.get(key);
243 if (value == null) {
244 throw new NullPointerException(
245 "ERROR in CollectionHandling.splice: "
246 + "value for key '" + key
247 + "' is null in 'to' properties.");
248 }
249 if (!java.lang.String.class.isInstance(value)) {
250 throw new RuntimeException(
251 "ERROR in CollectionHandling.splice: value '"
252 + value
253 + "' for key '"
254 + key
255 + "' has class '"
256 + value.getClass().getName()
257 + "' in 'to' properties. Only instances of "
258 + "java.lang.String are allowed.");
259 }
260 returnProperties.put(key, value);
261 }
262 }
263
264 if (fromProperties != null) {
265 enumeration = fromProperties.keys();
266 while (enumeration.hasMoreElements()) {
267 Object key = enumeration.nextElement();
268 Object value = fromProperties.get(key);
269 if (value == null) {
270 throw new NullPointerException(
271 "ERROR in CollectionHandling.splice: "
272 + "value for key '" + key
273 + "' is null in 'from' properties.");
274 }
275 if (!java.lang.String.class.isInstance(value)) {
276 throw new RuntimeException(
277 "ERROR in CollectionHandling.splice: value '"
278 + value
279 + "' for key '"
280 + key
281 + "' has class '"
282 + value.getClass().getName()
283 + "' in 'from' properties. "
284 + "Only instances of "
285 + "java.lang.String are allowed.");
286 }
287 returnProperties.put(key, value);
288 }
289 }
290 return returnProperties;
291 }
292 /***
293 * <p>
294 * Private default constructor ensures utility class functionality.
295 * </p>
296 */
297 private CollectionHandling() {
298 }
299 }