001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy 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
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.type.collection;
020
021import java.util.ArrayList;
022import java.util.Arrays;
023import java.util.Collection;
024import java.util.List;
025
026/**
027 * @author Stephane
028 */
029public class CollectionUtil
030{
031    public static <T> ArrayList<T> asArrayList(T... a)
032    {
033        return new ArrayList<T>(Arrays.asList(a));
034    }
035
036    public static <T> List<T> asList(T... a)
037    {
038        return Arrays.asList(a);
039    }
040
041    public static <T> ArrayList<T> createArrayList(T t, boolean addIfNull)
042    {
043        final ArrayList<T> result = new ArrayList<T>();
044
045        if (addIfNull || (t != null))
046            result.add(t);
047
048        return result;
049    }
050
051    public static <T> ArrayList<T> createArrayList(T t)
052    {
053        return createArrayList(t, true);
054    }
055
056    public static <T> boolean addUniq(List<T> list, T t, boolean addIfNull)
057    {
058        if (addIfNull || (t != null))
059        {
060            if (!list.contains(t))
061                return list.add(t);
062        }
063
064        return false;
065    }
066
067    public static <T> boolean addUniq(List<T> list, T t)
068    {
069        return addUniq(list, t, true);
070    }
071
072    /**
073     * Return <code>true</code> if both collections contains the same elements.
074     */
075    public static <T> boolean equals(Collection<T> c1, Collection<T> c2)
076    {
077        if (c1 == c2)
078            return true;
079        if (c1 == null)
080            return false;
081        if (c2 == null)
082            return false;
083
084        if (c1.size() != c2.size())
085            return false;
086
087        return c2.containsAll(c1);
088    }
089}