001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2005, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021:
022: package org.dbunit.util;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import java.util.Iterator;
028: import java.util.Set;
029:
030: import org.apache.commons.collections.set.ListOrderedSet;
031:
032: /**
033: * Helper for collections-related methods.
034: * <br>
035: * @author Felipe Leme <dbunit@felipeal.net>
036: * @version $Revision: 554 $
037: * @since Nov 5, 2005
038: *
039: */
040:
041: public class CollectionsHelper {
042:
043: /**
044: * Logger for this class
045: */
046: private static final Logger logger = LoggerFactory
047: .getLogger(CollectionsHelper.class);
048:
049: // class is "static"
050: private CollectionsHelper() {
051: }
052:
053: /**
054: * Returns a Set from an array of objects.
055: * Note the Iterator returned by this Set mantains the order of the array.
056: * @param objects array of objects
057: * @return Set with the elements of the array or null if entry is null
058: */
059: public static Set objectsToSet(Object[] objects) {
060: logger.debug("objectsToSet(objects=" + objects + ") - start");
061:
062: if (objects == null) {
063: return null;
064: }
065: Set set = new ListOrderedSet();
066: for (int i = 0; i < objects.length; i++) {
067: set.add(objects[i]);
068: }
069: return set;
070: }
071:
072: /**
073: * Returns an array of Objects from a Set.
074: * @param a Set
075: * @return array of Objects with the elements of the Set or null if set is null
076: */
077: public static Object[] setToObjects(Set set) {
078: logger.debug("setToObjects(set=" + set + ") - start");
079:
080: if (set == null) {
081: return null;
082: }
083: Object[] objects = new Object[set.size()];
084: int i = 0;
085: for (Iterator iter = set.iterator(); iter.hasNext(); i++) {
086: objects[i] = iter.next();
087: }
088: return objects;
089: }
090:
091: /**
092: * Returns an array of Strings from a Set.
093: * @param a Set of Strings
094: * @return array of Strings with the elements of the Set or null if set is null
095: */
096: public static String[] setToStrings(Set set) {
097: logger.debug("setToStrings(set=" + set + ") - start");
098:
099: if (set == null) {
100: return null;
101: }
102: String[] strings = new String[set.size()];
103: int i = 0;
104: for (Iterator iter = set.iterator(); iter.hasNext(); i++) {
105: strings[i] = (String) iter.next();
106: }
107: return strings;
108: };
109:
110: }
|