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()}
(NavigableMap<K, V> map, Range<K> range)
| 4459 | * @since 20.0 |
| 4460 | */ |
| 4461 | @GwtIncompatible // NavigableMap |
| 4462 | public static <K extends Comparable<? super K>, V extends @Nullable Object> |
| 4463 | NavigableMap<K, V> subMap(NavigableMap<K, V> map, Range<K> range) { |
| 4464 | if (map.comparator() != null |
| 4465 | && map.comparator() != Ordering.natural() |
| 4466 | && range.hasLowerBound() |
| 4467 | && range.hasUpperBound()) { |
| 4468 | checkArgument( |
| 4469 | map.comparator().compare(range.lowerEndpoint(), range.upperEndpoint()) <= 0, |
| 4470 | "map is using a custom comparator which is inconsistent with the natural ordering."); |
| 4471 | } |
| 4472 | if (range.hasLowerBound() && range.hasUpperBound()) { |
| 4473 | return map.subMap( |
| 4474 | range.lowerEndpoint(), |
| 4475 | range.lowerBoundType() == BoundType.CLOSED, |
| 4476 | range.upperEndpoint(), |
| 4477 | range.upperBoundType() == BoundType.CLOSED); |
| 4478 | } else if (range.hasLowerBound()) { |
| 4479 | return map.tailMap(range.lowerEndpoint(), range.lowerBoundType() == BoundType.CLOSED); |
| 4480 | } else if (range.hasUpperBound()) { |
| 4481 | return map.headMap(range.upperEndpoint(), range.upperBoundType() == BoundType.CLOSED); |
| 4482 | } |
| 4483 | return checkNotNull(map); |
| 4484 | } |
| 4485 | } |