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

The following examples show how to use java.util.IdentityHashMap#remove() . 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 6 votes vote down vote up
/**
 * java.util.IdentityHashMap#remove(java.lang.Object)
 * java.util.IdentityHashMap#keySet()
 */
public void test_remove() {
    IdentityHashMap map = new IdentityHashMap();
    map.put(null, null);
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.remove("key1");

    assertTrue("Did not remove key1", !map.containsKey("key1"));
    assertTrue("Did not remove the value for key1", !map
            .containsValue("value1"));

    assertTrue("Modified key2", map.get("key2") != null
            && map.get("key2") == "value2");
    assertNull("Modified null entry", map.get(null));
}
 
Example 2
Source File: ModuleReplaceAll.java    From symja_android_library with GNU General Public License v3.0 6 votes vote down vote up
private IdentityHashMap<ISymbol, IExpr> putSingleVariable(ISymbol symbol, IdentityHashMap<ISymbol, IExpr> variables,
		final String varAppend, boolean isFunction) {
	IExpr temp = fModuleVariables.get(symbol);
	if (isFunction) {
		if (variables == null) {
			variables = (IdentityHashMap<ISymbol, IExpr>) fModuleVariables.clone();
		}
		variables.put(symbol, F.Dummy(symbol.toString() + varAppend));
	} else if (temp != null) {
		if (variables == null) {
			variables = (IdentityHashMap<ISymbol, IExpr>) fModuleVariables.clone();
		}
		variables.remove(symbol);
		if (!temp.isPresent()) {
			variables.put(symbol, F.Dummy(symbol.toString() + varAppend));
		}
	}
	return variables;
}
 
Example 3
Source File: GridToStringBuilder.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Print value with length limitation.
 *
 * @param buf buffer to print to.
 * @param cls value class.
 * @param val value to print.
 */
private static void toString(SBLimitedLength buf, Class<?> cls, Object val) {
    if (val == null) {
        buf.a("null");

        return;
    }

    if (cls == null)
        cls = val.getClass();

    if (cls.isPrimitive()) {
        buf.a(val);

        return;
    }

    IdentityHashMap<Object, EntryReference> svdObjs = savedObjects.get();

    if (handleRecursion(buf, val, cls, svdObjs))
        return;

    svdObjs.put(val, new EntryReference(buf.length()));

    try {
        if (cls.isArray())
            addArray(buf, cls, val);
        else if (val instanceof Collection)
            addCollection(buf, (Collection) val);
        else if (val instanceof Map)
            addMap(buf, (Map<?, ?>) val);
        else
            buf.a(val);
    }
    finally {
        svdObjs.remove(val);
    }
}
 
Example 4
Source File: GrpcUnsafeBufferUtil.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Releases the {@link ByteBuf} backing the specified {@link Message}.
 */
public static void releaseBuffer(Object message, RequestContext ctx) {
    final IdentityHashMap<Object, ByteBuf> buffers = ctx.attr(BUFFERS);
    checkState(buffers != null,
               "Releasing buffer even though storeBuffer has not been called.");
    final ByteBuf removed = buffers.remove(message);
    if (removed == null) {
        throw new IllegalArgumentException("The provided message does not have a stored buffer.");
    }
    removed.release();
}
 
Example 5
Source File: IdentityHashMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.util.IdentityHashMap#remove(java.lang.Object)
 */
public void test_removeLjava_lang_Object() {
    // Test for method java.lang.Object
    // java.util.IdentityHashMap.remove(java.lang.Object)
    int size = hm.size();
    Integer x = ((Integer) hm.remove(objArray2[9]));
    assertTrue("Remove returned incorrect value", x.equals(new Integer(9)));
    assertNull("Failed to remove given key", hm.get(objArray2[9]));
    assertTrue("Failed to decrement size", hm.size() == (size - 1));
    assertNull("Remove of non-existent key returned non-null", hm
            .remove("LCLCLC"));

    IdentityHashMap m = new IdentityHashMap();
    m.put(null, "test");
    assertNull("Failed with same hash as null",
            m.remove(objArray[0]));
    assertEquals("Failed with null key", "test", m.remove(null));

    // Regression for HARMONY-37
    IdentityHashMap<String, String> hashMap = new IdentityHashMap<String, String>();
    hashMap.remove("absent");
    assertEquals("Assert 0: Size is incorrect", 0, hashMap.size());

    hashMap.put("key", "value");
    hashMap.remove("key");
    assertEquals("Assert 1: After removing non-null element size is incorrect", 0, hashMap.size());

    hashMap.put(null, null);
    assertEquals("Assert 2: adding literal null failed", 1, hashMap.size());
    hashMap.remove(null);
    assertEquals("Assert 3: After removing null element size is incorrect", 0, hashMap.size());
}
 
Example 6
Source File: IdentityHashMapTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * java.util.IdentityHashMap#containsKey(java.lang.Object)
 * java.util.IdentityHashMap#containsValue(java.lang.Object)
 * java.util.IdentityHashMap#put(java.lang.Object, java.lang.Object)
 * java.util.IdentityHashMap#get(java.lang.Object)
 */
public void test_null_Keys_and_Values() {
    // tests with null keys and values
    IdentityHashMap map = new IdentityHashMap();
    Object result;

    // null key and null value
    result = map.put(null, null);
    assertTrue("testA can not find null key", map.containsKey(null));
    assertTrue("testA can not find null value", map.containsValue(null));
    assertNull("testA can not get null value for null key",
            map.get(null));
    assertNull("testA put returned wrong value", result);

    // null value
    String value = "a value";
    result = map.put(null, value);
    assertTrue("testB can not find null key", map.containsKey(null));
    assertTrue("testB can not find a value with null key", map
            .containsValue(value));
    assertTrue("testB can not get value for null key",
            map.get(null) == value);
    assertNull("testB put returned wrong value", result);

    // a null key
    String key = "a key";
    result = map.put(key, null);
    assertTrue("testC can not find a key with null value", map
            .containsKey(key));
    assertTrue("testC can not find null value", map.containsValue(null));
    assertNull("testC can not get null value for key", map.get(key));
    assertNull("testC put returned wrong value", result);

    // another null key
    String anothervalue = "another value";
    result = map.put(null, anothervalue);
    assertTrue("testD can not find null key", map.containsKey(null));
    assertTrue("testD can not find a value with null key", map
            .containsValue(anothervalue));
    assertTrue("testD can not get value for null key",
            map.get(null) == anothervalue);
    assertTrue("testD put returned wrong value", result == value);

    // remove a null key
    result = map.remove(null);
    assertTrue("testE remove returned wrong value", result == anothervalue);
    assertTrue("testE should not find null key", !map.containsKey(null));
    assertTrue("testE should not find a value with null key", !map
            .containsValue(anothervalue));
    assertNull("testE should not get value for null key",
            map.get(null));
}