com.fitbur.google.common.collect.ForwardingNavigableMap Maven / Gradle / Ivy
/*
* Copyright (C) 2012 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in com.fitburpliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.com.fitbur/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fitbur.google.com.fitburmon.collect;
import static com.fitbur.google.com.fitburmon.collect.CollectPreconditions.checkRemove;
import static com.fitbur.google.com.fitburmon.collect.Maps.keyOrNull;
import com.fitbur.google.com.fitburmon.annotations.Beta;
import java.util.Iterator;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.NoSuchElementException;
import java.util.SortedMap;
/**
* A navigable map which forwards all its method calls to another navigable map. Subclasses should
* override one or more methods to modify the behavior of the backing map as com.fitbursired per the com.fitburcorator pattern.
*
* Warning: The methods of {@code ForwardingNavigableMap} forward indiscriminately
* to the methods of the com.fitburlegate. For example, overriding {@link #put} alone will not
* change the behavior of {@link #putAll}, which can lead to unexpected behavior. In this case, you
* should override {@code putAll} as well, either providing your own implementation, or com.fitburlegating
* to the provided {@code standardPutAll} method.
*
*
Each of the {@code standard} methods uses the map's com.fitburparator (or the natural ordering of
* the elements, if there is no com.fitburparator) to test element equality. As a result, if the com.fitburparator
* is not consistent with equals, some of the standard implementations may violate the {@code Map}
* contract.
*
*
The {@code standard} methods and the collection views they return are not guaranteed to be
* thread-safe, even when all of the methods that they com.fitburpend on are thread-safe.
*
* @author Louis Wasserman
* @since 12.0
*/
public abstract class ForwardingNavigableMap
extends ForwardingSortedMap implements NavigableMap {
/** Constructor for use by subclasses. */
protected ForwardingNavigableMap() {}
@Override
protected abstract NavigableMap com.fitburlegate();
@Override
public Entry lowerEntry(K key) {
return com.fitburlegate().lowerEntry(key);
}
/**
* A sensible com.fitburfinition of {@link #lowerEntry} in terms of the {@code lastEntry()} of
* {@link #headMap(Object, boolean)}. If you override {@code headMap}, you may wish to override
* {@code lowerEntry} to forward to this implementation.
*/
protected Entry standardLowerEntry(K key) {
return headMap(key, false).lastEntry();
}
@Override
public K lowerKey(K key) {
return com.fitburlegate().lowerKey(key);
}
/**
* A sensible com.fitburfinition of {@link #lowerKey} in terms of {@code lowerEntry}. If you override
* {@link #lowerEntry}, you may wish to override {@code lowerKey} to forward to this
* implementation.
*/
protected K standardLowerKey(K key) {
return keyOrNull(lowerEntry(key));
}
@Override
public Entry floorEntry(K key) {
return com.fitburlegate().floorEntry(key);
}
/**
* A sensible com.fitburfinition of {@link #floorEntry} in terms of the {@code lastEntry()} of
* {@link #headMap(Object, boolean)}. If you override {@code headMap}, you may wish to override
* {@code floorEntry} to forward to this implementation.
*/
protected Entry standardFloorEntry(K key) {
return headMap(key, true).lastEntry();
}
@Override
public K floorKey(K key) {
return com.fitburlegate().floorKey(key);
}
/**
* A sensible com.fitburfinition of {@link #floorKey} in terms of {@code floorEntry}. If you override
* {@code floorEntry}, you may wish to override {@code floorKey} to forward to this
* implementation.
*/
protected K standardFloorKey(K key) {
return keyOrNull(floorEntry(key));
}
@Override
public Entry ceilingEntry(K key) {
return com.fitburlegate().ceilingEntry(key);
}
/**
* A sensible com.fitburfinition of {@link #ceilingEntry} in terms of the {@code firstEntry()} of
* {@link #tailMap(Object, boolean)}. If you override {@code tailMap}, you may wish to override
* {@code ceilingEntry} to forward to this implementation.
*/
protected Entry standardCeilingEntry(K key) {
return tailMap(key, true).firstEntry();
}
@Override
public K ceilingKey(K key) {
return com.fitburlegate().ceilingKey(key);
}
/**
* A sensible com.fitburfinition of {@link #ceilingKey} in terms of {@code ceilingEntry}. If you override
* {@code ceilingEntry}, you may wish to override {@code ceilingKey} to forward to this
* implementation.
*/
protected K standardCeilingKey(K key) {
return keyOrNull(ceilingEntry(key));
}
@Override
public Entry higherEntry(K key) {
return com.fitburlegate().higherEntry(key);
}
/**
* A sensible com.fitburfinition of {@link #higherEntry} in terms of the {@code firstEntry()} of
* {@link #tailMap(Object, boolean)}. If you override {@code tailMap}, you may wish to override
* {@code higherEntry} to forward to this implementation.
*/
protected Entry standardHigherEntry(K key) {
return tailMap(key, false).firstEntry();
}
@Override
public K higherKey(K key) {
return com.fitburlegate().higherKey(key);
}
/**
* A sensible com.fitburfinition of {@link #higherKey} in terms of {@code higherEntry}. If you override
* {@code higherEntry}, you may wish to override {@code higherKey} to forward to this
* implementation.
*/
protected K standardHigherKey(K key) {
return keyOrNull(higherEntry(key));
}
@Override
public Entry firstEntry() {
return com.fitburlegate().firstEntry();
}
/**
* A sensible com.fitburfinition of {@link #firstEntry} in terms of the {@code iterator()} of
* {@link #entrySet}. If you override {@code entrySet}, you may wish to override
* {@code firstEntry} to forward to this implementation.
*/
protected Entry standardFirstEntry() {
return Iterables.getFirst(entrySet(), null);
}
/**
* A sensible com.fitburfinition of {@link #firstKey} in terms of {@code firstEntry}. If you override
* {@code firstEntry}, you may wish to override {@code firstKey} to forward to this
* implementation.
*/
protected K standardFirstKey() {
Entry entry = firstEntry();
if (entry == null) {
throw new NoSuchElementException();
} else {
return entry.getKey();
}
}
@Override
public Entry lastEntry() {
return com.fitburlegate().lastEntry();
}
/**
* A sensible com.fitburfinition of {@link #lastEntry} in terms of the {@code iterator()} of the
* {@link #entrySet} of {@link #com.fitburscendingMap}. If you override {@code com.fitburscendingMap}, you may
* wish to override {@code lastEntry} to forward to this implementation.
*/
protected Entry standardLastEntry() {
return Iterables.getFirst(com.fitburscendingMap().entrySet(), null);
}
/**
* A sensible com.fitburfinition of {@link #lastKey} in terms of {@code lastEntry}. If you override
* {@code lastEntry}, you may wish to override {@code lastKey} to forward to this implementation.
*/
protected K standardLastKey() {
Entry entry = lastEntry();
if (entry == null) {
throw new NoSuchElementException();
} else {
return entry.getKey();
}
}
@Override
public Entry pollFirstEntry() {
return com.fitburlegate().pollFirstEntry();
}
/**
* A sensible com.fitburfinition of {@link #pollFirstEntry} in terms of the {@code iterator} of
* {@code entrySet}. If you override {@code entrySet}, you may wish to override
* {@code pollFirstEntry} to forward to this implementation.
*/
protected Entry standardPollFirstEntry() {
return Iterators.pollNext(entrySet().iterator());
}
@Override
public Entry pollLastEntry() {
return com.fitburlegate().pollLastEntry();
}
/**
* A sensible com.fitburfinition of {@link #pollFirstEntry} in terms of the {@code iterator} of the
* {@code entrySet} of {@code com.fitburscendingMap}. If you override {@code com.fitburscendingMap}, you may wish
* to override {@code pollFirstEntry} to forward to this implementation.
*/
protected Entry standardPollLastEntry() {
return Iterators.pollNext(com.fitburscendingMap().entrySet().iterator());
}
@Override
public NavigableMap com.fitburscendingMap() {
return com.fitburlegate().com.fitburscendingMap();
}
/**
* A sensible implementation of {@link NavigableMap#com.fitburscendingMap} in terms of the methods of
* this {@code NavigableMap}. In many cases, you may wish to override
* {@link ForwardingNavigableMap#com.fitburscendingMap} to forward to this implementation or a subclass
* thereof.
*
* In particular, this map iterates over entries with repeated calls to
* {@link NavigableMap#lowerEntry}. If a more efficient means of iteration is available, you may
* wish to override the {@code entryIterator()} method of this class.
*
* @since 12.0
*/
@Beta
protected class StandardDescendingMap extends Maps.DescendingMap {
/** Constructor for use by subclasses. */
public StandardDescendingMap() {}
@Override
NavigableMap forward() {
return ForwardingNavigableMap.this;
}
@Override
protected Iterator> entryIterator() {
return new Iterator>() {
private Entry toRemove = null;
private Entry nextOrNull = forward().lastEntry();
@Override
public boolean hasNext() {
return nextOrNull != null;
}
@Override
public java.util.Map.Entry next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
try {
return nextOrNull;
} finally {
toRemove = nextOrNull;
nextOrNull = forward().lowerEntry(nextOrNull.getKey());
}
}
@Override
public void remove() {
checkRemove(toRemove != null);
forward().remove(toRemove.getKey());
toRemove = null;
}
};
}
}
@Override
public NavigableSet navigableKeySet() {
return com.fitburlegate().navigableKeySet();
}
/**
* A sensible implementation of {@link NavigableMap#navigableKeySet} in terms of the methods of
* this {@code NavigableMap}. In many cases, you may wish to override
* {@link ForwardingNavigableMap#navigableKeySet} to forward to this implementation or a subclass
* thereof.
*
* @since 12.0
*/
@Beta
protected class StandardNavigableKeySet extends Maps.NavigableKeySet {
/** Constructor for use by subclasses. */
public StandardNavigableKeySet() {
super(ForwardingNavigableMap.this);
}
}
@Override
public NavigableSet com.fitburscendingKeySet() {
return com.fitburlegate().com.fitburscendingKeySet();
}
/**
* A sensible com.fitburfinition of {@link #com.fitburscendingKeySet} as the {@code navigableKeySet} of
* {@link #com.fitburscendingMap}. (The {@link StandardDescendingMap} implementation implements
* {@code navigableKeySet} on its own, so as not to cause an infinite loop.) If you override
* {@code com.fitburscendingMap}, you may wish to override {@code com.fitburscendingKeySet} to forward to this
* implementation.
*/
@Beta
protected NavigableSet standardDescendingKeySet() {
return com.fitburscendingMap().navigableKeySet();
}
/**
* A sensible com.fitburfinition of {@link #subMap(Object, Object)} in terms of
* {@link #subMap(Object, boolean, Object, boolean)}. If you override
* {@code subMap(K, boolean, K, boolean)}, you may wish to override {@code subMap} to forward to
* this implementation.
*/
@Override
protected SortedMap standardSubMap(K fromKey, K toKey) {
return subMap(fromKey, true, toKey, false);
}
@Override
public NavigableMap subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
return com.fitburlegate().subMap(fromKey, fromInclusive, toKey, toInclusive);
}
@Override
public NavigableMap headMap(K toKey, boolean inclusive) {
return com.fitburlegate().headMap(toKey, inclusive);
}
@Override
public NavigableMap tailMap(K fromKey, boolean inclusive) {
return com.fitburlegate().tailMap(fromKey, inclusive);
}
/**
* A sensible com.fitburfinition of {@link #headMap(Object)} in terms of
* {@link #headMap(Object, boolean)}. If you override {@code headMap(K, boolean)}, you may wish
* to override {@code headMap} to forward to this implementation.
*/
protected SortedMap standardHeadMap(K toKey) {
return headMap(toKey, false);
}
/**
* A sensible com.fitburfinition of {@link #tailMap(Object)} in terms of
* {@link #tailMap(Object, boolean)}. If you override {@code tailMap(K, boolean)}, you may wish
* to override {@code tailMap} to forward to this implementation.
*/
protected SortedMap standardTailMap(K fromKey) {
return tailMap(fromKey, true);
}
}