Example usage for java.util Collections unmodifiableSet

List of usage examples for java.util Collections unmodifiableSet

Introduction

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

Prototype

public static <T> Set<T> unmodifiableSet(Set<? extends T> s) 

Source Link

Document

Returns an unmodifiable view of the specified set.

Usage

From source file:Main.java

/**
 * Creates an unmodifiable set filled with the given values.
 * /*  w ww  .  jav a 2 s. c  om*/
 * @param <T>
 *            the set's element type.
 * @param values
 *            the values.
 * @return a newly created set containing the given values.
 */
@SafeVarargs
public static <T> Set<T> createUnmodifiableSet(final T... values) {
    switch (values.length) {
    case 0:
        return Collections.emptySet();
    case 1:
        return Collections.singleton(values[0]);
    default:
        return Collections.unmodifiableSet(createHashSet(values));
    }
}

From source file:Main.java

/**
 * @return an <b>UNMODIFIABLE</b> Set&lt;T&gt;
 *//* w ww . j a v  a  2s. c  om*/
public static <T> Set<T> unmodifiableSet(final Set<? extends T> s) {
    return (s == null) ? Collections.<T>emptySet() : Collections.unmodifiableSet(s);
}

From source file:Main.java

public static Set<String> getProperties() {
    return Collections.unmodifiableSet(properties);
}

From source file:Main.java

public static <T> Set<T> nonNullFrom(Set<T> set) {
    if (set == null || set.isEmpty()) {
        return Collections.emptySet();
    }/* w  ww  . j  a  va2 s .co  m*/
    return Collections.unmodifiableSet(set);
}

From source file:Main.java

/**
 * Returns an unmodifiable set containing the given elements.
 *
 * @param ts the elements from which to create a set
 * @param <T> the type of the element in the set
 * @return an unmodifiable set containing the given elements or {@code null} in case the given element array is
 * {@code null}.//  w  w  w. jav a 2s  . com
 */
public static <T> Set<T> asSet(T... ts) {
    if (ts == null) {
        return null;
    } else if (ts.length == 0) {
        return Collections.emptySet();
    } else {
        Set<T> set = new HashSet<T>(ts.length);
        Collections.addAll(set, ts);
        return Collections.unmodifiableSet(set);
    }
}

From source file:Main.java

private static Set<String> getQueryParameterNames(Uri uri) {
    if (uri.isOpaque()) {
        throw new UnsupportedOperationException("This isn't a hierarchical URI.");
    }/*from  ww  w  .  j  ava 2  s  . co  m*/

    String query = uri.getEncodedQuery();
    if (query == null) {
        return Collections.emptySet();
    }

    Set<String> names = new LinkedHashSet<String>();
    int start = 0;
    do {
        int next = query.indexOf('&', start);
        int end = (next == -1) ? query.length() : next;

        int separator = query.indexOf('=', start);
        if (separator > end || separator == -1) {
            separator = end;
        }

        String name = query.substring(start, separator);
        names.add(Uri.decode(name));

        // Move start to end of name.
        start = end + 1;
    } while (start < query.length());

    return Collections.unmodifiableSet(names);
}

From source file:Main.java

public static Set<String> getQueryParameterNames(Uri uri) {
    if (uri.isOpaque()) {
        throw new UnsupportedOperationException("This isn't a hierarchical URI.");
    }//from w ww  . java  2  s .c  om

    String query = uri.getEncodedQuery();
    if (query == null) {
        return Collections.emptySet();
    }

    Set<String> names = new LinkedHashSet<String>();
    int start = 0;
    do {
        int next = query.indexOf('&', start);
        int end = next == -1 ? query.length() : next;

        int separator = query.indexOf('=', start);
        if (separator > end || separator == -1) {
            separator = end;
        }

        String name = query.substring(start, separator);
        names.add(Uri.decode(name));

        // Move start to end of name.
        start = end + 1;
    } while (start < query.length());

    return Collections.unmodifiableSet(names);
}

From source file:DataSet.java

public DataSet(final String name, final Integer... seasons) {
    this.name = name;
    this.data = Collections.unmodifiableSet(new TreeSet<>(Arrays.asList(seasons)));
}

From source file:Main.java

/**
 * @param <T>      the element type
 * @param elements the elements//  w w w  .j  av a  2 s  .  com
 *
 * @return an <b>UNMODIFIABLE</b> Set&lt;T&gt;
 */
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> Set<T> set(T... elements) {
    return Collections.unmodifiableSet(new HashSet<>(Arrays.asList(elements)));
}

From source file:Main.java

/**
 * Returns an unmodifiable view of the specified {@code mapSet}. This
 *  method allows modules to provide users with "read-only" access to
 * {@link Map}, but also to the value {@link Set}.
 *
 * @param <K>//from  ww w.j  ava  2 s  .  c o m
 *            the class of the map keys
 * @param <V>
 *            the class of the set values
 * @param mapSet
 *            the {@link Map} of {@link Set} for which an unmodifiable view
 *            is to be returned
 * @return an unmodifiable view of the specified map.
 *
 * @see Collections#unmodifiableMap(Map)
 * @see Collections#unmodifiableSet(Set)
 * @since 4.2
 */
@Nonnull
public static <K, V> Map<K, Set<V>> unmodifiableMapSet(@Nullable final Map<K, Set<V>> mapSet) {
    if (mapSet == null) {
        return Collections.emptyMap();
    }

    for (final Map.Entry<K, Set<V>> entry : mapSet.entrySet()) {
        final K key = entry.getKey();
        final Set<V> value = entry.getValue();

        mapSet.replace(key, Collections.unmodifiableSet(value));
    }

    return Collections.unmodifiableMap(mapSet);
}