@Override public void forEach(BiConsumer<? super K, ? super V> action) { checkNotNull(action); ImmutableList<K> keyList = keySet.asList(); for (int i = 0; i < size(); i++) { action.accept(keyList.get(i), valueList.get(i)); } }
private ImmutableSortedMap<K, V> getSubMap(int fromIndex, int toIndex) { if (fromIndex == 0 && toIndex == size()) { return this; } else if (fromIndex == toIndex) { return emptyMap(comparator()); } else { return from(keySet.getSubSet(fromIndex, toIndex), valueList.subList(fromIndex, toIndex)); } }
@Override public ImmutableSortedMap<K, V> descendingMap() { // TODO(kevinb): the descendingMap is never actually cached at all. Either it should be or the // code below simplified. ImmutableSortedMap<K, V> result = descendingMap; if (result == null) { if (isEmpty()) { return result = emptyMap(Ordering.from(comparator()).reverse()); } else { return result = new ImmutableSortedMap<K, V>( (RegularImmutableSortedSet<K>) keySet.descendingSet(), valueList.reverse(), this); } } return result; }
@Override public ImmutableSortedSet<K> descendingKeySet() { return keySet.descendingSet(); }
/** * This method returns a {@code ImmutableSortedMap}, consisting of the entries whose keys are * greater than (or equal to, if {@code inclusive}) {@code fromKey}. * * <p>The {@link SortedMap#tailMap} documentation states that a submap of a submap throws an * {@link IllegalArgumentException} if passed a {@code fromKey} less than an earlier {@code * fromKey}. However, this method doesn't throw an exception in that situation, but instead keeps * the original {@code fromKey}. * * @since 12.0 */ @Override public ImmutableSortedMap<K, V> tailMap(K fromKey, boolean inclusive) { return getSubMap(keySet.tailIndex(checkNotNull(fromKey), inclusive), size()); }
/** * This method returns a {@code ImmutableSortedMap}, consisting of the entries whose keys are less * than (or equal to, if {@code inclusive}) {@code toKey}. * * <p>The {@link SortedMap#headMap} documentation states that a submap of a submap throws an * {@link IllegalArgumentException} if passed a {@code toKey} greater than an earlier {@code * toKey}. However, this method doesn't throw an exception in that situation, but instead keeps * the original {@code toKey}. * * @since 12.0 */ @Override public ImmutableSortedMap<K, V> headMap(K toKey, boolean inclusive) { return getSubMap(0, keySet.headIndex(checkNotNull(toKey), inclusive)); }
@Override boolean isPartialView() { return keySet.isPartialView() || valueList.isPartialView(); }
@Override public V get(@Nullable Object key) { int index = keySet.indexOf(key); return (index == -1) ? null : valueList.get(index); }
@Override ImmutableSortedMap<K, V> createDescendingMap() { return new RegularImmutableSortedMap<K, V>( (RegularImmutableSortedSet<K>) keySet.descendingSet(), valueList.reverse(), this); }