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));
}
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));
}
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());
}
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());
}
}
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;
}
Aggregations