CollectionsHelper.java

  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. package org.dbunit.util;

  22. import java.util.Arrays;
  23. import java.util.LinkedHashSet;
  24. import java.util.Set;

  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;

  27. /**
  28.  * Helper for collections-related methods.
  29.  * <br>
  30.  * @author Felipe Leme (dbunit@felipeal.net)
  31.  * @author gommma (gommma AT users.sourceforge.net)
  32.  * @author Last changed by: $Author$
  33.  * @version $Revision$ $Date$
  34.  * @since Nov 5, 2005
  35.  */
  36. public class CollectionsHelper {

  37.     /**
  38.      * Logger for this class
  39.      */
  40.     private static final Logger logger = LoggerFactory.getLogger(CollectionsHelper.class);

  41.     // class is "static"
  42.     private CollectionsHelper() {}

  43.     /**
  44.      * Returns a Set from an array of objects.
  45.      * Note the Iterator returned by this Set preserves the order of the array.
  46.      * @param objects array of objects
  47.      * @return Set with the elements of the array or null if entry is null
  48.      */
  49.     public static Set objectsToSet( Object[] objects ) {
  50.         logger.debug("objectsToSet(objects={}) - start", objects);

  51.         if ( objects == null ) {
  52.             return null;
  53.         }
  54.         return new LinkedHashSet(Arrays.asList(objects));
  55.     }

  56.     /**
  57.      * Returns an array of Objects from a Set.
  58.      * @param set a Set
  59.      * @return array of Objects with the elements of the Set or null if set is null
  60.      */
  61.     public static Object[] setToObjects( Set set ) {
  62.         logger.debug("setToObjects(set={}) - start", set);

  63.         if ( set == null ) {
  64.             return null;
  65.         }
  66.         else {
  67.             return set.toArray();
  68.         }
  69.     }

  70.     /**
  71.      * Returns an array of Strings from a Set.
  72.      * @param set a Set of Strings
  73.      * @return array of Strings with the elements of the Set or null if set is null
  74.      */
  75.     public static String[] setToStrings( Set set ) {
  76.         logger.debug("setToStrings(set={}) - start", set);

  77.         if ( set == null ) {
  78.             return null;
  79.         }
  80.         else {
  81.             String[] strings = (String[]) set.toArray(new String[0]);
  82.             return strings;
  83.         }
  84.     };

  85. }