Example usage for java.util Collections unmodifiableMap

List of usage examples for java.util Collections unmodifiableMap

Introduction

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

Prototype

public static <K, V> Map<K, V> unmodifiableMap(Map<? extends K, ? extends V> m) 

Source Link

Document

Returns an unmodifiable view of the specified map.

Usage

From source file:Main.java

public static <T, K> Map<T, K> newReadOnlyMap(Map<T, K> map) {
    return Collections.unmodifiableMap(new HashMap<>(nullAsEmpty(map)));
}

From source file:Main.java

/**
 * @return an <b>UNMODIFIABLE</b> Map&lt;K, V&gt;
 *///from ww  w .j av a  2 s  .  co  m
public static <K, V> Map<K, V> unmodifiableMap(final Map<? extends K, ? extends V> m) {
    return (m == null) ? Collections.<K, V>emptyMap() : Collections.unmodifiableMap(m);
}

From source file:io.github.carlomicieli.footballdb.starter.utils.MapUtils.java

public static <K, V> Map<K, V> asMap(K k, V v) {
    Map<K, V> m = new HashMap<>();
    m.put(k, v);/*from   ww w .ja v  a  2s.co m*/
    return Collections.unmodifiableMap(m);
}

From source file:Main.java

/**
 * @param entries/* w ww .j  av a2  s  . c o m*/
 *                the <i>final</i> set of entries to add to the newly created
 * <i>unmodifiable</i> map
 * @param <K>     the key type
 * @param <V>     the value type
 *
 * @return an <i>unmodifiable</i> map with all given entries
 */
@SafeVarargs
@SuppressWarnings("varargs")
public static <K, V> Map<K, V> map(Entry<K, V>... entries) {
    return Collections
            .unmodifiableMap(Arrays.stream(entries).collect(Collectors.toMap(Entry::getKey, Entry::getValue)));
}

From source file:Maps.java

public static <K, V> Map<K, V> normalizeUnmodifiable(Map<K, V> map) {
    if (map.size() < 2) {
        return normalize(map);
    } else {//from   w w w .ja  v  a 2 s  . c  o m
        // TODO: implement an UnmodifiableHashMap?
        return Collections.unmodifiableMap(normalize(map));
    }
}

From source file:Main.java

public static Map narrowUnmodifiableMap(Map m) {
    if (m instanceof SortedMap)
        return Collections.unmodifiableSortedMap((SortedMap) m);
    else// w  w  w  . ja v  a 2 s.  c  o m
        return Collections.unmodifiableMap(m);
}

From source file:Main.java

/**
 * Protect the map. It returns a unmodifiable view of the given map. The map collection is not modified.
 *
 * @param map The map to protect.//from  w  ww  .j a  v a 2 s.c om
 * @param <K> The type of key.
 * @param <V> The type of value.
 *
 * @return The protected Map.
 */
public static <K, V> Map<K, V> protect(Map<K, V> map) {
    return Collections.unmodifiableMap(map);
}

From source file:Main.java

/**
 * Wraps a Map with the {@code Collections.unmodifiableMap}, but only once.
 *
 * <p>Checks the {@link Map} passed to ensure that it is not already a {@code Collections.unmodifiableMap}.
 * If the parameter is a null or empty, then it returns {@code Collections.emptyMap}.
 *
 * @param map {@link Map} to wrap with {@code Collections.unmodifiableMap}
 * @param <K> Key type//w  w w.  j a va 2s .com
 * @param <V> Value type
 * @return An unmodifiable Map
 */
public static <K, V> Map<K, V> unmodifiableMap(final Map<K, V> map) {
    if (isNotEmpty(map)) {
        if (!(exampleUnmodifiableMap.getClass().equals(map.getClass()))) {
            return Collections.unmodifiableMap(map);
        }
        return map;
    }
    return Collections.emptyMap();
}

From source file:com.dinochiesa.edgecallouts.util.CalloutUtil.java

public static Map<String, String> genericizeMap(Map properties) {
    // convert an untyped Map to a generic map
    Map<String, String> m = new HashMap<String, String>();
    Iterator iterator = properties.keySet().iterator();
    while (iterator.hasNext()) {
        Object key = iterator.next();
        Object value = properties.get(key);
        if ((key instanceof String) && (value instanceof String)) {
            m.put((String) key, (String) value);
        }// www  .  j  av  a2 s. c  om
    }
    return Collections.unmodifiableMap(m);
}

From source file:com.hp.autonomy.frontend.configuration.ValidationResults.java

private ValidationResults(final Map<String, ValidationResult<?>> map) {
    this.map = Collections.unmodifiableMap(map);
    boolean valid = true;

    for (final ValidationResult<?> result : map.values()) {
        valid = valid && result.isValid();
    }//from   w w  w . ja v  a  2 s . c om

    this.valid = valid;
}