com.googlecode.kevinarpe.papaya.container.ArrayAsImmutableList Maven / Gradle / Ivy
package com.googlecode.kevinarpe.papaya.container;
/*
* #%L
* This file is part of Papaya.
* %%
* Copyright (C) 2013 - 2014 Kevin Connor ARPE ([email protected])
* %%
* Papaya is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GPL Classpath Exception:
* This project is subject to the "Classpath" exception as provided in
* the LICENSE file that accompanied this code.
*
* Papaya is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Papaya. If not, see .
* #L%
*/
import com.googlecode.kevinarpe.papaya.annotation.FullyTested;
import com.googlecode.kevinarpe.papaya.argument.ObjectArgs;
import java.util.*;
/**
* This class has the same effect as:
* {@code Collections.unmodifiableList(Arrays.asList(arr.clone()))}. This collection is
* immutable, so no access is provided to the underlying array.
*
* The class constructor is private. Instead, use static method {@link #copyOf(Object[])}.
*
* This class is closely related to {@link ArrayAsFixedSizeList} and
* {@link ArrayAsUnmodifiableList}. To understand the difference between unmodifiable and
* immutable, learn more here:
* http://stackoverflow.com/questions/8892350/immutable-vs-unmodifiable-collection
*
* @author Kevin Connor ARPE ([email protected])
*
* @param
* element type
*
* @see #copyOf(Object[])
* @see UnmodifiableForwardingList
* @see Collections#unmodifiableList(List)
* @see Arrays#asList(Object[])
* @see ArrayAsFixedSizeList
* @see ArrayAsUnmodifiableList
*/
@FullyTested
public final class ArrayAsImmutableList
extends UnmodifiableForwardingList {
/**
* Constructs a new instance with a copy of the input array.
*
* @param arr
* array of values to be copied. Must not be {@code null}, but can by empty or contain
* {@code null} values.
* @param
* element type
*
* @return new instance
*
* @throws NullPointerException
* if {@code arr} is {@code null}
*/
public static ArrayAsImmutableList copyOf(TValue[] arr) {
return new ArrayAsImmutableList(arr);
}
private final TValue[] _arr;
private final List _arrAsList;
private ArrayAsImmutableList(TValue[] arr) {
ObjectArgs.checkNotNull(arr, "arr");
_arr = arr.clone();
_arrAsList = Arrays.asList(_arr);
}
@Override
protected List delegate() {
return _arrAsList;
}
}