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

net.dongliu.commons.collection.Array Maven / Gradle / Ivy

The newest version!
package net.dongliu.commons.collection;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.UnaryOperator;

/**
 * Array wrapper as collection
 *
 * @author Liu Dong {@literal }
 */
public class Array extends AbstractList implements RandomAccess, java.io.Serializable,
        ExList {
    private static final long serialVersionUID = -2764017481108945198L;
    private final T[] array;

    public Array(T[] array) {
        this.array = Objects.requireNonNull(array);
    }

    @SafeVarargs
    public static  Array of(T... values) {
        return new Array<>(values);
    }

    @Override
    public int size() {
        return array.length;
    }

    @Override
    public Object[] toArray() {
        return array.clone();
    }

    @Override
    @SuppressWarnings("unchecked")
    public  T1[] toArray(T1[] a) {
        int size = size();
        if (a.length < size)
            return Arrays.copyOf(this.array, size,
                    (Class) a.getClass());
        System.arraycopy(this.array, 0, a, 0, size);
        if (a.length > size)
            a[size] = null;
        return a;
    }

    @Override
    public T get(int index) {
        return array[index];
    }

    @Override
    public T set(int index, T element) {
        T oldValue = array[index];
        array[index] = element;
        return oldValue;
    }

    @Override
    public int indexOf(Object o) {
        T[] a = this.array;
        if (o == null) {
            for (int i = 0; i < a.length; i++)
                if (a[i] == null)
                    return i;
        } else {
            for (int i = 0; i < a.length; i++)
                if (o.equals(a[i]))
                    return i;
        }
        return -1;
    }

    @Override
    public boolean contains(Object o) {
        return indexOf(o) != -1;
    }

    @Override
    public Spliterator spliterator() {
        return Spliterators.spliterator(array, Spliterator.ORDERED);
    }

    @Override
    public void forEach(Consumer action) {
        Objects.requireNonNull(action);
        for (T e : array) {
            action.accept(e);
        }
    }

    @Override
    public void replaceAll(UnaryOperator operator) {
        Objects.requireNonNull(operator);
        T[] a = this.array;
        for (int i = 0; i < a.length; i++) {
            a[i] = operator.apply(a[i]);
        }
    }

    @Override
    public void sort(Comparator c) {
        Arrays.sort(array, c);
    }

    @Override
    public ExList subList(int fromIndex, int toIndex) {
        return new ForwardingList<>(super.subList(fromIndex, toIndex));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy