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.Arrays;
25  import java.util.LinkedHashSet;
26  import java.util.Set;
27  
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * Helper for collections-related methods.
33   * <br>
34   * @author Felipe Leme (dbunit@felipeal.net)
35   * @author gommma (gommma AT users.sourceforge.net)
36   * @author Last changed by: $Author$
37   * @version $Revision$ $Date$
38   * @since Nov 5, 2005
39   */
40  public class CollectionsHelper {
41  
42      /**
43       * Logger for this class
44       */
45      private static final Logger logger = LoggerFactory.getLogger(CollectionsHelper.class);
46  
47      // class is "static"
48      private CollectionsHelper() {}
49  
50      /**
51       * Returns a Set from an array of objects.
52       * Note the Iterator returned by this Set preserves the order of the array.
53       * @param objects array of objects
54       * @return Set with the elements of the array or null if entry is null
55       */
56      public static Set objectsToSet( Object[] objects ) {
57          logger.debug("objectsToSet(objects={}) - start", objects);
58  
59          if ( objects == null ) {
60              return null;
61          }
62          return new LinkedHashSet(Arrays.asList(objects));
63      }
64  
65      /**
66       * Returns an array of Objects from a Set.
67       * @param set a Set 
68       * @return array of Objects with the elements of the Set or null if set is null
69       */
70      public static Object[] setToObjects( Set set ) {
71          logger.debug("setToObjects(set={}) - start", set);
72  
73          if ( set == null ) {
74              return null;
75          }
76          else {
77              return set.toArray();
78          }
79      }
80  
81      /**
82       * Returns an array of Strings from a Set.
83       * @param set a Set of Strings
84       * @return array of Strings with the elements of the Set or null if set is null
85       */
86      public static String[] setToStrings( Set set ) {
87          logger.debug("setToStrings(set={}) - start", set);
88  
89          if ( set == null ) {
90              return null;
91          }
92          else {
93              String[] strings = (String[]) set.toArray(new String[0]);
94              return strings;
95          }
96      };
97  
98  }