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

io.microsphere.collection.ListUtils Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 io.microsphere.collection;

import io.microsphere.util.BaseUtils;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import static io.microsphere.collection.CollectionUtils.size;
import static io.microsphere.collection.CollectionUtils.toIterable;
import static io.microsphere.collection.CollectionUtils.toIterator;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;

/**
 * The utilities class for Java {@link List}
 *
 * @author Mercy
 * @see List
 * @since 1.0.0
 */
public abstract class ListUtils extends BaseUtils {

    public static boolean isList(Object values) {
        return values instanceof List;
    }

    public static  List ofList(E... elements) {
        return asList(elements);
    }

    public static  List ofList(Iterable iterable) {
        if (iterable == null) {
            return emptyList();
        } else if (isList(iterable)) {
            return (List) iterable;
        } else {
            return ofList(iterable.iterator());
        }
    }

    public static  List ofList(Enumeration enumeration) {
        return ofList(toIterator(enumeration));
    }

    public static  List ofList(Iterator iterator) {
        if (iterator == null) {
            return emptyList();
        }
        List list = newLinkedList();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        return unmodifiableList(list);
    }

    public static  ArrayList newArrayList(int size) {
        return new ArrayList<>(size);
    }

    public static  LinkedList newArrayList(Enumeration values) {
        return newLinkedList(toIterable(values));
    }

    public static  ArrayList newArrayList(Iterable values) {
        return newArrayList(values.iterator());
    }

    public static  ArrayList newArrayList(Iterator iterator) {
        ArrayList list = newArrayList();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        return list;
    }

    public static  ArrayList newArrayList() {
        return new ArrayList<>();
    }

    public static  LinkedList newLinkedList(Enumeration values) {
        return newLinkedList(toIterable(values));
    }

    public static  LinkedList newLinkedList(Iterable values) {
        return newLinkedList(values.iterator());
    }

    public static  LinkedList newLinkedList(Iterator iterator) {
        LinkedList list = newLinkedList();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        return list;
    }

    public static  LinkedList newLinkedList() {
        return new LinkedList<>();
    }

    public static  void forEach(List values, BiConsumer indexedElementConsumer) {
        int length = size(values);
        for (int i = 0; i < length; i++) {
            T value = values.get(i);
            indexedElementConsumer.accept(i, value);
        }
    }

    public static  void forEach(List values, Consumer consumer) {
        forEach(values, (i, e) -> consumer.accept(e));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy