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

/**
 * As long as the predicate is happy, grind and append results to result list
 *
 * @param seed - initial input to predicate (and to grinder)
 * @param grinder - grinder(seed) gets appended to result
 * @param predicate - predicate(seed) tells whether to keep grinding
 * @return result list/* w  w w . j a v  a  2 s  . co  m*/
 */
public static <A> List<A> unfold(A seed, Function<A, A> grinder, Function<A, Boolean> predicate) {
    List<A> result = Collections.EMPTY_LIST;
    A counter = seed;
    while (predicate.apply(counter)) {
        result = append(counter, result);
        counter = grinder.apply(counter);
    }
    return Collections.unmodifiableList(result);
}

From source file:Main.java

public static <K, V> Map<K, List<V>> copyNullSafeMultiHashMapReified(Class<V> valueType,
        Map<? extends K, List<?>> map) {
    if (valueType == null)
        throw new NullPointerException("valueType");
    if (map == null)
        throw new NullPointerException("map");

    @SuppressWarnings("unchecked")
    Map<K, List<V>> result = (Map<K, List<V>>) (Map<?, ?>) copyNullSafeMutableHashMap(map);
    for (Map.Entry<K, List<V>> entry : result.entrySet()) {
        List<V> value = entry.getValue();
        ArrayList<V> valueCopy = new ArrayList<V>(value);
        for (V element : valueCopy) {
            valueType.cast(element);/*from  w w w. j  a v a2  s  .c  o m*/
        }

        entry.setValue(Collections.unmodifiableList(valueCopy));
    }
    return result;
}

From source file:Main.java

public static <T> List<T> readList(String root, String filename, Class<T> type) {
    List<T> objects = new ArrayList<>();
    File file = new File(root, filename);
    try {/* w ww  . j  av a  2  s  .c o  m*/
        if (file.exists()) {
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            Object object = ois.readObject();
            if (object instanceof List) {
                for (Object it : (List) object) {
                    objects.add(type.cast(it));
                }
            }
            ois.close();
            return Collections.unmodifiableList(objects);
        }
    } catch (Exception e) {
        Log.e(TAG, String.format("Failed to read [%s]", file), e);
    }
    return Collections.emptyList();
}

From source file:Main.java

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

From source file:Main.java

public static <T> List<T> emptyOrUnmodifiable(final List<T> list) {
    return list == null ? Collections.<T>emptyList() : Collections.unmodifiableList(list);
}

From source file:Main.java

public static <A> List<A> tail(List list) {
    List result = new ArrayList<>(list);
    result.remove(0);//from www  .  j  a  v a 2 s  .  c o  m
    return Collections.unmodifiableList(result);
}

From source file:Main.java

/**
 * Returns the ciphers preferred to use during tests. They may be chosen because they are widely
 * available or because they are fast. There is no requirement that they provide confidentiality
 * or integrity./*w ww  . j  av  a  2s  . c o  m*/
 */
public static List<String> preferredCiphers() {
    String[] ciphers;
    try {
        ciphers = SSLContext.getDefault().getDefaultSSLParameters().getCipherSuites();
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    }
    List<String> ciphersMinusGcm = new ArrayList<String>();
    for (String cipher : ciphers) {
        // The GCM implementation in Java is _very_ slow (~1 MB/s)
        if (cipher.contains("_GCM_")) {
            continue;
        }
        ciphersMinusGcm.add(cipher);
    }
    return Collections.unmodifiableList(ciphersMinusGcm);
}

From source file:Main.java

public static final <T> List<T> collect(Iterator<? extends T> it, Comparator<? super T> cmp) {
    ArrayList<T> r = new ArrayList<T>();
    while (it.hasNext()) {
        T i = it.next();/*from w w w  .j a v a 2  s.  com*/
        int position = Collections.binarySearch(r, i, cmp);
        if (position < 0)
            position = -(position + 1);
        r.add(position, i);
    }
    return Collections.unmodifiableList(r);
}

From source file:Main.java

public static <T> List<T> append(List<T> list, T t) {
    List<T> ts = copy(list);
    ts.add(t);/*w  w w .ja v  a 2  s  .  c om*/
    return Collections.unmodifiableList(ts);
}

From source file:Main.java

/** Returns an immutable list containing {@code elements}. */
public static <T> List<T> immutableList(T... elements) {
    return Collections.unmodifiableList(Arrays.asList(elements.clone()));
}