Example usage for java.util Collections unmodifiableList

List of usage examples for java.util Collections unmodifiableList

Introduction

In this page you can find the example usage for java.util Collections unmodifiableList.

Prototype

public static <T> List<T> unmodifiableList(List<? extends T> list) 

Source Link

Document

Returns an unmodifiable view of the specified list.

Usage

From source file:Main.java

/**
 * @return an <b>UNMODIFIABLE</b> List&lt;T&gt;
 *///from  ww w. ja  v a  2 s . co m
public static <T> List<T> list(final T... elements) {
    return Collections.unmodifiableList(Arrays.asList(elements));
}

From source file:Main.java

/**
 * Copies the contents of a List to a new, unmodifiable List.  If the List is null, an empty List will be returned.
 * <p>// w  ww  .ja v a  2 s.co  m
 * This is especially useful for methods which take List parameters.  Directly assigning such a List to a member
 * variable can allow external classes to modify internal state.  Classes may have different state depending on the
 * size of a List; for example, a button might be disabled if the the List size is zero.  If an external object
 * can change the List, the containing class would have no way have knowing a modification occurred.
 * 
 * @param <T> The type of element in the List.
 * @param list The List to copy.
 * @return A new, unmodifiable List which contains all of the elements of the List parameter.  Will never be null.
 */
public static <T> List<T> copyToUnmodifiableList(List<T> list) {
    if (list != null) {
        List<T> listCopy = new ArrayList(list);
        return Collections.unmodifiableList(listCopy);
    } else {
        return Collections.emptyList();
    }
}

From source file:Main.java

@Nullable
static <T> List<T> nonEmptyOrNull(Collection<T> collection) {
    List<T> list = Collections.unmodifiableList(new ArrayList<>(collection));
    return list.isEmpty() ? null : list;
}

From source file:Main.java

/**
 * Wraps a possibly null list in an immutable wrapper.
 *
 * @param <T>    the type parameter
 * @param source Nullable list to wrap./*from www  .  j a  va  2  s .  c o m*/
 * @return {@link Collections#unmodifiableList(List)} if given list is not null, otherwise {@link java.util.Collections#EMPTY_LIST}.
 */
public static <T> List<T> wrap(final List<T> source) {
    if (source != null) {
        return Collections.unmodifiableList(source);
    }
    return Collections.emptyList();
}

From source file:Main.java

public static <T> List<T> nonNullFrom(List<T> list) {
    if (list == null || list.isEmpty()) {
        return Collections.emptyList();
    }//  w ww .j a v a 2 s  .c  om
    return Collections.unmodifiableList(list);
}

From source file:Main.java

static <E> List<E> getList(List<E> orig, Class<E> elemType) {
    if (orig == null || orig.isEmpty()) {
        return Collections.emptyList();
    } else if (orig.size() == 1) {
        return Collections.singletonList(orig.get(0));
    } else {/*  w ww  .  ja va 2 s .c o m*/
        return Collections.unmodifiableList(Arrays.asList(orig.toArray(createArray(elemType, orig.size()))));
    }
}

From source file:Main.java

private static List<Color> buildPalette(int[] raw) {
    Preconditions.checkArgument(raw.length == 256 * 3);
    Color[] pal = new Color[256];
    for (int i = 0; i < pal.length; i++) {
        pal[i] = new Color(raw[i * 3], raw[i * 3 + 1], raw[i * 3 + 2]);
    }/*from ww w  .java2s. com*/
    return Collections.unmodifiableList(Arrays.asList(pal));
}

From source file:com.mmj.app.web.tools.EnumViewTools.java

public static List<RightMenuEnum> getAllRightMenu() {
    if (rightMenuEnumList == null) {
        rightMenuEnumList = new ArrayList<RightMenuEnum>();
        for (RightMenuEnum _enum : RightMenuEnum.values()) {
            rightMenuEnumList.add(_enum);
        }//  w w w .j  a v a 2  s . c  o  m
        rightMenuEnumList = Collections.unmodifiableList(rightMenuEnumList);
    }
    return rightMenuEnumList;
}

From source file:com.github.blindpirate.gogradle.util.CollectionUtils.java

public static <T> List<T> immutableList(T... elements) {
    return Collections.unmodifiableList(asList(elements));
}

From source file:Main.java

/**
 * @param <T>      the element type
 * @param elements the elements//from w  w  w .  j  a  va  2 s  . c  o m
 *
 * @return an <b>UNMODIFIABLE</b> List&lt;T&gt;
 */
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> list(T... elements) {
    return Collections.unmodifiableList(Arrays.asList(elements));
}