AbstractSortedMapDecorator.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.map;

  18. import java.util.Comparator;
  19. import java.util.Iterator;
  20. import java.util.ListIterator;
  21. import java.util.Map;
  22. import java.util.Set;
  23. import java.util.SortedMap;

  24. import org.apache.commons.collections4.IterableSortedMap;
  25. import org.apache.commons.collections4.OrderedMapIterator;
  26. import org.apache.commons.collections4.iterators.ListIteratorWrapper;

  27. /**
  28.  * Provides a base decorator that enables additional functionality to be added
  29.  * to a Map via decoration.
  30.  * <p>
  31.  * Methods are forwarded directly to the decorated map.
  32.  * </p>
  33.  * <p>
  34.  * This implementation does not perform any special processing with the map views.
  35.  * Instead it simply returns the set/collection from the wrapped map. This may be
  36.  * undesirable, for example if you are trying to write a validating implementation
  37.  * it would provide a loophole around the validation.
  38.  * But, you might want that loophole, so this class is kept simple.
  39.  * </p>
  40.  *
  41.  * @param <K> the type of the keys in the map
  42.  * @param <V> the type of the values in the map
  43.  * @since 3.0
  44.  */
  45. public abstract class AbstractSortedMapDecorator<K, V> extends AbstractMapDecorator<K, V> implements
  46.         IterableSortedMap<K, V> {

  47.     /**
  48.      * OrderedMapIterator implementation.
  49.      *
  50.      * @param <K>  the key type
  51.      * @param <V>  the value type
  52.      */
  53.     protected static class SortedMapIterator<K, V> extends EntrySetToMapIteratorAdapter<K, V>
  54.             implements OrderedMapIterator<K, V> {

  55.         /**
  56.          * Create a new AbstractSortedMapDecorator.SortedMapIterator.
  57.          * @param entrySet  the entrySet to iterate
  58.          */
  59.         protected SortedMapIterator(final Set<Map.Entry<K, V>> entrySet) {
  60.             super(entrySet);
  61.         }

  62.         /**
  63.          * {@inheritDoc}
  64.          */
  65.         @Override
  66.         public boolean hasPrevious() {
  67.             return ((ListIterator<Map.Entry<K, V>>) iterator).hasPrevious();
  68.         }

  69.         /**
  70.          * {@inheritDoc}
  71.          */
  72.         @Override
  73.         public K previous() {
  74.             entry = ((ListIterator<Map.Entry<K, V>>) iterator).previous();
  75.             return getKey();
  76.         }

  77.         /**
  78.          * {@inheritDoc}
  79.          */
  80.         @Override
  81.         public synchronized void reset() {
  82.             super.reset();
  83.             iterator = new ListIteratorWrapper<>(iterator);
  84.         }
  85.     }

  86.     /**
  87.      * Constructor only used in deserialization, do not use otherwise.
  88.      * @since 3.1
  89.      */
  90.     protected AbstractSortedMapDecorator() {
  91.     }

  92.     /**
  93.      * Constructor that wraps (not copies).
  94.      *
  95.      * @param map  the map to decorate, must not be null
  96.      * @throws NullPointerException if the map is null
  97.      */
  98.     public AbstractSortedMapDecorator(final SortedMap<K, V> map) {
  99.         super(map);
  100.     }

  101.     @Override
  102.     public Comparator<? super K> comparator() {
  103.         return decorated().comparator();
  104.     }

  105.     /**
  106.      * Gets the map being decorated.
  107.      *
  108.      * @return the decorated map
  109.      */
  110.     @Override
  111.     protected SortedMap<K, V> decorated() {
  112.         return (SortedMap<K, V>) super.decorated();
  113.     }

  114.     @Override
  115.     public K firstKey() {
  116.         return decorated().firstKey();
  117.     }

  118.     @Override
  119.     public SortedMap<K, V> headMap(final K toKey) {
  120.         return decorated().headMap(toKey);
  121.     }

  122.     @Override
  123.     public K lastKey() {
  124.         return decorated().lastKey();
  125.     }

  126.     /**
  127.      * {@inheritDoc}
  128.      */
  129.     @Override
  130.     public OrderedMapIterator<K, V> mapIterator() {
  131.         return new SortedMapIterator<>(entrySet());
  132.     }

  133.     @Override
  134.     public K nextKey(final K key) {
  135.         final Iterator<K> it = tailMap(key).keySet().iterator();
  136.         it.next();
  137.         return it.hasNext() ? it.next() : null;
  138.     }

  139.     @Override
  140.     public K previousKey(final K key) {
  141.         final SortedMap<K, V> headMap = headMap(key);
  142.         return headMap.isEmpty() ? null : headMap.lastKey();
  143.     }

  144.     @Override
  145.     public SortedMap<K, V> subMap(final K fromKey, final K toKey) {
  146.         return decorated().subMap(fromKey, toKey);
  147.     }

  148.     @Override
  149.     public SortedMap<K, V> tailMap(final K fromKey) {
  150.         return decorated().tailMap(fromKey);
  151.     }
  152. }