View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2005, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  
22  package org.dbunit.util;
23  
24  import java.util.Set;
25  
26  import org.apache.commons.collections.set.ListOrderedSet;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * Helper for collections-related methods.
32   * <br>
33   * @author Felipe Leme (dbunit@felipeal.net)
34   * @author gommma (gommma AT users.sourceforge.net)
35   * @author Last changed by: $Author$
36   * @version $Revision$ $Date$
37   * @since Nov 5, 2005
38   */
39  public class CollectionsHelper {
40  
41      /**
42       * Logger for this class
43       */
44      private static final Logger logger = LoggerFactory.getLogger(CollectionsHelper.class);
45  
46      // class is "static"
47      private CollectionsHelper() {}
48  
49      /**
50       * Returns a Set from an array of objects.
51       * Note the Iterator returned by this Set preserves the order of the array.
52       * @param objects array of objects
53       * @return Set with the elements of the array or null if entry is null
54       */
55      public static Set objectsToSet( Object[] objects ) {
56          logger.debug("objectsToSet(objects={}) - start", objects);
57  
58          if ( objects == null ) {
59              return null;
60          }
61          Set set = new ListOrderedSet();
62          for (int i = 0; i < objects.length; i++) {
63              set.add(objects[i]);
64          }
65          return set;
66      }
67  
68      /**
69       * Returns an array of Objects from a Set.
70       * @param set a Set 
71       * @return array of Objects with the elements of the Set or null if set is null
72       */
73      public static Object[] setToObjects( Set set ) {
74          logger.debug("setToObjects(set={}) - start", set);
75  
76          if ( set == null ) {
77              return null;
78          }
79          else {
80              return set.toArray();
81          }
82      }
83  
84      /**
85       * Returns an array of Strings from a Set.
86       * @param set a Set of Strings
87       * @return array of Strings with the elements of the Set or null if set is null
88       */
89      public static String[] setToStrings( Set set ) {
90          logger.debug("setToStrings(set={}) - start", set);
91  
92          if ( set == null ) {
93              return null;
94          }
95          else {
96              String[] strings = (String[]) set.toArray(new String[0]);
97              return strings;
98          }
99      };
100 
101 }