Created
March 20, 2017 15:18
-
-
Save AndrewReitz/83c19c5b3ca0157ed78ba260d7e92374 to your computer and use it in GitHub Desktop.
Simple Java Immutable List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.Serializable; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.ListIterator; | |
import java.util.RandomAccess; | |
import static com.fernandocejas.arrow.checks.Preconditions.checkNotNull; | |
/** | |
* A list that does not support mutability. | |
* All methods that are not supported by this list (as they would break the non-mutable contract) | |
* are marked with {@link Deprecated} | |
* | |
* @see List | |
* @see java.util.Collections#unmodifiableList(List) | |
* @param <T> | |
*/ | |
public class ImmutableList<T> implements List<T>, RandomAccess, Serializable { | |
private final List<T> delegate; | |
public ImmutableList(Collection<T> collection) { | |
checkNotNull(collection, "collection == null"); | |
this.delegate = java.util.Collections.unmodifiableList(new ArrayList<>(collection)); | |
} | |
@Override public int size() { | |
return delegate.size(); | |
} | |
@Override public boolean isEmpty() { | |
return delegate.isEmpty(); | |
} | |
@Override public boolean contains(Object o) { | |
return delegate.contains(o); | |
} | |
@NonNull @Override public Iterator<T> iterator() { | |
return delegate.iterator(); | |
} | |
@NonNull @Override public Object[] toArray() { | |
return delegate.toArray(); | |
} | |
@NonNull @Override public <T1> T1[] toArray(@NonNull T1[] a) { | |
return delegate.toArray(a); | |
} | |
@Deprecated | |
@Override public boolean add(T t) { | |
throw new UnsupportedOperationException(); | |
} | |
@Deprecated | |
@Override public boolean remove(Object o) { | |
throw new UnsupportedOperationException(); | |
} | |
@Override public boolean containsAll(@NonNull Collection<?> c) { | |
return delegate.containsAll(c); | |
} | |
@Deprecated | |
@Override public boolean addAll(@NonNull Collection<? extends T> c) { | |
throw new UnsupportedOperationException(); | |
} | |
@Deprecated | |
@Override public boolean addAll(int index, @NonNull Collection<? extends T> c) { | |
throw new UnsupportedOperationException(); | |
} | |
@Deprecated | |
@Override public boolean removeAll(@NonNull Collection<?> c) { | |
throw new UnsupportedOperationException(); | |
} | |
@Deprecated | |
@Override public boolean retainAll(@NonNull Collection<?> c) { | |
throw new UnsupportedOperationException(); | |
} | |
@Deprecated | |
@Override public void clear() { | |
throw new UnsupportedOperationException(); | |
} | |
@Override public T get(int index) { | |
return delegate.get(index); | |
} | |
@Deprecated | |
@Override public T set(int index, T element) { | |
throw new UnsupportedOperationException(); | |
} | |
@Deprecated | |
@Override public void add(int index, T element) { | |
throw new UnsupportedOperationException(); | |
} | |
@Deprecated | |
@Override public T remove(int index) { | |
throw new UnsupportedOperationException(); | |
} | |
@Override public int indexOf(Object o) { | |
return delegate.indexOf(o); | |
} | |
@Override public int lastIndexOf(Object o) { | |
return delegate.lastIndexOf(o); | |
} | |
@Override public ListIterator<T> listIterator() { | |
return delegate.listIterator(); | |
} | |
@NonNull @Override public ListIterator<T> listIterator(int index) { | |
return delegate.listIterator(index); | |
} | |
@NonNull @Override public List<T> subList(int fromIndex, int toIndex) { | |
return delegate.subList(fromIndex, toIndex); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment