下面列出了java.util.TreeMap#ceilingKey ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 沿环的顺时针找到节点
*
* @param map
* @param key
* @return
*/
public static String doGetTableName(TreeMap<Long, String> map, String key) {
final Long hash = alg.hash(key);
Long target = hash;
if (!map.containsKey(hash)) {
target = map.ceilingKey(hash);
if (target == null && !map.isEmpty()) {
target = map.firstKey();
}
}
return map.get(target);
}
/**
* 沿环的顺时针找到节点
*
* @param map
* @param key
* @return
*/
public static String doGetTableName(TreeMap<Long, String> map, String key) {
final Long hash = alg.hash(key);
Long target = hash;
if (!map.containsKey(hash)) {
target = map.ceilingKey(hash);
if (target == null && !map.isEmpty()) {
target = map.firstKey();
}
}
return map.get(target);
}
/**
* ceilingKey returns next element
*/
public void testCeilingKey() {
TreeMap q = map5();
Object e1 = q.ceilingKey(three);
assertEquals(three, e1);
Object e2 = q.ceilingKey(zero);
assertEquals(one, e2);
Object e3 = q.ceilingKey(five);
assertEquals(five, e3);
Object e4 = q.ceilingKey(six);
assertNull(e4);
}
/**
* ceilingKey returns next element
*/
public void testCeilingKey() {
TreeMap q = map5();
Object e1 = q.ceilingKey(three);
assertEquals(three, e1);
Object e2 = q.ceilingKey(zero);
assertEquals(one, e2);
Object e3 = q.ceilingKey(five);
assertEquals(five, e3);
Object e4 = q.ceilingKey(six);
assertNull(e4);
}
@Override
public boolean containsEventSet(String attribute, Compare.Operator operator, Object value) {
if (primaryKeyData != null && attribute.equals(primaryKeyAttributes)) {
switch (operator) {
case LESS_THAN:
return ((TreeMap<Object, StreamEvent>) primaryKeyData).lowerKey(value) != null;
case GREATER_THAN:
return ((TreeMap<Object, StreamEvent>) primaryKeyData).higherKey(value) != null;
case LESS_THAN_EQUAL:
return ((TreeMap<Object, StreamEvent>) primaryKeyData).ceilingKey(value) != null;
case GREATER_THAN_EQUAL:
return ((TreeMap<Object, StreamEvent>) primaryKeyData).floorKey(value) != null;
case EQUAL:
return primaryKeyData.get(value) != null;
case NOT_EQUAL:
return primaryKeyData.size() > 1;
}
} else {
TreeMap<Object, Set<StreamEvent>> currentIndexedData = indexData.get(attribute);
switch (operator) {
case LESS_THAN:
return currentIndexedData.lowerKey(value) != null;
case GREATER_THAN:
return currentIndexedData.higherKey(value) != null;
case LESS_THAN_EQUAL:
return currentIndexedData.ceilingKey(value) != null;
case GREATER_THAN_EQUAL:
return currentIndexedData.floorKey(value) != null;
case EQUAL:
return currentIndexedData.get(value) != null;
case NOT_EQUAL:
return currentIndexedData.size() > 1;
}
}
throw new OperationNotSupportedException(operator + " not supported for '" + value + "' by " + getClass()
.getName());
}
public void pruneSnapshots(Stream stream) {
if (stream.initialRetentionPeriod == null
&& stream.retentions.isEmpty()) {
// no retentions, do not prune
return;
}
DateTime now = new DateTime(ISOChronology.getInstanceUTC());
TreeMap<DateTime, Boolean> keepSnapshot = new TreeMap<>();
HashMap<DateTime, Snapshot> snapshotMap = new HashMap<>();
Interval initialRetentionInterval = stream
.getInitialRetentionInterval(now);
// fill maps
for (Snapshot s : getSnapshots(stream).values()) {
keepSnapshot.put(s.date, s.date.isAfter(now)
|| initialRetentionInterval.contains(s.date));
snapshotMap.put(s.date, s);
}
// process retentions
for (Retention r : stream.retentions) {
for (DateTime time : r.retentionTimes(now)) {
DateTime key = keepSnapshot.ceilingKey(time);
if (key != null) {
keepSnapshot.put(key, true);
}
}
}
// delete streams which are not to be retained
for (Entry<DateTime, Boolean> entry : keepSnapshot.entrySet()) {
if (!entry.getValue()) {
deleteSnapshot(snapshotMap.get(entry.getKey()));
}
}
}
@Override
public boolean containsEventSet(String attribute, Compare.Operator operator, Object value) {
if (primaryKeyData != null && attribute.equals(primaryKeyAttributes)) {
StreamEvent foundEvent;
switch (operator) {
case LESS_THAN:
foundEvent = (StreamEvent) ((TreeMap<Object, StreamEvent>) primaryKeyData).
lowerKey(value);
if (foundEvent != null) {
handleCachePolicyAttributeUpdate(foundEvent);
return true;
} else {
return false;
}
case GREATER_THAN:
foundEvent = (StreamEvent) ((TreeMap<Object, StreamEvent>) primaryKeyData).
higherKey(value);
if (foundEvent != null) {
handleCachePolicyAttributeUpdate(foundEvent);
return true;
} else {
return false;
}
case LESS_THAN_EQUAL:
foundEvent = (StreamEvent) ((TreeMap<Object, StreamEvent>) primaryKeyData).
ceilingKey(value);
if (foundEvent != null) {
handleCachePolicyAttributeUpdate(foundEvent);
return true;
} else {
return false;
}
case GREATER_THAN_EQUAL:
foundEvent = (StreamEvent) ((TreeMap<Object, StreamEvent>) primaryKeyData).
floorKey(value);
if (foundEvent != null) {
handleCachePolicyAttributeUpdate(foundEvent);
return true;
} else {
return false;
}
case EQUAL:
foundEvent = primaryKeyData.get(value);
if (foundEvent != null) {
handleCachePolicyAttributeUpdate(foundEvent);
return true;
} else {
return false;
}
case NOT_EQUAL:
return primaryKeyData.size() > 1;
}
} else {
TreeMap<Object, Set<StreamEvent>> currentIndexedData = indexData.get(attribute);
switch (operator) {
case LESS_THAN:
return currentIndexedData.lowerKey(value) != null;
case GREATER_THAN:
return currentIndexedData.higherKey(value) != null;
case LESS_THAN_EQUAL:
return currentIndexedData.ceilingKey(value) != null;
case GREATER_THAN_EQUAL:
return currentIndexedData.floorKey(value) != null;
case EQUAL:
return currentIndexedData.get(value) != null;
case NOT_EQUAL:
return currentIndexedData.size() > 1;
}
}
throw new OperationNotSupportedException(operator + " not supported for '" + value + "' by " + getClass()
.getName());
}