| CollectionUtil.java |
1 /**
2 * Distribution License:
3 * JSword is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU Lesser General Public License, version 2.1 as published by
5 * the Free Software Foundation. This program is distributed in the hope
6 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 * See the GNU Lesser General Public License for more details.
9 *
10 * The License is available on the internet at:
11 * http://www.gnu.org/copyleft/lgpl.html
12 * or by writing to:
13 * Free Software Foundation, Inc.
14 * 59 Temple Place - Suite 330
15 * Boston, MA 02111-1307, USA
16 *
17 * Copyright: 2005
18 * The copyright to this program is held by it's authors.
19 *
20 * ID: $Id: CollectionUtil.java 2090 2011-03-07 04:13:05Z dmsmith $
21 */
22 package org.crosswire.common.util;
23
24 import java.io.IOException;
25 import java.net.URI;
26 import java.util.ArrayList;
27 import java.util.Enumeration;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Properties;
31 import java.util.Set;
32
33 /**
34 * Some utils to help work with Collections.
35 *
36 * @see gnu.lgpl.License for license details.<br>
37 * The copyright to this program is held by it's authors.
38 * @author Joe Walker [joe at eireneh dot com]
39 */
40 public final class CollectionUtil {
41 /**
42 * Dont do this
43 */
44 private CollectionUtil() {
45 }
46
47 /**
48 * Create a List from an Iterable.
49 *
50 * @param it
51 * The source of data for the list
52 * @return List
53 */
54 public static <T> List<T> createList(Iterable<T> it) {
55 List<T> reply = new ArrayList<T>();
56 for (T obj : it) {
57 reply.add(obj);
58 }
59
60 return reply;
61 }
62
63 /**
64 * Create a Set from an Iterable.
65 *
66 * @param it
67 * The source of data for the list
68 * @return the created set
69 */
70 public static <T> Set<T> createSet(Iterable<T> it) {
71 Set<T> reply = new HashSet<T>();
72 for (T obj : it) {
73 reply.add(obj);
74 }
75
76 return reply;
77 }
78
79 /**
80 * Convert a <code>Properties</code> into a <code>Map</code>.
81 *
82 * @param prop
83 * The Properties to convert
84 * @return The map
85 */
86 public static PropertyMap properties2Map(Properties prop) {
87 PropertyMap propMap = new PropertyMap();
88 for (Enumeration<Object> e = prop.keys(); e.hasMoreElements(); ) {
89 Object k = e.nextElement();
90 Object v = prop.get(k);
91 if (k instanceof String && v instanceof String) {
92 propMap.put((String) k, (String) v);
93 }
94 }
95 return propMap;
96 }
97
98 /**
99 * Convert a <code>Properties</code> located at <code>propURI</code> into a
100 * <code>Map</code>.
101 *
102 * @param propUri
103 * The URI of the Properties to convert
104 * @return The map
105 */
106 public static PropertyMap properties2Map(URI propUri) throws IOException {
107 return NetUtil.loadProperties(propUri);
108 }
109
110 }
111