001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.map; 018 019import java.util.Comparator; 020import java.util.Iterator; 021import java.util.ListIterator; 022import java.util.Map; 023import java.util.Set; 024import java.util.SortedMap; 025 026import org.apache.commons.collections4.IterableSortedMap; 027import org.apache.commons.collections4.OrderedMapIterator; 028import org.apache.commons.collections4.iterators.ListIteratorWrapper; 029 030/** 031 * Provides a base decorator that enables additional functionality to be added 032 * to a Map via decoration. 033 * <p> 034 * Methods are forwarded directly to the decorated map. 035 * </p> 036 * <p> 037 * This implementation does not perform any special processing with the map views. 038 * Instead it simply returns the set/collection from the wrapped map. This may be 039 * undesirable, for example if you are trying to write a validating implementation 040 * it would provide a loophole around the validation. 041 * But, you might want that loophole, so this class is kept simple. 042 * </p> 043 * 044 * @param <K> the type of the keys in the map 045 * @param <V> the type of the values in the map 046 * @since 3.0 047 */ 048public abstract class AbstractSortedMapDecorator<K, V> extends AbstractMapDecorator<K, V> implements 049 IterableSortedMap<K, V> { 050 051 /** 052 * OrderedMapIterator implementation. 053 * 054 * @param <K> the key type 055 * @param <V> the value type 056 */ 057 protected static class SortedMapIterator<K, V> extends EntrySetToMapIteratorAdapter<K, V> 058 implements OrderedMapIterator<K, V> { 059 060 /** 061 * Create a new AbstractSortedMapDecorator.SortedMapIterator. 062 * @param entrySet the entrySet to iterate 063 */ 064 protected SortedMapIterator(final Set<Map.Entry<K, V>> entrySet) { 065 super(entrySet); 066 } 067 068 /** 069 * {@inheritDoc} 070 */ 071 @Override 072 public boolean hasPrevious() { 073 return ((ListIterator<Map.Entry<K, V>>) iterator).hasPrevious(); 074 } 075 076 /** 077 * {@inheritDoc} 078 */ 079 @Override 080 public K previous() { 081 entry = ((ListIterator<Map.Entry<K, V>>) iterator).previous(); 082 return getKey(); 083 } 084 085 /** 086 * {@inheritDoc} 087 */ 088 @Override 089 public synchronized void reset() { 090 super.reset(); 091 iterator = new ListIteratorWrapper<>(iterator); 092 } 093 } 094 095 /** 096 * Constructor only used in deserialization, do not use otherwise. 097 * @since 3.1 098 */ 099 protected AbstractSortedMapDecorator() { 100 } 101 102 /** 103 * Constructor that wraps (not copies). 104 * 105 * @param map the map to decorate, must not be null 106 * @throws NullPointerException if the map is null 107 */ 108 public AbstractSortedMapDecorator(final SortedMap<K, V> map) { 109 super(map); 110 } 111 112 @Override 113 public Comparator<? super K> comparator() { 114 return decorated().comparator(); 115 } 116 117 /** 118 * Gets the map being decorated. 119 * 120 * @return the decorated map 121 */ 122 @Override 123 protected SortedMap<K, V> decorated() { 124 return (SortedMap<K, V>) super.decorated(); 125 } 126 127 @Override 128 public K firstKey() { 129 return decorated().firstKey(); 130 } 131 132 @Override 133 public SortedMap<K, V> headMap(final K toKey) { 134 return decorated().headMap(toKey); 135 } 136 137 @Override 138 public K lastKey() { 139 return decorated().lastKey(); 140 } 141 142 /** 143 * {@inheritDoc} 144 */ 145 @Override 146 public OrderedMapIterator<K, V> mapIterator() { 147 return new SortedMapIterator<>(entrySet()); 148 } 149 150 @Override 151 public K nextKey(final K key) { 152 final Iterator<K> it = tailMap(key).keySet().iterator(); 153 it.next(); 154 return it.hasNext() ? it.next() : null; 155 } 156 157 @Override 158 public K previousKey(final K key) { 159 final SortedMap<K, V> headMap = headMap(key); 160 return headMap.isEmpty() ? null : headMap.lastKey(); 161 } 162 163 @Override 164 public SortedMap<K, V> subMap(final K fromKey, final K toKey) { 165 return decorated().subMap(fromKey, toKey); 166 } 167 168 @Override 169 public SortedMap<K, V> tailMap(final K fromKey) { 170 return decorated().tailMap(fromKey); 171 } 172}