com.googlecode.kevinarpe.papaya.container.ArrayAsUnmodifiableList 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.Arrays;
import java.util.Collections;
import java.util.List;
/**
* This class has the same effect as:
* {@code Collections.unmodifiableList(Arrays.asList(arr))}. This collection is
* unmodifiable, so no access is provided to the underlying array.
*
* The class constructor is private. Instead, use static method {@link #referenceTo(Object[])}.
*
* This class is closely related to {@link ArrayAsFixedSizeList} and
* {@link ArrayAsImmutableList}. 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 #referenceTo(Object[])
* @see UnmodifiableForwardingList
* @see Collections#unmodifiableList(List)
* @see Arrays#asList(Object[])
* @see ArrayAsFixedSizeList
* @see ArrayAsImmutableList
*/
@FullyTested
public final class ArrayAsUnmodifiableList
extends UnmodifiableForwardingList {
/**
* Constructs a new instance with a reference to the input array.
*
* @param arr
* array of values to be referenced. 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 ArrayAsUnmodifiableList referenceTo(TValue[] arr) {
return new ArrayAsUnmodifiableList(arr);
}
private final TValue[] _arr;
private final List _arrAsList;
private ArrayAsUnmodifiableList(TValue[] arr) {
ObjectArgs.checkNotNull(arr, "arr");
_arr = arr;
_arrAsList = Arrays.asList(arr);
}
@Override
protected List delegate() {
return _arrAsList;
}
}