org.apache.commons.collections4.MapIterator Java Examples

The following examples show how to use org.apache.commons.collections4.MapIterator. 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: AbstractMapIteratorTest.java    From jesterj with Apache License 2.0 6 votes vote down vote up
public void testMapIteratorSetRemoveSet() {
    if (!supportsSetValue() || !supportsRemove()) {
        return;
    }
    final V newValue = addSetValues()[0];
    final MapIterator<K, V> it = makeObject();
    final Map<K, V> confirmed = getConfirmedMap();

    assertEquals(true, it.hasNext());
    final K key = it.next();

    it.setValue(newValue);
    it.remove();
    confirmed.remove(key);
    verify();

    try {
        it.setValue(newValue);
        fail();
    } catch (final IllegalStateException ex) {}
    verify();
}
 
Example #2
Source File: AbstractMapIteratorTest.java    From jesterj with Apache License 2.0 6 votes vote down vote up
public void testMapIteratorRemoveGetKey() {
    if (!supportsRemove()) {
        return;
    }
    final MapIterator<K, V> it = makeObject();
    final Map<K, V> confirmed = getConfirmedMap();

    assertEquals(true, it.hasNext());
    final K key = it.next();

    it.remove();
    confirmed.remove(key);
    verify();

    try {
        it.getKey();
        fail();
    } catch (final IllegalStateException ex) {}
    verify();
}
 
Example #3
Source File: AbstractMapIteratorTest.java    From jesterj with Apache License 2.0 6 votes vote down vote up
public void testMapIteratorRemoveGetValue() {
    if (!supportsRemove()) {
        return;
    }
    final MapIterator<K, V> it = makeObject();
    final Map<K, V> confirmed = getConfirmedMap();

    assertEquals(true, it.hasNext());
    final K key = it.next();

    it.remove();
    confirmed.remove(key);
    verify();

    try {
        it.getValue();
        fail();
    } catch (final IllegalStateException ex) {}
    verify();
}
 
Example #4
Source File: RecommendationServiceImpl.java    From inception with Apache License 2.0 5 votes vote down vote up
public void removePredictions(Recommender aRecommender)
{
    // Remove incoming predictions
    if (incomingPredictions != null) {
        incomingPredictions.removePredictions(aRecommender.getId());
    }

    // Remove active predictions
    if (activePredictions != null) {
        activePredictions.removePredictions(aRecommender.getId());
    }

    // Remove trainedModel
    contexts.remove(aRecommender);

    // Remove from activeRecommenders map.
    // We have to do this, otherwise training and prediction continues for the
    // recommender when a new task is triggered.
    MultiValuedMap<AnnotationLayer, EvaluatedRecommender> newActiveRecommenders = 
            new HashSetValuedHashMap<>();
    MapIterator<AnnotationLayer, EvaluatedRecommender> it = activeRecommenders
            .mapIterator();

    while (it.hasNext()) {
        AnnotationLayer layer = it.next();
        EvaluatedRecommender rec = it.getValue();
        if (!rec.getRecommender().equals(aRecommender)) {
            newActiveRecommenders.put(layer, rec);
        }
    }
    
    setActiveRecommenders(newActiveRecommenders);
}
 
Example #5
Source File: IterateThroughHashMapTest.java    From java_in_examples with Apache License 2.0 5 votes vote down vote up
@Benchmark
public long test9_UsingApacheIterableMap() throws IOException {
    long i = 0;
    MapIterator<Integer, Integer> it = iterableMap.mapIterator();
    while (it.hasNext()) {
        i += it.next() + it.getValue();
    }
    return i;
}
 
Example #6
Source File: AbstractMapIteratorTest.java    From jesterj with Apache License 2.0 5 votes vote down vote up
/**
 * Test that the full list iterator contract is correct.
 */
public void testFullMapIterator() {
    if (!supportsFullIterator()) {
        return;
    }

    final MapIterator<K, V> it = makeObject();
    final Map<K, V> map = getMap();
    assertEquals(true, it.hasNext());

    assertEquals(true, it.hasNext());
    final Set<K> set = new HashSet<>();
    while (it.hasNext()) {
        // getKey
        final K key = it.next();
        assertSame("it.next() should equals getKey()", key, it.getKey());
        assertTrue("Key must be in map",  map.containsKey(key));
        assertTrue("Key must be unique", set.add(key));

        // getValue
        final V value = it.getValue();
        if (!isGetStructuralModify()) {
            assertSame("Value must be mapped to key", map.get(key), value);
        }
        assertTrue("Value must be in map",  map.containsValue(value));

        verify();
    }
}
 
Example #7
Source File: AbstractIterableMapTest.java    From jesterj with Apache License 2.0 4 votes vote down vote up
@Override
public MapIterator<K, V> makeEmptyIterator() {
    resetEmpty();
    return AbstractIterableMapTest.this.getMap().mapIterator();
}
 
Example #8
Source File: AbstractIterableMapTest.java    From jesterj with Apache License 2.0 4 votes vote down vote up
@Override
public MapIterator<K, V> makeObject() {
    resetFull();
    return AbstractIterableMapTest.this.getMap().mapIterator();
}
 
Example #9
Source File: AbstractMapIteratorTest.java    From jesterj with Apache License 2.0 4 votes vote down vote up
public void testMapIteratorSet() {
    if (!supportsFullIterator()) {
        return;
    }

    final V newValue = addSetValues()[0];
    final V newValue2 = addSetValues().length == 1 ? addSetValues()[0] : addSetValues()[1];
    final MapIterator<K, V> it = makeObject();
    final Map<K, V> map = getMap();
    final Map<K, V> confirmed = getConfirmedMap();
    assertEquals(true, it.hasNext());
    final K key = it.next();
    final V value = it.getValue();

    if (!supportsSetValue()) {
        try {
            it.setValue(newValue);
            fail();
        } catch (final UnsupportedOperationException ex) {}
        return;
    }
    final V old = it.setValue(newValue);
    confirmed.put(key, newValue);
    assertSame("Key must not change after setValue", key, it.getKey());
    assertSame("Value must be changed after setValue", newValue, it.getValue());
    assertSame("setValue must return old value", value, old);
    assertEquals("Map must contain key", true, map.containsKey(key));
    // test against confirmed, as map may contain value twice
    assertEquals("Map must not contain old value",
        confirmed.containsValue(old), map.containsValue(old));
    assertEquals("Map must contain new value", true, map.containsValue(newValue));
    verify();

    it.setValue(newValue);  // same value - should be OK
    confirmed.put(key, newValue);
    assertSame("Key must not change after setValue", key, it.getKey());
    assertSame("Value must be changed after setValue", newValue, it.getValue());
    verify();

    it.setValue(newValue2);  // new value
    confirmed.put(key, newValue2);
    assertSame("Key must not change after setValue", key, it.getKey());
    assertSame("Value must be changed after setValue", newValue2, it.getValue());
    verify();
}
 
Example #10
Source File: AbstractMapIteratorTest.java    From jesterj with Apache License 2.0 2 votes vote down vote up
/**
 * Implement this method to return a map iterator over an empty map.
 *
 * @return an empty iterator
 */
@Override
public abstract MapIterator<K, V> makeEmptyIterator();
 
Example #11
Source File: AbstractMapIteratorTest.java    From jesterj with Apache License 2.0 2 votes vote down vote up
/**
 * Implement this method to return a map iterator over a map with elements.
 *
 * @return a full iterator
 */
@Override
public abstract MapIterator<K, V> makeObject();