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 or later
5    * as published by the Free Software Foundation. This program is distributed
6    * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
7    * the 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   * © CrossWire Bible Society, 2005 - 2016
18   *
19   */
20  package org.crosswire.common.util;
21  
22  import java.io.IOException;
23  import java.net.URI;
24  import java.util.ArrayList;
25  import java.util.Enumeration;
26  import java.util.HashSet;
27  import java.util.List;
28  import java.util.Properties;
29  import java.util.Set;
30  
31  /**
32   * Some utils to help work with Collections.
33   * 
34   * @see gnu.lgpl.License The GNU Lesser General Public License for details.
35   * @author Joe Walker
36   */
37  public final class CollectionUtil {
38      /**
39       * Dont do this
40       */
41      private CollectionUtil() {
42      }
43  
44      /**
45       * Create a List from an Iterable.
46       * 
47       * @param <T> The type of data for the list
48       * @param it  The source of data for the list
49       * @return List
50       */
51     public static <T> List<T> createList(Iterable<T> it) {
52          List<T> reply = new ArrayList<T>();
53          for (T obj : it) {
54              reply.add(obj);
55          }
56  
57          return reply;
58      }
59  
60      /**
61       * Create a Set from an Iterable.
62       * 
63       * @param <T> The type of data for the list
64       * @param it  The source of data for the list
65       * @return the created set
66       */
67      public static <T> Set<T> createSet(Iterable<T> it) {
68          Set<T> reply = new HashSet<T>();
69          for (T obj : it) {
70              reply.add(obj);
71          }
72  
73          return reply;
74      }
75  
76      /**
77       * Convert a <code>Properties</code> into a <code>Map</code>.
78       * 
79       * @param prop The Properties to convert
80       * @return The map
81       */
82      public static PropertyMap properties2Map(Properties prop) {
83          PropertyMap propMap = new PropertyMap();
84          for (Enumeration<Object> e = prop.keys(); e.hasMoreElements(); ) {
85              Object k = e.nextElement();
86              Object v = prop.get(k);
87              if (k instanceof String && v instanceof String) {
88                  propMap.put((String) k, (String) v);
89              }
90          }
91          return propMap;
92      }
93  
94      /**
95       * Convert a <code>Properties</code> located at <code>propURI</code> into a
96       * <code>Map</code>.
97       * 
98       * @param propUri
99       *            The URI of the Properties to convert
100      * @return The map
101      * @throws IOException If there is a problem with getting the properties from the URI
102      */
103     public static PropertyMap properties2Map(URI propUri) throws IOException {
104         return NetUtil.loadProperties(propUri);
105     }
106 
107 }
108