public void testSum() { AtomicLongMap<Object> map = AtomicLongMap.create(); long sum = 0; for (int i = 0; i < ITERATIONS; i++) { map.put(new Object(), i); sum += i; } assertEquals(ITERATIONS, map.size()); assertEquals(sum, map.sum()); }
public void testPutIfAbsent_zero() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; long value = random.nextInt(MAX_ADDEND); assertEquals(0L, map.get(key)); assertFalse(map.containsKey(key)); assertEquals(0L, map.putIfAbsent(key, value)); assertEquals(value, map.get(key)); assertEquals(value, map.put(key, 0L)); assertEquals(0L, map.get(key)); assertTrue(map.containsKey(key)); assertEquals(0L, map.putIfAbsent(key, value)); assertEquals(value, map.get(key)); }
public void testRemoveZeros() { AtomicLongMap<Object> map = AtomicLongMap.create(); Set<Object> nonZeroKeys = Sets.newHashSet(); for (int i = 0; i < ITERATIONS; i++) { Object key = new Object(); long value = i % 2; map.put(key, value); if (value != 0L) { nonZeroKeys.add(key); } } assertEquals(ITERATIONS, map.size()); assertTrue(map.asMap().containsValue(0L)); map.removeAllZeros(); assertFalse(map.asMap().containsValue(0L)); assertEquals(ITERATIONS / 2, map.size()); assertEquals(nonZeroKeys, map.asMap().keySet()); }
public void testPutIfAbsent() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; long newValue = random.nextInt(MAX_ADDEND); for (int i = 0; i < ITERATIONS; i++) { long before = map.get(key); long result = map.putIfAbsent(key, newValue); long after = map.get(key); assertEquals(before, result); assertEquals(before == 0 ? newValue : before, after); map.remove(key); before = map.get(key); result = map.putIfAbsent(key, newValue); after = map.get(key); assertEquals(0, before); assertEquals(before, result); assertEquals(newValue, after); map.put(key, 0L); before = map.get(key); result = map.putIfAbsent(key, newValue); after = map.get(key); assertEquals(0, before); assertEquals(before, result); assertEquals(newValue, after); newValue += newValue; } assertEquals(1, map.size()); assertTrue(!map.isEmpty()); assertTrue(map.containsKey(key)); }
public void testSerialization() { AtomicLongMap<String> map = AtomicLongMap.create(); map.put("key", 1L); AtomicLongMap<String> reserialized = SerializableTester.reserialize(map); assertEquals(map.asMap(), reserialized.asMap()); }
public void testPut_zero() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; long value = random.nextInt(MAX_ADDEND); assertEquals(0L, map.get(key)); assertFalse(map.containsKey(key)); assertEquals(0L, map.put(key, value)); assertEquals(value, map.get(key)); assertEquals(value, map.put(key, 0L)); assertEquals(0L, map.get(key)); assertTrue(map.containsKey(key)); assertEquals(0L, map.put(key, value)); assertEquals(value, map.get(key)); }
private void handleBroadcastMessage(byte[] message) { Node node = SERIALIZER.decode(message); Node oldNode = nodes.put(node.id(), node); if (oldNode != null && !oldNode.id().equals(node.id())) { post(new NodeDiscoveryEvent(NodeDiscoveryEvent.Type.LEAVE, oldNode)); post(new NodeDiscoveryEvent(NodeDiscoveryEvent.Type.JOIN, node)); } else if (oldNode == null) { post(new NodeDiscoveryEvent(NodeDiscoveryEvent.Type.JOIN, node)); } updateTimes.put(node.id(), System.currentTimeMillis()); }
public void testClear() { AtomicLongMap<Object> map = AtomicLongMap.create(); for (int i = 0; i < ITERATIONS; i++) { map.put(new Object(), i); } assertEquals(ITERATIONS, map.size()); map.clear(); assertEquals(0, map.size()); assertTrue(map.isEmpty()); }
break; case 6: long oldValue = map.put(key, delta); threadSum += delta - oldValue; break;
public void testPut() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; long newValue = random.nextInt(MAX_ADDEND); for (int i = 0; i < ITERATIONS; i++) { long before = map.get(key); long result = map.put(key, newValue); long after = map.get(key); assertEquals(newValue, after); assertEquals(before, result); newValue += newValue; } assertEquals(1, map.size()); assertTrue(!map.isEmpty()); assertTrue(map.containsKey(key)); }
public void testRemove() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; assertEquals(0, map.size()); assertTrue(map.isEmpty()); assertEquals(0L, map.remove(key)); long newValue = random.nextInt(MAX_ADDEND); for (int i = 0; i < ITERATIONS; i++) { map.put(key, newValue); assertTrue(map.containsKey(key)); long before = map.get(key); long result = map.remove(key); long after = map.get(key); assertFalse(map.containsKey(key)); assertEquals(before, result); assertEquals(0L, after); newValue += newValue; } assertEquals(0, map.size()); assertTrue(map.isEmpty()); }
public void testRemoveValue() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; assertEquals(0, map.size()); assertTrue(map.isEmpty()); assertFalse(map.remove(key, 0L)); long newValue = random.nextInt(MAX_ADDEND); for (int i = 0; i < ITERATIONS; i++) { map.put(key, newValue); assertTrue(map.containsKey(key)); long before = map.get(key); assertFalse(map.remove(key, newValue + 1)); assertFalse(map.remove(key, newValue - 1)); assertTrue(map.remove(key, newValue)); long after = map.get(key); assertFalse(map.containsKey(key)); assertEquals(0L, after); newValue += newValue; } assertEquals(0, map.size()); assertTrue(map.isEmpty()); }
public void testRemove_zero() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; assertEquals(0L, map.get(key)); assertFalse(map.containsKey(key)); assertEquals(0L, map.remove(key)); assertEquals(0L, map.get(key)); assertFalse(map.containsKey(key)); assertEquals(0L, map.put(key, 0L)); assertEquals(0L, map.get(key)); assertTrue(map.containsKey(key)); assertEquals(0L, map.remove(key)); assertEquals(0L, map.get(key)); assertFalse(map.containsKey(key)); }
public void testRemoveValue_zero() { AtomicLongMap<String> map = AtomicLongMap.create(); String key = "key"; assertEquals(0L, map.get(key)); assertFalse(map.containsKey(key)); assertFalse(map.remove(key, 0L)); assertEquals(0L, map.get(key)); assertFalse(map.containsKey(key)); assertEquals(0L, map.put(key, 0L)); assertEquals(0L, map.get(key)); assertTrue(map.containsKey(key)); assertTrue(map.remove(key, 0L)); assertEquals(0L, map.get(key)); assertFalse(map.containsKey(key)); }
@Override public void addMetric(Key key, Value val) { String myMetric = key.getColumnVisibility().toString(); String currLG = localityGroups.get(numLG - 1); if (metric.get(currLG).containsKey(myMetric)) { metric.get(currLG).getAndIncrement(myMetric); } else metric.get(currLG).put(myMetric, 1); numEntries.set(numLG - 1, numEntries.get(numLG - 1) + 1); if (!inBlock.contains(myMetric) && blocks.get(currLG).containsKey(myMetric)) { blocks.get(currLG).incrementAndGet(myMetric); inBlock.add(myMetric); } else if (!inBlock.contains(myMetric) && !blocks.get(currLG).containsKey(myMetric)) { blocks.get(currLG).put(myMetric, 1); inBlock.add(myMetric); } }
/** * Copies all of the mappings from the specified map to this map. The effect of this call is * equivalent to that of calling {@code put(k, v)} on this map once for each mapping from key * {@code k} to value {@code v} in the specified map. The behavior of this operation is undefined * if the specified map is modified while the operation is in progress. */ public void putAll(Map<? extends K, ? extends Long> m) { for (Map.Entry<? extends K, ? extends Long> entry : m.entrySet()) { put(entry.getKey(), entry.getValue()); } }
/** * Copies all of the mappings from the specified map to this map. The effect of this call is * equivalent to that of calling {@code put(k, v)} on this map once for each mapping from key * {@code k} to value {@code v} in the specified map. The behavior of this operation is undefined * if the specified map is modified while the operation is in progress. */ public void putAll(Map<? extends K, ? extends Long> m) { for (Map.Entry<? extends K, ? extends Long> entry : m.entrySet()) { put(entry.getKey(), entry.getValue()); } }
/** * Copies all of the mappings from the specified map to this map. The effect of this call is * equivalent to that of calling {@code put(k, v)} on this map once for each mapping from key * {@code k} to value {@code v} in the specified map. The behavior of this operation is undefined * if the specified map is modified while the operation is in progress. */ public void putAll(Map<? extends K, ? extends Long> m) { for (Map.Entry<? extends K, ? extends Long> entry : m.entrySet()) { put(entry.getKey(), entry.getValue()); } }
/** * Copies all of the mappings from the specified map to this map. The effect of this call is * equivalent to that of calling {@code put(k, v)} on this map once for each mapping from key * {@code k} to value {@code v} in the specified map. The behavior of this operation is undefined * if the specified map is modified while the operation is in progress. */ public void putAll(Map<? extends K, ? extends Long> m) { for (Map.Entry<? extends K, ? extends Long> entry : m.entrySet()) { put(entry.getKey(), entry.getValue()); } }
public void testClear() { AtomicLongMap<Object> map = AtomicLongMap.create(); for (int i = 0; i < ITERATIONS; i++) { map.put(new Object(), i); } assertEquals(ITERATIONS, map.size()); map.clear(); assertEquals(0, map.size()); assertTrue(map.isEmpty()); }