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 * https://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 * 063 * @param entrySet The entrySet to iterate 064 */ 065 protected SortedMapIterator(final Set<Map.Entry<K, V>> entrySet) { 066 super(entrySet); 067 } 068 069 /** 070 * {@inheritDoc} 071 */ 072 @Override 073 public boolean hasPrevious() { 074 return ((ListIterator<Map.Entry<K, V>>) iterator).hasPrevious(); 075 } 076 077 /** 078 * {@inheritDoc} 079 */ 080 @Override 081 public K previous() { 082 entry = ((ListIterator<Map.Entry<K, V>>) iterator).previous(); 083 return getKey(); 084 } 085 086 /** 087 * {@inheritDoc} 088 */ 089 @Override 090 public synchronized void reset() { 091 super.reset(); 092 iterator = new ListIteratorWrapper<>(iterator); 093 } 094 } 095 096 /** 097 * Constructor only used in deserialization, do not use otherwise. 098 * 099 * @since 3.1 100 */ 101 protected AbstractSortedMapDecorator() { 102 } 103 104 /** 105 * Constructor that wraps (not copies). 106 * 107 * @param map The map to decorate, must not be null 108 * @throws NullPointerException if the map is null 109 */ 110 public AbstractSortedMapDecorator(final SortedMap<K, V> map) { 111 super(map); 112 } 113 114 @Override 115 public Comparator<? super K> comparator() { 116 return decorated().comparator(); 117 } 118 119 /** 120 * Gets the map being decorated. 121 * 122 * @return The decorated map 123 */ 124 @Override 125 protected SortedMap<K, V> decorated() { 126 return (SortedMap<K, V>) super.decorated(); 127 } 128 129 @Override 130 public K firstKey() { 131 return decorated().firstKey(); 132 } 133 134 @Override 135 public SortedMap<K, V> headMap(final K toKey) { 136 return decorated().headMap(toKey); 137 } 138 139 @Override 140 public K lastKey() { 141 return decorated().lastKey(); 142 } 143 144 /** 145 * {@inheritDoc} 146 */ 147 @Override 148 public OrderedMapIterator<K, V> mapIterator() { 149 return new SortedMapIterator<>(entrySet()); 150 } 151 152 @Override 153 public K nextKey(final K key) { 154 final Iterator<K> it = tailMap(key).keySet().iterator(); 155 it.next(); 156 return it.hasNext() ? it.next() : null; 157 } 158 159 @Override 160 public K previousKey(final K key) { 161 final SortedMap<K, V> headMap = headMap(key); 162 return headMap.isEmpty() ? null : headMap.lastKey(); 163 } 164 165 @Override 166 public SortedMap<K, V> subMap(final K fromKey, final K toKey) { 167 return decorated().subMap(fromKey, toKey); 168 } 169 170 @Override 171 public SortedMap<K, V> tailMap(final K fromKey) { 172 return decorated().tailMap(fromKey); 173 } 174}