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

com.bestvike.collections.generic.Array Maven / Gradle / Ivy

package com.bestvike.collections.generic;

import com.bestvike.function.Predicate1;
import com.bestvike.linq.IEnumerator;
import com.bestvike.linq.adapter.enumerator.ArrayEnumerator;
import com.bestvike.linq.exception.ExceptionArgument;
import com.bestvike.linq.exception.ThrowHelper;
import com.bestvike.linq.util.ArrayUtils;

import java.util.Collection;
import java.util.List;

/**
 * Created by 许崇雷 on 2017-07-19.
 */
public final class Array implements IArray, Cloneable {
    private static final Array EMPTY = new Array<>(ArrayUtils.empty());
    private final Object[] elements;

    public Array(Object[] elements) {
        if (elements == null)
            ThrowHelper.throwArgumentNullException(ExceptionArgument.elements);
        this.elements = elements;
    }

    public static  Array empty() {
        //noinspection unchecked
        return (Array) EMPTY;
    }

    @Override
    public IEnumerator enumerator() {
        return new ArrayEnumerator<>(this.elements);
    }

    @Override
    public Object[] getArray() {
        return this.elements;
    }

    @Override
    public T get(int index) {
        //noinspection unchecked
        return (T) this.elements[index];
    }

    @Override
    public int _indexOf(T item) {
        return ArrayUtils.indexOf(this.elements, item);
    }

    @Override
    public int _lastIndexOf(T item) {
        return ArrayUtils.lastIndexOf(this.elements, item);
    }

    @Override
    public int _findIndex(Predicate1 match) {
        //noinspection unchecked
        return ArrayUtils.findIndex(this.elements, (Predicate1) match);
    }

    @Override
    public int _findLastIndex(Predicate1 match) {
        //noinspection unchecked
        return ArrayUtils.findLastIndex(this.elements, (Predicate1) match);
    }

    public void set(int index, T item) {
        this.elements[index] = item;
    }

    @Override
    public Collection getCollection() {
        return ArrayUtils.toCollection(this.elements);
    }

    @Override
    public int _getCount() {
        return this.elements.length;
    }

    @Override
    public boolean _contains(T value) {
        return ArrayUtils.contains(this.elements, value);
    }

    @Override
    public void _copyTo(Object[] array, int arrayIndex) {
        System.arraycopy(this.elements, 0, array, arrayIndex, this.elements.length);
    }

    @Override
    public T[] _toArray(Class clazz) {
        return ArrayUtils.toArray(this.elements, clazz);
    }

    @Override
    public Object[] _toArray() {
        return ArrayUtils.toArray(this.elements, Object.class);
    }

    @Override
    public List _toList() {
        return ArrayUtils.toList(this.elements);
    }

    @Override
    @SuppressWarnings("MethodDoesntCallSuperMethod")
    public Array clone() {
        return new Array<>(ArrayUtils.clone(this.elements));
    }
}