下面列出了java.util.NavigableSet# tailSet ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Tests the tailSet() method.
*/
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testTailSet(String description, NavigableSet navigableSet) {
assertThrows(() -> {
navigableSet.tailSet(null);
},
NullPointerException.class,
description + ": Must throw NullPointerException for null element");
assertThrows(() -> {
navigableSet.tailSet(new Object());
}, ClassCastException.class);
NavigableSet ss = navigableSet.tailSet("1", true);
assertEmptyNavigableSet(ss, description + ": Returned value is not empty navigable set.");
}
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testTailSetRanges(String description, NavigableSet navigableSet) {
NavigableSet subSet = navigableSet.tailSet(BigInteger.ONE, true);
// same subset
subSet.tailSet(BigInteger.ONE, true);
// slightly smaller
NavigableSet ns = subSet.tailSet(BigInteger.ONE, false);
// slight exapansion
assertThrows(() -> {
ns.tailSet(BigInteger.ONE, true);
},
IllegalArgumentException.class,
description + ": Expansion should not be allowed");
// much smaller
subSet.tailSet(isDescending(subSet) ? BigInteger.ZERO : BigInteger.TEN, false);
}
/**
* Returns a view of the portion of {@code set} whose elements are contained by {@code range}.
*
* <p>This method delegates to the appropriate methods of {@link NavigableSet} (namely
* {@link NavigableSet#subSet(Object, boolean, Object, boolean) subSet()},
* {@link NavigableSet#tailSet(Object, boolean) tailSet()}, and
* {@link NavigableSet#headSet(Object, boolean) headSet()}) 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 NavigableSet} 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 sets can lead to unexpected and undefined
* behavior.
*
* @since 20.0
*/
@Beta
@GwtIncompatible // NavigableSet
public static <K extends Comparable<? super K>> NavigableSet<K> subSet(NavigableSet<K> set, Range<K> range) {
if (set.comparator() != null && set.comparator() != Ordering.natural()
&& range.hasLowerBound()
&& range.hasUpperBound()) {
checkArgument(set.comparator().compare(range.lowerEndpoint(), range.upperEndpoint()) <= 0, "set is using a custom comparator which is inconsistent with the natural ordering.");
}
if (range.hasLowerBound() && range.hasUpperBound()) {
return set.subSet(
range.lowerEndpoint(), range.lowerBoundType() == BoundType.CLOSED,
range.upperEndpoint(), range.upperBoundType() == BoundType.CLOSED);
} else if (range.hasLowerBound()) {
return set.tailSet(range.lowerEndpoint(), range.lowerBoundType() == BoundType.CLOSED);
} else if (range.hasUpperBound()) {
return set.headSet(range.upperEndpoint(), range.upperBoundType() == BoundType.CLOSED);
}
return checkNotNull(set);
}
/**
* Returns a view of the portion of {@code set} whose elements are contained by {@code range}.
*
* <p>This method delegates to the appropriate methods of {@link NavigableSet} (namely
* {@link NavigableSet#subSet(Object, boolean, Object, boolean) subSet()},
* {@link NavigableSet#tailSet(Object, boolean) tailSet()}, and
* {@link NavigableSet#headSet(Object, boolean) headSet()}) 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 NavigableSet} 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 sets can lead to unexpected and undefined
* behavior.
*
* @since 20.0
*/
@Beta
@GwtIncompatible // NavigableSet
public static <K extends Comparable<? super K>> NavigableSet<K> subSet(NavigableSet<K> set, Range<K> range) {
if (set.comparator() != null && set.comparator() != Ordering.natural()
&& range.hasLowerBound()
&& range.hasUpperBound()) {
checkArgument(set.comparator().compare(range.lowerEndpoint(), range.upperEndpoint()) <= 0, "set is using a custom comparator which is inconsistent with the natural ordering.");
}
if (range.hasLowerBound() && range.hasUpperBound()) {
return set.subSet(
range.lowerEndpoint(),
range.lowerBoundType() == BoundType.CLOSED,
range.upperEndpoint(),
range.upperBoundType() == BoundType.CLOSED);
} else if (range.hasLowerBound()) {
return set.tailSet(range.lowerEndpoint(), range.lowerBoundType() == BoundType.CLOSED);
} else if (range.hasUpperBound()) {
return set.headSet(range.upperEndpoint(), range.upperBoundType() == BoundType.CLOSED);
}
return checkNotNull(set);
}
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testTailSetRanges(String description, NavigableSet navigableSet) {
NavigableSet subSet = navigableSet.tailSet(BigInteger.ONE, true);
// same subset
subSet.tailSet(BigInteger.ONE, true);
// slightly smaller
NavigableSet ns = subSet.tailSet(BigInteger.ONE, false);
// slight exapansion
assertThrows(() -> {
ns.tailSet(BigInteger.ONE, true);
},
IllegalArgumentException.class,
description + ": Expansion should not be allowed");
// much smaller
subSet.tailSet(isDescending(subSet) ? BigInteger.ZERO : BigInteger.TEN, false);
}
/**
* Returns a view of the portion of {@code set} whose elements are contained by {@code range}.
*
* <p>This method delegates to the appropriate methods of {@link NavigableSet} (namely
* {@link NavigableSet#subSet(Object, boolean, Object, boolean) subSet()},
* {@link NavigableSet#tailSet(Object, boolean) tailSet()}, and
* {@link NavigableSet#headSet(Object, boolean) headSet()}) 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 NavigableSet} 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 sets can lead to unexpected and undefined
* behavior.
*
* @since 20.0
*/
@Beta
@GwtIncompatible // NavigableSet
public static <K extends Comparable<? super K>> NavigableSet<K> subSet(
NavigableSet<K> set, Range<K> range) {
if (set.comparator() != null
&& set.comparator() != Ordering.natural()
&& range.hasLowerBound()
&& range.hasUpperBound()) {
checkArgument(
set.comparator().compare(range.lowerEndpoint(), range.upperEndpoint()) <= 0,
"set is using a custom comparator which is inconsistent with the natural ordering.");
}
if (range.hasLowerBound() && range.hasUpperBound()) {
return set.subSet(
range.lowerEndpoint(),
range.lowerBoundType() == BoundType.CLOSED,
range.upperEndpoint(),
range.upperBoundType() == BoundType.CLOSED);
} else if (range.hasLowerBound()) {
return set.tailSet(range.lowerEndpoint(), range.lowerBoundType() == BoundType.CLOSED);
} else if (range.hasUpperBound()) {
return set.headSet(range.upperEndpoint(), range.upperBoundType() == BoundType.CLOSED);
}
return checkNotNull(set);
}
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testTailSetRanges(String description, NavigableSet navigableSet) {
NavigableSet subSet = navigableSet.tailSet(BigInteger.ONE, true);
// same subset
subSet.tailSet(BigInteger.ONE, true);
// slightly smaller
NavigableSet ns = subSet.tailSet(BigInteger.ONE, false);
// slight exapansion
assertThrows(() -> {
ns.tailSet(BigInteger.ONE, true);
},
IllegalArgumentException.class,
description + ": Expansion should not be allowed");
// much smaller
subSet.tailSet(isDescending(subSet) ? BigInteger.ZERO : BigInteger.TEN, false);
}
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testTailSetRanges(String description, NavigableSet navigableSet) {
NavigableSet subSet = navigableSet.tailSet(BigInteger.ONE, true);
// same subset
subSet.tailSet(BigInteger.ONE, true);
// slightly smaller
NavigableSet ns = subSet.tailSet(BigInteger.ONE, false);
// slight exapansion
assertThrows(() -> {
ns.tailSet(BigInteger.ONE, true);
},
IllegalArgumentException.class,
description + ": Expansion should not be allowed");
// much smaller
subSet.tailSet(isDescending(subSet) ? BigInteger.ZERO : BigInteger.TEN, false);
}
@Override
public Iterator<ByteBuffer> scanRecords(UUID dataId, @Nullable ByteBuffer from, @Nullable ByteBuffer to,
int batchSize, int limit) {
NavigableSet<ByteBuffer> segments = _records.get(dataId);
if (segments == null) {
return Iterators.emptyIterator();
}
if (from != null) {
segments = segments.tailSet(from, true);
}
if (to != null) {
segments = segments.headSet(to, false);
}
return ImmutableList.copyOf(Iterators.limit(segments.iterator(), limit)).iterator();
}
/**
* Tests the tailSet() method.
*/
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testTailSet(String description, NavigableSet navigableSet) {
assertThrows(() -> {
navigableSet.tailSet(null);
},
NullPointerException.class,
description + ": Must throw NullPointerException for null element");
assertThrows(() -> {
navigableSet.tailSet(new Object());
}, ClassCastException.class);
NavigableSet ss = navigableSet.tailSet("1", true);
assertEmptyNavigableSet(ss, description + ": Returned value is not empty navigable set.");
}
/**
* Tests the tailSet() method.
*/
@Test(dataProvider = "NavigableSet<?>", dataProviderClass = EmptyNavigableSet.class)
public void testTailSet(String description, NavigableSet navigableSet) {
assertThrowsNPE(() -> {
navigableSet.tailSet(null);
},
description + ": Must throw NullPointerException for null element");
assertThrowsCCE(() -> {
navigableSet.tailSet(new Object());
}, description);
NavigableSet ss = navigableSet.tailSet("1", true);
assertEmptyNavigableSet(ss, description + ": Returned value is not empty navigable set.");
}
protected SpanInfo<Column> getColumnCellSpan(NavigableSet<Column> columns, Column column, Row row, Cell cell)
{
int span = 1;
Column lastCol = column;
for (Column tailCol : columns.tailSet(column, false))
{
Cell tailCell = row.getCell(tailCol);
if (tailCell == null || !cell.accept(spanCheck, tailCell))
{
break;
}
++span;
lastCol = tailCol;
}
return new SpanInfo<Column>(span, lastCol);
}
/**
* Return a container matching the attributes specified.
*
* @param size - Space needed in the Container.
* @param owner - Owner of the container - A specific nameservice.
* @param pipelineID - ID of the pipeline
* @param containerIDs - Set of containerIDs to choose from
* @return ContainerInfo, null if there is no match found.
*/
ContainerInfo getMatchingContainer(final long size, String owner,
PipelineID pipelineID, NavigableSet<ContainerID> containerIDs) {
if (containerIDs.isEmpty()) {
return null;
}
// Get the last used container and find container above the last used
// container ID.
final ContainerState key = new ContainerState(owner, pipelineID);
final ContainerID lastID =
lastUsedMap.getOrDefault(key, containerIDs.first());
// There is a small issue here. The first time, we will skip the first
// container. But in most cases it will not matter.
NavigableSet<ContainerID> resultSet = containerIDs.tailSet(lastID, false);
if (resultSet.size() == 0) {
resultSet = containerIDs;
}
ContainerInfo selectedContainer =
findContainerWithSpace(size, resultSet, owner, pipelineID);
if (selectedContainer == null) {
// If we did not find any space in the tailSet, we need to look for
// space in the headset, we need to pass true to deal with the
// situation that we have a lone container that has space. That is we
// ignored the last used container under the assumption we can find
// other containers with space, but if have a single container that is
// not true. Hence we need to include the last used container as the
// last element in the sorted set.
resultSet = containerIDs.headSet(lastID, true);
selectedContainer =
findContainerWithSpace(size, resultSet, owner, pipelineID);
}
return selectedContainer;
}
/**
* tailSet returns set with keys in requested range
*/
public void testTailSetContents() {
NavigableSet set = set5();
SortedSet sm = set.tailSet(two);
assertFalse(sm.contains(one));
assertTrue(sm.contains(two));
assertTrue(sm.contains(three));
assertTrue(sm.contains(four));
assertTrue(sm.contains(five));
Iterator i = sm.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());
SortedSet ssm = sm.tailSet(four);
assertEquals(four, ssm.first());
assertEquals(five, ssm.last());
assertTrue(ssm.remove(four));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, set.size());
}
/**
* tailSet returns set with keys in requested range
*/
public void testTailSetContents() {
NavigableSet set = set5();
SortedSet sm = set.tailSet(two);
assertFalse(sm.contains(one));
assertTrue(sm.contains(two));
assertTrue(sm.contains(three));
assertTrue(sm.contains(four));
assertTrue(sm.contains(five));
Iterator i = sm.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());
SortedSet ssm = sm.tailSet(four);
assertEquals(four, ssm.first());
assertEquals(five, ssm.last());
assertTrue(ssm.remove(four));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, set.size());
}
/**
* tailSet returns set with keys in requested range
*/
public void testDescendingTailSetContents() {
NavigableSet set = dset5();
SortedSet sm = set.tailSet(m2);
assertFalse(sm.contains(m1));
assertTrue(sm.contains(m2));
assertTrue(sm.contains(m3));
assertTrue(sm.contains(m4));
assertTrue(sm.contains(m5));
Iterator i = sm.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());
SortedSet ssm = sm.tailSet(m4);
assertEquals(m4, ssm.first());
assertEquals(m5, ssm.last());
assertTrue(ssm.remove(m4));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, set.size());
}
/**
* tailSet returns set with keys in requested range
*/
public void testTailSetContents() {
NavigableSet set = set5();
SortedSet sm = set.tailSet(two);
assertFalse(sm.contains(one));
assertTrue(sm.contains(two));
assertTrue(sm.contains(three));
assertTrue(sm.contains(four));
assertTrue(sm.contains(five));
Iterator i = sm.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());
SortedSet ssm = sm.tailSet(four);
assertEquals(four, ssm.first());
assertEquals(five, ssm.last());
assertTrue(ssm.remove(four));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, set.size());
}
/**
* tailSet returns set with keys in requested range
*/
public void testTailSetContents() {
NavigableSet set = set5();
SortedSet sm = set.tailSet(two);
assertFalse(sm.contains(one));
assertTrue(sm.contains(two));
assertTrue(sm.contains(three));
assertTrue(sm.contains(four));
assertTrue(sm.contains(five));
Iterator i = sm.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());
SortedSet ssm = sm.tailSet(four);
assertEquals(four, ssm.first());
assertEquals(five, ssm.last());
assertTrue(ssm.remove(four));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, set.size());
}
/**
* tailSet returns set with keys in requested range
*/
public void testTailSetContents() {
NavigableSet set = set5();
SortedSet sm = set.tailSet(two);
assertFalse(sm.contains(one));
assertTrue(sm.contains(two));
assertTrue(sm.contains(three));
assertTrue(sm.contains(four));
assertTrue(sm.contains(five));
Iterator i = sm.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());
SortedSet ssm = sm.tailSet(four);
assertEquals(four, ssm.first());
assertEquals(five, ssm.last());
assertTrue(ssm.remove(four));
assertEquals(1, ssm.size());
assertEquals(3, sm.size());
assertEquals(4, set.size());
}
private static Iterable<@NonNull ITmfStateInterval> searchforEndTime(NavigableSet<@NonNull ITmfStateInterval> tree, int quark, long time) {
ITmfStateInterval dummyInterval = new TmfStateInterval(-1, time, quark, (Object) null);
return tree.tailSet(dummyInterval);
}