All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xerial.util.CollectionUtil Maven / Gradle / Ivy

There is a newer version: 3.4.0
Show newest version
/*--------------------------------------------------------------------------
 *  Copyright 2007 Taro L. Saito
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *--------------------------------------------------------------------------*/
//--------------------------------------
// XerialJ Project
//
// CollectionUtil.java
// Since: May 8, 2007
//
// $URL$ 
// $Author$
//--------------------------------------
package org.xerial.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

public class CollectionUtil
{

    /**
     * Non constractable class
     */
    private CollectionUtil()
    {}

    public static  List toString(Collection collection)
    {
        ArrayList result = new ArrayList();
        for (In input : collection)
        {
            if (input != null)
                result.add(input.toString());
            else
                result.add(null);
        }
        return result;
    }

    public static  List collectFromNonGenericCollection(Iterable collection, Functor functor)
    {
        ArrayList result = new ArrayList();
        for (In input : collection)
        {
            result.add(functor.apply(input));
        }
        return result;
    }

    public static  List select(Iterable collection, Predicate filter)
    {
        ArrayList result = new ArrayList();
        for (In input : collection)
        {
            if (filter.apply(input))
                result.add(input);
        }
        return result;
    }

    public static  List collect(Iterable collection, Functor functor)
    {
        ArrayList result = new ArrayList();
        for (In input : collection)
        {
            result.add(functor.apply(input));
        }
        return result;
    }

    public static  List collect(In[] collection, Functor functor)
    {
        ArrayList result = new ArrayList();
        for (In input : collection)
        {
            result.add(functor.apply(input));
        }
        return result;
    }

    public static  List collect(Map map, BinaryFunctor functor)
    {
        ArrayList result = new ArrayList();
        for (K key : map.keySet())
        {
            result.add(functor.apply(key, map.get(key)));
        }
        return result;
    }

    public static  String displayMap(Map map, final String keyValueSeparator, final String elementSeparator)
    {
        return StringUtil.join(collect(map, new BinaryFunctor() {
            public Object apply(K key, V value)
            {
                return key.toString() + keyValueSeparator + value.toString();
            }
        }), ", ");
    }

    public static  TreeSet sort(Collection collection)
    {
        TreeSet sortedSet = new TreeSet();
        for (E e : collection)
        {
            sortedSet.add(e);
        }
        return sortedSet;
    }

    /**
     * Converts an array into a list
     * 
     * @param 
     * @param array
     * @return list representation of a given array
     */
    public static  List toList(E[] array)
    {
        ArrayList result = new ArrayList(array.length);
        for (int i = 0; i < array.length; i++)
            result.add(array[i]);
        return result;
    }

}