Search in sources :

Example 51 with IdentityHashMap

use of java.util.IdentityHashMap in project robovm by robovm.

the class IdentityHashMapTest method test_equalsLjava_lang_Object.

/**
     * java.util.IdentityHashMap#equals(java.lang.Object)
     */
public void test_equalsLjava_lang_Object() {
    IdentityHashMap mapOne = new IdentityHashMap();
    IdentityHashMap mapTwo = new IdentityHashMap();
    IdentityHashMap mapThree = new IdentityHashMap();
    IdentityHashMap mapFour = new IdentityHashMap();
    String one = "one";
    // use the new operator to ensure a
    String alsoOne = new String(one);
    // new reference is constructed
    String two = "two";
    // use the new operator to ensure a
    String alsoTwo = new String(two);
    // new reference is constructed
    mapOne.put(one, two);
    mapFour.put(one, two);
    // these two are not equal to the above two
    mapTwo.put(alsoOne, two);
    mapThree.put(one, alsoTwo);
    assertEquals("failure of equality of IdentityHashMaps", mapOne, mapFour);
    assertTrue("failure of non-equality of IdentityHashMaps one and two", !mapOne.equals(mapTwo));
    assertTrue("failure of non-equality of IdentityHashMaps one and three", !mapOne.equals(mapThree));
    assertTrue("failure of non-equality of IdentityHashMaps two and three", !mapTwo.equals(mapThree));
    HashMap hashMapTwo = new HashMap();
    HashMap hashMapThree = new HashMap();
    hashMapTwo.put(alsoOne, two);
    hashMapThree.put(one, alsoTwo);
    assertTrue("failure of non-equality of IdentityHashMaps one and Hashmap two", !mapOne.equals(hashMapTwo));
    assertTrue("failure of non-equality of IdentityHashMaps one and Hashmap three", !mapOne.equals(hashMapThree));
}
Also used : IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) IdentityHashMap(java.util.IdentityHashMap)

Example 52 with IdentityHashMap

use of java.util.IdentityHashMap in project robovm by robovm.

the class IdentityHashMapTest method test_keySet.

/**
     * java.util.IdentityHashMap#keySet()
     */
public void test_keySet() {
    // Test for method java.util.Set java.util.IdentityHashMap.keySet()
    Set s = hm.keySet();
    assertTrue("Returned set of incorrect size()", s.size() == hm.size());
    for (int i = 0; i < objArray.length; i++) {
        assertTrue("Returned set does not contain all keys", s.contains(objArray2[i]));
    }
    IdentityHashMap m = new IdentityHashMap();
    m.put(null, "test");
    assertTrue("Failed with null key", m.keySet().contains(null));
    assertNull("Failed with null key", m.keySet().iterator().next());
    Map map = new IdentityHashMap(101);
    map.put(new Integer(1), "1");
    map.put(new Integer(102), "102");
    map.put(new Integer(203), "203");
    Iterator it = map.keySet().iterator();
    Integer remove1 = (Integer) it.next();
    it.hasNext();
    it.remove();
    Integer remove2 = (Integer) it.next();
    it.remove();
    ArrayList list = new ArrayList(Arrays.asList(new Integer[] { new Integer(1), new Integer(102), new Integer(203) }));
    list.remove(remove1);
    list.remove(remove2);
    assertTrue("Wrong result", it.next().equals(list.get(0)));
    assertEquals("Wrong size", 1, map.size());
    assertTrue("Wrong contents", map.keySet().iterator().next().equals(list.get(0)));
    Map map2 = new IdentityHashMap(101);
    map2.put(new Integer(1), "1");
    map2.put(new Integer(4), "4");
    Iterator it2 = map2.keySet().iterator();
    Integer remove3 = (Integer) it2.next();
    Integer next;
    if (remove3.intValue() == 1)
        next = new Integer(4);
    else
        next = new Integer(1);
    it2.hasNext();
    it2.remove();
    assertTrue("Wrong result 2", it2.next().equals(next));
    assertEquals("Wrong size 2", 1, map2.size());
    assertTrue("Wrong contents 2", map2.keySet().iterator().next().equals(next));
}
Also used : Set(java.util.Set) IdentityHashMap(java.util.IdentityHashMap) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 53 with IdentityHashMap

use of java.util.IdentityHashMap in project robovm by robovm.

the class AbstractMapTest method test_values.

/**
     * java.util.AbstractMap#values()
     */
public void test_values() {
    AbstractMap map1 = new HashMap(0);
    assertSame("HashMap(0)", map1.values(), map1.values());
    AbstractMap map2 = new HashMap(10);
    assertSame("HashMap(10)", map2.values(), map2.values());
    Map map3 = Collections.EMPTY_MAP;
    assertSame("EMPTY_MAP", map3.values(), map3.values());
    AbstractMap map4 = new IdentityHashMap(1);
    assertSame("IdentityHashMap", map4.values(), map4.values());
    AbstractMap map5 = new LinkedHashMap(122);
    assertSame("IdentityHashMap", map5.values(), map5.values());
    AbstractMap map6 = new TreeMap();
    assertSame("TreeMap", map6.values(), map6.values());
    AbstractMap map7 = new WeakHashMap();
    assertSame("WeakHashMap", map7.values(), map7.values());
}
Also used : AbstractMap(java.util.AbstractMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) WeakHashMap(java.util.WeakHashMap) IdentityHashMap(java.util.IdentityHashMap) TreeMap(java.util.TreeMap) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AbstractMap(java.util.AbstractMap) TreeMap(java.util.TreeMap) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) LinkedHashMap(java.util.LinkedHashMap) WeakHashMap(java.util.WeakHashMap)

Example 54 with IdentityHashMap

use of java.util.IdentityHashMap in project dubbo by alibaba.

the class JavaBeanSerializeUtil method deserializeInternal.

private static void deserializeInternal(Object result, JavaBeanDescriptor beanDescriptor, ClassLoader loader, IdentityHashMap<JavaBeanDescriptor, Object> cache) {
    if (beanDescriptor.isEnumType() || beanDescriptor.isClassType() || beanDescriptor.isPrimitiveType()) {
        return;
    }
    if (beanDescriptor.isArrayType()) {
        int index = 0;
        for (Map.Entry<Object, Object> entry : beanDescriptor) {
            Object item = entry.getValue();
            if (item instanceof JavaBeanDescriptor) {
                JavaBeanDescriptor itemDescriptor = (JavaBeanDescriptor) entry.getValue();
                item = instantiateForDeserialize(itemDescriptor, loader, cache);
                deserializeInternal(item, itemDescriptor, loader, cache);
            }
            Array.set(result, index++, item);
        }
    } else if (beanDescriptor.isCollectionType()) {
        Collection collection = (Collection) result;
        for (Map.Entry<Object, Object> entry : beanDescriptor) {
            Object item = entry.getValue();
            if (item instanceof JavaBeanDescriptor) {
                JavaBeanDescriptor itemDescriptor = (JavaBeanDescriptor) entry.getValue();
                item = instantiateForDeserialize(itemDescriptor, loader, cache);
                deserializeInternal(item, itemDescriptor, loader, cache);
            }
            collection.add(item);
        }
    } else if (beanDescriptor.isMapType()) {
        Map map = (Map) result;
        for (Map.Entry<Object, Object> entry : beanDescriptor) {
            Object key = entry.getKey();
            Object value = entry.getValue();
            if (key != null && key instanceof JavaBeanDescriptor) {
                JavaBeanDescriptor keyDescriptor = (JavaBeanDescriptor) entry.getKey();
                key = instantiateForDeserialize(keyDescriptor, loader, cache);
                deserializeInternal(key, keyDescriptor, loader, cache);
            }
            if (value != null && value instanceof JavaBeanDescriptor) {
                JavaBeanDescriptor valueDescriptor = (JavaBeanDescriptor) entry.getValue();
                value = instantiateForDeserialize(valueDescriptor, loader, cache);
                deserializeInternal(value, valueDescriptor, loader, cache);
            }
            map.put(key, value);
        }
    } else if (beanDescriptor.isBeanType()) {
        for (Map.Entry<Object, Object> entry : beanDescriptor) {
            String property = entry.getKey().toString();
            Object value = entry.getValue();
            if (value == null) {
                continue;
            }
            if (value instanceof JavaBeanDescriptor) {
                JavaBeanDescriptor valueDescriptor = (JavaBeanDescriptor) entry.getValue();
                value = instantiateForDeserialize(valueDescriptor, loader, cache);
                deserializeInternal(value, valueDescriptor, loader, cache);
            }
            Method method = getSetterMethod(result.getClass(), property, value.getClass());
            boolean setByMethod = false;
            try {
                if (method != null) {
                    method.invoke(result, value);
                    setByMethod = true;
                }
            } catch (Exception e) {
                LogHelper.warn(logger, "Failed to set property through method " + method, e);
            }
            if (!setByMethod) {
                try {
                    Field field = result.getClass().getField(property);
                    if (field != null) {
                        field.set(result, value);
                    }
                } catch (NoSuchFieldException e1) {
                    LogHelper.warn(logger, "Failed to set field value", e1);
                } catch (IllegalAccessException e1) {
                    LogHelper.warn(logger, "Failed to set field value", e1);
                }
            }
        }
    } else {
        throw new IllegalArgumentException("Unsupported type " + beanDescriptor.getClassName() + ":" + beanDescriptor.getType());
    }
}
Also used : Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field) Collection(java.util.Collection) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 55 with IdentityHashMap

use of java.util.IdentityHashMap in project dubbo by alibaba.

the class PojoUtils method createMap.

private static Map createMap(Map src) {
    Class<? extends Map> cl = src.getClass();
    Map result = null;
    if (HashMap.class == cl) {
        result = new HashMap();
    } else if (Hashtable.class == cl) {
        result = new Hashtable();
    } else if (IdentityHashMap.class == cl) {
        result = new IdentityHashMap();
    } else if (LinkedHashMap.class == cl) {
        result = new LinkedHashMap();
    } else if (Properties.class == cl) {
        result = new Properties();
    } else if (TreeMap.class == cl) {
        result = new TreeMap();
    } else if (WeakHashMap.class == cl) {
        return new WeakHashMap();
    } else if (ConcurrentHashMap.class == cl) {
        result = new ConcurrentHashMap();
    } else if (ConcurrentSkipListMap.class == cl) {
        result = new ConcurrentSkipListMap();
    } else {
        try {
            result = cl.newInstance();
        } catch (Exception e) {
        /* ignore */
        }
        if (result == null) {
            try {
                Constructor<?> constructor = cl.getConstructor(Map.class);
                result = (Map) constructor.newInstance(Collections.EMPTY_MAP);
            } catch (Exception e) {
            /* ignore */
            }
        }
    }
    if (result == null) {
        result = new HashMap<Object, Object>();
    }
    return result;
}
Also used : ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) WeakHashMap(java.util.WeakHashMap) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Hashtable(java.util.Hashtable) IdentityHashMap(java.util.IdentityHashMap) Properties(java.util.Properties) TreeMap(java.util.TreeMap) InvocationTargetException(java.lang.reflect.InvocationTargetException) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) IdentityHashMap(java.util.IdentityHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) TreeMap(java.util.TreeMap) WeakHashMap(java.util.WeakHashMap)

Aggregations

IdentityHashMap (java.util.IdentityHashMap)142 Map (java.util.Map)44 HashMap (java.util.HashMap)42 ArrayList (java.util.ArrayList)31 HashSet (java.util.HashSet)20 LinkedHashMap (java.util.LinkedHashMap)18 Collection (java.util.Collection)16 Set (java.util.Set)16 TreeMap (java.util.TreeMap)16 Iterator (java.util.Iterator)14 AbstractMap (java.util.AbstractMap)13 List (java.util.List)11 Test (org.junit.Test)11 DependencyNode (org.sonatype.aether.graph.DependencyNode)10 WeakHashMap (java.util.WeakHashMap)8 LinkedList (java.util.LinkedList)7 TreeSet (java.util.TreeSet)7 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)7 Tree (edu.stanford.nlp.trees.Tree)6 IOException (java.io.IOException)6