Example usage for java.util Map getClass

List of usage examples for java.util Map getClass

Introduction

In this page you can find the example usage for java.util Map getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:cloudRessourceBase.DataUtil.java

public static Map convertJSONToMap(String json) {
    Gson gson = new Gson();
    Map<String, String> map = new HashMap<String, String>();
    map = (Map<String, String>) gson.fromJson(json, map.getClass());
    return map;//w  w w.j a va2 s.c o  m
}

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/*from ww  w  .j  a v  a  2 s  .  c o m*/
 * @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:therian.operator.immutablecheck.DefaultImmutableChecker.java

private static void addTypeTo(final Set<Class<?>> target, final Map<?, ?> map) {
    addImmutableTypeTo(target, map.getClass());
    addTypeTo(target, map.keySet());//  w  w  w.j a  v a2  s .co m
    addTypeTo(target, map.values());
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <E> Map<String, E> union(Map<String, E> mapA, Map<String, E> mapB) {
    Map<String, E> mapC = null;
    try {// ww w. j a va2  s  .c  o  m
        mapC = mapA.getClass().newInstance();
    } catch (Exception e) {
        e.printStackTrace();
        return mapC;
    }
    Iterator<String> akeys = mapA.keySet().iterator();
    Iterator<String> bkeys = mapB.keySet().iterator();
    while (akeys.hasNext() || bkeys.hasNext()) {
        if (akeys.hasNext()) {
            String ak = akeys.next();
            mapC.put(ak, mapA.get(ak));
        }
        if (bkeys.hasNext()) {
            String bk = bkeys.next();
            E e = mapB.get(bk);
            if (!mapA.containsKey(bk) || !mapA.get(bk).equals(e)) {
                mapC.put(bk, mapA.get(e));
            }
        }
    }
    return mapC;
}

From source file:Maps.java

public static <K, V> Map<K, V> normalize(Map<K, V> map) {
    switch (map.size()) {
    case 0://from ww  w .  j  a v a 2 s. c o m
        return create();
    case 1: {
        if (map.getClass() == SINGLETON_MAP_CLASS) {
            return map;
        }
        K key = map.keySet().iterator().next();
        return create(key, map.get(key));
    }
    default:
        if (map.getClass() == MULTI_MAP_CLASS) {
            return map;
        }
        return new HashMap<K, V>(map);
    }
}

From source file:org.apache.gobblin.utils.HttpUtils.java

/**
 * Convert a json encoded string to a Map
 *
 * @param jsonString json string// www. java 2s .c o  m
 * @return the Map encoded in the string
 */
public static Map<String, Object> toMap(String jsonString) {
    Map<String, Object> map = new HashMap<>();
    return GSON.fromJson(jsonString, map.getClass());
}

From source file:org.geoserver.catalog.impl.ModificationProxyCloner.java

/**
 * Shallow or deep copies the provided collection
 * //from w ww  .j  a v  a  2  s. c om
 * @param <K>
 * @param <V>
 * 
 * @param source
 * @param deepCopy If true, a deep copy will be done, otherwise the cloned collection will
 *        contain the exact same objects as the source
 * @return
 * @throws InstantiationException
 * @throws IllegalAccessException
 */
public static <K, V> Map<K, V> cloneMap(Map<K, V> source, boolean deepCopy)
        throws InstantiationException, IllegalAccessException {
    if (source == null) {
        // nothing to copy
        return null;
    }
    Map<K, V> copy = source.getClass().newInstance();
    if (deepCopy) {
        for (Map.Entry<K, V> entry : source.entrySet()) {
            K keyCopy = clone(entry.getKey());
            V valueCopy = clone(entry.getValue());
            copy.put(keyCopy, valueCopy);
        }
    } else {
        copy.putAll(source);
    }

    return copy;
}

From source file:Main.java

/**
 * Generate a usually multiline String reporting a Map's entries.
 *
 * @param theLabel See related function.
 * @param map See related function./*from  w w  w .  j  av a2 s  .  c o  m*/
 * @param maxNumberReported See related function.
 * @return See related function.
 * @see #format(String, java.util.Collection, int)
 */
public static String format(String theLabel, Map<?, ?> map, int maxNumberReported) {
    String label = theLabel;
    if (map == null) {
        if (label == null) {
            label = "";
        }
        return label + " null Map";
    }
    return format(label, map.entrySet(), maxNumberReported, map.getClass().getSimpleName());
}

From source file:com.igormaznitsa.upom.UPomModel.java

private static Map cloneMap(final Map map) throws Exception {
    final Class mapClass = map.getClass();
    final Constructor constructor = mapClass.getConstructor(Map.class);
    return (Map) constructor.newInstance(map);
}

From source file:com.impetus.kundera.utils.KunderaCoreUtils.java

/**
 * @param puProperty// ww w . ja  v  a  2 s .c  o  m
 * @return
 */
private static Map<String, Object> fetchPropertyMap(Map<String, Object> puProperty) {
    if (puProperty.getClass().isAssignableFrom(Map.class) || puProperty instanceof Map) {
        return puProperty;
    } else {
        throw new InvalidConfigurationException(
                "For cross data store persistence, please specify as: Map {pu,Map of properties}");
    }
}