Java Code Examples for java.util.IdentityHashMap#clone()

The following examples show how to use java.util.IdentityHashMap#clone() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IdentityHashMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.util.IdentityHashMap#clone()
 */
public void test_clone() {
    // Test for method java.lang.Object java.util.IdentityHashMap.clone()
    IdentityHashMap hm2 = (IdentityHashMap) hm.clone();
    assertTrue("Clone answered equivalent IdentityHashMap", hm2 != hm);
    for (int counter = 0; counter < hmSize; counter++)
        assertTrue("Clone answered unequal IdentityHashMap", hm
                .get(objArray2[counter]) == hm2.get(objArray2[counter]));

    IdentityHashMap map = new IdentityHashMap();
    map.put("key", "value");
    // get the keySet() and values() on the original Map
    Set keys = map.keySet();
    Collection values = map.values();
    assertEquals("values() does not work",
            "value", values.iterator().next());
    assertEquals("keySet() does not work",
            "key", keys.iterator().next());
    AbstractMap map2 = (AbstractMap) map.clone();
    map2.put("key", "value2");
    Collection values2 = map2.values();
    assertTrue("values() is identical", values2 != values);
    // values() and keySet() on the cloned() map should be different
    assertEquals("values() was not cloned",
            "value2", values2.iterator().next());
    map2.clear();
    map2.put("key2", "value3");
    Set key2 = map2.keySet();
    assertTrue("keySet() is identical", key2 != keys);
    assertEquals("keySet() was not cloned",
            "key2", key2.iterator().next());
}
 
Example 2
Source File: IdentityHashMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_clone_scenario1() {
    IdentityHashMap hashMap = new IdentityHashMap();
    assertEquals(0, hashMap.hashCode());
    Object cloneHashMap = hashMap.clone();
    ((IdentityHashMap) cloneHashMap).put("key", "value");
    assertEquals(0, hashMap.hashCode());
    assertTrue(0 != cloneHashMap.hashCode());
}
 
Example 3
Source File: IdentityHashMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_clone_scenario2() {
    IdentityHashMap hashMap = new IdentityHashMap();
    assertEquals(0, hashMap.hashCode());
    Object cloneHashMap = hashMap.clone();
    hashMap.put("key", "value");
    assertEquals(1, hashMap.size());
    assertEquals(0, ((IdentityHashMap) cloneHashMap).size());
    assertEquals("value", hashMap.get("key"));
    assertNull(((IdentityHashMap) cloneHashMap).get("key"));
    assertTrue(0 != hashMap.hashCode());
    assertEquals(0, cloneHashMap.hashCode());
}
 
Example 4
Source File: IdentityHashMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_clone_scenario3() {
    IdentityHashMap hashMap = new IdentityHashMap();
    assertEquals(0, hashMap.hashCode());
    hashMap.put("key", "value");
    Object cloneHashMap = hashMap.clone();
    assertEquals(1, hashMap.size());
    assertEquals(1, ((IdentityHashMap) cloneHashMap).size());
    assertEquals("value", hashMap.get("key"));
    assertEquals("value", ((IdentityHashMap) cloneHashMap).get("key"));
    assertEquals(hashMap.hashCode(), cloneHashMap.hashCode());
}
 
Example 5
Source File: IdentityHashMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_clone_scenario4() {
    IdentityHashMap hashMap = new IdentityHashMap();
    Object cloneHashMap = hashMap.clone();
    assertNull(((IdentityHashMap) cloneHashMap).get((Object) null));
    hashMap.put((Object) null, cloneHashMap);
    assertNull(((IdentityHashMap) cloneHashMap).get((Object) null));
    assertEquals(cloneHashMap, hashMap.get((Object) null));
}
 
Example 6
Source File: IdentityHashMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void test_clone_scenario5() throws Exception {
    IdentityHashMap hashMap = new IdentityHashMap();
    Object cloneHashMap = hashMap.clone();
    assertNull(hashMap.remove((Object) null));
    ((IdentityHashMap) cloneHashMap).put((Object) null, cloneHashMap);
    assertNull(hashMap.remove((Object) null));
    assertEquals(cloneHashMap, ((IdentityHashMap) cloneHashMap)
            .get((Object) null));
}