下面列出了java.util.NavigableMap#tailMap ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testTailMapRanges(String description, NavigableMap navigableMap) {
NavigableMap subMap = navigableMap.tailMap(BigInteger.ONE, true);
// same subset
subMap.tailMap(BigInteger.ONE, true);
// slightly smaller
NavigableMap ns = subMap.tailMap(BigInteger.ONE, false);
// slight exapansion
assertThrows(() -> {
ns.tailMap(BigInteger.ONE, true);
},
IllegalArgumentException.class,
description + ": Expansion should not be allowed");
// much smaller
subMap.tailMap(isDescending(subMap) ? BigInteger.ZERO : BigInteger.TEN, false);
}
/**
* Tests the tailMap() method.
*/
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testTailMap(String description, NavigableMap navigableMap) {
assertThrows(() -> {
navigableMap.tailMap(null);
},
NullPointerException.class,
description + ": Must throw NullPointerException for null element");
assertThrows(() -> {
navigableMap.tailMap(new Object());
}, ClassCastException.class);
NavigableMap ss = navigableMap.tailMap("1", true);
assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set.");
}
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testTailMapRanges(String description, NavigableMap navigableMap) {
NavigableMap subMap = navigableMap.tailMap(BigInteger.ONE, true);
// same subset
subMap.tailMap(BigInteger.ONE, true);
// slightly smaller
NavigableMap ns = subMap.tailMap(BigInteger.ONE, false);
// slight exapansion
assertThrows(() -> {
ns.tailMap(BigInteger.ONE, true);
},
IllegalArgumentException.class,
description + ": Expansion should not be allowed");
// much smaller
subMap.tailMap(isDescending(subMap) ? BigInteger.ZERO : BigInteger.TEN, false);
}
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testTailMapRanges(String description, NavigableMap navigableMap) {
NavigableMap subMap = navigableMap.tailMap(BigInteger.ONE, true);
// same subset
subMap.tailMap(BigInteger.ONE, true);
// slightly smaller
NavigableMap ns = subMap.tailMap(BigInteger.ONE, false);
// slight exapansion
assertThrows(() -> {
ns.tailMap(BigInteger.ONE, true);
},
IllegalArgumentException.class,
description + ": Expansion should not be allowed");
// much smaller
subMap.tailMap(isDescending(subMap) ? BigInteger.ZERO : BigInteger.TEN, false);
}
/**
* Returns a view of the portion of {@code map} whose keys are contained by {@code range}.
*
* <p>This method delegates to the appropriate methods of {@link NavigableMap} (namely
* {@link NavigableMap#subMap(Object, boolean, Object, boolean) subMap()},
* {@link NavigableMap#tailMap(Object, boolean) tailMap()}, and
* {@link NavigableMap#headMap(Object, boolean) headMap()}) to actually construct the view.
* Consult these methods for a full description of the returned view's behavior.
*
* <p><b>Warning:</b> {@code Range}s always represent a range of values using the values' natural
* ordering. {@code NavigableMap} on the other hand can specify a custom ordering via a
* {@link Comparator}, which can violate the natural ordering. Using this method (or in general
* using {@code Range}) with unnaturally-ordered maps can lead to unexpected and undefined
* behavior.
*
* @since 20.0
*/
@Beta
@GwtIncompatible // NavigableMap
public static <K extends Comparable<? super K>, V> NavigableMap<K, V> subMap(NavigableMap<K, V> map, Range<K> range) {
if (map.comparator() != null && map.comparator() != Ordering.natural()
&& range.hasLowerBound()
&& range.hasUpperBound()) {
checkArgument(map.comparator().compare(range.lowerEndpoint(), range.upperEndpoint()) <= 0, "map is using a custom comparator which is inconsistent with the natural ordering.");
}
if (range.hasLowerBound() && range.hasUpperBound()) {
return map.subMap(
range.lowerEndpoint(), range.lowerBoundType() == BoundType.CLOSED,
range.upperEndpoint(), range.upperBoundType() == BoundType.CLOSED);
} else if (range.hasLowerBound()) {
return map.tailMap(range.lowerEndpoint(), range.lowerBoundType() == BoundType.CLOSED);
} else if (range.hasUpperBound()) {
return map.headMap(range.upperEndpoint(), range.upperBoundType() == BoundType.CLOSED);
}
return checkNotNull(map);
}
/**
* Tests the tailMap() method.
*/
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testTailMap(String description, NavigableMap navigableMap) {
assertThrows(() -> {
navigableMap.tailMap(null);
},
NullPointerException.class,
description + ": Must throw NullPointerException for null element");
assertThrows(() -> {
navigableMap.tailMap(new Object());
}, ClassCastException.class);
NavigableMap ss = navigableMap.tailMap("1", true);
assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set.");
}
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testTailMapRanges(String description, NavigableMap navigableMap) {
NavigableMap subMap = navigableMap.tailMap(BigInteger.ONE, true);
// same subset
subMap.tailMap(BigInteger.ONE, true);
// slightly smaller
NavigableMap ns = subMap.tailMap(BigInteger.ONE, false);
// slight exapansion
assertThrows(() -> {
ns.tailMap(BigInteger.ONE, true);
},
IllegalArgumentException.class,
description + ": Expansion should not be allowed");
// much smaller
subMap.tailMap(isDescending(subMap) ? BigInteger.ZERO : BigInteger.TEN, false);
}
@Override
public SortedIterator<ByteBuffer> scan(
byte[] from, boolean fromInclusive,
byte[] to, boolean toInclusive,
boolean ascending,
MetadataFilter filter) {
if (filter == null || filter.accept(metadata.get(filter.getName()))) {
NavigableMap<byte[],byte[]> subset = ascending ? buffer : buffer.descendingMap();
if (from == null && to == null) {
// we're good
} else if (from == null) {
subset = subset.headMap(to, toInclusive);
} else if (to == null) {
subset = subset.tailMap(from, fromInclusive);
} else {
subset = subset.subMap(from, fromInclusive, to, toInclusive);
}
return new BufferIterator(subset.entrySet().iterator());
}
return new BufferIterator(Collections.<byte[], byte[]>emptyMap().entrySet().iterator());
}
@Override
public SortedIterator<ByteBuffer> scan(
byte[] from, boolean fromInclusive,
byte[] to, boolean toInclusive,
boolean ascending,
MetadataFilter filter) {
if (filter == null || filter.accept(metadata.get(filter.getName()))) {
NavigableMap<byte[],byte[]> subset = ascending ? data.get() : data.get().descendingMap();
if (from == null && to == null) {
// we're good
} else if (from == null) {
subset = subset.headMap(to, toInclusive);
} else if (to == null) {
subset = subset.tailMap(from, fromInclusive);
} else {
subset = subset.subMap(from, fromInclusive, to, toInclusive);
}
return new BufferIterator(subset.entrySet().iterator());
}
return new BufferIterator(Collections.<byte[], byte[]>emptyMap().entrySet().iterator());
}
/**
* Returns a view of the portion of {@code map} whose keys are contained by {@code range}.
*
* <p>This method delegates to the appropriate methods of {@link NavigableMap} (namely
* {@link NavigableMap#subMap(Object, boolean, Object, boolean) subMap()},
* {@link NavigableMap#tailMap(Object, boolean) tailMap()}, and
* {@link NavigableMap#headMap(Object, boolean) headMap()}) to actually construct the view.
* Consult these methods for a full description of the returned view's behavior.
*
* <p><b>Warning:</b> {@code Range}s always represent a range of values using the values' natural
* ordering. {@code NavigableMap} on the other hand can specify a custom ordering via a
* {@link Comparator}, which can violate the natural ordering. Using this method (or in general
* using {@code Range}) with unnaturally-ordered maps can lead to unexpected and undefined
* behavior.
*
* @since 20.0
*/
@Beta
@GwtIncompatible // NavigableMap
public static <K extends Comparable<? super K>, V> NavigableMap<K, V> subMap(NavigableMap<K, V> map, Range<K> range) {
if (map.comparator() != null && map.comparator() != Ordering.natural()
&& range.hasLowerBound()
&& range.hasUpperBound()) {
checkArgument(map.comparator().compare(range.lowerEndpoint(), range.upperEndpoint()) <= 0, "map is using a custom comparator which is inconsistent with the natural ordering.");
}
if (range.hasLowerBound() && range.hasUpperBound()) {
return map.subMap(
range.lowerEndpoint(), range.lowerBoundType() == BoundType.CLOSED,
range.upperEndpoint(), range.upperBoundType() == BoundType.CLOSED);
} else if (range.hasLowerBound()) {
return map.tailMap(range.lowerEndpoint(), range.lowerBoundType() == BoundType.CLOSED);
} else if (range.hasUpperBound()) {
return map.headMap(range.upperEndpoint(), range.upperBoundType() == BoundType.CLOSED);
}
return checkNotNull(map);
}
/**
* Tests the tailMap() method.
*/
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testTailMap(String description, NavigableMap navigableMap) {
assertThrows(() -> {
navigableMap.tailMap(null);
},
NullPointerException.class,
description + ": Must throw NullPointerException for null element");
assertThrows(() -> {
navigableMap.tailMap(new Object());
}, ClassCastException.class);
NavigableMap ss = navigableMap.tailMap("1", true);
assertEmptyNavigableMap(ss, description + ": Returned value is not empty navigable set.");
}
private void selectMaps() {
// Sorting beforehand, saves future key remaps, as bigger values are placed at the end
List<MapInfo> sortedDist =
mapScores.entrySet().stream()
.sorted(Comparator.comparingDouble(Map.Entry::getValue))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
NavigableMap<Double, MapInfo> cumulativeScores = new TreeMap<>();
double maxWeight = cummulativeMap(0, sortedDist, cumulativeScores);
for (int i = 0; i < voteSize; i++) {
NavigableMap<Double, MapInfo> subMap =
cumulativeScores.tailMap(Math.random() * maxWeight, true);
Map.Entry<Double, MapInfo> selected = subMap.pollFirstEntry();
if (selected == null) break; // No more maps to poll
votes.put(selected.getValue(), new HashSet<>()); // Add map to votes
if (votes.size() >= voteSize) break; // Skip replace logic after all maps have been selected
// Remove map from pool, updating cumulative scores
double selectedWeight = getWeight(selected.getValue());
maxWeight -= selectedWeight;
NavigableMap<Double, MapInfo> temp = new TreeMap<>();
cummulativeMap(selected.getKey() - selectedWeight, subMap.values(), temp);
subMap.clear();
cumulativeScores.putAll(temp);
}
}
private void findMatchingValues(NavigableMap<Endpoint, Integer> map, Endpoint endpoint, List<UUID> result) {
// Mark this endpoint as a marker, because it is used to do a tailMap traversal to query matching edges
endpoint.setMarker();
// Find the first key
Endpoint floorKey = map.floorKey(endpoint);
Map<Endpoint, Integer> view;
if (floorKey == null) {
// This means that the element being searched is the minimum
view = map;
} else {
view = map.tailMap(floorKey);
}
boolean isFirst = true;
for (Map.Entry<Endpoint, Integer> entry : view.entrySet()) {
Endpoint key = entry.getKey();
if (endpoint.isMatch(key)) {
// Matching entry, must be added to result
result.add(key.getEdgeId());
} else {
// Done with the search -- the tree map is sorted
if (isFirst) {
// continue
} else {
break;
}
}
isFirst = false;
}
}
/** Construct an {@link Iterable} object,
* so that an {@link Iterator} can be created
* for iterating the given {@link NavigableMap}.
* The iteration begins from the starting key exclusively.
*/
public CyclicIteration(NavigableMap<K, V> navigablemap, K startingkey) {
if (navigablemap == null || navigablemap.isEmpty()) {
this.navigablemap = null;
this.tailmap = null;
}
else {
this.navigablemap = navigablemap;
this.tailmap = navigablemap.tailMap(startingkey, false);
}
}
/**
* Returns an HRegionLocationList extracted from the result.
* (Copied from MetaTableAccessor)
* @return an HRegionLocationList containing all locations for the region range or null if
* we can't deserialize the result.
*/
public static RegionLocations getRegionLocations(final Result r) {
if (r == null) {
return null;
}
RegionInfo regionInfo = getRegionInfo(r, REGIONINFO_QUALIFIER);
if (regionInfo == null) {
return null;
}
List<HRegionLocation> locations = new ArrayList<>(1);
NavigableMap<byte[], NavigableMap<byte[],byte[]>> familyMap = r.getNoVersionMap();
locations.add(getRegionLocation(r, regionInfo, 0));
NavigableMap<byte[], byte[]> infoMap = familyMap.get(CATALOG_FAMILY);
if (infoMap == null) {
return new RegionLocations(locations);
}
// iterate until all serverName columns are seen
int replicaId = 0;
byte[] serverColumn = getServerColumn(replicaId);
SortedMap<byte[], byte[]> serverMap;
serverMap = infoMap.tailMap(serverColumn, false);
if (serverMap.isEmpty()) {
return new RegionLocations(locations);
}
for (Map.Entry<byte[], byte[]> entry : serverMap.entrySet()) {
replicaId = parseReplicaIdFromServerColumn(entry.getKey());
if (replicaId < 0) {
break;
}
HRegionLocation location = getRegionLocation(r, regionInfo, replicaId);
// In case the region replica is newly created, it's location might be null. We usually do not
// have HRL's in RegionLocations object with null ServerName. They are handled as null HRLs.
if (location.getServerName() == null) {
locations.add(null);
} else {
locations.add(location);
}
}
return new RegionLocations(locations);
}
/**
* headMap returns map with keys in requested range
*/
public void testTailMapContents() {
TreeMap map = map5();
NavigableMap sm = map.tailMap(two, true);
assertFalse(sm.containsKey(one));
assertTrue(sm.containsKey(two));
assertTrue(sm.containsKey(three));
assertTrue(sm.containsKey(four));
assertTrue(sm.containsKey(five));
Iterator i = sm.keySet().iterator();
Object k;
k = (Integer)(i.next());
assertEquals(two, k);
k = (Integer)(i.next());
assertEquals(three, k);
k = (Integer)(i.next());
assertEquals(four, k);
k = (Integer)(i.next());
assertEquals(five, k);
assertFalse(i.hasNext());
Iterator r = sm.descendingKeySet().iterator();
k = (Integer)(r.next());
assertEquals(five, k);
k = (Integer)(r.next());
assertEquals(four, k);
k = (Integer)(r.next());
assertEquals(three, k);
k = (Integer)(r.next());
assertEquals(two, k);
assertFalse(r.hasNext());
Iterator ei = sm.entrySet().iterator();
Map.Entry e;
e = (Map.Entry)(ei.next());
assertEquals(two, e.getKey());
assertEquals("B", e.getValue());
e = (Map.Entry)(ei.next());
assertEquals(three, e.getKey());
assertEquals("C", e.getValue());
e = (Map.Entry)(ei.next());
assertEquals(four, e.getKey());
assertEquals("D", e.getValue());
e = (Map.Entry)(ei.next());
assertEquals(five, e.getKey());
assertEquals("E", e.getValue());
assertFalse(i.hasNext());
NavigableMap ssm = sm.tailMap(four, true);
assertEquals(four, ssm.firstKey());
assertEquals(five, ssm.lastKey());
assertEquals("D", ssm.remove(four));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, map.size());
}
/**
* tailMap returns map with keys in requested range
*/
public void testTailMapContents() {
ConcurrentSkipListMap map = map5();
NavigableMap sm = map.tailMap(two, true);
assertFalse(sm.containsKey(one));
assertTrue(sm.containsKey(two));
assertTrue(sm.containsKey(three));
assertTrue(sm.containsKey(four));
assertTrue(sm.containsKey(five));
Iterator i = sm.keySet().iterator();
Object k;
k = (Integer)(i.next());
assertEquals(two, k);
k = (Integer)(i.next());
assertEquals(three, k);
k = (Integer)(i.next());
assertEquals(four, k);
k = (Integer)(i.next());
assertEquals(five, k);
assertFalse(i.hasNext());
Iterator r = sm.descendingKeySet().iterator();
k = (Integer)(r.next());
assertEquals(five, k);
k = (Integer)(r.next());
assertEquals(four, k);
k = (Integer)(r.next());
assertEquals(three, k);
k = (Integer)(r.next());
assertEquals(two, k);
assertFalse(r.hasNext());
Iterator ei = sm.entrySet().iterator();
Map.Entry e;
e = (Map.Entry)(ei.next());
assertEquals(two, e.getKey());
assertEquals("B", e.getValue());
e = (Map.Entry)(ei.next());
assertEquals(three, e.getKey());
assertEquals("C", e.getValue());
e = (Map.Entry)(ei.next());
assertEquals(four, e.getKey());
assertEquals("D", e.getValue());
e = (Map.Entry)(ei.next());
assertEquals(five, e.getKey());
assertEquals("E", e.getValue());
assertFalse(i.hasNext());
NavigableMap ssm = sm.tailMap(four, true);
assertEquals(four, ssm.firstKey());
assertEquals(five, ssm.lastKey());
assertEquals("D", ssm.remove(four));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, map.size());
}
/**
* headMap returns map with keys in requested range
*/
public void testDescendingTailMapContents() {
NavigableMap map = dmap5();
SortedMap sm = map.tailMap(m2);
assertFalse(sm.containsKey(m1));
assertTrue(sm.containsKey(m2));
assertTrue(sm.containsKey(m3));
assertTrue(sm.containsKey(m4));
assertTrue(sm.containsKey(m5));
Iterator i = sm.keySet().iterator();
Object k;
k = (Integer)(i.next());
assertEquals(m2, k);
k = (Integer)(i.next());
assertEquals(m3, k);
k = (Integer)(i.next());
assertEquals(m4, k);
k = (Integer)(i.next());
assertEquals(m5, k);
assertFalse(i.hasNext());
Iterator ei = sm.entrySet().iterator();
Map.Entry e;
e = (Map.Entry)(ei.next());
assertEquals(m2, e.getKey());
assertEquals("B", e.getValue());
e = (Map.Entry)(ei.next());
assertEquals(m3, e.getKey());
assertEquals("C", e.getValue());
e = (Map.Entry)(ei.next());
assertEquals(m4, e.getKey());
assertEquals("D", e.getValue());
e = (Map.Entry)(ei.next());
assertEquals(m5, e.getKey());
assertEquals("E", e.getValue());
assertFalse(i.hasNext());
SortedMap ssm = sm.tailMap(m4);
assertEquals(m4, ssm.firstKey());
assertEquals(m5, ssm.lastKey());
assertEquals("D", ssm.remove(m4));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, map.size());
}
/**
* tailMap returns map with keys in requested range
*/
public void testTailMapContents() {
ConcurrentSkipListMap map = map5();
NavigableMap sm = map.tailMap(two, true);
assertFalse(sm.containsKey(one));
assertTrue(sm.containsKey(two));
assertTrue(sm.containsKey(three));
assertTrue(sm.containsKey(four));
assertTrue(sm.containsKey(five));
Iterator i = sm.keySet().iterator();
Object k;
k = (Integer)(i.next());
assertEquals(two, k);
k = (Integer)(i.next());
assertEquals(three, k);
k = (Integer)(i.next());
assertEquals(four, k);
k = (Integer)(i.next());
assertEquals(five, k);
assertFalse(i.hasNext());
Iterator r = sm.descendingKeySet().iterator();
k = (Integer)(r.next());
assertEquals(five, k);
k = (Integer)(r.next());
assertEquals(four, k);
k = (Integer)(r.next());
assertEquals(three, k);
k = (Integer)(r.next());
assertEquals(two, k);
assertFalse(r.hasNext());
Iterator ei = sm.entrySet().iterator();
Map.Entry e;
e = (Map.Entry)(ei.next());
assertEquals(two, e.getKey());
assertEquals("B", e.getValue());
e = (Map.Entry)(ei.next());
assertEquals(three, e.getKey());
assertEquals("C", e.getValue());
e = (Map.Entry)(ei.next());
assertEquals(four, e.getKey());
assertEquals("D", e.getValue());
e = (Map.Entry)(ei.next());
assertEquals(five, e.getKey());
assertEquals("E", e.getValue());
assertFalse(i.hasNext());
NavigableMap ssm = sm.tailMap(four, true);
assertEquals(four, ssm.firstKey());
assertEquals(five, ssm.lastKey());
assertEquals("D", ssm.remove(four));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, map.size());
}
/**
* headMap returns map with keys in requested range
*/
public void testTailMapContents() {
TreeMap map = map5();
NavigableMap sm = map.tailMap(two, true);
assertFalse(sm.containsKey(one));
assertTrue(sm.containsKey(two));
assertTrue(sm.containsKey(three));
assertTrue(sm.containsKey(four));
assertTrue(sm.containsKey(five));
Iterator i = sm.keySet().iterator();
Object k;
k = (Integer)(i.next());
assertEquals(two, k);
k = (Integer)(i.next());
assertEquals(three, k);
k = (Integer)(i.next());
assertEquals(four, k);
k = (Integer)(i.next());
assertEquals(five, k);
assertFalse(i.hasNext());
Iterator r = sm.descendingKeySet().iterator();
k = (Integer)(r.next());
assertEquals(five, k);
k = (Integer)(r.next());
assertEquals(four, k);
k = (Integer)(r.next());
assertEquals(three, k);
k = (Integer)(r.next());
assertEquals(two, k);
assertFalse(r.hasNext());
Iterator ei = sm.entrySet().iterator();
Map.Entry e;
e = (Map.Entry)(ei.next());
assertEquals(two, e.getKey());
assertEquals("B", e.getValue());
e = (Map.Entry)(ei.next());
assertEquals(three, e.getKey());
assertEquals("C", e.getValue());
e = (Map.Entry)(ei.next());
assertEquals(four, e.getKey());
assertEquals("D", e.getValue());
e = (Map.Entry)(ei.next());
assertEquals(five, e.getKey());
assertEquals("E", e.getValue());
assertFalse(i.hasNext());
NavigableMap ssm = sm.tailMap(four, true);
assertEquals(four, ssm.firstKey());
assertEquals(five, ssm.lastKey());
assertEquals("D", ssm.remove(four));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, map.size());
}