Java Code Examples for java.util.NavigableMap#headMap()

The following examples show how to use java.util.NavigableMap#headMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EmptyNavigableMap.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testheadMapRanges(String description, NavigableMap navigableMap) {
    NavigableMap subMap = navigableMap.headMap(BigInteger.ONE, true);

    // same subset
    subMap.headMap(BigInteger.ONE, true);

    // slightly smaller
    NavigableMap ns = subMap.headMap(BigInteger.ONE, false);

    // slight exapansion
    assertThrows(() -> {
        ns.headMap(BigInteger.ONE, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true);
}
 
Example 2
Source File: RangeUtil.java    From kylin with Apache License 2.0 6 votes vote down vote up
/**
 * for NavigableMap sorted by C, given a range of C, return the sub map whose key falls in the range
 */
public static <C extends Comparable<?>, V> NavigableMap<C, V> filter(NavigableMap<C, V> values, Range<C> filterRange) {
    if (filterRange == null || filterRange.isEmpty()) {
        return Maps.newTreeMap();
    } else if (filterRange.equals(Range.all())) {
        return values;
    }

    if (filterRange.hasUpperBound() && !filterRange.hasLowerBound()) {
        return values.headMap(filterRange.upperEndpoint(), upperBoundInclusive(filterRange));
    } else if (filterRange.hasLowerBound() && !filterRange.hasUpperBound()) {
        return values.tailMap(filterRange.lowerEndpoint(), lowerBoundInclusive(filterRange));
    } else {
        return values.subMap(filterRange.lowerEndpoint(), lowerBoundInclusive(filterRange), //
                filterRange.upperEndpoint(), upperBoundInclusive(filterRange));
    }
}
 
Example 3
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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);
}
 
Example 4
Source File: InMemoryDataReaderDAO.java    From emodb with Apache License 2.0 6 votes vote down vote up
@Override
public Iterator<Change> readTimeline(Key key, boolean includeContentData,
                                     UUID start, UUID end, boolean reversed, long limit, ReadConsistency consistency) {
    checkNotNull(key, "key");

    String table = key.getTable().getName();

    Ordering<UUID> ordering = reversed ? TimeUUIDs.ordering().reverse() : TimeUUIDs.ordering();
    NavigableMap<UUID, Change> map = Maps.newTreeMap(ordering);
    if (includeContentData) {
        map.putAll(safeGet(_contentChanges, table, key.getKey()));
    }
    if (start != null) {
        map = map.tailMap(start, true);
    }
    if (end != null) {
        map = map.headMap(end, true);
    }
    return Iterators.limit(map.values().iterator(), (int) Math.min(limit, Integer.MAX_VALUE));
}
 
Example 5
Source File: TreeSubMapTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * headMap returns map with keys in requested range
 */
public void testHeadMapContents() {
    NavigableMap map = map5();
    SortedMap sm = map.headMap(four);
    assertTrue(sm.containsKey(one));
    assertTrue(sm.containsKey(two));
    assertTrue(sm.containsKey(three));
    assertFalse(sm.containsKey(four));
    assertFalse(sm.containsKey(five));
    Iterator i = sm.keySet().iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(one, k);
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    assertFalse(i.hasNext());
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, map.size());
    assertEquals(four, map.firstKey());
}
 
Example 6
Source File: EmptyNavigableMap.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testheadMapRanges(String description, NavigableMap navigableMap) {
    NavigableMap subMap = navigableMap.headMap(BigInteger.ONE, true);

    // same subset
    subMap.headMap(BigInteger.ONE, true);

    // slightly smaller
    NavigableMap ns = subMap.headMap(BigInteger.ONE, false);

    // slight exapansion
    assertThrows(() -> {
        ns.headMap(BigInteger.ONE, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true);
}
 
Example 7
Source File: SortedBuffer.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 8
Source File: SortedBuffer.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 9
Source File: EmptyNavigableMap.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testheadMapRanges(String description, NavigableMap navigableMap) {
    NavigableMap subMap = navigableMap.headMap(BigInteger.ONE, true);

    // same subset
    subMap.headMap(BigInteger.ONE, true);

    // slightly smaller
    NavigableMap ns = subMap.headMap(BigInteger.ONE, false);

    // slight exapansion
    assertThrows(() -> {
        ns.headMap(BigInteger.ONE, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true);
}
 
Example 10
Source File: EmptyNavigableMap.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testheadMapRanges(String description, NavigableMap navigableMap) {
    NavigableMap subMap = navigableMap.headMap(BigInteger.ONE, true);

    // same subset
    subMap.headMap(BigInteger.ONE, true);

    // slightly smaller
    NavigableMap ns = subMap.headMap(BigInteger.ONE, false);

    // slight exapansion
    assertThrows(() -> {
        ns.headMap(BigInteger.ONE, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true);
}
 
Example 11
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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);
}
 
Example 12
Source File: TreeSubMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * headMap returns map with keys in requested range
 */
public void testDescendingHeadMapContents() {
    NavigableMap 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());
}
 
Example 13
Source File: TreeSubMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * headMap returns map with keys in requested range
 */
public void testHeadMapContents() {
    NavigableMap map = map5();
    SortedMap sm = map.headMap(four);
    assertTrue(sm.containsKey(one));
    assertTrue(sm.containsKey(two));
    assertTrue(sm.containsKey(three));
    assertFalse(sm.containsKey(four));
    assertFalse(sm.containsKey(five));
    Iterator i = sm.keySet().iterator();
    Object k;
    k = (Integer)(i.next());
    assertEquals(one, k);
    k = (Integer)(i.next());
    assertEquals(two, k);
    k = (Integer)(i.next());
    assertEquals(three, k);
    assertFalse(i.hasNext());
    sm.clear();
    assertTrue(sm.isEmpty());
    assertEquals(2, map.size());
    assertEquals(four, map.firstKey());
}
 
Example 14
Source File: KafkaSpout.java    From incubator-heron with Apache License 2.0 6 votes vote down vote up
private void rewindAndDiscardAck(TopicPartition topicPartition,
                                 NavigableMap<Long, Long> ackRanges) {
  if (failureRegistry.containsKey(topicPartition)) {
    long earliestFailedOffset = failureRegistry.get(topicPartition);
    //rewind back to the earliest failed offset
    consumer.seek(topicPartition, earliestFailedOffset);
    //discard the ack whose offset is greater than the earliest failed offset
    //if there
    //is any because we've rewound the consumer back
    SortedMap<Long, Long> sortedMap = ackRanges.headMap(earliestFailedOffset);
    if (!sortedMap.isEmpty()) {
      sortedMap.put(sortedMap.lastKey(),
          Math.min(earliestFailedOffset,
              sortedMap.get(sortedMap.lastKey())));
    }
    ackRegistry.put(topicPartition, new TreeMap<>(sortedMap));
    //failure for this partition has been dealt with
    failureRegistry.remove(topicPartition);
  }
}
 
Example 15
Source File: EmptyNavigableMap.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testheadMapRanges(String description, NavigableMap navigableMap) {
    NavigableMap subMap = navigableMap.headMap(BigInteger.ONE, true);

    // same subset
    subMap.headMap(BigInteger.ONE, true);

    // slightly smaller
    NavigableMap ns = subMap.headMap(BigInteger.ONE, false);

    // slight exapansion
    assertThrows(() -> {
        ns.headMap(BigInteger.ONE, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true);
}
 
Example 16
Source File: RequestMetadata.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
/**
 * Get the best matched {@link RequestMetadata} via specified {@link RequestMetadata}.
 * @param requestMetadataMap the source of {@link NavigableMap}
 * @param requestMetadata the match object
 * @return if not matched, return <code>null</code>
 */
public static RequestMetadata getBestMatch(
		NavigableMap<RequestMetadata, RequestMetadata> requestMetadataMap,
		RequestMetadata requestMetadata) {

	RequestMetadata key = requestMetadata;

	RequestMetadata result = requestMetadataMap.get(key);

	if (result == null) {
		SortedMap<RequestMetadata, RequestMetadata> headMap = requestMetadataMap
				.headMap(key, true);
		result = headMap.isEmpty() ? null : requestMetadataMap.get(headMap.lastKey());
	}

	return result;
}
 
Example 17
Source File: EmptyNavigableMap.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testheadMapRanges(String description, NavigableMap navigableMap) {
    NavigableMap subMap = navigableMap.headMap(BigInteger.ONE, true);

    // same subset
    subMap.headMap(BigInteger.ONE, true);

    // slightly smaller
    NavigableMap ns = subMap.headMap(BigInteger.ONE, false);

    // slight exapansion
    assertThrows(() -> {
        ns.headMap(BigInteger.ONE, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true);
}
 
Example 18
Source File: EmptyNavigableMap.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testheadMapRanges(String description, NavigableMap navigableMap) {
    NavigableMap subMap = navigableMap.headMap(BigInteger.ONE, true);

    // same subset
    subMap.headMap(BigInteger.ONE, true);

    // slightly smaller
    NavigableMap ns = subMap.headMap(BigInteger.ONE, false);

    // slight exapansion
    assertThrows(() -> {
        ns.headMap(BigInteger.ONE, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subMap.headMap(isDescending(subMap) ? BigInteger.TEN : BigInteger.ZERO, true);
}
 
Example 19
Source File: Maps.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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);
}
 
Example 20
Source File: NoFileSortedOplog.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
@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());
}