View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: CollectionHandling.java,v $
31   * Revision 1.3  2005/04/09 18:04:17  colinmacleod
32   * Changed copyright text to GPL v2 explicitly.
33   *
34   * Revision 1.2  2005/01/06 22:21:45  colinmacleod
35   * Moved up a version number.
36   * Changed copyright notices to 2005.
37   * Updated the documentation:
38   *   - started working on multiproject:site docu.
39   *   - changed the logo.
40   * Added checkstyle and fixed LOADS of style issues.
41   * Added separate thirdparty subproject.
42   * Added struts (in web), util and webgui (in webtheme) from ivata op.
43   *
44   * Revision 1.3  2004/11/03 16:15:43  colinmacleod
45   * Cosmetic changes.
46   *
47   * Revision 1.2  2004/03/21 21:16:36  colinmacleod
48   * Shortened name to ivata op.
49   *
50   * Revision 1.1.1.1  2004/01/27 20:59:46  colinmacleod
51   * Moved ivata op to SourceForge.
52   *
53   * Revision 1.2  2003/10/15 14:13:53  colin
54   * fixing for XDoclet
55   *
56   * Revision 1.9  2003/02/24 19:27:31  colin
57   * restructured file paths
58   *
59   * Revision 1.8  2003/02/14 15:22:21  colin
60   * changed properties splice routine for force non-null, String values
61   *
62   * Revision 1.7  2003/02/04 17:43:52  colin
63   * copyright notice
64   *
65   * Revision 1.6  2003/01/31 10:39:02  colin
66   * added checking for null properties
67   *
68   * Revision 1.5  2002/11/12 10:10:42  colin
69   * extended convertTo/FromLines to let you specify deliminators
70   *
71   * Revision 1.4  2002/09/16 14:17:14  colin
72   * added convertToLines & convertFromLines
73   *
74   * Revision 1.3  2002/08/11 11:58:26  colin
75   * Added checking for null properties as parameter in poperty
76   * splicing method.
77   *
78   * Revision 1.2  2002/06/25 09:23:46  colin
79   * added Collection merge method
80   *
81   * Revision 1.1  2002/06/13 11:22:21  colin
82   * first version with rose model integration.
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         // prerequisites - can't do anything with nulls
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         // prerequisites - can't do anything with nulls
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             // if this is not the last, add a newline seperator
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         // first add the to properties, so they'll be overwritten by the
237         // 'original' ones afterwards
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         // overwrite any of the properties with the from properties
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 }