public void testGet_concurrent() { assertTrue(ArbitraryInstances.get(BlockingDeque.class).isEmpty()); assertTrue(ArbitraryInstances.get(BlockingQueue.class).isEmpty()); assertTrue(ArbitraryInstances.get(DelayQueue.class).isEmpty()); assertTrue(ArbitraryInstances.get(SynchronousQueue.class).isEmpty()); assertTrue(ArbitraryInstances.get(PriorityBlockingQueue.class).isEmpty()); assertTrue(ArbitraryInstances.get(ConcurrentMap.class).isEmpty()); assertTrue(ArbitraryInstances.get(ConcurrentNavigableMap.class).isEmpty()); ArbitraryInstances.get(Executor.class).execute(ArbitraryInstances.get(Runnable.class)); assertNotNull(ArbitraryInstances.get(ThreadFactory.class)); assertFreshInstanceReturned( BlockingQueue.class, BlockingDeque.class, PriorityBlockingQueue.class, DelayQueue.class, SynchronousQueue.class, ConcurrentMap.class, ConcurrentNavigableMap.class, AtomicReference.class, AtomicBoolean.class, AtomicInteger.class, AtomicLong.class, AtomicDouble.class); }
/** * @param tableName * @return Map of cached locations for passed <code>tableName</code> */ private ConcurrentNavigableMap<byte[], RegionLocations> getTableLocations(final TableName tableName) { // find the map of cached locations for this table ConcurrentNavigableMap<byte[], RegionLocations> result; result = this.cachedRegionLocations.get(tableName); // if tableLocations for this table isn't built yet, make one if (result == null) { result = new CopyOnWriteArrayMap<>(Bytes.BYTES_COMPARATOR); ConcurrentNavigableMap<byte[], RegionLocations> old = this.cachedRegionLocations.putIfAbsent(tableName, result); if (old != null) { return old; } } return result; }
/** * pollLastEntry returns entries in order */ public void testDescendingPollLastEntry() { ConcurrentNavigableMap map = dmap5(); Map.Entry e = map.pollLastEntry(); assertEquals(m5, e.getKey()); assertEquals("E", e.getValue()); e = map.pollLastEntry(); assertEquals(m4, e.getKey()); map.put(m5, "E"); e = map.pollLastEntry(); assertEquals(m5, e.getKey()); assertEquals("E", e.getValue()); e = map.pollLastEntry(); assertEquals(m3, e.getKey()); map.remove(m2); e = map.pollLastEntry(); assertEquals(m1, e.getKey()); try { e.setValue("E"); shouldThrow(); } catch (UnsupportedOperationException success) {} e = map.pollLastEntry(); assertNull(e); }
@Override public Optional<ServerAddress> resolveByUuid(UUID uuid) { Assert.notNull(uuid); if (circle.isEmpty()) { return Optional.empty(); } Long hash = hashFunction.newHasher().putLong(uuid.getMostSignificantBits()).putLong(uuid.getLeastSignificantBits()) .hash().asLong(); if (!circle.containsKey(hash)) { ConcurrentNavigableMap<Long, ServerInstance> tailMap = circle.tailMap(hash); hash = tailMap.isEmpty() ? circle.firstKey() : tailMap.firstKey(); } ServerInstance result = circle.get(hash); if (!currentServer.equals(result)) { return Optional.of(result.getServerAddress()); } else { return Optional.empty(); } }
/** * pollFirstEntry returns entries in order */ public void testDescendingPollFirstEntry() { ConcurrentNavigableMap map = dmap5(); Map.Entry e = map.pollFirstEntry(); assertEquals(m1, e.getKey()); assertEquals("A", e.getValue()); e = map.pollFirstEntry(); assertEquals(m2, e.getKey()); map.put(m1, "A"); e = map.pollFirstEntry(); assertEquals(m1, e.getKey()); assertEquals("A", e.getValue()); e = map.pollFirstEntry(); assertEquals(m3, e.getKey()); map.remove(m4); e = map.pollFirstEntry(); assertEquals(m5, e.getKey()); try { e.setValue("A"); shouldThrow(); } catch (UnsupportedOperationException success) {} e = map.pollFirstEntry(); assertNull(e); }
@Override public ConcurrentNavigableMap<K, V> tailMap(K fromKey, boolean inclusive) { ArrayHolder<K, V> current = this.holder; int index = current.find(fromKey); if (!inclusive && index >= 0) { index++; } else if (index < 0) { index = -(index + 1); } return new CopyOnWriteArrayMap<>( this.keyComparator, new ArrayHolder<>( current.entries, index, current.endIndex, current.keyComparator, current.comparator)); }
/** * entrySet contains all pairs */ public void testEntrySet() { ConcurrentNavigableMap map = map5(); Set s = map.entrySet(); assertEquals(5, s.size()); Iterator it = s.iterator(); while (it.hasNext()) { Map.Entry e = (Map.Entry) it.next(); assertTrue( (e.getKey().equals(one) && e.getValue().equals("A")) || (e.getKey().equals(two) && e.getValue().equals("B")) || (e.getKey().equals(three) && e.getValue().equals("C")) || (e.getKey().equals(four) && e.getValue().equals("D")) || (e.getKey().equals(five) && e.getValue().equals("E"))); } }
static void putAllCollections(Map<Class<?>, IntFunction<?>> map, Map<Class<?>, Function<?, ?>> unmodMap) { safePut(map, ArrayList.class, ArrayList::new); safePut(map, HashSet.class, LinkedHashSet::new); safePut(map, Properties.class, x -> new Properties()); safePut(map, Hashtable.class, Hashtable::new); safePut(map, Collection.class, ArrayList::new); safePut(map, Set.class, LinkedHashSet::new); safePut(map, List.class, ArrayList::new); safePut(map, SortedSet.class, x -> new TreeSet<>()); safePut(map, Queue.class, x -> new ConcurrentLinkedQueue<>()); safePut(map, Deque.class, x -> new ConcurrentLinkedDeque<>()); safePut(map, BlockingQueue.class, x -> new LinkedBlockingQueue<>()); safePut(map, BlockingDeque.class, x -> new LinkedBlockingDeque<>()); safePut(map, HashMap.class, LinkedHashMap::new); safePut(map, LinkedHashMap.class, LinkedHashMap::new); safePut(map, ConcurrentHashMap.class, ConcurrentHashMap::new); safePut(map, Map.class, LinkedHashMap::new); safePut(map, ConcurrentMap.class, x -> new ConcurrentSkipListMap<>()); safePut(map, ConcurrentNavigableMap.class, x -> new ConcurrentSkipListMap<>()); safePut(map, SortedMap.class, i -> new TreeMap<>()); }
/** * pollFirstEntry returns entries in order */ public void testPollFirstEntry() { ConcurrentNavigableMap map = map5(); Map.Entry e = map.pollFirstEntry(); assertEquals(one, e.getKey()); assertEquals("A", e.getValue()); e = map.pollFirstEntry(); assertEquals(two, e.getKey()); map.put(one, "A"); e = map.pollFirstEntry(); assertEquals(one, e.getKey()); assertEquals("A", e.getValue()); e = map.pollFirstEntry(); assertEquals(three, e.getKey()); map.remove(four); e = map.pollFirstEntry(); assertEquals(five, e.getKey()); try { e.setValue("A"); shouldThrow(); } catch (UnsupportedOperationException success) {} e = map.pollFirstEntry(); assertNull(e); }
/** * pollLastEntry returns entries in order */ public void testPollLastEntry() { ConcurrentNavigableMap map = map5(); Map.Entry e = map.pollLastEntry(); assertEquals(five, e.getKey()); assertEquals("E", e.getValue()); e = map.pollLastEntry(); assertEquals(four, e.getKey()); map.put(five, "E"); e = map.pollLastEntry(); assertEquals(five, e.getKey()); assertEquals("E", e.getValue()); e = map.pollLastEntry(); assertEquals(three, e.getKey()); map.remove(two); e = map.pollLastEntry(); assertEquals(one, e.getKey()); try { e.setValue("E"); shouldThrow(); } catch (UnsupportedOperationException success) {} e = map.pollLastEntry(); assertNull(e); }
/** * headMap returns map with keys in requested range */ public void testDescendingHeadMapContents() { ConcurrentNavigableMap map = dmap5(); SortedMap sm = map.headMap(m4); assertTrue(sm.containsKey(m1)); assertTrue(sm.containsKey(m2)); assertTrue(sm.containsKey(m3)); assertFalse(sm.containsKey(m4)); assertFalse(sm.containsKey(m5)); Iterator i = sm.keySet().iterator(); Object k; k = (Integer)(i.next()); assertEquals(m1, k); k = (Integer)(i.next()); assertEquals(m2, k); k = (Integer)(i.next()); assertEquals(m3, k); assertFalse(i.hasNext()); sm.clear(); assertTrue(sm.isEmpty()); assertEquals(2, map.size()); assertEquals(m4, map.firstKey()); }
public void testSubMapContents2() { ConcurrentNavigableMap map = map5(); SortedMap sm = map.subMap(two, three); assertEquals(1, sm.size()); assertEquals(two, sm.firstKey()); assertEquals(two, sm.lastKey()); assertFalse(sm.containsKey(one)); assertTrue(sm.containsKey(two)); assertFalse(sm.containsKey(three)); assertFalse(sm.containsKey(four)); assertFalse(sm.containsKey(five)); Iterator i = sm.keySet().iterator(); Object k; k = (Integer)(i.next()); assertEquals(two, k); assertFalse(i.hasNext()); Iterator j = sm.keySet().iterator(); j.next(); j.remove(); assertFalse(map.containsKey(two)); assertEquals(4, map.size()); assertEquals(0, sm.size()); assertTrue(sm.isEmpty()); assertSame(sm.remove(three), null); assertEquals(4, map.size()); }
/** * entrySet contains all pairs */ public void testDescendingEntrySet() { ConcurrentNavigableMap map = dmap5(); Set s = map.entrySet(); assertEquals(5, s.size()); Iterator it = s.iterator(); while (it.hasNext()) { Map.Entry e = (Map.Entry) it.next(); assertTrue( (e.getKey().equals(m1) && e.getValue().equals("A")) || (e.getKey().equals(m2) && e.getValue().equals("B")) || (e.getKey().equals(m3) && e.getValue().equals("C")) || (e.getKey().equals(m4) && e.getValue().equals("D")) || (e.getKey().equals(m5) && e.getValue().equals("E"))); } }
/** * replace(null, x) throws NPE */ public void testDescendingReplace_NullPointerException() { try { ConcurrentNavigableMap c = dmap5(); c.replace(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} }
/** * @throws ClassCastException {@inheritDoc} * @throws NullPointerException if {@code toKey} is null * @throws IllegalArgumentException {@inheritDoc} */ public ConcurrentNavigableMap<K,V> headMap(K toKey, boolean inclusive) { if (toKey == null) throw new NullPointerException(); return new SubMap<K,V> (this, null, false, toKey, inclusive, false); }
/** * @throws ClassCastException {@inheritDoc} * @throws NullPointerException if {@code fromKey} is null * @throws IllegalArgumentException {@inheritDoc} */ public ConcurrentNavigableMap<K,V> tailMap(K fromKey, boolean inclusive) { if (fromKey == null) throw new NullPointerException(); return new SubMap<K,V> (this, fromKey, inclusive, null, false, false); }
/** * toString contains toString of elements */ public void testDescendingToString() { ConcurrentNavigableMap map = dmap5(); String s = map.toString(); for (int i = 1; i <= 5; ++i) { assertTrue(s.contains(String.valueOf(i))); } }
private ConcurrentNavigableMap<byte[], Long> getOrCreateStoreFlushedSequenceId( byte[] regionName) { ConcurrentNavigableMap<byte[], Long> storeFlushedSequenceId = storeFlushedSequenceIdsByRegion.get(regionName); if (storeFlushedSequenceId != null) { return storeFlushedSequenceId; } storeFlushedSequenceId = new ConcurrentSkipListMap<byte[], Long>(Bytes.BYTES_COMPARATOR); ConcurrentNavigableMap<byte[], Long> alreadyPut = storeFlushedSequenceIdsByRegion.putIfAbsent(regionName, storeFlushedSequenceId); return alreadyPut == null ? storeFlushedSequenceId : alreadyPut; }
/** * Returns a new map from Integers 1-5 to Strings "A"-"E". */ private static ConcurrentNavigableMap map5() { ConcurrentSkipListMap map = new ConcurrentSkipListMap(); assertTrue(map.isEmpty()); map.put(zero, "Z"); map.put(one, "A"); map.put(five, "E"); map.put(three, "C"); map.put(two, "B"); map.put(four, "D"); map.put(seven, "F"); assertFalse(map.isEmpty()); assertEquals(7, map.size()); return map.subMap(one, true, seven, false); }
/** * Returns a new map from Integers -5 to -1 to Strings "A"-"E". */ private static ConcurrentNavigableMap dmap5() { ConcurrentSkipListMap map = new ConcurrentSkipListMap(); assertTrue(map.isEmpty()); map.put(m1, "A"); map.put(m5, "E"); map.put(m3, "C"); map.put(m2, "B"); map.put(m4, "D"); assertFalse(map.isEmpty()); assertEquals(5, map.size()); return map.descendingMap(); }
/** * higherEntry returns next entry. */ public void testDescendingHigherEntry() { ConcurrentNavigableMap map = dmap5(); Map.Entry e1 = map.higherEntry(m3); assertEquals(m4, e1.getKey()); Map.Entry e2 = map.higherEntry(zero); assertEquals(m1, e2.getKey()); Map.Entry e3 = map.higherEntry(m5); assertNull(e3); Map.Entry e4 = map.higherEntry(m6); assertNull(e4); }
/** * Updates last flushed sequence Ids for the regions on server sn * @param sn * @param hsl */ private void updateLastFlushedSequenceIds(ServerName sn, ServerLoad hsl) { Map<byte[], RegionLoad> regionsLoad = hsl.getRegionsLoad(); for (Entry<byte[], RegionLoad> entry : regionsLoad.entrySet()) { byte[] encodedRegionName = Bytes.toBytes(HRegionInfo.encodeRegionName(entry.getKey())); Long existingValue = flushedSequenceIdByRegion.get(encodedRegionName); long l = entry.getValue().getCompleteSequenceId(); // Don't let smaller sequence ids override greater sequence ids. if (LOG.isTraceEnabled()) { LOG.trace(Bytes.toString(encodedRegionName) + ", existingValue=" + existingValue + ", completeSequenceId=" + l); } if (existingValue == null || (l != HConstants.NO_SEQNUM && l > existingValue)) { flushedSequenceIdByRegion.put(encodedRegionName, l); } else if (l != HConstants.NO_SEQNUM && l < existingValue) { LOG.warn("RegionServer " + sn + " indicates a last flushed sequence id (" + l + ") that is less than the previous last flushed sequence id (" + existingValue + ") for region " + Bytes.toString(entry.getKey()) + " Ignoring."); } ConcurrentNavigableMap<byte[], Long> storeFlushedSequenceId = getOrCreateStoreFlushedSequenceId(encodedRegionName); for (StoreSequenceId storeSeqId : entry.getValue().getStoreCompleteSequenceId()) { byte[] family = storeSeqId.getFamilyName().toByteArray(); existingValue = storeFlushedSequenceId.get(family); l = storeSeqId.getSequenceId(); if (LOG.isTraceEnabled()) { LOG.trace(Bytes.toString(encodedRegionName) + ", family=" + Bytes.toString(family) + ", existingValue=" + existingValue + ", completeSequenceId=" + l); } // Don't let smaller sequence ids override greater sequence ids. if (existingValue == null || (l != HConstants.NO_SEQNUM && l > existingValue.longValue())) { storeFlushedSequenceId.put(family, l); } } } }
/** * Maps with same contents are equal */ public void testEquals() { ConcurrentNavigableMap map1 = map5(); ConcurrentNavigableMap map2 = map5(); assertEquals(map1, map2); assertEquals(map2, map1); map1.clear(); assertFalse(map1.equals(map2)); assertFalse(map2.equals(map1)); }
/** * get returns the correct element at the given key, * or null if not present */ public void testGet() { ConcurrentNavigableMap map = map5(); assertEquals("A", (String)map.get(one)); ConcurrentNavigableMap empty = map0(); assertNull(empty.get(one)); }
/** * replace(null, x, y) throws NPE */ public void testDescendingReplaceValue_NullPointerException() { try { ConcurrentNavigableMap c = dmap5(); c.replace(null, m1, "whatever"); shouldThrow(); } catch (NullPointerException success) {} }
/** * remove(null, x) throws NPE */ public void testDescendingRemove2_NullPointerException() { try { ConcurrentNavigableMap c = dmap5(); c.remove(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} }
/** * keySet returns a Set containing all the keys */ public void testKeySet() { ConcurrentNavigableMap map = map5(); Set s = map.keySet(); assertEquals(5, s.size()); assertTrue(s.contains(one)); assertTrue(s.contains(two)); assertTrue(s.contains(three)); assertTrue(s.contains(four)); assertTrue(s.contains(five)); }
/** * floorEntry returns preceding entry. */ public void testDescendingFloorEntry() { ConcurrentNavigableMap map = dmap5(); Map.Entry e1 = map.floorEntry(m3); assertEquals(m3, e1.getKey()); Map.Entry e2 = map.floorEntry(m6); assertEquals(m5, e2.getKey()); Map.Entry e3 = map.floorEntry(m1); assertEquals(m1, e3.getKey()); Map.Entry e4 = map.floorEntry(zero); assertNull(e4); }
/** * keySet.toArray returns contains all keys */ public void testKeySetToArray() { ConcurrentNavigableMap map = map5(); Set s = map.keySet(); Object[] ar = s.toArray(); assertTrue(s.containsAll(Arrays.asList(ar))); assertEquals(5, ar.length); ar[0] = m10; assertFalse(s.containsAll(Arrays.asList(ar))); }
/** * descendingkeySet.toArray returns contains all keys */ public void testDescendingKeySetToArray() { ConcurrentNavigableMap map = map5(); Set s = map.descendingKeySet(); Object[] ar = s.toArray(); assertEquals(5, ar.length); assertTrue(s.containsAll(Arrays.asList(ar))); ar[0] = m10; assertFalse(s.containsAll(Arrays.asList(ar))); }
/** * Values.toArray contains all values */ public void testValuesToArray() { ConcurrentNavigableMap map = map5(); Collection v = map.values(); Object[] ar = v.toArray(); ArrayList s = new ArrayList(Arrays.asList(ar)); assertEquals(5, ar.length); assertTrue(s.contains("A")); assertTrue(s.contains("B")); assertTrue(s.contains("C")); assertTrue(s.contains("D")); assertTrue(s.contains("E")); }
/** * putAll adds all key-value pairs from the given map */ public void testPutAll() { ConcurrentNavigableMap empty = map0(); ConcurrentNavigableMap map = map5(); empty.putAll(map); assertEquals(5, empty.size()); assertTrue(empty.containsKey(one)); assertTrue(empty.containsKey(two)); assertTrue(empty.containsKey(three)); assertTrue(empty.containsKey(four)); assertTrue(empty.containsKey(five)); }
/** * containsKey(null) of empty map throws NPE */ public void testDescendingContainsKey_NullPointerException() { try { ConcurrentNavigableMap c = dmap5(); c.containsKey(null); shouldThrow(); } catch (NullPointerException success) {} }
/** * replace value fails when the given key not mapped to expected value */ public void testReplaceValue() { ConcurrentNavigableMap map = map5(); assertEquals("A", map.get(one)); assertFalse(map.replace(one, "Z", "Z")); assertEquals("A", map.get(one)); }
/** * remove removes the correct key-value pair from the map */ public void testRemove() { ConcurrentNavigableMap map = map5(); map.remove(five); assertEquals(4, map.size()); assertFalse(map.containsKey(five)); }
/** * higherEntry returns next entry. */ public void testHigherEntry() { ConcurrentNavigableMap map = map5(); Map.Entry e1 = map.higherEntry(three); assertEquals(four, e1.getKey()); Map.Entry e2 = map.higherEntry(zero); assertEquals(one, e2.getKey()); Map.Entry e3 = map.higherEntry(five); assertNull(e3); Map.Entry e4 = map.higherEntry(six); assertNull(e4); }
/** * floorEntry returns preceding entry. */ public void testFloorEntry() { ConcurrentNavigableMap map = map5(); Map.Entry e1 = map.floorEntry(three); assertEquals(three, e1.getKey()); Map.Entry e2 = map.floorEntry(six); assertEquals(five, e2.getKey()); Map.Entry e3 = map.floorEntry(one); assertEquals(one, e3.getKey()); Map.Entry e4 = map.floorEntry(zero); assertNull(e4); }
/** * ceilingEntry returns next entry. */ public void testCeilingEntry() { ConcurrentNavigableMap map = map5(); Map.Entry e1 = map.ceilingEntry(three); assertEquals(three, e1.getKey()); Map.Entry e2 = map.ceilingEntry(zero); assertEquals(one, e2.getKey()); Map.Entry e3 = map.ceilingEntry(five); assertEquals(five, e3.getKey()); Map.Entry e4 = map.ceilingEntry(six); assertNull(e4); }