1. /*
  2. * Copyright 2003-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections.bidimap;
  17. import java.io.IOException;
  18. import java.io.ObjectInputStream;
  19. import java.io.ObjectOutputStream;
  20. import java.io.Serializable;
  21. import java.util.ArrayList;
  22. import java.util.Comparator;
  23. import java.util.Iterator;
  24. import java.util.ListIterator;
  25. import java.util.Map;
  26. import java.util.SortedMap;
  27. import java.util.TreeMap;
  28. import org.apache.commons.collections.BidiMap;
  29. import org.apache.commons.collections.OrderedBidiMap;
  30. import org.apache.commons.collections.OrderedMap;
  31. import org.apache.commons.collections.OrderedMapIterator;
  32. import org.apache.commons.collections.ResettableIterator;
  33. import org.apache.commons.collections.SortedBidiMap;
  34. import org.apache.commons.collections.map.AbstractSortedMapDecorator;
  35. /**
  36. * Implementation of <code>BidiMap</code> that uses two <code>TreeMap</code> instances.
  37. * <p>
  38. * The setValue() method on iterators will succeed only if the new value being set is
  39. * not already in the bidimap.
  40. * <p>
  41. * When considering whether to use this class, the {@link TreeBidiMap} class should
  42. * also be considered. It implements the interface using a dedicated design, and does
  43. * not store each object twice, which can save on memory use.
  44. * <p>
  45. * NOTE: From Commons Collections 3.1, all subclasses will use <code>TreeMap</code>
  46. * and the flawed <code>createMap</code> method is ignored.
  47. *
  48. * @since Commons Collections 3.0
  49. * @version $Id: DualTreeBidiMap.java,v 1.14 2004/06/11 23:27:37 scolebourne Exp $
  50. *
  51. * @author Matthew Hawthorne
  52. * @author Stephen Colebourne
  53. */
  54. public class DualTreeBidiMap
  55. extends AbstractDualBidiMap implements SortedBidiMap, Serializable {
  56. /** Ensure serialization compatibility */
  57. private static final long serialVersionUID = 721969328361809L;
  58. /** The comparator to use */
  59. protected final Comparator comparator;
  60. /**
  61. * Creates an empty <code>DualTreeBidiMap</code>
  62. */
  63. public DualTreeBidiMap() {
  64. super(new TreeMap(), new TreeMap());
  65. this.comparator = null;
  66. }
  67. /**
  68. * Constructs a <code>DualTreeBidiMap</code> and copies the mappings from
  69. * specified <code>Map</code>.
  70. *
  71. * @param map the map whose mappings are to be placed in this map
  72. */
  73. public DualTreeBidiMap(Map map) {
  74. super(new TreeMap(), new TreeMap());
  75. putAll(map);
  76. this.comparator = null;
  77. }
  78. /**
  79. * Constructs a <code>DualTreeBidiMap</code> using the specified Comparator.
  80. *
  81. * @param comparator the Comparator
  82. */
  83. public DualTreeBidiMap(Comparator comparator) {
  84. super(new TreeMap(comparator), new TreeMap(comparator));
  85. this.comparator = comparator;
  86. }
  87. /**
  88. * Constructs a <code>DualTreeBidiMap</code> that decorates the specified maps.
  89. *
  90. * @param normalMap the normal direction map
  91. * @param reverseMap the reverse direction map
  92. * @param inverseBidiMap the inverse BidiMap
  93. */
  94. protected DualTreeBidiMap(Map normalMap, Map reverseMap, BidiMap inverseBidiMap) {
  95. super(normalMap, reverseMap, inverseBidiMap);
  96. this.comparator = ((SortedMap) normalMap).comparator();
  97. }
  98. /**
  99. * Creates a new instance of this object.
  100. *
  101. * @param normalMap the normal direction map
  102. * @param reverseMap the reverse direction map
  103. * @param inverseMap the inverse BidiMap
  104. * @return new bidi map
  105. */
  106. protected BidiMap createBidiMap(Map normalMap, Map reverseMap, BidiMap inverseMap) {
  107. return new DualTreeBidiMap(normalMap, reverseMap, inverseMap);
  108. }
  109. //-----------------------------------------------------------------------
  110. public Comparator comparator() {
  111. return ((SortedMap) maps[0]).comparator();
  112. }
  113. public Object firstKey() {
  114. return ((SortedMap) maps[0]).firstKey();
  115. }
  116. public Object lastKey() {
  117. return ((SortedMap) maps[0]).lastKey();
  118. }
  119. public Object nextKey(Object key) {
  120. if (isEmpty()) {
  121. return null;
  122. }
  123. if (maps[0] instanceof OrderedMap) {
  124. return ((OrderedMap) maps[0]).nextKey(key);
  125. }
  126. SortedMap sm = (SortedMap) maps[0];
  127. Iterator it = sm.tailMap(key).keySet().iterator();
  128. it.next();
  129. if (it.hasNext()) {
  130. return it.next();
  131. }
  132. return null;
  133. }
  134. public Object previousKey(Object key) {
  135. if (isEmpty()) {
  136. return null;
  137. }
  138. if (maps[0] instanceof OrderedMap) {
  139. return ((OrderedMap) maps[0]).previousKey(key);
  140. }
  141. SortedMap sm = (SortedMap) maps[0];
  142. SortedMap hm = sm.headMap(key);
  143. if (hm.isEmpty()) {
  144. return null;
  145. }
  146. return hm.lastKey();
  147. }
  148. //-----------------------------------------------------------------------
  149. /**
  150. * Obtains an ordered map iterator.
  151. * <p>
  152. * This implementation copies the elements to an ArrayList in order to
  153. * provide the forward/backward behaviour.
  154. *
  155. * @return a new ordered map iterator
  156. */
  157. public OrderedMapIterator orderedMapIterator() {
  158. return new BidiOrderedMapIterator(this);
  159. }
  160. public SortedBidiMap inverseSortedBidiMap() {
  161. return (SortedBidiMap) inverseBidiMap();
  162. }
  163. public OrderedBidiMap inverseOrderedBidiMap() {
  164. return (OrderedBidiMap) inverseBidiMap();
  165. }
  166. //-----------------------------------------------------------------------
  167. public SortedMap headMap(Object toKey) {
  168. SortedMap sub = ((SortedMap) maps[0]).headMap(toKey);
  169. return new ViewMap(this, sub);
  170. }
  171. public SortedMap tailMap(Object fromKey) {
  172. SortedMap sub = ((SortedMap) maps[0]).tailMap(fromKey);
  173. return new ViewMap(this, sub);
  174. }
  175. public SortedMap subMap(Object fromKey, Object toKey) {
  176. SortedMap sub = ((SortedMap) maps[0]).subMap(fromKey, toKey);
  177. return new ViewMap(this, sub);
  178. }
  179. //-----------------------------------------------------------------------
  180. /**
  181. * Internal sorted map view.
  182. */
  183. protected static class ViewMap extends AbstractSortedMapDecorator {
  184. /** The parent bidi map. */
  185. final DualTreeBidiMap bidi;
  186. /**
  187. * Constructor.
  188. * @param bidi the parent bidi map
  189. * @param sm the subMap sorted map
  190. */
  191. protected ViewMap(DualTreeBidiMap bidi, SortedMap sm) {
  192. // the implementation is not great here...
  193. // use the maps[0] as the filtered map, but maps[1] as the full map
  194. // this forces containsValue and clear to be overridden
  195. super((SortedMap) bidi.createBidiMap(sm, bidi.maps[1], bidi.inverseBidiMap));
  196. this.bidi = (DualTreeBidiMap) map;
  197. }
  198. public boolean containsValue(Object value) {
  199. // override as default implementation jumps to [1]
  200. return bidi.maps[0].containsValue(value);
  201. }
  202. public void clear() {
  203. // override as default implementation jumps to [1]
  204. for (Iterator it = keySet().iterator(); it.hasNext();) {
  205. it.next();
  206. it.remove();
  207. }
  208. }
  209. public SortedMap headMap(Object toKey) {
  210. return new ViewMap(bidi, super.headMap(toKey));
  211. }
  212. public SortedMap tailMap(Object fromKey) {
  213. return new ViewMap(bidi, super.tailMap(fromKey));
  214. }
  215. public SortedMap subMap(Object fromKey, Object toKey) {
  216. return new ViewMap(bidi, super.subMap(fromKey, toKey));
  217. }
  218. }
  219. //-----------------------------------------------------------------------
  220. /**
  221. * Inner class MapIterator.
  222. */
  223. protected static class BidiOrderedMapIterator implements OrderedMapIterator, ResettableIterator {
  224. /** The parent map */
  225. protected final AbstractDualBidiMap parent;
  226. /** The iterator being decorated */
  227. protected ListIterator iterator;
  228. /** The last returned entry */
  229. private Map.Entry last = null;
  230. /**
  231. * Constructor.
  232. * @param parent the parent map
  233. */
  234. protected BidiOrderedMapIterator(AbstractDualBidiMap parent) {
  235. super();
  236. this.parent = parent;
  237. iterator = new ArrayList(parent.entrySet()).listIterator();
  238. }
  239. public boolean hasNext() {
  240. return iterator.hasNext();
  241. }
  242. public Object next() {
  243. last = (Map.Entry) iterator.next();
  244. return last.getKey();
  245. }
  246. public boolean hasPrevious() {
  247. return iterator.hasPrevious();
  248. }
  249. public Object previous() {
  250. last = (Map.Entry) iterator.previous();
  251. return last.getKey();
  252. }
  253. public void remove() {
  254. iterator.remove();
  255. parent.remove(last.getKey());
  256. last = null;
  257. }
  258. public Object getKey() {
  259. if (last == null) {
  260. throw new IllegalStateException("Iterator getKey() can only be called after next() and before remove()");
  261. }
  262. return last.getKey();
  263. }
  264. public Object getValue() {
  265. if (last == null) {
  266. throw new IllegalStateException("Iterator getValue() can only be called after next() and before remove()");
  267. }
  268. return last.getValue();
  269. }
  270. public Object setValue(Object value) {
  271. if (last == null) {
  272. throw new IllegalStateException("Iterator setValue() can only be called after next() and before remove()");
  273. }
  274. if (parent.maps[1].containsKey(value) &&
  275. parent.maps[1].get(value) != last.getKey()) {
  276. throw new IllegalArgumentException("Cannot use setValue() when the object being set is already in the map");
  277. }
  278. return parent.put(last.getKey(), value);
  279. }
  280. public void reset() {
  281. iterator = new ArrayList(parent.entrySet()).listIterator();
  282. last = null;
  283. }
  284. public String toString() {
  285. if (last != null) {
  286. return "MapIterator[" + getKey() + "=" + getValue() + "]";
  287. } else {
  288. return "MapIterator[]";
  289. }
  290. }
  291. }
  292. // Serialization
  293. //-----------------------------------------------------------------------
  294. private void writeObject(ObjectOutputStream out) throws IOException {
  295. out.defaultWriteObject();
  296. out.writeObject(maps[0]);
  297. }
  298. private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
  299. in.defaultReadObject();
  300. maps[0] = new TreeMap(comparator);
  301. maps[1] = new TreeMap(comparator);
  302. Map map = (Map) in.readObject();
  303. putAll(map);
  304. }
  305. }