All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gwt.emul.java.util.AbstractNavigableMap Maven / Gradle / Ivy

There is a newer version: 2.10.0
Show newest version
/*
 * Copyright 2014 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/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 java.util;

/**
 * Skeletal implementation of a NavigableMap.
 */
abstract class AbstractNavigableMap extends AbstractMap implements NavigableMap {

  class DescendingMap extends AbstractNavigableMap {
    @Override
    public void clear() {
      ascendingMap().clear();
    }

    @Override
    public Comparator comparator() {
      return Collections.reverseOrder(ascendingMap().comparator());
    }

    @Override
    public NavigableMap descendingMap() {
      return ascendingMap();
    }

    @Override
    public NavigableMap headMap(K toKey, boolean inclusive) {
      return ascendingMap().tailMap(toKey, inclusive).descendingMap();
    }

    @Override
    public V put(K key, V value) {
      return ascendingMap().put(key, value);
    }

    @Override
    public V remove(Object key) {
      return ascendingMap().remove(key);
    }

    @Override
    public int size() {
      return ascendingMap().size();
    }

    @Override
    public NavigableMap subMap(K fromKey, boolean fromInclusive,
        K toKey, boolean toInclusive) {
      return ascendingMap().subMap(toKey, toInclusive, fromKey, fromInclusive).descendingMap();
    }

    @Override
    public NavigableMap tailMap(K fromKey, boolean inclusive) {
      return ascendingMap().headMap(fromKey, inclusive).descendingMap();
    }

    AbstractNavigableMap ascendingMap() {
      return AbstractNavigableMap.this;
    }

    @Override
    Iterator> descendingEntryIterator() {
      return ascendingMap().entryIterator();
    }

    @Override
    Iterator> entryIterator() {
      return ascendingMap().descendingEntryIterator();
    }

    @Override
    Entry getEntry(K key) {
      return ascendingMap().getEntry(key);
    }

    @Override
    Entry getFirstEntry() {
      return ascendingMap().getLastEntry();
    }

    @Override
    Entry getLastEntry() {
      return ascendingMap().getFirstEntry();
    }

    @Override
    Entry getCeilingEntry(K key) {
      return ascendingMap().getFloorEntry(key);
    }

    @Override
    Entry getFloorEntry(K key) {
      return ascendingMap().getCeilingEntry(key);
    }

    @Override
    Entry getHigherEntry(K key) {
      return ascendingMap().getLowerEntry(key);
    }

    @Override
    Entry getLowerEntry(K key) {
      return ascendingMap().getHigherEntry(key);
    }

    @Override
    boolean removeEntry(Entry entry) {
      return ascendingMap().removeEntry(entry);
    }
  }

  class EntrySet extends AbstractSet> {
    @Override
    public boolean contains(Object o) {
      return (o instanceof Entry) && containsEntry((Entry) o);
    }

    @Override
    public Iterator> iterator() {
      return entryIterator();
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean remove(Object o) {
      if (o instanceof Entry) {
        Entry entry = (Entry) o;
        return removeEntry(entry);
      }
      return false;
    }

    @Override
    public int size() {
      return AbstractNavigableMap.this.size();
    }
  }

  private static final class NavigableKeySet extends AbstractSet
      implements NavigableSet {

    private final NavigableMap map;

    NavigableKeySet(NavigableMap map) {
      this.map = map;
    }

    @Override
    public K ceiling(K k) {
      return map.ceilingKey(k);
    }

    @Override
    public void clear() {
      map.clear();
    }

    @Override
    public Comparator comparator() {
      return map.comparator();
    }

    @Override
    public boolean contains(Object o) {
      return map.containsKey(o);
    }

    @Override
    public Iterator descendingIterator() {
      return descendingSet().iterator();
    }

    @Override
    public NavigableSet descendingSet() {
      return map.descendingMap().navigableKeySet();
    }

    @Override
    public K first() {
      return map.firstKey();
    }

    @Override
    public K floor(K k) {
      return map.floorKey(k);
    }

    @Override
    public SortedSet headSet(K toElement) {
      return headSet(toElement, false);
    }

    @Override
    public NavigableSet headSet(K toElement, boolean inclusive) {
      return map.headMap(toElement, inclusive).navigableKeySet();
    }

    @Override
    public K higher(K k) {
      return map.higherKey(k);
    }

    @Override
    public Iterator iterator() {
      final Iterator> entryIterator = map.entrySet().iterator();
      return new Iterator() {
        @Override
        public boolean hasNext() {
          return entryIterator.hasNext();
        }

        @Override
        public K next() {
          Entry entry = entryIterator.next();
          return entry.getKey();
        }

        @Override
        public void remove() {
          entryIterator.remove();
        }
      };
    }

    @Override
    public K last() {
      return map.lastKey();
    }

    @Override
    public K lower(K k) {
      return map.lowerKey(k);
    }

    @Override
    public K pollFirst() {
      return getEntryKeyOrNull(map.pollFirstEntry());
    }

    @Override
    public K pollLast() {
      return getEntryKeyOrNull(map.pollLastEntry());
    }

    @Override
    public boolean remove(Object o) {
      if (map.containsKey(o)) {
        map.remove(o);
        return true;
      }
      return false;
    }

    @Override
    public int size() {
      return map.size();
    }

    @Override
    public NavigableSet subSet(K fromElement, boolean fromInclusive,
        K toElement, boolean toInclusive) {
      return map.subMap(fromElement, fromInclusive, toElement, toInclusive).navigableKeySet();
    }

    @Override
    public SortedSet subSet(K fromElement, K toElement) {
      return subSet(fromElement, true, toElement, false);
    }

    @Override
    public SortedSet tailSet(K fromElement) {
      return tailSet(fromElement, true);
    }

    @Override
    public NavigableSet tailSet(K fromElement, boolean inclusive) {
      return map.tailMap(fromElement, inclusive).navigableKeySet();
    }
  }

  private static  Entry copyOf(Entry entry) {
    return entry == null ? null : new SimpleImmutableEntry(entry);
  }

  private static  K getKeyOrNSE(Entry entry) {
    if (entry == null) {
      throw new NoSuchElementException();
    }
    return entry.getKey();
  }

  @Override
  public Entry ceilingEntry(K key) {
    return copyOf(getCeilingEntry(key));
  }

  @Override
  public K ceilingKey(K key) {
    return getEntryKeyOrNull(getCeilingEntry(key));
  }

  @SuppressWarnings("unchecked")
  @Override
  public boolean containsKey(Object k) {
    K key = (K) k;
    return getEntry(key) != null;
  }

  @Override
  public NavigableSet descendingKeySet() {
    return descendingMap().navigableKeySet();
  }

  @Override
  public NavigableMap descendingMap() {
    return new DescendingMap();
  }

  @Override
  public Set> entrySet() {
    return new EntrySet();
  }

  @Override
  public Entry firstEntry() {
    return copyOf(getFirstEntry());
  }

  @Override
  public K firstKey() {
    return getKeyOrNSE(getFirstEntry());
  }

  @Override
  public Entry floorEntry(K key) {
    return copyOf(getFloorEntry(key));
  }

  @Override
  public K floorKey(K key) {
    return getEntryKeyOrNull(getFloorEntry(key));
  }

  @SuppressWarnings("unchecked")
  @Override
  public V get(Object k) {
    K key = (K) k;
    return getEntryValueOrNull(getEntry(key));
  }

  @Override
  public SortedMap headMap(K toKey) {
    return headMap(toKey, false);
  }

  @Override
  public Entry higherEntry(K key) {
    return copyOf(getHigherEntry(key));
  }

  @Override
  public K higherKey(K key) {
    return getEntryKeyOrNull(getHigherEntry(key));
  }

  @Override
  public Set keySet() {
    return navigableKeySet();
  }

  @Override
  public Entry lastEntry() {
    return copyOf(getLastEntry());
  }

  @Override
  public K lastKey() {
    return getKeyOrNSE(getLastEntry());
  }

  @Override
  public Entry lowerEntry(K key) {
    return copyOf(getLowerEntry(key));
  }

  @Override
  public K lowerKey(K key) {
    return getEntryKeyOrNull(getLowerEntry(key));
  }

  @Override
  public NavigableSet navigableKeySet() {
    return new NavigableKeySet(this);
  }

  @Override
  public Entry pollFirstEntry() {
    return pollEntry(getFirstEntry());
  }

  @Override
  public Entry pollLastEntry() {
    return pollEntry(getLastEntry());
  }

  @Override
  public SortedMap subMap(K fromKey, K toKey) {
    return subMap(fromKey, true, toKey, false);
  }

  @Override
  public SortedMap tailMap(K fromKey) {
    return tailMap(fromKey, true);
  }

  @SuppressWarnings("unchecked")
  @Override
  boolean containsEntry(Entry entry) {
    K key = (K) entry.getKey();
    Entry lookupEntry = getEntry(key);
    return lookupEntry != null && Objects.equals(lookupEntry.getValue(), entry.getValue());
  }

  /**
   * Returns an iterator over the entries in this map in descending order.
   */
  abstract Iterator> descendingEntryIterator();

  /**
   * Returns an iterator over the entries in this map in ascending order.
   */
  abstract Iterator> entryIterator();

  /**
   * Returns the entry corresponding to the specified key. If no such entry exists returns
   * {@code null}.
   */
  abstract Entry getEntry(K key);

  /**
   * Returns the first entry or {@code null} if map is empty.
   */
  abstract Entry getFirstEntry();

  /**
   * Returns the last entry or {@code null} if map is empty.
   */
  abstract Entry getLastEntry();

  /**
   * Gets the entry corresponding to the specified key or the entry for the least key greater than
   * the specified key. If no such entry exists returns {@code null}.
   */
  abstract Entry getCeilingEntry(K key);

  /**
   * Gets the entry corresponding to the specified key or the entry for the greatest key less than
   * the specified key. If no such entry exists returns {@code null}.
   */
  abstract Entry getFloorEntry(K key);

  /**
   * Gets the entry for the least key greater than the specified key. If no such entry exists
   * returns {@code null}.
   */
  abstract Entry getHigherEntry(K key);

  /**
   * Returns the entry for the greatest key less than the specified key. If no such entry exists
   * returns {@code null}.
   */
  abstract Entry getLowerEntry(K key);

  /**
   * Remove an entry from the tree, returning whether it was found.
   */
  abstract boolean removeEntry(Entry entry);

  private Entry pollEntry(Entry entry) {
    if (entry != null) {
      removeEntry(entry);
    }
    return copyOf(entry);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy