com.landawn.abacus.util.ImmutableList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-common Show documentation
Show all versions of abacus-common Show documentation
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
/*
* 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 extends E> 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 extends E> 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 extends E> 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 extends E> 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 extends E> 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 super E> c) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
}