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

com.landawn.abacus.util.ImmutableList Maven / Gradle / Ivy

Go to download

A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.

There is a newer version: 5.3.16
Show newest version
/*
 * Copyright (C) 2016 HaiYang Li
 *
 * 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 com.landawn.abacus.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.ListIterator;
import java.util.function.UnaryOperator;

/**
 *
 * @author Haiyang Li
 * @param 
 * @since 0.8
 */
public final class ImmutableList extends ImmutableCollection implements List {

    @SuppressWarnings("rawtypes")
    private static final ImmutableList EMPTY = new ImmutableList(Collections.EMPTY_LIST);

    private final List list;

    ImmutableList(List list) {
        super(Collections.unmodifiableList(list));
        this.list = (List) coll;
    }

    /**
     *
     * @param 
     * @return
     */
    public static  ImmutableList empty() {
        return EMPTY;
    }

    /**
     *
     * @param 
     * @param e
     * @return
     */
    public static  ImmutableList just(E e) {
        return new ImmutableList<>(Collections.singletonList(e));
    }

    /**
     *
     * @param 
     * @param e
     * @return
     */
    public static  ImmutableList of(E e) {
        return new ImmutableList<>(Collections.singletonList(e));
    }

    /**
     *
     * @param 
     * @param a the elements in this array are shared by the returned ImmutableList.
     * @return
     */
    @SafeVarargs
    public static  ImmutableList of(E... a) {
        if (N.isNullOrEmpty(a)) {
            return empty();
        }

        return new ImmutableList<>(Arrays.asList(a));
    }

    /**
     *
     * @param 
     * @param list the elements in this list are shared by the returned ImmutableList.
     * @return
     */
    public static  ImmutableList of(final List list) {
        if (list == null) {
            return empty();
        } else if (list instanceof ImmutableList) {
            return (ImmutableList) list;
        }

        return new ImmutableList<>(list);
    }

    /**
     *
     * @param 
     * @param a
     * @return
     */
    @SafeVarargs
    public static  ImmutableList copyOf(final E... a) {
        if (N.isNullOrEmpty(a)) {
            return empty();
        } else if (a.length == 1) {
            return new ImmutableList<>(Collections.singletonList(a[0]));
        } else {
            return new ImmutableList<>(Arrays.asList(N.clone(a)));
        }
    }

    /**
     *
     * @param 
     * @param c
     * @return
     */
    public static  ImmutableList copyOf(final Collection c) {
        if (N.isNullOrEmpty(c)) {
            return empty();
        } else if (c.size() == 1) {
            return new ImmutableList<>(Collections.singletonList(N.firstOrNullIfEmpty(c)));
        } else {
            return new ImmutableList<>(new ArrayList<>(c));
        }
    }

    /**
     *
     * @param 
     * @param c
     * @return
     * @deprecated throws {@code UnsupportedOperationException}
     * @throws UnsupportedOperationException
     */
    @Deprecated
    public static  ImmutableCollection of(final Collection c) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     *
     * @param index
     * @return
     */
    @Override
    public E get(int index) {
        return list.get(index);
    }

    /**
     *
     * @param o
     * @return
     */
    @Override
    public int indexOf(Object o) {
        return list.indexOf(o);
    }

    /**
     * Last index of.
     *
     * @param o
     * @return
     */
    @Override
    public int lastIndexOf(Object o) {
        return list.lastIndexOf(o);
    }

    @Override
    public ListIterator listIterator() {
        return list.listIterator();
    }

    /**
     *
     * @param index
     * @return
     */
    @Override
    public ListIterator listIterator(int index) {
        return list.listIterator(index);
    }

    /**
     *
     * @param fromIndex
     * @param toIndex
     * @return
     */
    @Override
    public List subList(int fromIndex, int toIndex) {
        return list.subList(fromIndex, toIndex);
    }

    /**
     * Adds the all.
     *
     * @param index
     * @param newElements
     * @return
     * @deprecated throws {@code UnsupportedOperationException}
     * @throws UnsupportedOperationException
     */
    @Deprecated
    @Override
    public boolean addAll(int index, Collection newElements) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     *
     * @param index
     * @param element
     * @return
     * @deprecated throws {@code UnsupportedOperationException}
     * @throws UnsupportedOperationException
     */
    @Deprecated
    @Override
    public E set(int index, E element) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     *
     * @param index
     * @param element
     * @deprecated throws {@code UnsupportedOperationException}
     * @throws UnsupportedOperationException
     */
    @Deprecated
    @Override
    public void add(int index, E element) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     *
     * @param index
     * @return
     * @deprecated throws {@code UnsupportedOperationException}
     * @throws UnsupportedOperationException
     */
    @Deprecated
    @Override
    public E remove(int index) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     *
     * @param operator
     * @deprecated throws {@code UnsupportedOperationException}
     * @throws UnsupportedOperationException
     */
    @Deprecated
    @Override
    public void replaceAll(UnaryOperator operator) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     *
     * @param c
     * @deprecated throws {@code UnsupportedOperationException}
     * @throws UnsupportedOperationException
     */
    @Deprecated
    @Override
    public void sort(Comparator c) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy