Java Code Examples for java.util.AbstractMap#put()

The following examples show how to use java.util.AbstractMap#put() . 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: DefaultEventBlockManagementService.java    From eventeum with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void updateLatestBlock(String eventSpecHash, BigInteger blockNumber, String address) {
    AbstractMap<String, BigInteger> events = latestBlocks.get(address);

    if (events == null) {
        events = new ConcurrentHashMap<>();
        latestBlocks.put(address, events);
    }

    final BigInteger currentLatest = events.get(eventSpecHash);


    if (currentLatest == null || blockNumber.compareTo(currentLatest) > 0) {
        events.put(eventSpecHash, blockNumber);
    }
}
 
Example 2
Source File: OperationLogTestBase.java    From pravega with Apache License 2.0 6 votes vote down vote up
void performReadIndexChecks(Collection<OperationWithCompletion> operations, ReadIndex readIndex) throws Exception {
    AbstractMap<Long, Integer> expectedLengths = getExpectedLengths(operations);
    AbstractMap<Long, InputStream> expectedData = getExpectedContents(operations);
    for (Map.Entry<Long, InputStream> e : expectedData.entrySet()) {
        int expectedLength = expectedLengths.getOrDefault(e.getKey(), -1);
        @Cleanup
        ReadResult readResult = readIndex.read(e.getKey(), 0, expectedLength, TIMEOUT);
        int readLength = 0;
        while (readResult.hasNext()) {
            BufferView entry = readResult.next().getContent().join();
            int length = entry.getLength();
            readLength += length;
            int streamSegmentOffset = expectedLengths.getOrDefault(e.getKey(), 0);
            expectedLengths.put(e.getKey(), streamSegmentOffset + length);
            AssertExtensions.assertStreamEquals(String.format("Unexpected data returned from ReadIndex. StreamSegmentId = %d, Offset = %d.",
                    e.getKey(), streamSegmentOffset), e.getValue(), entry.getReader(), length);
        }

        Assert.assertEquals("Not enough bytes were read from the ReadIndex for StreamSegment " + e.getKey(), expectedLength, readLength);
    }
}
 
Example 3
Source File: AbstractMapTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * java.util.AbstractMap#clear()
 */
public void test_clear() {
    // normal clear()
    AbstractMap map = new HashMap();
    map.put(1, 1);
    map.clear();
    assertTrue(map.isEmpty());

    // Special entrySet return a Set with no clear method.
    AbstractMap myMap = new MocAbstractMap();
    try {
        myMap.clear();
        fail("Should throw UnsupportedOprationException");
    } catch (UnsupportedOperationException e) {
        // expected
    }
}
 
Example 4
Source File: AbstractMapTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * java.util.AbstractMap#containsKey(Object)
 */
public void test_containsKey() {
    AbstractMap map = new AMT();

    assertFalse(map.containsKey("k"));
    assertFalse(map.containsKey(null));

    map.put("k", "v");
    map.put("key", null);
    map.put(null, "value");
    map.put(null, null);

    assertTrue(map.containsKey("k"));
    assertTrue(map.containsKey("key"));
    assertTrue(map.containsKey(null));
}
 
Example 5
Source File: AbstractMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.util.AbstractMap#remove(java.lang.Object)
 */
public void test_removeLjava_lang_Object() {
    Object key = new Object();
    Object value = new Object();

    AbstractMap map1 = new HashMap(0);
    map1.put("key", value);
    assertSame("HashMap(0)", map1.remove("key"), value);

    AbstractMap map4 = new IdentityHashMap(1);
    map4.put(key, value);
    assertSame("IdentityHashMap", map4.remove(key), value);

    AbstractMap map5 = new LinkedHashMap(122);
    map5.put(key, value);
    assertSame("LinkedHashMap", map5.remove(key), value);

    AbstractMap map6 = new TreeMap(new Comparator() {
        // Bogus comparator
        public int compare(Object object1, Object object2) {
            return 0;
        }
    });
    map6.put(key, value);
    assertSame("TreeMap", map6.remove(key), value);

    AbstractMap map7 = new WeakHashMap();
    map7.put(key, value);
    assertSame("WeakHashMap", map7.remove(key), value);

    AbstractMap aSpecialMap = new MyMap();
    aSpecialMap.put(specialKey, specialValue);
    Object valueOut = aSpecialMap.remove(specialKey);
    assertSame("MyMap", valueOut, specialValue);
}
 
Example 6
Source File: AbstractMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.util.AbstractMap#containsValue(Object)
 */
public void test_containValue() {
    AbstractMap map = new AMT();

    assertFalse(map.containsValue("v"));
    assertFalse(map.containsValue(null));

    map.put("k", "v");
    map.put("key", null);
    map.put(null, "value");

    assertTrue(map.containsValue("v"));
    assertTrue(map.containsValue("value"));
    assertTrue(map.containsValue(null));
}
 
Example 7
Source File: AbstractMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.util.AbstractMap#get(Object)
 */
public void test_get() {
    AbstractMap map = new AMT();
    assertNull(map.get("key"));
    assertNull(map.get(null));

    map.put("k", "v");
    map.put("key", null);
    map.put(null, "value");

    assertEquals("v", map.get("k"));
    assertNull(map.get("key"));
    assertEquals("value", map.get(null));
}
 
Example 8
Source File: TreeMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.util.TreeMap#clone()
 */
public void test_clone() {
    // Test for method java.lang.Object java.util.TreeMap.clone()
    TreeMap clonedMap = (TreeMap) tm.clone();
    assertTrue("Cloned map does not equal the original map", clonedMap
            .equals(tm));
    assertTrue("Cloned map is the same reference as the original map",
            clonedMap != tm);
    for (Object element : objArray) {
        assertTrue("Cloned map contains incorrect elements", clonedMap
                .get(element.toString()) == tm.get(element.toString()));
    }

    TreeMap map = new TreeMap();
    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 9
Source File: LinkedHashMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.util.LinkedHashMap#clone()
 */
public void test_clone() {
    // Test for method java.lang.Object java.util.LinkedHashMap.clone()
    LinkedHashMap hm2 = (LinkedHashMap) hm.clone();
    assertTrue("Clone answered equivalent LinkedHashMap", hm2 != hm);
    for (int counter = 0; counter < hmSize; counter++)
        assertTrue("Clone answered unequal LinkedHashMap", hm
                .get(objArray2[counter]) == hm2.get(objArray2[counter]));

    LinkedHashMap map = new LinkedHashMap();
    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 10
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 11
Source File: MapKeyValueCustomXstreamConverter.java    From gatf with Apache License 2.0 5 votes vote down vote up
public Object unmarshal(final HierarchicalStreamReader reader, final UnmarshallingContext context)
{
    final AbstractMap<String, String> map = new HashMap<String, String>();
    while (reader.hasMoreChildren())
    {
        reader.moveDown();
        map.put(reader.getNodeName(), reader.getValue());
        reader.moveUp();
    }
    return map;
}
 
Example 12
Source File: HashMapTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * java.util.HashMap#clone()
 */
public void test_clone() {
    // Test for method java.lang.Object java.util.HashMap.clone()
    HashMap hm2 = (HashMap) hm.clone();
    assertTrue("Clone answered equivalent HashMap", hm2 != hm);
    for (int counter = 0; counter < hmSize; counter++)
        assertTrue("Clone answered unequal HashMap", hm
                .get(objArray2[counter]) == hm2.get(objArray2[counter]));

    HashMap map = new HashMap();
    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());

    // regresion test for HARMONY-4603
    HashMap hashmap = new HashMap();
    MockClonable mock = new MockClonable(1);
    hashmap.put(1, mock);
    assertEquals(1, ((MockClonable) hashmap.get(1)).i);
    HashMap hm3 = (HashMap) hashmap.clone();
    assertEquals(1, ((MockClonable) hm3.get(1)).i);
    mock.i = 0;
    assertEquals(0, ((MockClonable) hashmap.get(1)).i);
    assertEquals(0, ((MockClonable) hm3.get(1)).i);
}