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

jetbrick.collection.ListUtils Maven / Gradle / Ivy

/**
 * Copyright 2013-2016 Guoqiang Chen, Shanghai, China. All rights reserved.
 *
 *   Author: Guoqiang Chen
 *    Email: [email protected]
 *   WebURL: https://github.com/subchen
 *
 * 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.
 */
package jetbrick.collection;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

public final class ListUtils {

    public static  List asList(T[] items) {
        if (items == null) {
            return null;
        }
        return Arrays.asList(items);
    }

    public static  List asList(Collection items) {
        if (items == null) {
            return null;
        }
        if (items instanceof List) {
            return (List) items;
        }
        return new ArrayList(items);
    }

    public static  List asList(Iterator items) {
        if (items == null) {
            return null;
        }
        List results = new ArrayList();
        while (items.hasNext()) {
            results.add(items.next());
        }
        return results;
    }

    public static  List asList(Iterable items) {
        if (items == null) {
            return null;
        }
        if (items instanceof List) {
            return (List) items;
        } else if (items instanceof Collection) {
            return new ArrayList((Collection) items);
        } else {
            return asList(items.iterator());
        }
    }

    public static  List asList(Enumeration items) {
        if (items == null) {
            return null;
        }
        List results = new ArrayList();
        while (items.hasMoreElements()) {
            results.add(items.nextElement());
        }
        return results;
    }

    // -----------------------------------------------------------------------

    public static  T[] asArray(Collection items, Class elementType) {
        if (items == null) {
            return null;
        }
        @SuppressWarnings("unchecked")
        T[] results = (T[]) Array.newInstance(elementType, items.size());
        int i = 0;
        for (T item : items) {
            results[i++] = item;
        }
        return results;
    }

    public static  T[] asArray(Iterator items, Class elementType) {
        if (items == null) {
            return null;
        }
        return asArray(asList(items), elementType);
    }

    public static  T[] asArray(Iterable items, Class elementType) {
        if (items == null) {
            return null;
        }
        if (items instanceof Collection) {
            return asArray((Collection) items, elementType);
        } else {
            return asArray(asList(items.iterator()), elementType);
        }
    }

    public static  T[] asArray(Enumeration items, Class elementType) {
        if (items == null) {
            return null;
        }
        return asArray(asList(items), elementType);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy