Example usage for java.util Collections unmodifiableCollection

List of usage examples for java.util Collections unmodifiableCollection

Introduction

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

Prototype

public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) 

Source Link

Document

Returns an unmodifiable view of the specified collection.

Usage

From source file:Main.java

public static void main(String args[]) {
    List<Character> list = new ArrayList<Character>();

    list.add('X');

    System.out.println("Element added to list: " + list.get(0));

    Collection<Character> immutableCol = Collections.unmodifiableCollection(list);

    immutableCol.add('Y');
}

From source file:Main.java

public static void main(String[] args) {
    // create array list
    List<Character> list = new ArrayList<Character>();

    // populate the list
    list.add('X');
    list.add('Y');

    System.out.println("Initial list: " + list);

    Collection<Character> immutablelist = Collections.unmodifiableCollection(list);

    // try to modify the list
    immutablelist.add('Z');
}

From source file:Main.java

public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> collection) {
    if (collection == null) {
        return Collections.emptyList();
    } else {/*www.jav a2 s  .co  m*/
        return Collections.unmodifiableCollection(collection);
    }
}

From source file:Main.java

/**
 * Converts the specified Array to an unmodifiable Collection containing the same elements.
 *
 * @param source Array of Objects to be converted to an unmodifiable Collection.
 * @return Collection of Objects containing the elements of the specified Array.
 *//*w  ww.j a  v  a2s .  c o  m*/
public static <E> Collection<E> unmodifiableCollection(E... source) {
    return Collections.unmodifiableCollection(Arrays.asList(source));
}

From source file:Main.java

public static <T> Collection<T> unmodifiableCollection(T... ts) {
    return Collections.unmodifiableCollection(Arrays.asList(ts));
}

From source file:Main.java

/**
 * Protect the collection. It returns a unmodifiable view of the given collection. The given collection is not
 * modified./*from  w ww  . java  2 s.c om*/
 *
 * @param collection The collection to protect.
 * @param <T>        The type of value.
 *
 * @return The protected Collection.
 */
public static <T> Collection<T> protect(Collection<T> collection) {
    return Collections.unmodifiableCollection(collection);
}

From source file:Main.java

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

From source file:com.technophobia.substeps.runner.syntax.FileUtils.java

public static Collection<File> getFiles(final File fFile, final String extension) {

    //  the parameter might be a dir or a single file
    final Collection<File> files;
    if (fFile.isFile()) {
        List<File> fileList = new ArrayList<>();
        fileList.add(fFile);//w w  w.ja  v  a 2 s  .  com
        files = Collections.unmodifiableCollection(fileList);
    } else {
        files = org.apache.commons.io.FileUtils.listFiles(fFile, new String[] { extension }, true);
    }

    return files;
}

From source file:ch.rasc.s4ws.snake.SnakeTimer.java

protected static Collection<Snake> getSnakes() {
    return Collections.unmodifiableCollection(snakes.values());
}

From source file:com.gmind7.bakery.websocket.SnakeTimer.java

public static Collection<Snake> getSnakes() {
    return Collections.unmodifiableCollection(snakes.values());
}