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

The following examples show how to use java.util.NavigableMap#subMap() . 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 openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testSubMapRanges(String description, NavigableMap navigableMap) {
    Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO;
    Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;

    NavigableMap subMap = navigableMap.subMap(first, true, last, true);

    // same subset
    subMap.subMap(first, true, last, true);

    // slightly smaller
    NavigableMap ns = subMap.subMap(first, false, last, false);
    // slight exapansion
    assertThrows(() -> {
        ns.subMap(first, true, last, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subMap.subMap(first, false, BigInteger.ONE, false);
}
 
Example 2
Source File: TreeSubMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void testSubMapContents2() {
    NavigableMap 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());
}
 
Example 3
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 4
Source File: EmptyNavigableMap.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testSubMapRanges(String description, NavigableMap navigableMap) {
    Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO;
    Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;

    NavigableMap subMap = navigableMap.subMap(first, true, last, true);

    // same subset
    subMap.subMap(first, true, last, true);

    // slightly smaller
    NavigableMap ns = subMap.subMap(first, false, last, false);
    // slight expansion
    assertThrowsIAE(() -> {
        ns.subMap(first, true, last, true);
    },
        description + ": Expansion should not be allowed");

    // much smaller
    subMap.subMap(first, false, BigInteger.ONE, false);
}
 
Example 5
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 testSubMapRanges(String description, NavigableMap navigableMap) {
    Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO;
    Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;

    NavigableMap subMap = navigableMap.subMap(first, true, last, true);

    // same subset
    subMap.subMap(first, true, last, true);

    // slightly smaller
    NavigableMap ns = subMap.subMap(first, false, last, false);
    // slight exapansion
    assertThrows(() -> {
        ns.subMap(first, true, last, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subMap.subMap(first, false, BigInteger.ONE, false);
}
 
Example 6
Source File: AbstractPlacemarkDataController.java    From collect-earth with MIT License 6 votes vote down vote up
/**
 * Sort parameters based on schema order (BFS)
 * @param parameters Parameters to be sorted
 * @return The parameters sorted alphabetically
 */
public Map<String, String> sortParameters(Map<String, String> parameters) {
	NavigableMap<String, String> navigableParameters = new TreeMap<String, String>(parameters);
	//extract parameter names in order
	BalloonInputFieldsUtils collectParametersHandler = new BalloonInputFieldsUtils();
	Map<String, String> sortedParameterNameByNodePath = collectParametersHandler.getHtmlParameterNameByNodePath(earthSurveyService.getRootEntityDefinition());
	List<String> sortedParameterNames = new ArrayList<String>(sortedParameterNameByNodePath.values());
	
	//create a new map and put the parameters in order there
	Map<String, String> result = new LinkedHashMap<String, String>(navigableParameters.size());
	for (String parameterName : sortedParameterNames) {
		//get all the entries with key starting with parameterName
		SortedMap<String,String> subMap = new TreeMap<String, String>( navigableParameters.subMap(parameterName, parameterName + Character.MAX_VALUE) );
		Set<Entry<String,String>> entrySet = subMap.entrySet();
		for (Entry<String, String> entry : entrySet) {
				result.put(entry.getKey(), entry.getValue());
				navigableParameters.remove(entry.getKey());
		}
	}
	//add remaining parameters (if any)
	result.putAll(navigableParameters);
	return result;
}
 
Example 7
Source File: TreeSubMapTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testSubMapContents2() {
    NavigableMap 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());
}
 
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: 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 10
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 11
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 testSubMapRanges(String description, NavigableMap navigableMap) {
    Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO;
    Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;

    NavigableMap subMap = navigableMap.subMap(first, true, last, true);

    // same subset
    subMap.subMap(first, true, last, true);

    // slightly smaller
    NavigableMap ns = subMap.subMap(first, false, last, false);
    // slight exapansion
    assertThrows(() -> {
        ns.subMap(first, true, last, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subMap.subMap(first, false, BigInteger.ONE, false);
}
 
Example 12
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 testSubMapRanges(String description, NavigableMap navigableMap) {
    Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO;
    Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;

    NavigableMap subMap = navigableMap.subMap(first, true, last, true);

    // same subset
    subMap.subMap(first, true, last, true);

    // slightly smaller
    NavigableMap ns = subMap.subMap(first, false, last, false);
    // slight exapansion
    assertThrows(() -> {
        ns.subMap(first, true, last, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subMap.subMap(first, false, BigInteger.ONE, false);
}
 
Example 13
Source File: EmptyNavigableMap.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test(dataProvider = "NavigableMap<?,?>", dataProviderClass = EmptyNavigableMap.class)
public void testSubMapRanges(String description, NavigableMap navigableMap) {
    Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO;
    Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;

    NavigableMap subMap = navigableMap.subMap(first, true, last, true);

    // same subset
    subMap.subMap(first, true, last, true);

    // slightly smaller
    NavigableMap ns = subMap.subMap(first, false, last, false);
    // slight exapansion
    assertThrows(() -> {
        ns.subMap(first, true, last, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subMap.subMap(first, false, BigInteger.ONE, false);
}
 
Example 14
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 testSubMapRanges(String description, NavigableMap navigableMap) {
    Object first = isDescending(navigableMap) ? BigInteger.TEN : BigInteger.ZERO;
    Object last = (BigInteger.ZERO == first) ? BigInteger.TEN : BigInteger.ZERO;

    NavigableMap subMap = navigableMap.subMap(first, true, last, true);

    // same subset
    subMap.subMap(first, true, last, true);

    // slightly smaller
    NavigableMap ns = subMap.subMap(first, false, last, false);
    // slight exapansion
    assertThrows(() -> {
        ns.subMap(first, true, last, true);
    },
        IllegalArgumentException.class,
        description + ": Expansion should not be allowed");

    // much smaller
    subMap.subMap(first, false, BigInteger.ONE, false);
}
 
Example 15
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());
}
 
Example 16
Source File: ObjectStoreConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, NavigableMap<String, IndexableField> indexables) {
    String from = path;
    char character = path.charAt(path.length() - 1);
    character++;
    String to = path.substring(0, path.length() - 1) + character;
    indexables = indexables.subMap(from, true, to, false);
    Class<?> clazz = TypeUtility.getRawType(type, null);

    try {
        // TODO 此处需要代码重构
        Object instance = context.getInstance(clazz);
        for (Entry<Field, StoreConverter> keyValue : context.getStoreKeyValues(clazz).entrySet()) {
            // TODO 此处代码可以优反射次数.
            field = keyValue.getKey();
            StoreConverter converter = keyValue.getValue();
            annotation = field.getAnnotation(LuceneStore.class);
            String name = field.getName();
            type = field.getGenericType();
            Object data = converter.decode(context, path + "." + name, field, annotation, type, indexables);
            field.set(instance, data);
        }
        return instance;
    } catch (Exception exception) {
        // TODO
        throw new StorageException(exception);
    }
}
 
Example 17
Source File: ArrayStoreConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, NavigableMap<String, IndexableField> indexables) {
    String from = path;
    char character = path.charAt(path.length() - 1);
    character++;
    String to = path.substring(0, path.length() - 1) + character;
    indexables = indexables.subMap(from, true, to, false);
    Class<?> componentClass = null;
    Type componentType = null;
    if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = GenericArrayType.class.cast(type);
        componentType = genericArrayType.getGenericComponentType();
        componentClass = TypeUtility.getRawType(componentType, null);
    } else {
        Class<?> clazz = TypeUtility.getRawType(type, null);
        componentType = clazz.getComponentType();
        componentClass = clazz.getComponentType();
    }
    Specification specification = Specification.getSpecification(componentClass);
    StoreConverter converter = context.getStoreConverter(specification);
    IndexableField indexable = indexables.get(path + ".size");
    int size = indexable.numericValue().intValue();
    Object array = Array.newInstance(componentClass, size);
    for (int index = 0; index < size; index++) {
        Object element = converter.decode(context, path + "[" + index + "]", field, annotation, componentType, indexables);
        Array.set(array, index, element);
    }
    return array;
}
 
Example 18
Source File: NumberStoreConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, NavigableMap<String, IndexableField> indexables) {
    String from = path;
    char character = path.charAt(path.length() - 1);
    character++;
    String to = path.substring(0, path.length() - 1) + character;
    indexables = indexables.subMap(from, true, to, false);
    IndexableField indexable = indexables.firstEntry().getValue();
    Class<?> clazz = TypeUtility.getRawType(type, null);
    clazz = ClassUtility.primitiveToWrapper(clazz);
    Number number = indexable.numericValue();
    if (Byte.class.isAssignableFrom(clazz)) {
        return number.byteValue();
    }
    if (Short.class.isAssignableFrom(clazz)) {
        return number.shortValue();
    }
    if (Integer.class.isAssignableFrom(clazz)) {
        return number.intValue();
    }
    if (Long.class.isAssignableFrom(clazz)) {
        return number.longValue();
    }
    if (Float.class.isAssignableFrom(clazz)) {
        return number.floatValue();
    }
    if (Double.class.isAssignableFrom(clazz)) {
        return number.doubleValue();
    }
    throw new StorageException();
}
 
Example 19
Source File: InstantStoreConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public Object decode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, NavigableMap<String, IndexableField> indexables) {
    String from = path;
    char character = path.charAt(path.length() - 1);
    character++;
    String to = path.substring(0, path.length() - 1) + character;
    indexables = indexables.subMap(from, true, to, false);
    IndexableField indexable = indexables.firstEntry().getValue();
    Class<?> clazz = TypeUtility.getRawType(type, null);
    clazz = ClassUtility.primitiveToWrapper(clazz);
    Number number = indexable.numericValue();
    if (Instant.class.isAssignableFrom(clazz)) {
        return Instant.ofEpochMilli(number.longValue());
    }
    if (Date.class.isAssignableFrom(clazz)) {
        return new Date(number.longValue());
    }
    if (LocalDate.class.isAssignableFrom(clazz)) {

    }
    if (LocalTime.class.isAssignableFrom(clazz)) {

    }
    if (LocalDateTime.class.isAssignableFrom(clazz)) {

    }
    if (ZonedDateTime.class.isAssignableFrom(clazz)) {

    }
    if (ZoneOffset.class.isAssignableFrom(clazz)) {

    }
    throw new StorageException();
}
 
Example 20
Source File: TreeSubMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * subMap returns map with keys in requested range
 */
public void testDescendingSubMapContents() {
    NavigableMap map = dmap5();
    SortedMap sm = map.subMap(m2, m4);
    assertEquals(m2, sm.firstKey());
    assertEquals(m3, sm.lastKey());
    assertEquals(2, sm.size());
    assertFalse(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(m2, k);
    k = (Integer)(i.next());
    assertEquals(m3, k);
    assertFalse(i.hasNext());
    Iterator j = sm.keySet().iterator();
    j.next();
    j.remove();
    assertFalse(map.containsKey(m2));
    assertEquals(4, map.size());
    assertEquals(1, sm.size());
    assertEquals(m3, sm.firstKey());
    assertEquals(m3, sm.lastKey());
    assertEquals("C", sm.remove(m3));
    assertTrue(sm.isEmpty());
    assertEquals(3, map.size());
}