--- /dev/null
+/*\r
+ * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.\r
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.\r
+ *\r
+ * This code is free software; you can redistribute it and/or modify it\r
+ * under the terms of the GNU General Public License version 2 only, as\r
+ * published by the Free Software Foundation. Oracle designates this\r
+ * particular file as subject to the "Classpath" exception as provided\r
+ * by Oracle in the LICENSE file that accompanied this code.\r
+ *\r
+ * This code is distributed in the hope that it will be useful, but WITHOUT\r
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\r
+ * version 2 for more details (a copy is included in the LICENSE file that\r
+ * accompanied this code).\r
+ *\r
+ * You should have received a copy of the GNU General Public License version\r
+ * 2 along with this work; if not, write to the Free Software Foundation,\r
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.\r
+ *\r
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA\r
+ * or visit www.oracle.com if you need additional information or have any\r
+ * questions.\r
+ */\r
+\r
+package org.apache.poi.util.java7_util;\r
+\r
+import java.util.AbstractCollection;\r
+import java.util.AbstractSet;\r
+import java.util.Collection;\r
+import java.util.Iterator;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+/**\r
+ * This class provides a skeletal implementation of the <tt>Map</tt>\r
+ * interface, to minimize the effort required to implement this interface.\r
+ *\r
+ * <p>To implement an unmodifiable map, the programmer needs only to extend this\r
+ * class and provide an implementation for the <tt>entrySet</tt> method, which\r
+ * returns a set-view of the map's mappings. Typically, the returned set\r
+ * will, in turn, be implemented atop <tt>AbstractSet</tt>. This set should\r
+ * not support the <tt>add</tt> or <tt>remove</tt> methods, and its iterator\r
+ * should not support the <tt>remove</tt> method.\r
+ *\r
+ * <p>To implement a modifiable map, the programmer must additionally override\r
+ * this class's <tt>put</tt> method (which otherwise throws an\r
+ * <tt>UnsupportedOperationException</tt>), and the iterator returned by\r
+ * <tt>entrySet().iterator()</tt> must additionally implement its\r
+ * <tt>remove</tt> method.\r
+ *\r
+ * <p>The programmer should generally provide a void (no argument) and map\r
+ * constructor, as per the recommendation in the <tt>Map</tt> interface\r
+ * specification.\r
+ *\r
+ * <p>The documentation for each non-abstract method in this class describes its\r
+ * implementation in detail. Each of these methods may be overridden if the\r
+ * map being implemented admits a more efficient implementation.\r
+ *\r
+ * <p>This class is a member of the\r
+ * <a href="{@docRoot}/../technotes/guides/collections/index.html">\r
+ * Java Collections Framework</a>.\r
+ *\r
+ * @param <K> the type of keys maintained by this map\r
+ * @param <V> the type of mapped values\r
+ *\r
+ * @author Josh Bloch\r
+ * @author Neal Gafter\r
+ * @see Map\r
+ * @see Collection\r
+ * @since 1.2\r
+ */\r
+\r
+public abstract class AbstractMap7<K,V> implements Map<K,V> {\r
+ /**\r
+ * Sole constructor. (For invocation by subclass constructors, typically\r
+ * implicit.)\r
+ */\r
+ protected AbstractMap7() {\r
+ }\r
+\r
+ // Query Operations\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ *\r
+ * <p>This implementation returns <tt>entrySet().size()</tt>.\r
+ */\r
+ public int size() {\r
+ return entrySet().size();\r
+ }\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ *\r
+ * <p>This implementation returns <tt>size() == 0</tt>.\r
+ */\r
+ public boolean isEmpty() {\r
+ return size() == 0;\r
+ }\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ *\r
+ * <p>This implementation iterates over <tt>entrySet()</tt> searching\r
+ * for an entry with the specified value. If such an entry is found,\r
+ * <tt>true</tt> is returned. If the iteration terminates without\r
+ * finding such an entry, <tt>false</tt> is returned. Note that this\r
+ * implementation requires linear time in the size of the map.\r
+ *\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException {@inheritDoc}\r
+ */\r
+ public boolean containsValue(Object value) {\r
+ Iterator<Entry<K,V>> i = entrySet().iterator();\r
+ if (value==null) {\r
+ while (i.hasNext()) {\r
+ Entry<K,V> e = i.next();\r
+ if (e.getValue()==null)\r
+ return true;\r
+ }\r
+ } else {\r
+ while (i.hasNext()) {\r
+ Entry<K,V> e = i.next();\r
+ if (value.equals(e.getValue()))\r
+ return true;\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ *\r
+ * <p>This implementation iterates over <tt>entrySet()</tt> searching\r
+ * for an entry with the specified key. If such an entry is found,\r
+ * <tt>true</tt> is returned. If the iteration terminates without\r
+ * finding such an entry, <tt>false</tt> is returned. Note that this\r
+ * implementation requires linear time in the size of the map; many\r
+ * implementations will override this method.\r
+ *\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException {@inheritDoc}\r
+ */\r
+ public boolean containsKey(Object key) {\r
+ Iterator<Map.Entry<K,V>> i = entrySet().iterator();\r
+ if (key==null) {\r
+ while (i.hasNext()) {\r
+ Entry<K,V> e = i.next();\r
+ if (e.getKey()==null)\r
+ return true;\r
+ }\r
+ } else {\r
+ while (i.hasNext()) {\r
+ Entry<K,V> e = i.next();\r
+ if (key.equals(e.getKey()))\r
+ return true;\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ *\r
+ * <p>This implementation iterates over <tt>entrySet()</tt> searching\r
+ * for an entry with the specified key. If such an entry is found,\r
+ * the entry's value is returned. If the iteration terminates without\r
+ * finding such an entry, <tt>null</tt> is returned. Note that this\r
+ * implementation requires linear time in the size of the map; many\r
+ * implementations will override this method.\r
+ *\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException {@inheritDoc}\r
+ */\r
+ public V get(Object key) {\r
+ Iterator<Entry<K,V>> i = entrySet().iterator();\r
+ if (key==null) {\r
+ while (i.hasNext()) {\r
+ Entry<K,V> e = i.next();\r
+ if (e.getKey()==null)\r
+ return e.getValue();\r
+ }\r
+ } else {\r
+ while (i.hasNext()) {\r
+ Entry<K,V> e = i.next();\r
+ if (key.equals(e.getKey()))\r
+ return e.getValue();\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+\r
+\r
+ // Modification Operations\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ *\r
+ * <p>This implementation always throws an\r
+ * <tt>UnsupportedOperationException</tt>.\r
+ *\r
+ * @throws UnsupportedOperationException {@inheritDoc}\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException {@inheritDoc}\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+ */\r
+ public V put(K key, V value) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ *\r
+ * <p>This implementation iterates over <tt>entrySet()</tt> searching for an\r
+ * entry with the specified key. If such an entry is found, its value is\r
+ * obtained with its <tt>getValue</tt> operation, the entry is removed\r
+ * from the collection (and the backing map) with the iterator's\r
+ * <tt>remove</tt> operation, and the saved value is returned. If the\r
+ * iteration terminates without finding such an entry, <tt>null</tt> is\r
+ * returned. Note that this implementation requires linear time in the\r
+ * size of the map; many implementations will override this method.\r
+ *\r
+ * <p>Note that this implementation throws an\r
+ * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt>\r
+ * iterator does not support the <tt>remove</tt> method and this map\r
+ * contains a mapping for the specified key.\r
+ *\r
+ * @throws UnsupportedOperationException {@inheritDoc}\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException {@inheritDoc}\r
+ */\r
+ public V remove(Object key) {\r
+ Iterator<Entry<K,V>> i = entrySet().iterator();\r
+ Entry<K,V> correctEntry = null;\r
+ if (key==null) {\r
+ while (correctEntry==null && i.hasNext()) {\r
+ Entry<K,V> e = i.next();\r
+ if (e.getKey()==null)\r
+ correctEntry = e;\r
+ }\r
+ } else {\r
+ while (correctEntry==null && i.hasNext()) {\r
+ Entry<K,V> e = i.next();\r
+ if (key.equals(e.getKey()))\r
+ correctEntry = e;\r
+ }\r
+ }\r
+\r
+ V oldValue = null;\r
+ if (correctEntry !=null) {\r
+ oldValue = correctEntry.getValue();\r
+ i.remove();\r
+ }\r
+ return oldValue;\r
+ }\r
+\r
+\r
+ // Bulk Operations\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ *\r
+ * <p>This implementation iterates over the specified map's\r
+ * <tt>entrySet()</tt> collection, and calls this map's <tt>put</tt>\r
+ * operation once for each entry returned by the iteration.\r
+ *\r
+ * <p>Note that this implementation throws an\r
+ * <tt>UnsupportedOperationException</tt> if this map does not support\r
+ * the <tt>put</tt> operation and the specified map is nonempty.\r
+ *\r
+ * @throws UnsupportedOperationException {@inheritDoc}\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException {@inheritDoc}\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+ */\r
+ public void putAll(Map<? extends K, ? extends V> m) {\r
+ for (Map.Entry<? extends K, ? extends V> e : m.entrySet())\r
+ put(e.getKey(), e.getValue());\r
+ }\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ *\r
+ * <p>This implementation calls <tt>entrySet().clear()</tt>.\r
+ *\r
+ * <p>Note that this implementation throws an\r
+ * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt>\r
+ * does not support the <tt>clear</tt> operation.\r
+ *\r
+ * @throws UnsupportedOperationException {@inheritDoc}\r
+ */\r
+ public void clear() {\r
+ entrySet().clear();\r
+ }\r
+\r
+\r
+ // Views\r
+\r
+ /**\r
+ * Each of these fields are initialized to contain an instance of the\r
+ * appropriate view the first time this view is requested. The views are\r
+ * stateless, so there's no reason to create more than one of each.\r
+ */\r
+ transient volatile Set<K> keySet = null;\r
+ transient volatile Collection<V> values = null;\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ *\r
+ * <p>This implementation returns a set that subclasses {@link AbstractSet}.\r
+ * The subclass's iterator method returns a "wrapper object" over this\r
+ * map's <tt>entrySet()</tt> iterator. The <tt>size</tt> method\r
+ * delegates to this map's <tt>size</tt> method and the\r
+ * <tt>contains</tt> method delegates to this map's\r
+ * <tt>containsKey</tt> method.\r
+ *\r
+ * <p>The set is created the first time this method is called,\r
+ * and returned in response to all subsequent calls. No synchronization\r
+ * is performed, so there is a slight chance that multiple calls to this\r
+ * method will not all return the same set.\r
+ */\r
+ public Set<K> keySet() {\r
+ if (keySet == null) {\r
+ keySet = new AbstractSet<K>() {\r
+ public Iterator<K> iterator() {\r
+ return new Iterator<K>() {\r
+ private Iterator<Entry<K,V>> i = entrySet().iterator();\r
+\r
+ public boolean hasNext() {\r
+ return i.hasNext();\r
+ }\r
+\r
+ public K next() {\r
+ return i.next().getKey();\r
+ }\r
+\r
+ public void remove() {\r
+ i.remove();\r
+ }\r
+ };\r
+ }\r
+\r
+ public int size() {\r
+ return AbstractMap7.this.size();\r
+ }\r
+\r
+ public boolean isEmpty() {\r
+ return AbstractMap7.this.isEmpty();\r
+ }\r
+\r
+ public void clear() {\r
+ AbstractMap7.this.clear();\r
+ }\r
+\r
+ public boolean contains(Object k) {\r
+ return AbstractMap7.this.containsKey(k);\r
+ }\r
+ };\r
+ }\r
+ return keySet;\r
+ }\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ *\r
+ * <p>This implementation returns a collection that subclasses {@link\r
+ * AbstractCollection}. The subclass's iterator method returns a\r
+ * "wrapper object" over this map's <tt>entrySet()</tt> iterator.\r
+ * The <tt>size</tt> method delegates to this map's <tt>size</tt>\r
+ * method and the <tt>contains</tt> method delegates to this map's\r
+ * <tt>containsValue</tt> method.\r
+ *\r
+ * <p>The collection is created the first time this method is called, and\r
+ * returned in response to all subsequent calls. No synchronization is\r
+ * performed, so there is a slight chance that multiple calls to this\r
+ * method will not all return the same collection.\r
+ */\r
+ public Collection<V> values() {\r
+ if (values == null) {\r
+ values = new AbstractCollection<V>() {\r
+ public Iterator<V> iterator() {\r
+ return new Iterator<V>() {\r
+ private Iterator<Entry<K,V>> i = entrySet().iterator();\r
+\r
+ public boolean hasNext() {\r
+ return i.hasNext();\r
+ }\r
+\r
+ public V next() {\r
+ return i.next().getValue();\r
+ }\r
+\r
+ public void remove() {\r
+ i.remove();\r
+ }\r
+ };\r
+ }\r
+\r
+ public int size() {\r
+ return AbstractMap7.this.size();\r
+ }\r
+\r
+ public boolean isEmpty() {\r
+ return AbstractMap7.this.isEmpty();\r
+ }\r
+\r
+ public void clear() {\r
+ AbstractMap7.this.clear();\r
+ }\r
+\r
+ public boolean contains(Object v) {\r
+ return AbstractMap7.this.containsValue(v);\r
+ }\r
+ };\r
+ }\r
+ return values;\r
+ }\r
+\r
+ public abstract Set<Entry<K,V>> entrySet();\r
+\r
+\r
+ // Comparison and hashing\r
+\r
+ /**\r
+ * Compares the specified object with this map for equality. Returns\r
+ * <tt>true</tt> if the given object is also a map and the two maps\r
+ * represent the same mappings. More formally, two maps <tt>m1</tt> and\r
+ * <tt>m2</tt> represent the same mappings if\r
+ * <tt>m1.entrySet().equals(m2.entrySet())</tt>. This ensures that the\r
+ * <tt>equals</tt> method works properly across different implementations\r
+ * of the <tt>Map</tt> interface.\r
+ *\r
+ * <p>This implementation first checks if the specified object is this map;\r
+ * if so it returns <tt>true</tt>. Then, it checks if the specified\r
+ * object is a map whose size is identical to the size of this map; if\r
+ * not, it returns <tt>false</tt>. If so, it iterates over this map's\r
+ * <tt>entrySet</tt> collection, and checks that the specified map\r
+ * contains each mapping that this map contains. If the specified map\r
+ * fails to contain such a mapping, <tt>false</tt> is returned. If the\r
+ * iteration completes, <tt>true</tt> is returned.\r
+ *\r
+ * @param o object to be compared for equality with this map\r
+ * @return <tt>true</tt> if the specified object is equal to this map\r
+ */\r
+ public boolean equals(Object o) {\r
+ if (o == this)\r
+ return true;\r
+\r
+ if (!(o instanceof Map))\r
+ return false;\r
+ Map<K,V> m = (Map<K,V>) o;\r
+ if (m.size() != size())\r
+ return false;\r
+\r
+ try {\r
+ Iterator<Entry<K,V>> i = entrySet().iterator();\r
+ while (i.hasNext()) {\r
+ Entry<K,V> e = i.next();\r
+ K key = e.getKey();\r
+ V value = e.getValue();\r
+ if (value == null) {\r
+ if (!(m.get(key)==null && m.containsKey(key)))\r
+ return false;\r
+ } else {\r
+ if (!value.equals(m.get(key)))\r
+ return false;\r
+ }\r
+ }\r
+ } catch (ClassCastException unused) {\r
+ return false;\r
+ } catch (NullPointerException unused) {\r
+ return false;\r
+ }\r
+\r
+ return true;\r
+ }\r
+\r
+ /**\r
+ * Returns the hash code value for this map. The hash code of a map is\r
+ * defined to be the sum of the hash codes of each entry in the map's\r
+ * <tt>entrySet()</tt> view. This ensures that <tt>m1.equals(m2)</tt>\r
+ * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps\r
+ * <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of\r
+ * {@link Object#hashCode}.\r
+ *\r
+ * <p>This implementation iterates over <tt>entrySet()</tt>, calling\r
+ * {@link Map.Entry#hashCode hashCode()} on each element (entry) in the\r
+ * set, and adding up the results.\r
+ *\r
+ * @return the hash code value for this map\r
+ * @see Map.Entry#hashCode()\r
+ * @see Object#equals(Object)\r
+ * @see Set#equals(Object)\r
+ */\r
+ public int hashCode() {\r
+ int h = 0;\r
+ Iterator<Entry<K,V>> i = entrySet().iterator();\r
+ while (i.hasNext())\r
+ h += i.next().hashCode();\r
+ return h;\r
+ }\r
+\r
+ /**\r
+ * Returns a string representation of this map. The string representation\r
+ * consists of a list of key-value mappings in the order returned by the\r
+ * map's <tt>entrySet</tt> view's iterator, enclosed in braces\r
+ * (<tt>"{}"</tt>). Adjacent mappings are separated by the characters\r
+ * <tt>", "</tt> (comma and space). Each key-value mapping is rendered as\r
+ * the key followed by an equals sign (<tt>"="</tt>) followed by the\r
+ * associated value. Keys and values are converted to strings as by\r
+ * {@link String#valueOf(Object)}.\r
+ *\r
+ * @return a string representation of this map\r
+ */\r
+ public String toString() {\r
+ Iterator<Entry<K,V>> i = entrySet().iterator();\r
+ if (! i.hasNext())\r
+ return "{}";\r
+\r
+ StringBuilder sb = new StringBuilder();\r
+ sb.append('{');\r
+ for (;;) {\r
+ Entry<K,V> e = i.next();\r
+ K key = e.getKey();\r
+ V value = e.getValue();\r
+ sb.append(key == this ? "(this Map)" : key);\r
+ sb.append('=');\r
+ sb.append(value == this ? "(this Map)" : value);\r
+ if (! i.hasNext())\r
+ return sb.append('}').toString();\r
+ sb.append(',').append(' ');\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Returns a shallow copy of this <tt>AbstractMap7</tt> instance: the keys\r
+ * and values themselves are not cloned.\r
+ *\r
+ * @return a shallow copy of this map\r
+ */\r
+ protected Object clone() throws CloneNotSupportedException {\r
+ AbstractMap7<K,V> result = (AbstractMap7<K,V>)super.clone();\r
+ result.keySet = null;\r
+ result.values = null;\r
+ return result;\r
+ }\r
+\r
+ /**\r
+ * Utility method for SimpleEntry and SimpleImmutableEntry.\r
+ * Test for equality, checking for nulls.\r
+ */\r
+ private static boolean eq(Object o1, Object o2) {\r
+ return o1 == null ? o2 == null : o1.equals(o2);\r
+ }\r
+\r
+ // Implementation Note: SimpleEntry and SimpleImmutableEntry\r
+ // are distinct unrelated classes, even though they share\r
+ // some code. Since you can't add or subtract final-ness\r
+ // of a field in a subclass, they can't share representations,\r
+ // and the amount of duplicated code is too small to warrant\r
+ // exposing a common abstract class.\r
+\r
+\r
+ /**\r
+ * An Entry maintaining a key and a value. The value may be\r
+ * changed using the <tt>setValue</tt> method. This class\r
+ * facilitates the process of building custom map\r
+ * implementations. For example, it may be convenient to return\r
+ * arrays of <tt>SimpleEntry</tt> instances in method\r
+ * <tt>Map.entrySet().toArray</tt>.\r
+ *\r
+ * @since 1.6\r
+ */\r
+ public static class SimpleEntry<K,V>\r
+ implements Entry<K,V>, java.io.Serializable\r
+ {\r
+ private static final long serialVersionUID = -8499721149061103585L;\r
+\r
+ private final K key;\r
+ private V value;\r
+\r
+ /**\r
+ * Creates an entry representing a mapping from the specified\r
+ * key to the specified value.\r
+ *\r
+ * @param key the key represented by this entry\r
+ * @param value the value represented by this entry\r
+ */\r
+ public SimpleEntry(K key, V value) {\r
+ this.key = key;\r
+ this.value = value;\r
+ }\r
+\r
+ /**\r
+ * Creates an entry representing the same mapping as the\r
+ * specified entry.\r
+ *\r
+ * @param entry the entry to copy\r
+ */\r
+ public SimpleEntry(Entry<? extends K, ? extends V> entry) {\r
+ this.key = entry.getKey();\r
+ this.value = entry.getValue();\r
+ }\r
+\r
+ /**\r
+ * Returns the key corresponding to this entry.\r
+ *\r
+ * @return the key corresponding to this entry\r
+ */\r
+ public K getKey() {\r
+ return key;\r
+ }\r
+\r
+ /**\r
+ * Returns the value corresponding to this entry.\r
+ *\r
+ * @return the value corresponding to this entry\r
+ */\r
+ public V getValue() {\r
+ return value;\r
+ }\r
+\r
+ /**\r
+ * Replaces the value corresponding to this entry with the specified\r
+ * value.\r
+ *\r
+ * @param value new value to be stored in this entry\r
+ * @return the old value corresponding to the entry\r
+ */\r
+ public V setValue(V value) {\r
+ V oldValue = this.value;\r
+ this.value = value;\r
+ return oldValue;\r
+ }\r
+\r
+ /**\r
+ * Compares the specified object with this entry for equality.\r
+ * Returns {@code true} if the given object is also a map entry and\r
+ * the two entries represent the same mapping. More formally, two\r
+ * entries {@code e1} and {@code e2} represent the same mapping\r
+ * if<pre>\r
+ * (e1.getKey()==null ?\r
+ * e2.getKey()==null :\r
+ * e1.getKey().equals(e2.getKey()))\r
+ * &&\r
+ * (e1.getValue()==null ?\r
+ * e2.getValue()==null :\r
+ * e1.getValue().equals(e2.getValue()))</pre>\r
+ * This ensures that the {@code equals} method works properly across\r
+ * different implementations of the {@code Map.Entry} interface.\r
+ *\r
+ * @param o object to be compared for equality with this map entry\r
+ * @return {@code true} if the specified object is equal to this map\r
+ * entry\r
+ * @see #hashCode\r
+ */\r
+ public boolean equals(Object o) {\r
+ if (!(o instanceof Map.Entry))\r
+ return false;\r
+ Map.Entry e = (Map.Entry)o;\r
+ return eq(key, e.getKey()) && eq(value, e.getValue());\r
+ }\r
+\r
+ /**\r
+ * Returns the hash code value for this map entry. The hash code\r
+ * of a map entry {@code e} is defined to be: <pre>\r
+ * (e.getKey()==null ? 0 : e.getKey().hashCode()) ^\r
+ * (e.getValue()==null ? 0 : e.getValue().hashCode())</pre>\r
+ * This ensures that {@code e1.equals(e2)} implies that\r
+ * {@code e1.hashCode()==e2.hashCode()} for any two Entries\r
+ * {@code e1} and {@code e2}, as required by the general\r
+ * contract of {@link Object#hashCode}.\r
+ *\r
+ * @return the hash code value for this map entry\r
+ * @see #equals\r
+ */\r
+ public int hashCode() {\r
+ return (key == null ? 0 : key.hashCode()) ^\r
+ (value == null ? 0 : value.hashCode());\r
+ }\r
+\r
+ /**\r
+ * Returns a String representation of this map entry. This\r
+ * implementation returns the string representation of this\r
+ * entry's key followed by the equals character ("<tt>=</tt>")\r
+ * followed by the string representation of this entry's value.\r
+ *\r
+ * @return a String representation of this map entry\r
+ */\r
+ public String toString() {\r
+ return key + "=" + value;\r
+ }\r
+\r
+ }\r
+\r
+ /**\r
+ * An Entry maintaining an immutable key and value. This class\r
+ * does not support method <tt>setValue</tt>. This class may be\r
+ * convenient in methods that return thread-safe snapshots of\r
+ * key-value mappings.\r
+ *\r
+ * @since 1.6\r
+ */\r
+ public static class SimpleImmutableEntry<K,V>\r
+ implements Entry<K,V>, java.io.Serializable\r
+ {\r
+ private static final long serialVersionUID = 7138329143949025153L;\r
+\r
+ private final K key;\r
+ private final V value;\r
+\r
+ /**\r
+ * Creates an entry representing a mapping from the specified\r
+ * key to the specified value.\r
+ *\r
+ * @param key the key represented by this entry\r
+ * @param value the value represented by this entry\r
+ */\r
+ public SimpleImmutableEntry(K key, V value) {\r
+ this.key = key;\r
+ this.value = value;\r
+ }\r
+\r
+ /**\r
+ * Creates an entry representing the same mapping as the\r
+ * specified entry.\r
+ *\r
+ * @param entry the entry to copy\r
+ */\r
+ public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry) {\r
+ this.key = entry.getKey();\r
+ this.value = entry.getValue();\r
+ }\r
+\r
+ /**\r
+ * Returns the key corresponding to this entry.\r
+ *\r
+ * @return the key corresponding to this entry\r
+ */\r
+ public K getKey() {\r
+ return key;\r
+ }\r
+\r
+ /**\r
+ * Returns the value corresponding to this entry.\r
+ *\r
+ * @return the value corresponding to this entry\r
+ */\r
+ public V getValue() {\r
+ return value;\r
+ }\r
+\r
+ /**\r
+ * Replaces the value corresponding to this entry with the specified\r
+ * value (optional operation). This implementation simply throws\r
+ * <tt>UnsupportedOperationException</tt>, as this class implements\r
+ * an <i>immutable</i> map entry.\r
+ *\r
+ * @param value new value to be stored in this entry\r
+ * @return (Does not return)\r
+ * @throws UnsupportedOperationException always\r
+ */\r
+ public V setValue(V value) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ /**\r
+ * Compares the specified object with this entry for equality.\r
+ * Returns {@code true} if the given object is also a map entry and\r
+ * the two entries represent the same mapping. More formally, two\r
+ * entries {@code e1} and {@code e2} represent the same mapping\r
+ * if<pre>\r
+ * (e1.getKey()==null ?\r
+ * e2.getKey()==null :\r
+ * e1.getKey().equals(e2.getKey()))\r
+ * &&\r
+ * (e1.getValue()==null ?\r
+ * e2.getValue()==null :\r
+ * e1.getValue().equals(e2.getValue()))</pre>\r
+ * This ensures that the {@code equals} method works properly across\r
+ * different implementations of the {@code Map.Entry} interface.\r
+ *\r
+ * @param o object to be compared for equality with this map entry\r
+ * @return {@code true} if the specified object is equal to this map\r
+ * entry\r
+ * @see #hashCode\r
+ */\r
+ public boolean equals(Object o) {\r
+ if (!(o instanceof Map.Entry))\r
+ return false;\r
+ Map.Entry e = (Map.Entry)o;\r
+ return eq(key, e.getKey()) && eq(value, e.getValue());\r
+ }\r
+\r
+ /**\r
+ * Returns the hash code value for this map entry. The hash code\r
+ * of a map entry {@code e} is defined to be: <pre>\r
+ * (e.getKey()==null ? 0 : e.getKey().hashCode()) ^\r
+ * (e.getValue()==null ? 0 : e.getValue().hashCode())</pre>\r
+ * This ensures that {@code e1.equals(e2)} implies that\r
+ * {@code e1.hashCode()==e2.hashCode()} for any two Entries\r
+ * {@code e1} and {@code e2}, as required by the general\r
+ * contract of {@link Object#hashCode}.\r
+ *\r
+ * @return the hash code value for this map entry\r
+ * @see #equals\r
+ */\r
+ public int hashCode() {\r
+ return (key == null ? 0 : key.hashCode()) ^\r
+ (value == null ? 0 : value.hashCode());\r
+ }\r
+\r
+ /**\r
+ * Returns a String representation of this map entry. This\r
+ * implementation returns the string representation of this\r
+ * entry's key followed by the equals character ("<tt>=</tt>")\r
+ * followed by the string representation of this entry's value.\r
+ *\r
+ * @return a String representation of this map entry\r
+ */\r
+ public String toString() {\r
+ return key + "=" + value;\r
+ }\r
+\r
+ }\r
+\r
+}
\ No newline at end of file
--- /dev/null
+/*\r
+ * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.\r
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.\r
+ *\r
+ * This code is free software; you can redistribute it and/or modify it\r
+ * under the terms of the GNU General Public License version 2 only, as\r
+ * published by the Free Software Foundation. Oracle designates this\r
+ * particular file as subject to the "Classpath" exception as provided\r
+ * by Oracle in the LICENSE file that accompanied this code.\r
+ *\r
+ * This code is distributed in the hope that it will be useful, but WITHOUT\r
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\r
+ * version 2 for more details (a copy is included in the LICENSE file that\r
+ * accompanied this code).\r
+ *\r
+ * You should have received a copy of the GNU General Public License version\r
+ * 2 along with this work; if not, write to the Free Software Foundation,\r
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.\r
+ *\r
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA\r
+ * or visit www.oracle.com if you need additional information or have any\r
+ * questions.\r
+ */\r
+\r
+package org.apache.poi.util.java7_util;\r
+\r
+import java.util.AbstractCollection;\r
+import java.util.Collection;\r
+import java.util.Iterator;\r
+import java.util.Set;\r
+\r
+/**\r
+ * This class provides a skeletal implementation of the <tt>Set</tt>\r
+ * interface to minimize the effort required to implement this\r
+ * interface. <p>\r
+ *\r
+ * The process of implementing a set by extending this class is identical\r
+ * to that of implementing a Collection by extending AbstractCollection,\r
+ * except that all of the methods and constructors in subclasses of this\r
+ * class must obey the additional constraints imposed by the <tt>Set</tt>\r
+ * interface (for instance, the add method must not permit addition of\r
+ * multiple instances of an object to a set).<p>\r
+ *\r
+ * Note that this class does not override any of the implementations from\r
+ * the <tt>AbstractCollection</tt> class. It merely adds implementations\r
+ * for <tt>equals</tt> and <tt>hashCode</tt>.<p>\r
+ *\r
+ * This class is a member of the\r
+ * <a href="{@docRoot}/../technotes/guides/collections/index.html">\r
+ * Java Collections Framework</a>.\r
+ *\r
+ * @param <E> the type of elements maintained by this set\r
+ *\r
+ * @author Josh Bloch\r
+ * @author Neal Gafter\r
+ * @see Collection\r
+ * @see AbstractCollection\r
+ * @see Set\r
+ * @since 1.2\r
+ */\r
+\r
+public abstract class AbstractSet7<E> extends AbstractCollection<E> implements Set<E> {\r
+ /**\r
+ * Sole constructor. (For invocation by subclass constructors, typically\r
+ * implicit.)\r
+ */\r
+ protected AbstractSet7() {\r
+ }\r
+\r
+ // Comparison and hashing\r
+\r
+ /**\r
+ * Compares the specified object with this set for equality. Returns\r
+ * <tt>true</tt> if the given object is also a set, the two sets have\r
+ * the same size, and every member of the given set is contained in\r
+ * this set. This ensures that the <tt>equals</tt> method works\r
+ * properly across different implementations of the <tt>Set</tt>\r
+ * interface.<p>\r
+ *\r
+ * This implementation first checks if the specified object is this\r
+ * set; if so it returns <tt>true</tt>. Then, it checks if the\r
+ * specified object is a set whose size is identical to the size of\r
+ * this set; if not, it returns false. If so, it returns\r
+ * <tt>containsAll((Collection) o)</tt>.\r
+ *\r
+ * @param o object to be compared for equality with this set\r
+ * @return <tt>true</tt> if the specified object is equal to this set\r
+ */\r
+ public boolean equals(Object o) {\r
+ if (o == this)\r
+ return true;\r
+\r
+ if (!(o instanceof Set))\r
+ return false;\r
+ Collection c = (Collection) o;\r
+ if (c.size() != size())\r
+ return false;\r
+ try {\r
+ return containsAll(c);\r
+ } catch (ClassCastException unused) {\r
+ return false;\r
+ } catch (NullPointerException unused) {\r
+ return false;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Returns the hash code value for this set. The hash code of a set is\r
+ * defined to be the sum of the hash codes of the elements in the set,\r
+ * where the hash code of a <tt>null</tt> element is defined to be zero.\r
+ * This ensures that <tt>s1.equals(s2)</tt> implies that\r
+ * <tt>s1.hashCode()==s2.hashCode()</tt> for any two sets <tt>s1</tt>\r
+ * and <tt>s2</tt>, as required by the general contract of\r
+ * {@link Object#hashCode}.\r
+ *\r
+ * <p>This implementation iterates over the set, calling the\r
+ * <tt>hashCode</tt> method on each element in the set, and adding up\r
+ * the results.\r
+ *\r
+ * @return the hash code value for this set\r
+ * @see Object#equals(Object)\r
+ * @see Set#equals(Object)\r
+ */\r
+ public int hashCode() {\r
+ int h = 0;\r
+ Iterator<E> i = iterator();\r
+ while (i.hasNext()) {\r
+ E obj = i.next();\r
+ if (obj != null)\r
+ h += obj.hashCode();\r
+ }\r
+ return h;\r
+ }\r
+\r
+ /**\r
+ * Removes from this set all of its elements that are contained in the\r
+ * specified collection (optional operation). If the specified\r
+ * collection is also a set, this operation effectively modifies this\r
+ * set so that its value is the <i>asymmetric set difference</i> of\r
+ * the two sets.\r
+ *\r
+ * <p>This implementation determines which is the smaller of this set\r
+ * and the specified collection, by invoking the <tt>size</tt>\r
+ * method on each. If this set has fewer elements, then the\r
+ * implementation iterates over this set, checking each element\r
+ * returned by the iterator in turn to see if it is contained in\r
+ * the specified collection. If it is so contained, it is removed\r
+ * from this set with the iterator's <tt>remove</tt> method. If\r
+ * the specified collection has fewer elements, then the\r
+ * implementation iterates over the specified collection, removing\r
+ * from this set each element returned by the iterator, using this\r
+ * set's <tt>remove</tt> method.\r
+ *\r
+ * <p>Note that this implementation will throw an\r
+ * <tt>UnsupportedOperationException</tt> if the iterator returned by the\r
+ * <tt>iterator</tt> method does not implement the <tt>remove</tt> method.\r
+ *\r
+ * @param c collection containing elements to be removed from this set\r
+ * @return <tt>true</tt> if this set changed as a result of the call\r
+ * @throws UnsupportedOperationException if the <tt>removeAll</tt> operation\r
+ * is not supported by this set\r
+ * @throws ClassCastException if the class of an element of this set\r
+ * is incompatible with the specified collection\r
+ * (<a href="Collection.html#optional-restrictions">optional</a>)\r
+ * @throws NullPointerException if this set contains a null element and the\r
+ * specified collection does not permit null elements\r
+ * (<a href="Collection.html#optional-restrictions">optional</a>),\r
+ * or if the specified collection is null\r
+ * @see #remove(Object)\r
+ * @see #contains(Object)\r
+ */\r
+ public boolean removeAll(Collection<?> c) {\r
+ boolean modified = false;\r
+\r
+ if (size() > c.size()) {\r
+ for (Iterator<?> i = c.iterator(); i.hasNext(); )\r
+ modified |= remove(i.next());\r
+ } else {\r
+ for (Iterator<?> i = iterator(); i.hasNext(); ) {\r
+ if (c.contains(i.next())) {\r
+ i.remove();\r
+ modified = true;\r
+ }\r
+ }\r
+ }\r
+ return modified;\r
+ }\r
+\r
+}
\ No newline at end of file
--- /dev/null
+/*\r
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.\r
+ *\r
+ * This code is free software; you can redistribute it and/or modify it\r
+ * under the terms of the GNU General Public License version 2 only, as\r
+ * published by the Free Software Foundation. Oracle designates this\r
+ * particular file as subject to the "Classpath" exception as provided\r
+ * by Oracle in the LICENSE file that accompanied this code.\r
+ *\r
+ * This code is distributed in the hope that it will be useful, but WITHOUT\r
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\r
+ * version 2 for more details (a copy is included in the LICENSE file that\r
+ * accompanied this code).\r
+ *\r
+ * You should have received a copy of the GNU General Public License version\r
+ * 2 along with this work; if not, write to the Free Software Foundation,\r
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.\r
+ *\r
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA\r
+ * or visit www.oracle.com if you need additional information or have any\r
+ * questions.\r
+ */\r
+\r
+/*\r
+ * This file is available under and governed by the GNU General Public\r
+ * License version 2 only, as published by the Free Software Foundation.\r
+ * However, the following notice accompanied the original version of this\r
+ * file:\r
+ *\r
+ * Written by Doug Lea and Josh Bloch with assistance from members of JCP\r
+ * JSR-166 Expert Group and released to the public domain, as explained at\r
+ * http://creativecommons.org/publicdomain/zero/1.0/\r
+ */\r
+\r
+package org.apache.poi.util.java7_util;\r
+\r
+import java.util.Collections;\r
+import java.util.Comparator;\r
+import java.util.Map;\r
+import java.util.SortedMap;\r
+\r
+/**\r
+ * A {@link SortedMap} extended with navigation methods returning the\r
+ * closest matches for given search targets. Methods\r
+ * {@code lowerEntry}, {@code floorEntry}, {@code ceilingEntry},\r
+ * and {@code higherEntry} return {@code Map.Entry} objects\r
+ * associated with keys respectively less than, less than or equal,\r
+ * greater than or equal, and greater than a given key, returning\r
+ * {@code null} if there is no such key. Similarly, methods\r
+ * {@code lowerKey}, {@code floorKey}, {@code ceilingKey}, and\r
+ * {@code higherKey} return only the associated keys. All of these\r
+ * methods are designed for locating, not traversing entries.\r
+ *\r
+ * <p>A {@code NavigableMap7} may be accessed and traversed in either\r
+ * ascending or descending key order. The {@code descendingMap}\r
+ * method returns a view of the map with the senses of all relational\r
+ * and directional methods inverted. The performance of ascending\r
+ * operations and views is likely to be faster than that of descending\r
+ * ones. Methods {@code subMap}, {@code headMap},\r
+ * and {@code tailMap} differ from the like-named {@code\r
+ * SortedMap} methods in accepting additional arguments describing\r
+ * whether lower and upper bounds are inclusive versus exclusive.\r
+ * Submaps of any {@code NavigableMap7} must implement the {@code\r
+ * NavigableMap7} interface.\r
+ *\r
+ * <p>This interface additionally defines methods {@code firstEntry},\r
+ * {@code pollFirstEntry}, {@code lastEntry}, and\r
+ * {@code pollLastEntry} that return and/or remove the least and\r
+ * greatest mappings, if any exist, else returning {@code null}.\r
+ *\r
+ * <p>Implementations of entry-returning methods are expected to\r
+ * return {@code Map.Entry} pairs representing snapshots of mappings\r
+ * at the time they were produced, and thus generally do <em>not</em>\r
+ * support the optional {@code Entry.setValue} method. Note however\r
+ * that it is possible to change mappings in the associated map using\r
+ * method {@code put}.\r
+ *\r
+ * <p>Methods\r
+ * {@link #subMap(Object, Object) subMap(K, K)},\r
+ * {@link #headMap(Object) headMap(K)}, and\r
+ * {@link #tailMap(Object) tailMap(K)}\r
+ * are specified to return {@code SortedMap} to allow existing\r
+ * implementations of {@code SortedMap} to be compatibly retrofitted to\r
+ * implement {@code NavigableMap7}, but extensions and implementations\r
+ * of this interface are encouraged to override these methods to return\r
+ * {@code NavigableMap7}. Similarly,\r
+ * {@link #keySet()} can be overriden to return {@code NavigableSet}.\r
+ *\r
+ * <p>This interface is a member of the\r
+ * <a href="{@docRoot}/../technotes/guides/collections/index.html">\r
+ * Java Collections Framework</a>.\r
+ *\r
+ * @author Doug Lea\r
+ * @author Josh Bloch\r
+ * @param <K> the type of keys maintained by this map\r
+ * @param <V> the type of mapped values\r
+ * @since 1.6\r
+ */\r
+public interface NavigableMap7<K,V> extends SortedMap<K,V> {\r
+ /**\r
+ * Returns a key-value mapping associated with the greatest key\r
+ * strictly less than the given key, or {@code null} if there is\r
+ * no such key.\r
+ *\r
+ * @param key the key\r
+ * @return an entry with the greatest key less than {@code key},\r
+ * or {@code null} if there is no such key\r
+ * @throws ClassCastException if the specified key cannot be compared\r
+ * with the keys currently in the map\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map does not permit null keys\r
+ */\r
+ Map.Entry<K,V> lowerEntry(K key);\r
+\r
+ /**\r
+ * Returns the greatest key strictly less than the given key, or\r
+ * {@code null} if there is no such key.\r
+ *\r
+ * @param key the key\r
+ * @return the greatest key less than {@code key},\r
+ * or {@code null} if there is no such key\r
+ * @throws ClassCastException if the specified key cannot be compared\r
+ * with the keys currently in the map\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map does not permit null keys\r
+ */\r
+ K lowerKey(K key);\r
+\r
+ /**\r
+ * Returns a key-value mapping associated with the greatest key\r
+ * less than or equal to the given key, or {@code null} if there\r
+ * is no such key.\r
+ *\r
+ * @param key the key\r
+ * @return an entry with the greatest key less than or equal to\r
+ * {@code key}, or {@code null} if there is no such key\r
+ * @throws ClassCastException if the specified key cannot be compared\r
+ * with the keys currently in the map\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map does not permit null keys\r
+ */\r
+ Map.Entry<K,V> floorEntry(K key);\r
+\r
+ /**\r
+ * Returns the greatest key less than or equal to the given key,\r
+ * or {@code null} if there is no such key.\r
+ *\r
+ * @param key the key\r
+ * @return the greatest key less than or equal to {@code key},\r
+ * or {@code null} if there is no such key\r
+ * @throws ClassCastException if the specified key cannot be compared\r
+ * with the keys currently in the map\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map does not permit null keys\r
+ */\r
+ K floorKey(K key);\r
+\r
+ /**\r
+ * Returns a key-value mapping associated with the least key\r
+ * greater than or equal to the given key, or {@code null} if\r
+ * there is no such key.\r
+ *\r
+ * @param key the key\r
+ * @return an entry with the least key greater than or equal to\r
+ * {@code key}, or {@code null} if there is no such key\r
+ * @throws ClassCastException if the specified key cannot be compared\r
+ * with the keys currently in the map\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map does not permit null keys\r
+ */\r
+ Map.Entry<K,V> ceilingEntry(K key);\r
+\r
+ /**\r
+ * Returns the least key greater than or equal to the given key,\r
+ * or {@code null} if there is no such key.\r
+ *\r
+ * @param key the key\r
+ * @return the least key greater than or equal to {@code key},\r
+ * or {@code null} if there is no such key\r
+ * @throws ClassCastException if the specified key cannot be compared\r
+ * with the keys currently in the map\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map does not permit null keys\r
+ */\r
+ K ceilingKey(K key);\r
+\r
+ /**\r
+ * Returns a key-value mapping associated with the least key\r
+ * strictly greater than the given key, or {@code null} if there\r
+ * is no such key.\r
+ *\r
+ * @param key the key\r
+ * @return an entry with the least key greater than {@code key},\r
+ * or {@code null} if there is no such key\r
+ * @throws ClassCastException if the specified key cannot be compared\r
+ * with the keys currently in the map\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map does not permit null keys\r
+ */\r
+ Map.Entry<K,V> higherEntry(K key);\r
+\r
+ /**\r
+ * Returns the least key strictly greater than the given key, or\r
+ * {@code null} if there is no such key.\r
+ *\r
+ * @param key the key\r
+ * @return the least key greater than {@code key},\r
+ * or {@code null} if there is no such key\r
+ * @throws ClassCastException if the specified key cannot be compared\r
+ * with the keys currently in the map\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map does not permit null keys\r
+ */\r
+ K higherKey(K key);\r
+\r
+ /**\r
+ * Returns a key-value mapping associated with the least\r
+ * key in this map, or {@code null} if the map is empty.\r
+ *\r
+ * @return an entry with the least key,\r
+ * or {@code null} if this map is empty\r
+ */\r
+ Map.Entry<K,V> firstEntry();\r
+\r
+ /**\r
+ * Returns a key-value mapping associated with the greatest\r
+ * key in this map, or {@code null} if the map is empty.\r
+ *\r
+ * @return an entry with the greatest key,\r
+ * or {@code null} if this map is empty\r
+ */\r
+ Map.Entry<K,V> lastEntry();\r
+\r
+ /**\r
+ * Removes and returns a key-value mapping associated with\r
+ * the least key in this map, or {@code null} if the map is empty.\r
+ *\r
+ * @return the removed first entry of this map,\r
+ * or {@code null} if this map is empty\r
+ */\r
+ Map.Entry<K,V> pollFirstEntry();\r
+\r
+ /**\r
+ * Removes and returns a key-value mapping associated with\r
+ * the greatest key in this map, or {@code null} if the map is empty.\r
+ *\r
+ * @return the removed last entry of this map,\r
+ * or {@code null} if this map is empty\r
+ */\r
+ Map.Entry<K,V> pollLastEntry();\r
+\r
+ /**\r
+ * Returns a reverse order view of the mappings contained in this map.\r
+ * The descending map is backed by this map, so changes to the map are\r
+ * reflected in the descending map, and vice-versa. If either map is\r
+ * modified while an iteration over a collection view of either map\r
+ * is in progress (except through the iterator's own {@code remove}\r
+ * operation), the results of the iteration are undefined.\r
+ *\r
+ * <p>The returned map has an ordering equivalent to\r
+ * <tt>{@link Collections#reverseOrder(Comparator) Collections.reverseOrder}(comparator())</tt>.\r
+ * The expression {@code m.descendingMap().descendingMap()} returns a\r
+ * view of {@code m} essentially equivalent to {@code m}.\r
+ *\r
+ * @return a reverse order view of this map\r
+ */\r
+ NavigableMap7<K,V> descendingMap();\r
+\r
+ /**\r
+ * Returns a {@link NavigableSet} view of the keys contained in this map.\r
+ * The set's iterator returns the keys in ascending order.\r
+ * The set is backed by the map, so changes to the map are reflected in\r
+ * the set, and vice-versa. If the map is modified while an iteration\r
+ * over the set is in progress (except through the iterator's own {@code\r
+ * remove} operation), the results of the iteration are undefined. The\r
+ * set supports element removal, which removes the corresponding mapping\r
+ * from the map, via the {@code Iterator.remove}, {@code Set.remove},\r
+ * {@code removeAll}, {@code retainAll}, and {@code clear} operations.\r
+ * It does not support the {@code add} or {@code addAll} operations.\r
+ *\r
+ * @return a navigable set view of the keys in this map\r
+ */\r
+ NavigableSet7<K> navigableKeySet();\r
+\r
+ /**\r
+ * Returns a reverse order {@link NavigableSet} view of the keys contained in this map.\r
+ * The set's iterator returns the keys in descending order.\r
+ * The set is backed by the map, so changes to the map are reflected in\r
+ * the set, and vice-versa. If the map is modified while an iteration\r
+ * over the set is in progress (except through the iterator's own {@code\r
+ * remove} operation), the results of the iteration are undefined. The\r
+ * set supports element removal, which removes the corresponding mapping\r
+ * from the map, via the {@code Iterator.remove}, {@code Set.remove},\r
+ * {@code removeAll}, {@code retainAll}, and {@code clear} operations.\r
+ * It does not support the {@code add} or {@code addAll} operations.\r
+ *\r
+ * @return a reverse order navigable set view of the keys in this map\r
+ */\r
+ NavigableSet7<K> descendingKeySet();\r
+\r
+ /**\r
+ * Returns a view of the portion of this map whose keys range from\r
+ * {@code fromKey} to {@code toKey}. If {@code fromKey} and\r
+ * {@code toKey} are equal, the returned map is empty unless\r
+ * {@code fromInclusive} and {@code toInclusive} are both true. The\r
+ * returned map is backed by this map, so changes in the returned map are\r
+ * reflected in this map, and vice-versa. The returned map supports all\r
+ * optional map operations that this map supports.\r
+ *\r
+ * <p>The returned map will throw an {@code IllegalArgumentException}\r
+ * on an attempt to insert a key outside of its range, or to construct a\r
+ * submap either of whose endpoints lie outside its range.\r
+ *\r
+ * @param fromKey low endpoint of the keys in the returned map\r
+ * @param fromInclusive {@code true} if the low endpoint\r
+ * is to be included in the returned view\r
+ * @param toKey high endpoint of the keys in the returned map\r
+ * @param toInclusive {@code true} if the high endpoint\r
+ * is to be included in the returned view\r
+ * @return a view of the portion of this map whose keys range from\r
+ * {@code fromKey} to {@code toKey}\r
+ * @throws ClassCastException if {@code fromKey} and {@code toKey}\r
+ * cannot be compared to one another using this map's comparator\r
+ * (or, if the map has no comparator, using natural ordering).\r
+ * Implementations may, but are not required to, throw this\r
+ * exception if {@code fromKey} or {@code toKey}\r
+ * cannot be compared to keys currently in the map.\r
+ * @throws NullPointerException if {@code fromKey} or {@code toKey}\r
+ * is null and this map does not permit null keys\r
+ * @throws IllegalArgumentException if {@code fromKey} is greater than\r
+ * {@code toKey}; or if this map itself has a restricted\r
+ * range, and {@code fromKey} or {@code toKey} lies\r
+ * outside the bounds of the range\r
+ */\r
+ NavigableMap7<K,V> subMap(K fromKey, boolean fromInclusive,\r
+ K toKey, boolean toInclusive);\r
+\r
+ /**\r
+ * Returns a view of the portion of this map whose keys are less than (or\r
+ * equal to, if {@code inclusive} is true) {@code toKey}. The returned\r
+ * map is backed by this map, so changes in the returned map are reflected\r
+ * in this map, and vice-versa. The returned map supports all optional\r
+ * map operations that this map supports.\r
+ *\r
+ * <p>The returned map will throw an {@code IllegalArgumentException}\r
+ * on an attempt to insert a key outside its range.\r
+ *\r
+ * @param toKey high endpoint of the keys in the returned map\r
+ * @param inclusive {@code true} if the high endpoint\r
+ * is to be included in the returned view\r
+ * @return a view of the portion of this map whose keys are less than\r
+ * (or equal to, if {@code inclusive} is true) {@code toKey}\r
+ * @throws ClassCastException if {@code toKey} is not compatible\r
+ * with this map's comparator (or, if the map has no comparator,\r
+ * if {@code toKey} does not implement {@link Comparable}).\r
+ * Implementations may, but are not required to, throw this\r
+ * exception if {@code toKey} cannot be compared to keys\r
+ * currently in the map.\r
+ * @throws NullPointerException if {@code toKey} is null\r
+ * and this map does not permit null keys\r
+ * @throws IllegalArgumentException if this map itself has a\r
+ * restricted range, and {@code toKey} lies outside the\r
+ * bounds of the range\r
+ */\r
+ NavigableMap7<K,V> headMap(K toKey, boolean inclusive);\r
+\r
+ /**\r
+ * Returns a view of the portion of this map whose keys are greater than (or\r
+ * equal to, if {@code inclusive} is true) {@code fromKey}. The returned\r
+ * map is backed by this map, so changes in the returned map are reflected\r
+ * in this map, and vice-versa. The returned map supports all optional\r
+ * map operations that this map supports.\r
+ *\r
+ * <p>The returned map will throw an {@code IllegalArgumentException}\r
+ * on an attempt to insert a key outside its range.\r
+ *\r
+ * @param fromKey low endpoint of the keys in the returned map\r
+ * @param inclusive {@code true} if the low endpoint\r
+ * is to be included in the returned view\r
+ * @return a view of the portion of this map whose keys are greater than\r
+ * (or equal to, if {@code inclusive} is true) {@code fromKey}\r
+ * @throws ClassCastException if {@code fromKey} is not compatible\r
+ * with this map's comparator (or, if the map has no comparator,\r
+ * if {@code fromKey} does not implement {@link Comparable}).\r
+ * Implementations may, but are not required to, throw this\r
+ * exception if {@code fromKey} cannot be compared to keys\r
+ * currently in the map.\r
+ * @throws NullPointerException if {@code fromKey} is null\r
+ * and this map does not permit null keys\r
+ * @throws IllegalArgumentException if this map itself has a\r
+ * restricted range, and {@code fromKey} lies outside the\r
+ * bounds of the range\r
+ */\r
+ NavigableMap7<K,V> tailMap(K fromKey, boolean inclusive);\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ *\r
+ * <p>Equivalent to {@code subMap(fromKey, true, toKey, false)}.\r
+ *\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException {@inheritDoc}\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+ */\r
+ SortedMap<K,V> subMap(K fromKey, K toKey);\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ *\r
+ * <p>Equivalent to {@code headMap(toKey, false)}.\r
+ *\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException {@inheritDoc}\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+ */\r
+ SortedMap<K,V> headMap(K toKey);\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ *\r
+ * <p>Equivalent to {@code tailMap(fromKey, true)}.\r
+ *\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException {@inheritDoc}\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+ */\r
+ SortedMap<K,V> tailMap(K fromKey);\r
+}
\ No newline at end of file
--- /dev/null
+/*\r
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.\r
+ *\r
+ * This code is free software; you can redistribute it and/or modify it\r
+ * under the terms of the GNU General Public License version 2 only, as\r
+ * published by the Free Software Foundation. Oracle designates this\r
+ * particular file as subject to the "Classpath" exception as provided\r
+ * by Oracle in the LICENSE file that accompanied this code.\r
+ *\r
+ * This code is distributed in the hope that it will be useful, but WITHOUT\r
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\r
+ * version 2 for more details (a copy is included in the LICENSE file that\r
+ * accompanied this code).\r
+ *\r
+ * You should have received a copy of the GNU General Public License version\r
+ * 2 along with this work; if not, write to the Free Software Foundation,\r
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.\r
+ *\r
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA\r
+ * or visit www.oracle.com if you need additional information or have any\r
+ * questions.\r
+ */\r
+\r
+/*\r
+ * This file is available under and governed by the GNU General Public\r
+ * License version 2 only, as published by the Free Software Foundation.\r
+ * However, the following notice accompanied the original version of this\r
+ * file:\r
+ *\r
+ * Written by Doug Lea and Josh Bloch with assistance from members of JCP\r
+ * JSR-166 Expert Group and released to the public domain, as explained at\r
+ * http://creativecommons.org/publicdomain/zero/1.0/\r
+ */\r
+\r
+package org.apache.poi.util.java7_util;\r
+\r
+import java.util.Collections;\r
+import java.util.Comparator;\r
+import java.util.Iterator;\r
+import java.util.SortedSet;\r
+\r
+/**\r
+ * A {@link SortedSet} extended with navigation methods reporting\r
+ * closest matches for given search targets. Methods {@code lower},\r
+ * {@code floor}, {@code ceiling}, and {@code higher} return elements\r
+ * respectively less than, less than or equal, greater than or equal,\r
+ * and greater than a given element, returning {@code null} if there\r
+ * is no such element. A {@code NavigableSet7} may be accessed and\r
+ * traversed in either ascending or descending order. The {@code\r
+ * descendingSet} method returns a view of the set with the senses of\r
+ * all relational and directional methods inverted. The performance of\r
+ * ascending operations and views is likely to be faster than that of\r
+ * descending ones. This interface additionally defines methods\r
+ * {@code pollFirst} and {@code pollLast} that return and remove the\r
+ * lowest and highest element, if one exists, else returning {@code\r
+ * null}. Methods {@code subSet}, {@code headSet},\r
+ * and {@code tailSet} differ from the like-named {@code\r
+ * SortedSet} methods in accepting additional arguments describing\r
+ * whether lower and upper bounds are inclusive versus exclusive.\r
+ * Subsets of any {@code NavigableSet7} must implement the {@code\r
+ * NavigableSet7} interface.\r
+ *\r
+ * <p> The return values of navigation methods may be ambiguous in\r
+ * implementations that permit {@code null} elements. However, even\r
+ * in this case the result can be disambiguated by checking\r
+ * {@code contains(null)}. To avoid such issues, implementations of\r
+ * this interface are encouraged to <em>not</em> permit insertion of\r
+ * {@code null} elements. (Note that sorted sets of {@link\r
+ * Comparable} elements intrinsically do not permit {@code null}.)\r
+ *\r
+ * <p>Methods\r
+ * {@link #subSet(Object, Object) subSet(E, E)},\r
+ * {@link #headSet(Object) headSet(E)}, and\r
+ * {@link #tailSet(Object) tailSet(E)}\r
+ * are specified to return {@code SortedSet} to allow existing\r
+ * implementations of {@code SortedSet} to be compatibly retrofitted to\r
+ * implement {@code NavigableSet7}, but extensions and implementations\r
+ * of this interface are encouraged to override these methods to return\r
+ * {@code NavigableSet7}.\r
+ *\r
+ * <p>This interface is a member of the\r
+ * <a href="{@docRoot}/../technotes/guides/collections/index.html">\r
+ * Java Collections Framework</a>.\r
+ *\r
+ * @author Doug Lea\r
+ * @author Josh Bloch\r
+ * @param <E> the type of elements maintained by this set\r
+ * @since 1.6\r
+ */\r
+public interface NavigableSet7<E> extends SortedSet<E> {\r
+ /**\r
+ * Returns the greatest element in this set strictly less than the\r
+ * given element, or {@code null} if there is no such element.\r
+ *\r
+ * @param e the value to match\r
+ * @return the greatest element less than {@code e},\r
+ * or {@code null} if there is no such element\r
+ * @throws ClassCastException if the specified element cannot be\r
+ * compared with the elements currently in the set\r
+ * @throws NullPointerException if the specified element is null\r
+ * and this set does not permit null elements\r
+ */\r
+ E lower(E e);\r
+\r
+ /**\r
+ * Returns the greatest element in this set less than or equal to\r
+ * the given element, or {@code null} if there is no such element.\r
+ *\r
+ * @param e the value to match\r
+ * @return the greatest element less than or equal to {@code e},\r
+ * or {@code null} if there is no such element\r
+ * @throws ClassCastException if the specified element cannot be\r
+ * compared with the elements currently in the set\r
+ * @throws NullPointerException if the specified element is null\r
+ * and this set does not permit null elements\r
+ */\r
+ E floor(E e);\r
+\r
+ /**\r
+ * Returns the least element in this set greater than or equal to\r
+ * the given element, or {@code null} if there is no such element.\r
+ *\r
+ * @param e the value to match\r
+ * @return the least element greater than or equal to {@code e},\r
+ * or {@code null} if there is no such element\r
+ * @throws ClassCastException if the specified element cannot be\r
+ * compared with the elements currently in the set\r
+ * @throws NullPointerException if the specified element is null\r
+ * and this set does not permit null elements\r
+ */\r
+ E ceiling(E e);\r
+\r
+ /**\r
+ * Returns the least element in this set strictly greater than the\r
+ * given element, or {@code null} if there is no such element.\r
+ *\r
+ * @param e the value to match\r
+ * @return the least element greater than {@code e},\r
+ * or {@code null} if there is no such element\r
+ * @throws ClassCastException if the specified element cannot be\r
+ * compared with the elements currently in the set\r
+ * @throws NullPointerException if the specified element is null\r
+ * and this set does not permit null elements\r
+ */\r
+ E higher(E e);\r
+\r
+ /**\r
+ * Retrieves and removes the first (lowest) element,\r
+ * or returns {@code null} if this set is empty.\r
+ *\r
+ * @return the first element, or {@code null} if this set is empty\r
+ */\r
+ E pollFirst();\r
+\r
+ /**\r
+ * Retrieves and removes the last (highest) element,\r
+ * or returns {@code null} if this set is empty.\r
+ *\r
+ * @return the last element, or {@code null} if this set is empty\r
+ */\r
+ E pollLast();\r
+\r
+ /**\r
+ * Returns an iterator over the elements in this set, in ascending order.\r
+ *\r
+ * @return an iterator over the elements in this set, in ascending order\r
+ */\r
+ Iterator<E> iterator();\r
+\r
+ /**\r
+ * Returns a reverse order view of the elements contained in this set.\r
+ * The descending set is backed by this set, so changes to the set are\r
+ * reflected in the descending set, and vice-versa. If either set is\r
+ * modified while an iteration over either set is in progress (except\r
+ * through the iterator's own {@code remove} operation), the results of\r
+ * the iteration are undefined.\r
+ *\r
+ * <p>The returned set has an ordering equivalent to\r
+ * <tt>{@link Collections#reverseOrder(Comparator) Collections.reverseOrder}(comparator())</tt>.\r
+ * The expression {@code s.descendingSet().descendingSet()} returns a\r
+ * view of {@code s} essentially equivalent to {@code s}.\r
+ *\r
+ * @return a reverse order view of this set\r
+ */\r
+ NavigableSet7<E> descendingSet();\r
+\r
+ /**\r
+ * Returns an iterator over the elements in this set, in descending order.\r
+ * Equivalent in effect to {@code descendingSet().iterator()}.\r
+ *\r
+ * @return an iterator over the elements in this set, in descending order\r
+ */\r
+ Iterator<E> descendingIterator();\r
+\r
+ /**\r
+ * Returns a view of the portion of this set whose elements range from\r
+ * {@code fromElement} to {@code toElement}. If {@code fromElement} and\r
+ * {@code toElement} are equal, the returned set is empty unless {@code\r
+ * fromInclusive} and {@code toInclusive} are both true. The returned set\r
+ * is backed by this set, so changes in the returned set are reflected in\r
+ * this set, and vice-versa. The returned set supports all optional set\r
+ * operations that this set supports.\r
+ *\r
+ * <p>The returned set will throw an {@code IllegalArgumentException}\r
+ * on an attempt to insert an element outside its range.\r
+ *\r
+ * @param fromElement low endpoint of the returned set\r
+ * @param fromInclusive {@code true} if the low endpoint\r
+ * is to be included in the returned view\r
+ * @param toElement high endpoint of the returned set\r
+ * @param toInclusive {@code true} if the high endpoint\r
+ * is to be included in the returned view\r
+ * @return a view of the portion of this set whose elements range from\r
+ * {@code fromElement}, inclusive, to {@code toElement}, exclusive\r
+ * @throws ClassCastException if {@code fromElement} and\r
+ * {@code toElement} cannot be compared to one another using this\r
+ * set's comparator (or, if the set has no comparator, using\r
+ * natural ordering). Implementations may, but are not required\r
+ * to, throw this exception if {@code fromElement} or\r
+ * {@code toElement} cannot be compared to elements currently in\r
+ * the set.\r
+ * @throws NullPointerException if {@code fromElement} or\r
+ * {@code toElement} is null and this set does\r
+ * not permit null elements\r
+ * @throws IllegalArgumentException if {@code fromElement} is\r
+ * greater than {@code toElement}; or if this set itself\r
+ * has a restricted range, and {@code fromElement} or\r
+ * {@code toElement} lies outside the bounds of the range.\r
+ */\r
+ NavigableSet7<E> subSet(E fromElement, boolean fromInclusive,\r
+ E toElement, boolean toInclusive);\r
+\r
+ /**\r
+ * Returns a view of the portion of this set whose elements are less than\r
+ * (or equal to, if {@code inclusive} is true) {@code toElement}. The\r
+ * returned set is backed by this set, so changes in the returned set are\r
+ * reflected in this set, and vice-versa. The returned set supports all\r
+ * optional set operations that this set supports.\r
+ *\r
+ * <p>The returned set will throw an {@code IllegalArgumentException}\r
+ * on an attempt to insert an element outside its range.\r
+ *\r
+ * @param toElement high endpoint of the returned set\r
+ * @param inclusive {@code true} if the high endpoint\r
+ * is to be included in the returned view\r
+ * @return a view of the portion of this set whose elements are less than\r
+ * (or equal to, if {@code inclusive} is true) {@code toElement}\r
+ * @throws ClassCastException if {@code toElement} is not compatible\r
+ * with this set's comparator (or, if the set has no comparator,\r
+ * if {@code toElement} does not implement {@link Comparable}).\r
+ * Implementations may, but are not required to, throw this\r
+ * exception if {@code toElement} cannot be compared to elements\r
+ * currently in the set.\r
+ * @throws NullPointerException if {@code toElement} is null and\r
+ * this set does not permit null elements\r
+ * @throws IllegalArgumentException if this set itself has a\r
+ * restricted range, and {@code toElement} lies outside the\r
+ * bounds of the range\r
+ */\r
+ NavigableSet7<E> headSet(E toElement, boolean inclusive);\r
+\r
+ /**\r
+ * Returns a view of the portion of this set whose elements are greater\r
+ * than (or equal to, if {@code inclusive} is true) {@code fromElement}.\r
+ * The returned set is backed by this set, so changes in the returned set\r
+ * are reflected in this set, and vice-versa. The returned set supports\r
+ * all optional set operations that this set supports.\r
+ *\r
+ * <p>The returned set will throw an {@code IllegalArgumentException}\r
+ * on an attempt to insert an element outside its range.\r
+ *\r
+ * @param fromElement low endpoint of the returned set\r
+ * @param inclusive {@code true} if the low endpoint\r
+ * is to be included in the returned view\r
+ * @return a view of the portion of this set whose elements are greater\r
+ * than or equal to {@code fromElement}\r
+ * @throws ClassCastException if {@code fromElement} is not compatible\r
+ * with this set's comparator (or, if the set has no comparator,\r
+ * if {@code fromElement} does not implement {@link Comparable}).\r
+ * Implementations may, but are not required to, throw this\r
+ * exception if {@code fromElement} cannot be compared to elements\r
+ * currently in the set.\r
+ * @throws NullPointerException if {@code fromElement} is null\r
+ * and this set does not permit null elements\r
+ * @throws IllegalArgumentException if this set itself has a\r
+ * restricted range, and {@code fromElement} lies outside the\r
+ * bounds of the range\r
+ */\r
+ NavigableSet7<E> tailSet(E fromElement, boolean inclusive);\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ *\r
+ * <p>Equivalent to {@code subSet(fromElement, true, toElement, false)}.\r
+ *\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException {@inheritDoc}\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+ */\r
+ SortedSet<E> subSet(E fromElement, E toElement);\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ *\r
+ * <p>Equivalent to {@code headSet(toElement, false)}.\r
+ *\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException {@inheritDoc}\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+na */\r
+ SortedSet<E> headSet(E toElement);\r
+\r
+ /**\r
+ * {@inheritDoc}\r
+ *\r
+ * <p>Equivalent to {@code tailSet(fromElement, true)}.\r
+ *\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException {@inheritDoc}\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+ */\r
+ SortedSet<E> tailSet(E fromElement);\r
+}
\ No newline at end of file
--- /dev/null
+/*\r
+ * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.\r
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.\r
+ *\r
+ * This code is free software; you can redistribute it and/or modify it\r
+ * under the terms of the GNU General Public License version 2 only, as\r
+ * published by the Free Software Foundation. Oracle designates this\r
+ * particular file as subject to the "Classpath" exception as provided\r
+ * by Oracle in the LICENSE file that accompanied this code.\r
+ *\r
+ * This code is distributed in the hope that it will be useful, but WITHOUT\r
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\r
+ * version 2 for more details (a copy is included in the LICENSE file that\r
+ * accompanied this code).\r
+ *\r
+ * You should have received a copy of the GNU General Public License version\r
+ * 2 along with this work; if not, write to the Free Software Foundation,\r
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.\r
+ *\r
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA\r
+ * or visit www.oracle.com if you need additional information or have any\r
+ * questions.\r
+ */\r
+\r
+package org.apache.poi.util.java7_util;\r
+\r
+import java.io.IOException;\r
+import java.util.AbstractCollection;\r
+import java.util.AbstractSet;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.Comparator;\r
+import java.util.ConcurrentModificationException;\r
+import java.util.HashMap;\r
+import java.util.Hashtable;\r
+import java.util.Iterator;\r
+import java.util.Map;\r
+import java.util.NoSuchElementException;\r
+import java.util.Set;\r
+import java.util.SortedMap;\r
+import java.util.SortedSet;\r
+\r
+/**\r
+ * A Red-Black tree based {@link NavigableMap7} implementation.\r
+ * The map is sorted according to the {@linkplain Comparable natural\r
+ * ordering} of its keys, or by a {@link Comparator} provided at map\r
+ * creation time, depending on which constructor is used.\r
+ *\r
+ * <p>This implementation provides guaranteed log(n) time cost for the\r
+ * {@code containsKey}, {@code get}, {@code put} and {@code remove}\r
+ * operations. Algorithms are adaptations of those in Cormen, Leiserson, and\r
+ * Rivest's <em>Introduction to Algorithms</em>.\r
+ *\r
+ * <p>Note that the ordering maintained by a tree map, like any sorted map, and\r
+ * whether or not an explicit comparator is provided, must be <em>consistent\r
+ * with {@code equals}</em> if this sorted map is to correctly implement the\r
+ * {@code Map} interface. (See {@code Comparable} or {@code Comparator} for a\r
+ * precise definition of <em>consistent with equals</em>.) This is so because\r
+ * the {@code Map} interface is defined in terms of the {@code equals}\r
+ * operation, but a sorted map performs all key comparisons using its {@code\r
+ * compareTo} (or {@code compare}) method, so two keys that are deemed equal by\r
+ * this method are, from the standpoint of the sorted map, equal. The behavior\r
+ * of a sorted map <em>is</em> well-defined even if its ordering is\r
+ * inconsistent with {@code equals}; it just fails to obey the general contract\r
+ * of the {@code Map} interface.\r
+ *\r
+ * <p><strong>Note that this implementation is not synchronized.</strong>\r
+ * If multiple threads access a map concurrently, and at least one of the\r
+ * threads modifies the map structurally, it <em>must</em> be synchronized\r
+ * externally. (A structural modification is any operation that adds or\r
+ * deletes one or more mappings; merely changing the value associated\r
+ * with an existing key is not a structural modification.) This is\r
+ * typically accomplished by synchronizing on some object that naturally\r
+ * encapsulates the map.\r
+ * If no such object exists, the map should be "wrapped" using the\r
+ * {@link Collections#synchronizedSortedMap Collections.synchronizedSortedMap}\r
+ * method. This is best done at creation time, to prevent accidental\r
+ * unsynchronized access to the map: <pre>\r
+ * SortedMap m = Collections.synchronizedSortedMap(new TreeMap7(...));</pre>\r
+ *\r
+ * <p>The iterators returned by the {@code iterator} method of the collections\r
+ * returned by all of this class's "collection view methods" are\r
+ * <em>fail-fast</em>: if the map is structurally modified at any time after\r
+ * the iterator is created, in any way except through the iterator's own\r
+ * {@code remove} method, the iterator will throw a {@link\r
+ * ConcurrentModificationException}. Thus, in the face of concurrent\r
+ * modification, the iterator fails quickly and cleanly, rather than risking\r
+ * arbitrary, non-deterministic behavior at an undetermined time in the future.\r
+ *\r
+ * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed\r
+ * as it is, generally speaking, impossible to make any hard guarantees in the\r
+ * presence of unsynchronized concurrent modification. Fail-fast iterators\r
+ * throw {@code ConcurrentModificationException} on a best-effort basis.\r
+ * Therefore, it would be wrong to write a program that depended on this\r
+ * exception for its correctness: <em>the fail-fast behavior of iterators\r
+ * should be used only to detect bugs.</em>\r
+ *\r
+ * <p>All {@code Map.Entry} pairs returned by methods in this class\r
+ * and its views represent snapshots of mappings at the time they were\r
+ * produced. They do <strong>not</strong> support the {@code Entry.setValue}\r
+ * method. (Note however that it is possible to change mappings in the\r
+ * associated map using {@code put}.)\r
+ *\r
+ * <p>This class is a member of the\r
+ * <a href="{@docRoot}/../technotes/guides/collections/index.html">\r
+ * Java Collections Framework</a>.\r
+ *\r
+ * @param <K> the type of keys maintained by this map\r
+ * @param <V> the type of mapped values\r
+ *\r
+ * @author Josh Bloch and Doug Lea\r
+ * @see Map\r
+ * @see HashMap\r
+ * @see Hashtable\r
+ * @see Comparable\r
+ * @see Comparator\r
+ * @see Collection\r
+ * @since 1.2\r
+ */\r
+\r
+public class TreeMap7<K,V>\r
+ extends AbstractMap7<K,V>\r
+ implements NavigableMap7<K,V>, Cloneable, java.io.Serializable\r
+{\r
+ /**\r
+ * The comparator used to maintain order in this tree map, or\r
+ * null if it uses the natural ordering of its keys.\r
+ *\r
+ * @serial\r
+ */\r
+ private final Comparator<? super K> comparator;\r
+\r
+ private transient Entry<K,V> root = null;\r
+\r
+ /**\r
+ * The number of entries in the tree\r
+ */\r
+ private transient int size = 0;\r
+\r
+ /**\r
+ * The number of structural modifications to the tree.\r
+ */\r
+ private transient int modCount = 0;\r
+\r
+ /**\r
+ * Constructs a new, empty tree map, using the natural ordering of its\r
+ * keys. All keys inserted into the map must implement the {@link\r
+ * Comparable} interface. Furthermore, all such keys must be\r
+ * <em>mutually comparable</em>: {@code k1.compareTo(k2)} must not throw\r
+ * a {@code ClassCastException} for any keys {@code k1} and\r
+ * {@code k2} in the map. If the user attempts to put a key into the\r
+ * map that violates this constraint (for example, the user attempts to\r
+ * put a string key into a map whose keys are integers), the\r
+ * {@code put(Object key, Object value)} call will throw a\r
+ * {@code ClassCastException}.\r
+ */\r
+ public TreeMap7() {\r
+ comparator = null;\r
+ }\r
+\r
+ /**\r
+ * Constructs a new, empty tree map, ordered according to the given\r
+ * comparator. All keys inserted into the map must be <em>mutually\r
+ * comparable</em> by the given comparator: {@code comparator.compare(k1,\r
+ * k2)} must not throw a {@code ClassCastException} for any keys\r
+ * {@code k1} and {@code k2} in the map. If the user attempts to put\r
+ * a key into the map that violates this constraint, the {@code put(Object\r
+ * key, Object value)} call will throw a\r
+ * {@code ClassCastException}.\r
+ *\r
+ * @param comparator the comparator that will be used to order this map.\r
+ * If {@code null}, the {@linkplain Comparable natural\r
+ * ordering} of the keys will be used.\r
+ */\r
+ public TreeMap7(Comparator<? super K> comparator) {\r
+ this.comparator = comparator;\r
+ }\r
+\r
+ /**\r
+ * Constructs a new tree map containing the same mappings as the given\r
+ * map, ordered according to the <em>natural ordering</em> of its keys.\r
+ * All keys inserted into the new map must implement the {@link\r
+ * Comparable} interface. Furthermore, all such keys must be\r
+ * <em>mutually comparable</em>: {@code k1.compareTo(k2)} must not throw\r
+ * a {@code ClassCastException} for any keys {@code k1} and\r
+ * {@code k2} in the map. This method runs in n*log(n) time.\r
+ *\r
+ * @param m the map whose mappings are to be placed in this map\r
+ * @throws ClassCastException if the keys in m are not {@link Comparable},\r
+ * or are not mutually comparable\r
+ * @throws NullPointerException if the specified map is null\r
+ */\r
+ public TreeMap7(Map<? extends K, ? extends V> m) {\r
+ comparator = null;\r
+ putAll(m);\r
+ }\r
+\r
+ /**\r
+ * Constructs a new tree map containing the same mappings and\r
+ * using the same ordering as the specified sorted map. This\r
+ * method runs in linear time.\r
+ *\r
+ * @param m the sorted map whose mappings are to be placed in this map,\r
+ * and whose comparator is to be used to sort this map\r
+ * @throws NullPointerException if the specified map is null\r
+ */\r
+ public TreeMap7(SortedMap<K, ? extends V> m) {\r
+ comparator = m.comparator();\r
+ try {\r
+ buildFromSorted(m.size(), m.entrySet().iterator(), null, null);\r
+ } catch (java.io.IOException cannotHappen) {\r
+ } catch (ClassNotFoundException cannotHappen) {\r
+ }\r
+ }\r
+\r
+\r
+ // Query Operations\r
+\r
+ /**\r
+ * Returns the number of key-value mappings in this map.\r
+ *\r
+ * @return the number of key-value mappings in this map\r
+ */\r
+ public int size() {\r
+ return size;\r
+ }\r
+\r
+ /**\r
+ * Returns {@code true} if this map contains a mapping for the specified\r
+ * key.\r
+ *\r
+ * @param key key whose presence in this map is to be tested\r
+ * @return {@code true} if this map contains a mapping for the\r
+ * specified key\r
+ * @throws ClassCastException if the specified key cannot be compared\r
+ * with the keys currently in the map\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map uses natural ordering, or its comparator\r
+ * does not permit null keys\r
+ */\r
+ public boolean containsKey(Object key) {\r
+ return getEntry(key) != null;\r
+ }\r
+\r
+ /**\r
+ * Returns {@code true} if this map maps one or more keys to the\r
+ * specified value. More formally, returns {@code true} if and only if\r
+ * this map contains at least one mapping to a value {@code v} such\r
+ * that {@code (value==null ? v==null : value.equals(v))}. This\r
+ * operation will probably require time linear in the map size for\r
+ * most implementations.\r
+ *\r
+ * @param value value whose presence in this map is to be tested\r
+ * @return {@code true} if a mapping to {@code value} exists;\r
+ * {@code false} otherwise\r
+ * @since 1.2\r
+ */\r
+ public boolean containsValue(Object value) {\r
+ for (Entry<K,V> e = getFirstEntry(); e != null; e = successor(e))\r
+ if (valEquals(value, e.value))\r
+ return true;\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * Returns the value to which the specified key is mapped,\r
+ * or {@code null} if this map contains no mapping for the key.\r
+ *\r
+ * <p>More formally, if this map contains a mapping from a key\r
+ * {@code k} to a value {@code v} such that {@code key} compares\r
+ * equal to {@code k} according to the map's ordering, then this\r
+ * method returns {@code v}; otherwise it returns {@code null}.\r
+ * (There can be at most one such mapping.)\r
+ *\r
+ * <p>A return value of {@code null} does not <em>necessarily</em>\r
+ * indicate that the map contains no mapping for the key; it's also\r
+ * possible that the map explicitly maps the key to {@code null}.\r
+ * The {@link #containsKey containsKey} operation may be used to\r
+ * distinguish these two cases.\r
+ *\r
+ * @throws ClassCastException if the specified key cannot be compared\r
+ * with the keys currently in the map\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map uses natural ordering, or its comparator\r
+ * does not permit null keys\r
+ */\r
+ public V get(Object key) {\r
+ Entry<K,V> p = getEntry(key);\r
+ return (p==null ? null : p.value);\r
+ }\r
+\r
+ public Comparator<? super K> comparator() {\r
+ return comparator;\r
+ }\r
+\r
+ /**\r
+ * @throws NoSuchElementException {@inheritDoc}\r
+ */\r
+ public K firstKey() {\r
+ return key(getFirstEntry());\r
+ }\r
+\r
+ /**\r
+ * @throws NoSuchElementException {@inheritDoc}\r
+ */\r
+ public K lastKey() {\r
+ return key(getLastEntry());\r
+ }\r
+\r
+ /**\r
+ * Copies all of the mappings from the specified map to this map.\r
+ * These mappings replace any mappings that this map had for any\r
+ * of the keys currently in the specified map.\r
+ *\r
+ * @param map mappings to be stored in this map\r
+ * @throws ClassCastException if the class of a key or value in\r
+ * the specified map prevents it from being stored in this map\r
+ * @throws NullPointerException if the specified map is null or\r
+ * the specified map contains a null key and this map does not\r
+ * permit null keys\r
+ */\r
+ public void putAll(Map<? extends K, ? extends V> map) {\r
+ int mapSize = map.size();\r
+ if (size==0 && mapSize!=0 && map instanceof SortedMap) {\r
+ Comparator c = ((SortedMap)map).comparator();\r
+ if (c == comparator || (c != null && c.equals(comparator))) {\r
+ ++modCount;\r
+ try {\r
+ buildFromSorted(mapSize, map.entrySet().iterator(),\r
+ null, null);\r
+ } catch (java.io.IOException cannotHappen) {\r
+ } catch (ClassNotFoundException cannotHappen) {\r
+ }\r
+ return;\r
+ }\r
+ }\r
+ super.putAll(map);\r
+ }\r
+\r
+ /**\r
+ * Returns this map's entry for the given key, or {@code null} if the map\r
+ * does not contain an entry for the key.\r
+ *\r
+ * @return this map's entry for the given key, or {@code null} if the map\r
+ * does not contain an entry for the key\r
+ * @throws ClassCastException if the specified key cannot be compared\r
+ * with the keys currently in the map\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map uses natural ordering, or its comparator\r
+ * does not permit null keys\r
+ */\r
+ final Entry<K,V> getEntry(Object key) {\r
+ // Offload comparator-based version for sake of performance\r
+ if (comparator != null)\r
+ return getEntryUsingComparator(key);\r
+ if (key == null)\r
+ throw new NullPointerException();\r
+ Comparable<? super K> k = (Comparable<? super K>) key;\r
+ Entry<K,V> p = root;\r
+ while (p != null) {\r
+ int cmp = k.compareTo(p.key);\r
+ if (cmp < 0)\r
+ p = p.left;\r
+ else if (cmp > 0)\r
+ p = p.right;\r
+ else\r
+ return p;\r
+ }\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * Version of getEntry using comparator. Split off from getEntry\r
+ * for performance. (This is not worth doing for most methods,\r
+ * that are less dependent on comparator performance, but is\r
+ * worthwhile here.)\r
+ */\r
+ final Entry<K,V> getEntryUsingComparator(Object key) {\r
+ K k = (K) key;\r
+ Comparator<? super K> cpr = comparator;\r
+ if (cpr != null) {\r
+ Entry<K,V> p = root;\r
+ while (p != null) {\r
+ int cmp = cpr.compare(k, p.key);\r
+ if (cmp < 0)\r
+ p = p.left;\r
+ else if (cmp > 0)\r
+ p = p.right;\r
+ else\r
+ return p;\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * Gets the entry corresponding to the specified key; if no such entry\r
+ * exists, returns the entry for the least key greater than the specified\r
+ * key; if no such entry exists (i.e., the greatest key in the Tree is less\r
+ * than the specified key), returns {@code null}.\r
+ */\r
+ final Entry<K,V> getCeilingEntry(K key) {\r
+ Entry<K,V> p = root;\r
+ while (p != null) {\r
+ int cmp = compare(key, p.key);\r
+ if (cmp < 0) {\r
+ if (p.left != null)\r
+ p = p.left;\r
+ else\r
+ return p;\r
+ } else if (cmp > 0) {\r
+ if (p.right != null) {\r
+ p = p.right;\r
+ } else {\r
+ Entry<K,V> parent = p.parent;\r
+ Entry<K,V> ch = p;\r
+ while (parent != null && ch == parent.right) {\r
+ ch = parent;\r
+ parent = parent.parent;\r
+ }\r
+ return parent;\r
+ }\r
+ } else\r
+ return p;\r
+ }\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * Gets the entry corresponding to the specified key; if no such entry\r
+ * exists, returns the entry for the greatest key less than the specified\r
+ * key; if no such entry exists, returns {@code null}.\r
+ */\r
+ final Entry<K,V> getFloorEntry(K key) {\r
+ Entry<K,V> p = root;\r
+ while (p != null) {\r
+ int cmp = compare(key, p.key);\r
+ if (cmp > 0) {\r
+ if (p.right != null)\r
+ p = p.right;\r
+ else\r
+ return p;\r
+ } else if (cmp < 0) {\r
+ if (p.left != null) {\r
+ p = p.left;\r
+ } else {\r
+ Entry<K,V> parent = p.parent;\r
+ Entry<K,V> ch = p;\r
+ while (parent != null && ch == parent.left) {\r
+ ch = parent;\r
+ parent = parent.parent;\r
+ }\r
+ return parent;\r
+ }\r
+ } else\r
+ return p;\r
+\r
+ }\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * Gets the entry for the least key greater than the specified\r
+ * key; if no such entry exists, returns the entry for the least\r
+ * key greater than the specified key; if no such entry exists\r
+ * returns {@code null}.\r
+ */\r
+ final Entry<K,V> getHigherEntry(K key) {\r
+ Entry<K,V> p = root;\r
+ while (p != null) {\r
+ int cmp = compare(key, p.key);\r
+ if (cmp < 0) {\r
+ if (p.left != null)\r
+ p = p.left;\r
+ else\r
+ return p;\r
+ } else {\r
+ if (p.right != null) {\r
+ p = p.right;\r
+ } else {\r
+ Entry<K,V> parent = p.parent;\r
+ Entry<K,V> ch = p;\r
+ while (parent != null && ch == parent.right) {\r
+ ch = parent;\r
+ parent = parent.parent;\r
+ }\r
+ return parent;\r
+ }\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * Returns the entry for the greatest key less than the specified key; if\r
+ * no such entry exists (i.e., the least key in the Tree is greater than\r
+ * the specified key), returns {@code null}.\r
+ */\r
+ final Entry<K,V> getLowerEntry(K key) {\r
+ Entry<K,V> p = root;\r
+ while (p != null) {\r
+ int cmp = compare(key, p.key);\r
+ if (cmp > 0) {\r
+ if (p.right != null)\r
+ p = p.right;\r
+ else\r
+ return p;\r
+ } else {\r
+ if (p.left != null) {\r
+ p = p.left;\r
+ } else {\r
+ Entry<K,V> parent = p.parent;\r
+ Entry<K,V> ch = p;\r
+ while (parent != null && ch == parent.left) {\r
+ ch = parent;\r
+ parent = parent.parent;\r
+ }\r
+ return parent;\r
+ }\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * Associates the specified value with the specified key in this map.\r
+ * If the map previously contained a mapping for the key, the old\r
+ * value is replaced.\r
+ *\r
+ * @param key key with which the specified value is to be associated\r
+ * @param value value to be associated with the specified key\r
+ *\r
+ * @return the previous value associated with {@code key}, or\r
+ * {@code null} if there was no mapping for {@code key}.\r
+ * (A {@code null} return can also indicate that the map\r
+ * previously associated {@code null} with {@code key}.)\r
+ * @throws ClassCastException if the specified key cannot be compared\r
+ * with the keys currently in the map\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map uses natural ordering, or its comparator\r
+ * does not permit null keys\r
+ */\r
+ public V put(K key, V value) {\r
+ Entry<K,V> t = root;\r
+ if (t == null) {\r
+ compare(key, key); // type (and possibly null) check\r
+\r
+ root = new Entry<K,V>(key, value, null);\r
+ size = 1;\r
+ modCount++;\r
+ return null;\r
+ }\r
+ int cmp;\r
+ Entry<K,V> parent;\r
+ // split comparator and comparable paths\r
+ Comparator<? super K> cpr = comparator;\r
+ if (cpr != null) {\r
+ do {\r
+ parent = t;\r
+ cmp = cpr.compare(key, t.key);\r
+ if (cmp < 0)\r
+ t = t.left;\r
+ else if (cmp > 0)\r
+ t = t.right;\r
+ else\r
+ return t.setValue(value);\r
+ } while (t != null);\r
+ }\r
+ else {\r
+ if (key == null)\r
+ throw new NullPointerException();\r
+ Comparable<? super K> k = (Comparable<? super K>) key;\r
+ do {\r
+ parent = t;\r
+ cmp = k.compareTo(t.key);\r
+ if (cmp < 0)\r
+ t = t.left;\r
+ else if (cmp > 0)\r
+ t = t.right;\r
+ else\r
+ return t.setValue(value);\r
+ } while (t != null);\r
+ }\r
+ Entry<K,V> e = new Entry<K,V>(key, value, parent);\r
+ if (cmp < 0)\r
+ parent.left = e;\r
+ else\r
+ parent.right = e;\r
+ fixAfterInsertion(e);\r
+ size++;\r
+ modCount++;\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * Removes the mapping for this key from this TreeMap7 if present.\r
+ *\r
+ * @param key key for which mapping should be removed\r
+ * @return the previous value associated with {@code key}, or\r
+ * {@code null} if there was no mapping for {@code key}.\r
+ * (A {@code null} return can also indicate that the map\r
+ * previously associated {@code null} with {@code key}.)\r
+ * @throws ClassCastException if the specified key cannot be compared\r
+ * with the keys currently in the map\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map uses natural ordering, or its comparator\r
+ * does not permit null keys\r
+ */\r
+ public V remove(Object key) {\r
+ Entry<K,V> p = getEntry(key);\r
+ if (p == null)\r
+ return null;\r
+\r
+ V oldValue = p.value;\r
+ deleteEntry(p);\r
+ return oldValue;\r
+ }\r
+\r
+ /**\r
+ * Removes all of the mappings from this map.\r
+ * The map will be empty after this call returns.\r
+ */\r
+ public void clear() {\r
+ modCount++;\r
+ size = 0;\r
+ root = null;\r
+ }\r
+\r
+ /**\r
+ * Returns a shallow copy of this {@code TreeMap7} instance. (The keys and\r
+ * values themselves are not cloned.)\r
+ *\r
+ * @return a shallow copy of this map\r
+ */\r
+ public Object clone() {\r
+ TreeMap7<K,V> clone = null;\r
+ try {\r
+ clone = (TreeMap7<K,V>) super.clone();\r
+ } catch (CloneNotSupportedException e) {\r
+ throw new InternalError();\r
+ }\r
+\r
+ // Put clone into "virgin" state (except for comparator)\r
+ clone.root = null;\r
+ clone.size = 0;\r
+ clone.modCount = 0;\r
+ clone.entrySet = null;\r
+ clone.navigableKeySet = null;\r
+ clone.descendingMap = null;\r
+\r
+ // Initialize clone with our mappings\r
+ try {\r
+ clone.buildFromSorted(size, entrySet().iterator(), null, null);\r
+ } catch (java.io.IOException cannotHappen) {\r
+ } catch (ClassNotFoundException cannotHappen) {\r
+ }\r
+\r
+ return clone;\r
+ }\r
+\r
+ // NavigableMap7 API methods\r
+\r
+ /**\r
+ * @since 1.6\r
+ */\r
+ public Map.Entry<K,V> firstEntry() {\r
+ return exportEntry(getFirstEntry());\r
+ }\r
+\r
+ /**\r
+ * @since 1.6\r
+ */\r
+ public Map.Entry<K,V> lastEntry() {\r
+ return exportEntry(getLastEntry());\r
+ }\r
+\r
+ /**\r
+ * @since 1.6\r
+ */\r
+ public Map.Entry<K,V> pollFirstEntry() {\r
+ Entry<K,V> p = getFirstEntry();\r
+ Map.Entry<K,V> result = exportEntry(p);\r
+ if (p != null)\r
+ deleteEntry(p);\r
+ return result;\r
+ }\r
+\r
+ /**\r
+ * @since 1.6\r
+ */\r
+ public Map.Entry<K,V> pollLastEntry() {\r
+ Entry<K,V> p = getLastEntry();\r
+ Map.Entry<K,V> result = exportEntry(p);\r
+ if (p != null)\r
+ deleteEntry(p);\r
+ return result;\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map uses natural ordering, or its comparator\r
+ * does not permit null keys\r
+ * @since 1.6\r
+ */\r
+ public Map.Entry<K,V> lowerEntry(K key) {\r
+ return exportEntry(getLowerEntry(key));\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map uses natural ordering, or its comparator\r
+ * does not permit null keys\r
+ * @since 1.6\r
+ */\r
+ public K lowerKey(K key) {\r
+ return keyOrNull(getLowerEntry(key));\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map uses natural ordering, or its comparator\r
+ * does not permit null keys\r
+ * @since 1.6\r
+ */\r
+ public Map.Entry<K,V> floorEntry(K key) {\r
+ return exportEntry(getFloorEntry(key));\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map uses natural ordering, or its comparator\r
+ * does not permit null keys\r
+ * @since 1.6\r
+ */\r
+ public K floorKey(K key) {\r
+ return keyOrNull(getFloorEntry(key));\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map uses natural ordering, or its comparator\r
+ * does not permit null keys\r
+ * @since 1.6\r
+ */\r
+ public Map.Entry<K,V> ceilingEntry(K key) {\r
+ return exportEntry(getCeilingEntry(key));\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map uses natural ordering, or its comparator\r
+ * does not permit null keys\r
+ * @since 1.6\r
+ */\r
+ public K ceilingKey(K key) {\r
+ return keyOrNull(getCeilingEntry(key));\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map uses natural ordering, or its comparator\r
+ * does not permit null keys\r
+ * @since 1.6\r
+ */\r
+ public Map.Entry<K,V> higherEntry(K key) {\r
+ return exportEntry(getHigherEntry(key));\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if the specified key is null\r
+ * and this map uses natural ordering, or its comparator\r
+ * does not permit null keys\r
+ * @since 1.6\r
+ */\r
+ public K higherKey(K key) {\r
+ return keyOrNull(getHigherEntry(key));\r
+ }\r
+\r
+ // Views\r
+\r
+ /**\r
+ * Fields initialized to contain an instance of the entry set view\r
+ * the first time this view is requested. Views are stateless, so\r
+ * there's no reason to create more than one.\r
+ */\r
+ private transient EntrySet entrySet = null;\r
+ private transient KeySet<K> navigableKeySet = null;\r
+ private transient NavigableMap7<K,V> descendingMap = null;\r
+\r
+ /**\r
+ * Returns a {@link Set} view of the keys contained in this map.\r
+ * The set's iterator returns the keys in ascending order.\r
+ * The set is backed by the map, so changes to the map are\r
+ * reflected in the set, and vice-versa. If the map is modified\r
+ * while an iteration over the set is in progress (except through\r
+ * the iterator's own {@code remove} operation), the results of\r
+ * the iteration are undefined. The set supports element removal,\r
+ * which removes the corresponding mapping from the map, via the\r
+ * {@code Iterator.remove}, {@code Set.remove},\r
+ * {@code removeAll}, {@code retainAll}, and {@code clear}\r
+ * operations. It does not support the {@code add} or {@code addAll}\r
+ * operations.\r
+ */\r
+ public Set<K> keySet() {\r
+ return navigableKeySet();\r
+ }\r
+\r
+ /**\r
+ * @since 1.6\r
+ */\r
+ public NavigableSet7<K> navigableKeySet() {\r
+ KeySet<K> nks = navigableKeySet;\r
+ return (nks != null) ? nks : (navigableKeySet = new KeySet(this));\r
+ }\r
+\r
+ /**\r
+ * @since 1.6\r
+ */\r
+ public NavigableSet7<K> descendingKeySet() {\r
+ return descendingMap().navigableKeySet();\r
+ }\r
+\r
+ /**\r
+ * Returns a {@link Collection} view of the values contained in this map.\r
+ * The collection's iterator returns the values in ascending order\r
+ * of the corresponding keys.\r
+ * The collection is backed by the map, so changes to the map are\r
+ * reflected in the collection, and vice-versa. If the map is\r
+ * modified while an iteration over the collection is in progress\r
+ * (except through the iterator's own {@code remove} operation),\r
+ * the results of the iteration are undefined. The collection\r
+ * supports element removal, which removes the corresponding\r
+ * mapping from the map, via the {@code Iterator.remove},\r
+ * {@code Collection.remove}, {@code removeAll},\r
+ * {@code retainAll} and {@code clear} operations. It does not\r
+ * support the {@code add} or {@code addAll} operations.\r
+ */\r
+ public Collection<V> values() {\r
+ Collection<V> vs = values;\r
+ return (vs != null) ? vs : (values = new Values());\r
+ }\r
+\r
+ /**\r
+ * Returns a {@link Set} view of the mappings contained in this map.\r
+ * The set's iterator returns the entries in ascending key order.\r
+ * The set is backed by the map, so changes to the map are\r
+ * reflected in the set, and vice-versa. If the map is modified\r
+ * while an iteration over the set is in progress (except through\r
+ * the iterator's own {@code remove} operation, or through the\r
+ * {@code setValue} operation on a map entry returned by the\r
+ * iterator) the results of the iteration are undefined. The set\r
+ * supports element removal, which removes the corresponding\r
+ * mapping from the map, via the {@code Iterator.remove},\r
+ * {@code Set.remove}, {@code removeAll}, {@code retainAll} and\r
+ * {@code clear} operations. It does not support the\r
+ * {@code add} or {@code addAll} operations.\r
+ */\r
+ public Set<Map.Entry<K,V>> entrySet() {\r
+ EntrySet es = entrySet;\r
+ return (es != null) ? es : (entrySet = new EntrySet());\r
+ }\r
+\r
+ /**\r
+ * @since 1.6\r
+ */\r
+ public NavigableMap7<K, V> descendingMap() {\r
+ NavigableMap7<K, V> km = descendingMap;\r
+ return (km != null) ? km :\r
+ (descendingMap = new DescendingSubMap(this,\r
+ true, null, true,\r
+ true, null, true));\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if {@code fromKey} or {@code toKey} is\r
+ * null and this map uses natural ordering, or its comparator\r
+ * does not permit null keys\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+ * @since 1.6\r
+ */\r
+ public NavigableMap7<K,V> subMap(K fromKey, boolean fromInclusive,\r
+ K toKey, boolean toInclusive) {\r
+ return new AscendingSubMap(this,\r
+ false, fromKey, fromInclusive,\r
+ false, toKey, toInclusive);\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if {@code toKey} is null\r
+ * and this map uses natural ordering, or its comparator\r
+ * does not permit null keys\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+ * @since 1.6\r
+ */\r
+ public NavigableMap7<K,V> headMap(K toKey, boolean inclusive) {\r
+ return new AscendingSubMap(this,\r
+ true, null, true,\r
+ false, toKey, inclusive);\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if {@code fromKey} is null\r
+ * and this map uses natural ordering, or its comparator\r
+ * does not permit null keys\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+ * @since 1.6\r
+ */\r
+ public NavigableMap7<K,V> tailMap(K fromKey, boolean inclusive) {\r
+ return new AscendingSubMap(this,\r
+ false, fromKey, inclusive,\r
+ true, null, true);\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if {@code fromKey} or {@code toKey} is\r
+ * null and this map uses natural ordering, or its comparator\r
+ * does not permit null keys\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+ */\r
+ public SortedMap<K,V> subMap(K fromKey, K toKey) {\r
+ return subMap(fromKey, true, toKey, false);\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if {@code toKey} is null\r
+ * and this map uses natural ordering, or its comparator\r
+ * does not permit null keys\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+ */\r
+ public SortedMap<K,V> headMap(K toKey) {\r
+ return headMap(toKey, false);\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if {@code fromKey} is null\r
+ * and this map uses natural ordering, or its comparator\r
+ * does not permit null keys\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+ */\r
+ public SortedMap<K,V> tailMap(K fromKey) {\r
+ return tailMap(fromKey, true);\r
+ }\r
+\r
+ // View class support\r
+\r
+ class Values extends AbstractCollection<V> {\r
+ public Iterator<V> iterator() {\r
+ return new ValueIterator(getFirstEntry());\r
+ }\r
+\r
+ public int size() {\r
+ return TreeMap7.this.size();\r
+ }\r
+\r
+ public boolean contains(Object o) {\r
+ return TreeMap7.this.containsValue(o);\r
+ }\r
+\r
+ public boolean remove(Object o) {\r
+ for (Entry<K,V> e = getFirstEntry(); e != null; e = successor(e)) {\r
+ if (valEquals(e.getValue(), o)) {\r
+ deleteEntry(e);\r
+ return true;\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+\r
+ public void clear() {\r
+ TreeMap7.this.clear();\r
+ }\r
+ }\r
+\r
+ class EntrySet extends AbstractSet<Map.Entry<K,V>> {\r
+ public Iterator<Map.Entry<K,V>> iterator() {\r
+ return new EntryIterator(getFirstEntry());\r
+ }\r
+\r
+ public boolean contains(Object o) {\r
+ if (!(o instanceof Map.Entry))\r
+ return false;\r
+ Map.Entry<K,V> entry = (Map.Entry<K,V>) o;\r
+ V value = entry.getValue();\r
+ Entry<K,V> p = getEntry(entry.getKey());\r
+ return p != null && valEquals(p.getValue(), value);\r
+ }\r
+\r
+ public boolean remove(Object o) {\r
+ if (!(o instanceof Map.Entry))\r
+ return false;\r
+ Map.Entry<K,V> entry = (Map.Entry<K,V>) o;\r
+ V value = entry.getValue();\r
+ Entry<K,V> p = getEntry(entry.getKey());\r
+ if (p != null && valEquals(p.getValue(), value)) {\r
+ deleteEntry(p);\r
+ return true;\r
+ }\r
+ return false;\r
+ }\r
+\r
+ public int size() {\r
+ return TreeMap7.this.size();\r
+ }\r
+\r
+ public void clear() {\r
+ TreeMap7.this.clear();\r
+ }\r
+ }\r
+\r
+ /*\r
+ * Unlike Values and EntrySet, the KeySet class is static,\r
+ * delegating to a NavigableMap7 to allow use by SubMaps, which\r
+ * outweighs the ugliness of needing type-tests for the following\r
+ * Iterator methods that are defined appropriately in main versus\r
+ * submap classes.\r
+ */\r
+\r
+ Iterator<K> keyIterator() {\r
+ return new KeyIterator(getFirstEntry());\r
+ }\r
+\r
+ Iterator<K> descendingKeyIterator() {\r
+ return new DescendingKeyIterator(getLastEntry());\r
+ }\r
+\r
+ static final class KeySet<E> extends AbstractSet<E> implements NavigableSet7<E> {\r
+ private final NavigableMap7<E, Object> m;\r
+ KeySet(NavigableMap7<E,Object> map) { m = map; }\r
+\r
+ public Iterator<E> iterator() {\r
+ if (m instanceof TreeMap7)\r
+ return ((TreeMap7<E,Object>)m).keyIterator();\r
+ else\r
+ return (Iterator<E>)(((TreeMap7.NavigableSubMap)m).keyIterator());\r
+ }\r
+\r
+ public Iterator<E> descendingIterator() {\r
+ if (m instanceof TreeMap7)\r
+ return ((TreeMap7<E,Object>)m).descendingKeyIterator();\r
+ else\r
+ return (Iterator<E>)(((TreeMap7.NavigableSubMap)m).descendingKeyIterator());\r
+ }\r
+\r
+ public int size() { return m.size(); }\r
+ public boolean isEmpty() { return m.isEmpty(); }\r
+ public boolean contains(Object o) { return m.containsKey(o); }\r
+ public void clear() { m.clear(); }\r
+ public E lower(E e) { return m.lowerKey(e); }\r
+ public E floor(E e) { return m.floorKey(e); }\r
+ public E ceiling(E e) { return m.ceilingKey(e); }\r
+ public E higher(E e) { return m.higherKey(e); }\r
+ public E first() { return m.firstKey(); }\r
+ public E last() { return m.lastKey(); }\r
+ public Comparator<? super E> comparator() { return m.comparator(); }\r
+ public E pollFirst() {\r
+ Map.Entry<E,Object> e = m.pollFirstEntry();\r
+ return (e == null) ? null : e.getKey();\r
+ }\r
+ public E pollLast() {\r
+ Map.Entry<E,Object> e = m.pollLastEntry();\r
+ return (e == null) ? null : e.getKey();\r
+ }\r
+ public boolean remove(Object o) {\r
+ int oldSize = size();\r
+ m.remove(o);\r
+ return size() != oldSize;\r
+ }\r
+ public NavigableSet7<E> subSet(E fromElement, boolean fromInclusive,\r
+ E toElement, boolean toInclusive) {\r
+ return new KeySet<E>(m.subMap(fromElement, fromInclusive,\r
+ toElement, toInclusive));\r
+ }\r
+ public NavigableSet7<E> headSet(E toElement, boolean inclusive) {\r
+ return new KeySet<E>(m.headMap(toElement, inclusive));\r
+ }\r
+ public NavigableSet7<E> tailSet(E fromElement, boolean inclusive) {\r
+ return new KeySet<E>(m.tailMap(fromElement, inclusive));\r
+ }\r
+ public SortedSet<E> subSet(E fromElement, E toElement) {\r
+ return subSet(fromElement, true, toElement, false);\r
+ }\r
+ public SortedSet<E> headSet(E toElement) {\r
+ return headSet(toElement, false);\r
+ }\r
+ public SortedSet<E> tailSet(E fromElement) {\r
+ return tailSet(fromElement, true);\r
+ }\r
+ public NavigableSet7<E> descendingSet() {\r
+ return new KeySet(m.descendingMap());\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Base class for TreeMap7 Iterators\r
+ */\r
+ abstract class PrivateEntryIterator<T> implements Iterator<T> {\r
+ Entry<K,V> next;\r
+ Entry<K,V> lastReturned;\r
+ int expectedModCount;\r
+\r
+ PrivateEntryIterator(Entry<K,V> first) {\r
+ expectedModCount = modCount;\r
+ lastReturned = null;\r
+ next = first;\r
+ }\r
+\r
+ public final boolean hasNext() {\r
+ return next != null;\r
+ }\r
+\r
+ final Entry<K,V> nextEntry() {\r
+ Entry<K,V> e = next;\r
+ if (e == null)\r
+ throw new NoSuchElementException();\r
+ if (modCount != expectedModCount)\r
+ throw new ConcurrentModificationException();\r
+ next = successor(e);\r
+ lastReturned = e;\r
+ return e;\r
+ }\r
+\r
+ final Entry<K,V> prevEntry() {\r
+ Entry<K,V> e = next;\r
+ if (e == null)\r
+ throw new NoSuchElementException();\r
+ if (modCount != expectedModCount)\r
+ throw new ConcurrentModificationException();\r
+ next = predecessor(e);\r
+ lastReturned = e;\r
+ return e;\r
+ }\r
+\r
+ public void remove() {\r
+ if (lastReturned == null)\r
+ throw new IllegalStateException();\r
+ if (modCount != expectedModCount)\r
+ throw new ConcurrentModificationException();\r
+ // deleted entries are replaced by their successors\r
+ if (lastReturned.left != null && lastReturned.right != null)\r
+ next = lastReturned;\r
+ deleteEntry(lastReturned);\r
+ expectedModCount = modCount;\r
+ lastReturned = null;\r
+ }\r
+ }\r
+\r
+ final class EntryIterator extends PrivateEntryIterator<Map.Entry<K,V>> {\r
+ EntryIterator(Entry<K,V> first) {\r
+ super(first);\r
+ }\r
+ public Map.Entry<K,V> next() {\r
+ return nextEntry();\r
+ }\r
+ }\r
+\r
+ final class ValueIterator extends PrivateEntryIterator<V> {\r
+ ValueIterator(Entry<K,V> first) {\r
+ super(first);\r
+ }\r
+ public V next() {\r
+ return nextEntry().value;\r
+ }\r
+ }\r
+\r
+ final class KeyIterator extends PrivateEntryIterator<K> {\r
+ KeyIterator(Entry<K,V> first) {\r
+ super(first);\r
+ }\r
+ public K next() {\r
+ return nextEntry().key;\r
+ }\r
+ }\r
+\r
+ final class DescendingKeyIterator extends PrivateEntryIterator<K> {\r
+ DescendingKeyIterator(Entry<K,V> first) {\r
+ super(first);\r
+ }\r
+ public K next() {\r
+ return prevEntry().key;\r
+ }\r
+ }\r
+\r
+ // Little utilities\r
+\r
+ /**\r
+ * Compares two keys using the correct comparison method for this TreeMap7.\r
+ */\r
+ final int compare(Object k1, Object k2) {\r
+ return comparator==null ? ((Comparable<? super K>)k1).compareTo((K)k2)\r
+ : comparator.compare((K)k1, (K)k2);\r
+ }\r
+\r
+ /**\r
+ * Test two values for equality. Differs from o1.equals(o2) only in\r
+ * that it copes with {@code null} o1 properly.\r
+ */\r
+ static final boolean valEquals(Object o1, Object o2) {\r
+ return (o1==null ? o2==null : o1.equals(o2));\r
+ }\r
+\r
+ /**\r
+ * Return SimpleImmutableEntry for entry, or null if null\r
+ */\r
+ static <K,V> Map.Entry<K,V> exportEntry(TreeMap7.Entry<K,V> e) {\r
+ return (e == null) ? null :\r
+ new AbstractMap7.SimpleImmutableEntry<K,V>(e);\r
+ }\r
+\r
+ /**\r
+ * Return key for entry, or null if null\r
+ */\r
+ static <K,V> K keyOrNull(TreeMap7.Entry<K,V> e) {\r
+ return (e == null) ? null : e.key;\r
+ }\r
+\r
+ /**\r
+ * Returns the key corresponding to the specified Entry.\r
+ * @throws NoSuchElementException if the Entry is null\r
+ */\r
+ static <K> K key(Entry<K,?> e) {\r
+ if (e==null)\r
+ throw new NoSuchElementException();\r
+ return e.key;\r
+ }\r
+\r
+\r
+ // SubMaps\r
+\r
+ /**\r
+ * Dummy value serving as unmatchable fence key for unbounded\r
+ * SubMapIterators\r
+ */\r
+ private static final Object UNBOUNDED = new Object();\r
+\r
+ /**\r
+ * @serial include\r
+ */\r
+ abstract static class NavigableSubMap<K,V> extends AbstractMap7<K,V>\r
+ implements NavigableMap7<K,V>, java.io.Serializable {\r
+ /**\r
+ * The backing map.\r
+ */\r
+ final TreeMap7<K,V> m;\r
+\r
+ /**\r
+ * Endpoints are represented as triples (fromStart, lo,\r
+ * loInclusive) and (toEnd, hi, hiInclusive). If fromStart is\r
+ * true, then the low (absolute) bound is the start of the\r
+ * backing map, and the other values are ignored. Otherwise,\r
+ * if loInclusive is true, lo is the inclusive bound, else lo\r
+ * is the exclusive bound. Similarly for the upper bound.\r
+ */\r
+ final K lo, hi;\r
+ final boolean fromStart, toEnd;\r
+ final boolean loInclusive, hiInclusive;\r
+\r
+ NavigableSubMap(TreeMap7<K,V> m,\r
+ boolean fromStart, K lo, boolean loInclusive,\r
+ boolean toEnd, K hi, boolean hiInclusive) {\r
+ if (!fromStart && !toEnd) {\r
+ if (m.compare(lo, hi) > 0)\r
+ throw new IllegalArgumentException("fromKey > toKey");\r
+ } else {\r
+ if (!fromStart) // type check\r
+ m.compare(lo, lo);\r
+ if (!toEnd)\r
+ m.compare(hi, hi);\r
+ }\r
+\r
+ this.m = m;\r
+ this.fromStart = fromStart;\r
+ this.lo = lo;\r
+ this.loInclusive = loInclusive;\r
+ this.toEnd = toEnd;\r
+ this.hi = hi;\r
+ this.hiInclusive = hiInclusive;\r
+ }\r
+\r
+ // internal utilities\r
+\r
+ final boolean tooLow(Object key) {\r
+ if (!fromStart) {\r
+ int c = m.compare(key, lo);\r
+ if (c < 0 || (c == 0 && !loInclusive))\r
+ return true;\r
+ }\r
+ return false;\r
+ }\r
+\r
+ final boolean tooHigh(Object key) {\r
+ if (!toEnd) {\r
+ int c = m.compare(key, hi);\r
+ if (c > 0 || (c == 0 && !hiInclusive))\r
+ return true;\r
+ }\r
+ return false;\r
+ }\r
+\r
+ final boolean inRange(Object key) {\r
+ return !tooLow(key) && !tooHigh(key);\r
+ }\r
+\r
+ final boolean inClosedRange(Object key) {\r
+ return (fromStart || m.compare(key, lo) >= 0)\r
+ && (toEnd || m.compare(hi, key) >= 0);\r
+ }\r
+\r
+ final boolean inRange(Object key, boolean inclusive) {\r
+ return inclusive ? inRange(key) : inClosedRange(key);\r
+ }\r
+\r
+ /*\r
+ * Absolute versions of relation operations.\r
+ * Subclasses map to these using like-named "sub"\r
+ * versions that invert senses for descending maps\r
+ */\r
+\r
+ final TreeMap7.Entry<K,V> absLowest() {\r
+ TreeMap7.Entry<K,V> e =\r
+ (fromStart ? m.getFirstEntry() :\r
+ (loInclusive ? m.getCeilingEntry(lo) :\r
+ m.getHigherEntry(lo)));\r
+ return (e == null || tooHigh(e.key)) ? null : e;\r
+ }\r
+\r
+ final TreeMap7.Entry<K,V> absHighest() {\r
+ TreeMap7.Entry<K,V> e =\r
+ (toEnd ? m.getLastEntry() :\r
+ (hiInclusive ? m.getFloorEntry(hi) :\r
+ m.getLowerEntry(hi)));\r
+ return (e == null || tooLow(e.key)) ? null : e;\r
+ }\r
+\r
+ final TreeMap7.Entry<K,V> absCeiling(K key) {\r
+ if (tooLow(key))\r
+ return absLowest();\r
+ TreeMap7.Entry<K,V> e = m.getCeilingEntry(key);\r
+ return (e == null || tooHigh(e.key)) ? null : e;\r
+ }\r
+\r
+ final TreeMap7.Entry<K,V> absHigher(K key) {\r
+ if (tooLow(key))\r
+ return absLowest();\r
+ TreeMap7.Entry<K,V> e = m.getHigherEntry(key);\r
+ return (e == null || tooHigh(e.key)) ? null : e;\r
+ }\r
+\r
+ final TreeMap7.Entry<K,V> absFloor(K key) {\r
+ if (tooHigh(key))\r
+ return absHighest();\r
+ TreeMap7.Entry<K,V> e = m.getFloorEntry(key);\r
+ return (e == null || tooLow(e.key)) ? null : e;\r
+ }\r
+\r
+ final TreeMap7.Entry<K,V> absLower(K key) {\r
+ if (tooHigh(key))\r
+ return absHighest();\r
+ TreeMap7.Entry<K,V> e = m.getLowerEntry(key);\r
+ return (e == null || tooLow(e.key)) ? null : e;\r
+ }\r
+\r
+ /** Returns the absolute high fence for ascending traversal */\r
+ final TreeMap7.Entry<K,V> absHighFence() {\r
+ return (toEnd ? null : (hiInclusive ?\r
+ m.getHigherEntry(hi) :\r
+ m.getCeilingEntry(hi)));\r
+ }\r
+\r
+ /** Return the absolute low fence for descending traversal */\r
+ final TreeMap7.Entry<K,V> absLowFence() {\r
+ return (fromStart ? null : (loInclusive ?\r
+ m.getLowerEntry(lo) :\r
+ m.getFloorEntry(lo)));\r
+ }\r
+\r
+ // Abstract methods defined in ascending vs descending classes\r
+ // These relay to the appropriate absolute versions\r
+\r
+ abstract TreeMap7.Entry<K,V> subLowest();\r
+ abstract TreeMap7.Entry<K,V> subHighest();\r
+ abstract TreeMap7.Entry<K,V> subCeiling(K key);\r
+ abstract TreeMap7.Entry<K,V> subHigher(K key);\r
+ abstract TreeMap7.Entry<K,V> subFloor(K key);\r
+ abstract TreeMap7.Entry<K,V> subLower(K key);\r
+\r
+ /** Returns ascending iterator from the perspective of this submap */\r
+ abstract Iterator<K> keyIterator();\r
+\r
+ /** Returns descending iterator from the perspective of this submap */\r
+ abstract Iterator<K> descendingKeyIterator();\r
+\r
+ // public methods\r
+\r
+ public boolean isEmpty() {\r
+ return (fromStart && toEnd) ? m.isEmpty() : entrySet().isEmpty();\r
+ }\r
+\r
+ public int size() {\r
+ return (fromStart && toEnd) ? m.size() : entrySet().size();\r
+ }\r
+\r
+ public final boolean containsKey(Object key) {\r
+ return inRange(key) && m.containsKey(key);\r
+ }\r
+\r
+ public final V put(K key, V value) {\r
+ if (!inRange(key))\r
+ throw new IllegalArgumentException("key out of range");\r
+ return m.put(key, value);\r
+ }\r
+\r
+ public final V get(Object key) {\r
+ return !inRange(key) ? null : m.get(key);\r
+ }\r
+\r
+ public final V remove(Object key) {\r
+ return !inRange(key) ? null : m.remove(key);\r
+ }\r
+\r
+ public final Map.Entry<K,V> ceilingEntry(K key) {\r
+ return exportEntry(subCeiling(key));\r
+ }\r
+\r
+ public final K ceilingKey(K key) {\r
+ return keyOrNull(subCeiling(key));\r
+ }\r
+\r
+ public final Map.Entry<K,V> higherEntry(K key) {\r
+ return exportEntry(subHigher(key));\r
+ }\r
+\r
+ public final K higherKey(K key) {\r
+ return keyOrNull(subHigher(key));\r
+ }\r
+\r
+ public final Map.Entry<K,V> floorEntry(K key) {\r
+ return exportEntry(subFloor(key));\r
+ }\r
+\r
+ public final K floorKey(K key) {\r
+ return keyOrNull(subFloor(key));\r
+ }\r
+\r
+ public final Map.Entry<K,V> lowerEntry(K key) {\r
+ return exportEntry(subLower(key));\r
+ }\r
+\r
+ public final K lowerKey(K key) {\r
+ return keyOrNull(subLower(key));\r
+ }\r
+\r
+ public final K firstKey() {\r
+ return key(subLowest());\r
+ }\r
+\r
+ public final K lastKey() {\r
+ return key(subHighest());\r
+ }\r
+\r
+ public final Map.Entry<K,V> firstEntry() {\r
+ return exportEntry(subLowest());\r
+ }\r
+\r
+ public final Map.Entry<K,V> lastEntry() {\r
+ return exportEntry(subHighest());\r
+ }\r
+\r
+ public final Map.Entry<K,V> pollFirstEntry() {\r
+ TreeMap7.Entry<K,V> e = subLowest();\r
+ Map.Entry<K,V> result = exportEntry(e);\r
+ if (e != null)\r
+ m.deleteEntry(e);\r
+ return result;\r
+ }\r
+\r
+ public final Map.Entry<K,V> pollLastEntry() {\r
+ TreeMap7.Entry<K,V> e = subHighest();\r
+ Map.Entry<K,V> result = exportEntry(e);\r
+ if (e != null)\r
+ m.deleteEntry(e);\r
+ return result;\r
+ }\r
+\r
+ // Views\r
+ transient NavigableMap7<K,V> descendingMapView = null;\r
+ transient EntrySetView entrySetView = null;\r
+ transient KeySet<K> navigableKeySetView = null;\r
+\r
+ public final NavigableSet7<K> navigableKeySet() {\r
+ KeySet<K> nksv = navigableKeySetView;\r
+ return (nksv != null) ? nksv :\r
+ (navigableKeySetView = new TreeMap7.KeySet(this));\r
+ }\r
+\r
+ public final Set<K> keySet() {\r
+ return navigableKeySet();\r
+ }\r
+\r
+ public NavigableSet7<K> descendingKeySet() {\r
+ return descendingMap().navigableKeySet();\r
+ }\r
+\r
+ public final SortedMap<K,V> subMap(K fromKey, K toKey) {\r
+ return subMap(fromKey, true, toKey, false);\r
+ }\r
+\r
+ public final SortedMap<K,V> headMap(K toKey) {\r
+ return headMap(toKey, false);\r
+ }\r
+\r
+ public final SortedMap<K,V> tailMap(K fromKey) {\r
+ return tailMap(fromKey, true);\r
+ }\r
+\r
+ // View classes\r
+\r
+ abstract class EntrySetView extends AbstractSet<Map.Entry<K,V>> {\r
+ private transient int size = -1, sizeModCount;\r
+\r
+ public int size() {\r
+ if (fromStart && toEnd)\r
+ return m.size();\r
+ if (size == -1 || sizeModCount != m.modCount) {\r
+ sizeModCount = m.modCount;\r
+ size = 0;\r
+ Iterator i = iterator();\r
+ while (i.hasNext()) {\r
+ size++;\r
+ i.next();\r
+ }\r
+ }\r
+ return size;\r
+ }\r
+\r
+ public boolean isEmpty() {\r
+ TreeMap7.Entry<K,V> n = absLowest();\r
+ return n == null || tooHigh(n.key);\r
+ }\r
+\r
+ public boolean contains(Object o) {\r
+ if (!(o instanceof Map.Entry))\r
+ return false;\r
+ Map.Entry<K,V> entry = (Map.Entry<K,V>) o;\r
+ K key = entry.getKey();\r
+ if (!inRange(key))\r
+ return false;\r
+ TreeMap7.Entry node = m.getEntry(key);\r
+ return node != null &&\r
+ valEquals(node.getValue(), entry.getValue());\r
+ }\r
+\r
+ public boolean remove(Object o) {\r
+ if (!(o instanceof Map.Entry))\r
+ return false;\r
+ Map.Entry<K,V> entry = (Map.Entry<K,V>) o;\r
+ K key = entry.getKey();\r
+ if (!inRange(key))\r
+ return false;\r
+ TreeMap7.Entry<K,V> node = m.getEntry(key);\r
+ if (node!=null && valEquals(node.getValue(),\r
+ entry.getValue())) {\r
+ m.deleteEntry(node);\r
+ return true;\r
+ }\r
+ return false;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Iterators for SubMaps\r
+ */\r
+ abstract class SubMapIterator<T> implements Iterator<T> {\r
+ TreeMap7.Entry<K,V> lastReturned;\r
+ TreeMap7.Entry<K,V> next;\r
+ final Object fenceKey;\r
+ int expectedModCount;\r
+\r
+ SubMapIterator(TreeMap7.Entry<K,V> first,\r
+ TreeMap7.Entry<K,V> fence) {\r
+ expectedModCount = m.modCount;\r
+ lastReturned = null;\r
+ next = first;\r
+ fenceKey = fence == null ? UNBOUNDED : fence.key;\r
+ }\r
+\r
+ public final boolean hasNext() {\r
+ return next != null && next.key != fenceKey;\r
+ }\r
+\r
+ final TreeMap7.Entry<K,V> nextEntry() {\r
+ TreeMap7.Entry<K,V> e = next;\r
+ if (e == null || e.key == fenceKey)\r
+ throw new NoSuchElementException();\r
+ if (m.modCount != expectedModCount)\r
+ throw new ConcurrentModificationException();\r
+ next = successor(e);\r
+ lastReturned = e;\r
+ return e;\r
+ }\r
+\r
+ final TreeMap7.Entry<K,V> prevEntry() {\r
+ TreeMap7.Entry<K,V> e = next;\r
+ if (e == null || e.key == fenceKey)\r
+ throw new NoSuchElementException();\r
+ if (m.modCount != expectedModCount)\r
+ throw new ConcurrentModificationException();\r
+ next = predecessor(e);\r
+ lastReturned = e;\r
+ return e;\r
+ }\r
+\r
+ final void removeAscending() {\r
+ if (lastReturned == null)\r
+ throw new IllegalStateException();\r
+ if (m.modCount != expectedModCount)\r
+ throw new ConcurrentModificationException();\r
+ // deleted entries are replaced by their successors\r
+ if (lastReturned.left != null && lastReturned.right != null)\r
+ next = lastReturned;\r
+ m.deleteEntry(lastReturned);\r
+ lastReturned = null;\r
+ expectedModCount = m.modCount;\r
+ }\r
+\r
+ final void removeDescending() {\r
+ if (lastReturned == null)\r
+ throw new IllegalStateException();\r
+ if (m.modCount != expectedModCount)\r
+ throw new ConcurrentModificationException();\r
+ m.deleteEntry(lastReturned);\r
+ lastReturned = null;\r
+ expectedModCount = m.modCount;\r
+ }\r
+\r
+ }\r
+\r
+ final class SubMapEntryIterator extends SubMapIterator<Map.Entry<K,V>> {\r
+ SubMapEntryIterator(TreeMap7.Entry<K,V> first,\r
+ TreeMap7.Entry<K,V> fence) {\r
+ super(first, fence);\r
+ }\r
+ public Map.Entry<K,V> next() {\r
+ return nextEntry();\r
+ }\r
+ public void remove() {\r
+ removeAscending();\r
+ }\r
+ }\r
+\r
+ final class SubMapKeyIterator extends SubMapIterator<K> {\r
+ SubMapKeyIterator(TreeMap7.Entry<K,V> first,\r
+ TreeMap7.Entry<K,V> fence) {\r
+ super(first, fence);\r
+ }\r
+ public K next() {\r
+ return nextEntry().key;\r
+ }\r
+ public void remove() {\r
+ removeAscending();\r
+ }\r
+ }\r
+\r
+ final class DescendingSubMapEntryIterator extends SubMapIterator<Map.Entry<K,V>> {\r
+ DescendingSubMapEntryIterator(TreeMap7.Entry<K,V> last,\r
+ TreeMap7.Entry<K,V> fence) {\r
+ super(last, fence);\r
+ }\r
+\r
+ public Map.Entry<K,V> next() {\r
+ return prevEntry();\r
+ }\r
+ public void remove() {\r
+ removeDescending();\r
+ }\r
+ }\r
+\r
+ final class DescendingSubMapKeyIterator extends SubMapIterator<K> {\r
+ DescendingSubMapKeyIterator(TreeMap7.Entry<K,V> last,\r
+ TreeMap7.Entry<K,V> fence) {\r
+ super(last, fence);\r
+ }\r
+ public K next() {\r
+ return prevEntry().key;\r
+ }\r
+ public void remove() {\r
+ removeDescending();\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * @serial include\r
+ */\r
+ static final class AscendingSubMap<K,V> extends NavigableSubMap<K,V> {\r
+ private static final long serialVersionUID = 912986545866124060L;\r
+\r
+ AscendingSubMap(TreeMap7<K,V> m,\r
+ boolean fromStart, K lo, boolean loInclusive,\r
+ boolean toEnd, K hi, boolean hiInclusive) {\r
+ super(m, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);\r
+ }\r
+\r
+ public Comparator<? super K> comparator() {\r
+ return m.comparator();\r
+ }\r
+\r
+ public NavigableMap7<K,V> subMap(K fromKey, boolean fromInclusive,\r
+ K toKey, boolean toInclusive) {\r
+ if (!inRange(fromKey, fromInclusive))\r
+ throw new IllegalArgumentException("fromKey out of range");\r
+ if (!inRange(toKey, toInclusive))\r
+ throw new IllegalArgumentException("toKey out of range");\r
+ return new AscendingSubMap(m,\r
+ false, fromKey, fromInclusive,\r
+ false, toKey, toInclusive);\r
+ }\r
+\r
+ public NavigableMap7<K,V> headMap(K toKey, boolean inclusive) {\r
+ if (!inRange(toKey, inclusive))\r
+ throw new IllegalArgumentException("toKey out of range");\r
+ return new AscendingSubMap(m,\r
+ fromStart, lo, loInclusive,\r
+ false, toKey, inclusive);\r
+ }\r
+\r
+ public NavigableMap7<K,V> tailMap(K fromKey, boolean inclusive) {\r
+ if (!inRange(fromKey, inclusive))\r
+ throw new IllegalArgumentException("fromKey out of range");\r
+ return new AscendingSubMap(m,\r
+ false, fromKey, inclusive,\r
+ toEnd, hi, hiInclusive);\r
+ }\r
+\r
+ public NavigableMap7<K,V> descendingMap() {\r
+ NavigableMap7<K,V> mv = descendingMapView;\r
+ return (mv != null) ? mv :\r
+ (descendingMapView =\r
+ new DescendingSubMap(m,\r
+ fromStart, lo, loInclusive,\r
+ toEnd, hi, hiInclusive));\r
+ }\r
+\r
+ Iterator<K> keyIterator() {\r
+ return new SubMapKeyIterator(absLowest(), absHighFence());\r
+ }\r
+\r
+ Iterator<K> descendingKeyIterator() {\r
+ return new DescendingSubMapKeyIterator(absHighest(), absLowFence());\r
+ }\r
+\r
+ final class AscendingEntrySetView extends EntrySetView {\r
+ public Iterator<Map.Entry<K,V>> iterator() {\r
+ return new SubMapEntryIterator(absLowest(), absHighFence());\r
+ }\r
+ }\r
+\r
+ public Set<Map.Entry<K,V>> entrySet() {\r
+ EntrySetView es = entrySetView;\r
+ return (es != null) ? es : new AscendingEntrySetView();\r
+ }\r
+\r
+ TreeMap7.Entry<K,V> subLowest() { return absLowest(); }\r
+ TreeMap7.Entry<K,V> subHighest() { return absHighest(); }\r
+ TreeMap7.Entry<K,V> subCeiling(K key) { return absCeiling(key); }\r
+ TreeMap7.Entry<K,V> subHigher(K key) { return absHigher(key); }\r
+ TreeMap7.Entry<K,V> subFloor(K key) { return absFloor(key); }\r
+ TreeMap7.Entry<K,V> subLower(K key) { return absLower(key); }\r
+ }\r
+\r
+ /**\r
+ * @serial include\r
+ */\r
+ static final class DescendingSubMap<K,V> extends NavigableSubMap<K,V> {\r
+ private static final long serialVersionUID = 912986545866120460L;\r
+ DescendingSubMap(TreeMap7<K,V> m,\r
+ boolean fromStart, K lo, boolean loInclusive,\r
+ boolean toEnd, K hi, boolean hiInclusive) {\r
+ super(m, fromStart, lo, loInclusive, toEnd, hi, hiInclusive);\r
+ }\r
+\r
+ private final Comparator<? super K> reverseComparator =\r
+ Collections.reverseOrder(m.comparator);\r
+\r
+ public Comparator<? super K> comparator() {\r
+ return reverseComparator;\r
+ }\r
+\r
+ public NavigableMap7<K,V> subMap(K fromKey, boolean fromInclusive,\r
+ K toKey, boolean toInclusive) {\r
+ if (!inRange(fromKey, fromInclusive))\r
+ throw new IllegalArgumentException("fromKey out of range");\r
+ if (!inRange(toKey, toInclusive))\r
+ throw new IllegalArgumentException("toKey out of range");\r
+ return new DescendingSubMap(m,\r
+ false, toKey, toInclusive,\r
+ false, fromKey, fromInclusive);\r
+ }\r
+\r
+ public NavigableMap7<K,V> headMap(K toKey, boolean inclusive) {\r
+ if (!inRange(toKey, inclusive))\r
+ throw new IllegalArgumentException("toKey out of range");\r
+ return new DescendingSubMap(m,\r
+ false, toKey, inclusive,\r
+ toEnd, hi, hiInclusive);\r
+ }\r
+\r
+ public NavigableMap7<K,V> tailMap(K fromKey, boolean inclusive) {\r
+ if (!inRange(fromKey, inclusive))\r
+ throw new IllegalArgumentException("fromKey out of range");\r
+ return new DescendingSubMap(m,\r
+ fromStart, lo, loInclusive,\r
+ false, fromKey, inclusive);\r
+ }\r
+\r
+ public NavigableMap7<K,V> descendingMap() {\r
+ NavigableMap7<K,V> mv = descendingMapView;\r
+ return (mv != null) ? mv :\r
+ (descendingMapView =\r
+ new AscendingSubMap(m,\r
+ fromStart, lo, loInclusive,\r
+ toEnd, hi, hiInclusive));\r
+ }\r
+\r
+ Iterator<K> keyIterator() {\r
+ return new DescendingSubMapKeyIterator(absHighest(), absLowFence());\r
+ }\r
+\r
+ Iterator<K> descendingKeyIterator() {\r
+ return new SubMapKeyIterator(absLowest(), absHighFence());\r
+ }\r
+\r
+ final class DescendingEntrySetView extends EntrySetView {\r
+ public Iterator<Map.Entry<K,V>> iterator() {\r
+ return new DescendingSubMapEntryIterator(absHighest(), absLowFence());\r
+ }\r
+ }\r
+\r
+ public Set<Map.Entry<K,V>> entrySet() {\r
+ EntrySetView es = entrySetView;\r
+ return (es != null) ? es : new DescendingEntrySetView();\r
+ }\r
+\r
+ TreeMap7.Entry<K,V> subLowest() { return absHighest(); }\r
+ TreeMap7.Entry<K,V> subHighest() { return absLowest(); }\r
+ TreeMap7.Entry<K,V> subCeiling(K key) { return absFloor(key); }\r
+ TreeMap7.Entry<K,V> subHigher(K key) { return absLower(key); }\r
+ TreeMap7.Entry<K,V> subFloor(K key) { return absCeiling(key); }\r
+ TreeMap7.Entry<K,V> subLower(K key) { return absHigher(key); }\r
+ }\r
+\r
+ /**\r
+ * This class exists solely for the sake of serialization\r
+ * compatibility with previous releases of TreeMap7 that did not\r
+ * support NavigableMap7. It translates an old-version SubMap into\r
+ * a new-version AscendingSubMap. This class is never otherwise\r
+ * used.\r
+ *\r
+ * @serial include\r
+ */\r
+ private class SubMap extends AbstractMap7<K,V>\r
+ implements SortedMap<K,V>, java.io.Serializable {\r
+ private static final long serialVersionUID = -6520786458950516097L;\r
+ private boolean fromStart = false, toEnd = false;\r
+ private K fromKey, toKey;\r
+ private Object readResolve() {\r
+ return new AscendingSubMap(TreeMap7.this,\r
+ fromStart, fromKey, true,\r
+ toEnd, toKey, false);\r
+ }\r
+ public Set<Map.Entry<K,V>> entrySet() { throw new InternalError(); }\r
+ public K lastKey() { throw new InternalError(); }\r
+ public K firstKey() { throw new InternalError(); }\r
+ public SortedMap<K,V> subMap(K fromKey, K toKey) { throw new InternalError(); }\r
+ public SortedMap<K,V> headMap(K toKey) { throw new InternalError(); }\r
+ public SortedMap<K,V> tailMap(K fromKey) { throw new InternalError(); }\r
+ public Comparator<? super K> comparator() { throw new InternalError(); }\r
+ }\r
+\r
+\r
+ // Red-black mechanics\r
+\r
+ private static final boolean RED = false;\r
+ private static final boolean BLACK = true;\r
+\r
+ /**\r
+ * Node in the Tree. Doubles as a means to pass key-value pairs back to\r
+ * user (see Map.Entry).\r
+ */\r
+\r
+ static final class Entry<K,V> implements Map.Entry<K,V> {\r
+ K key;\r
+ V value;\r
+ Entry<K,V> left = null;\r
+ Entry<K,V> right = null;\r
+ Entry<K,V> parent;\r
+ boolean color = BLACK;\r
+\r
+ /**\r
+ * Make a new cell with given key, value, and parent, and with\r
+ * {@code null} child links, and BLACK color.\r
+ */\r
+ Entry(K key, V value, Entry<K,V> parent) {\r
+ this.key = key;\r
+ this.value = value;\r
+ this.parent = parent;\r
+ }\r
+\r
+ /**\r
+ * Returns the key.\r
+ *\r
+ * @return the key\r
+ */\r
+ public K getKey() {\r
+ return key;\r
+ }\r
+\r
+ /**\r
+ * Returns the value associated with the key.\r
+ *\r
+ * @return the value associated with the key\r
+ */\r
+ public V getValue() {\r
+ return value;\r
+ }\r
+\r
+ /**\r
+ * Replaces the value currently associated with the key with the given\r
+ * value.\r
+ *\r
+ * @return the value associated with the key before this method was\r
+ * called\r
+ */\r
+ public V setValue(V value) {\r
+ V oldValue = this.value;\r
+ this.value = value;\r
+ return oldValue;\r
+ }\r
+\r
+ public boolean equals(Object o) {\r
+ if (!(o instanceof Map.Entry))\r
+ return false;\r
+ Map.Entry<?,?> e = (Map.Entry<?,?>)o;\r
+\r
+ return valEquals(key,e.getKey()) && valEquals(value,e.getValue());\r
+ }\r
+\r
+ public int hashCode() {\r
+ int keyHash = (key==null ? 0 : key.hashCode());\r
+ int valueHash = (value==null ? 0 : value.hashCode());\r
+ return keyHash ^ valueHash;\r
+ }\r
+\r
+ public String toString() {\r
+ return key + "=" + value;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Returns the first Entry in the TreeMap7 (according to the TreeMap7's\r
+ * key-sort function). Returns null if the TreeMap7 is empty.\r
+ */\r
+ final Entry<K,V> getFirstEntry() {\r
+ Entry<K,V> p = root;\r
+ if (p != null)\r
+ while (p.left != null)\r
+ p = p.left;\r
+ return p;\r
+ }\r
+\r
+ /**\r
+ * Returns the last Entry in the TreeMap7 (according to the TreeMap7's\r
+ * key-sort function). Returns null if the TreeMap7 is empty.\r
+ */\r
+ final Entry<K,V> getLastEntry() {\r
+ Entry<K,V> p = root;\r
+ if (p != null)\r
+ while (p.right != null)\r
+ p = p.right;\r
+ return p;\r
+ }\r
+\r
+ /**\r
+ * Returns the successor of the specified Entry, or null if no such.\r
+ */\r
+ static <K,V> TreeMap7.Entry<K,V> successor(Entry<K,V> t) {\r
+ if (t == null)\r
+ return null;\r
+ else if (t.right != null) {\r
+ Entry<K,V> p = t.right;\r
+ while (p.left != null)\r
+ p = p.left;\r
+ return p;\r
+ } else {\r
+ Entry<K,V> p = t.parent;\r
+ Entry<K,V> ch = t;\r
+ while (p != null && ch == p.right) {\r
+ ch = p;\r
+ p = p.parent;\r
+ }\r
+ return p;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Returns the predecessor of the specified Entry, or null if no such.\r
+ */\r
+ static <K,V> Entry<K,V> predecessor(Entry<K,V> t) {\r
+ if (t == null)\r
+ return null;\r
+ else if (t.left != null) {\r
+ Entry<K,V> p = t.left;\r
+ while (p.right != null)\r
+ p = p.right;\r
+ return p;\r
+ } else {\r
+ Entry<K,V> p = t.parent;\r
+ Entry<K,V> ch = t;\r
+ while (p != null && ch == p.left) {\r
+ ch = p;\r
+ p = p.parent;\r
+ }\r
+ return p;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Balancing operations.\r
+ *\r
+ * Implementations of rebalancings during insertion and deletion are\r
+ * slightly different than the CLR version. Rather than using dummy\r
+ * nilnodes, we use a set of accessors that deal properly with null. They\r
+ * are used to avoid messiness surrounding nullness checks in the main\r
+ * algorithms.\r
+ */\r
+\r
+ private static <K,V> boolean colorOf(Entry<K,V> p) {\r
+ return (p == null ? BLACK : p.color);\r
+ }\r
+\r
+ private static <K,V> Entry<K,V> parentOf(Entry<K,V> p) {\r
+ return (p == null ? null: p.parent);\r
+ }\r
+\r
+ private static <K,V> void setColor(Entry<K,V> p, boolean c) {\r
+ if (p != null)\r
+ p.color = c;\r
+ }\r
+\r
+ private static <K,V> Entry<K,V> leftOf(Entry<K,V> p) {\r
+ return (p == null) ? null: p.left;\r
+ }\r
+\r
+ private static <K,V> Entry<K,V> rightOf(Entry<K,V> p) {\r
+ return (p == null) ? null: p.right;\r
+ }\r
+\r
+ /** From CLR */\r
+ private void rotateLeft(Entry<K,V> p) {\r
+ if (p != null) {\r
+ Entry<K,V> r = p.right;\r
+ p.right = r.left;\r
+ if (r.left != null)\r
+ r.left.parent = p;\r
+ r.parent = p.parent;\r
+ if (p.parent == null)\r
+ root = r;\r
+ else if (p.parent.left == p)\r
+ p.parent.left = r;\r
+ else\r
+ p.parent.right = r;\r
+ r.left = p;\r
+ p.parent = r;\r
+ }\r
+ }\r
+\r
+ /** From CLR */\r
+ private void rotateRight(Entry<K,V> p) {\r
+ if (p != null) {\r
+ Entry<K,V> l = p.left;\r
+ p.left = l.right;\r
+ if (l.right != null) l.right.parent = p;\r
+ l.parent = p.parent;\r
+ if (p.parent == null)\r
+ root = l;\r
+ else if (p.parent.right == p)\r
+ p.parent.right = l;\r
+ else p.parent.left = l;\r
+ l.right = p;\r
+ p.parent = l;\r
+ }\r
+ }\r
+\r
+ /** From CLR */\r
+ private void fixAfterInsertion(Entry<K,V> x) {\r
+ x.color = RED;\r
+\r
+ while (x != null && x != root && x.parent.color == RED) {\r
+ if (parentOf(x) == leftOf(parentOf(parentOf(x)))) {\r
+ Entry<K,V> y = rightOf(parentOf(parentOf(x)));\r
+ if (colorOf(y) == RED) {\r
+ setColor(parentOf(x), BLACK);\r
+ setColor(y, BLACK);\r
+ setColor(parentOf(parentOf(x)), RED);\r
+ x = parentOf(parentOf(x));\r
+ } else {\r
+ if (x == rightOf(parentOf(x))) {\r
+ x = parentOf(x);\r
+ rotateLeft(x);\r
+ }\r
+ setColor(parentOf(x), BLACK);\r
+ setColor(parentOf(parentOf(x)), RED);\r
+ rotateRight(parentOf(parentOf(x)));\r
+ }\r
+ } else {\r
+ Entry<K,V> y = leftOf(parentOf(parentOf(x)));\r
+ if (colorOf(y) == RED) {\r
+ setColor(parentOf(x), BLACK);\r
+ setColor(y, BLACK);\r
+ setColor(parentOf(parentOf(x)), RED);\r
+ x = parentOf(parentOf(x));\r
+ } else {\r
+ if (x == leftOf(parentOf(x))) {\r
+ x = parentOf(x);\r
+ rotateRight(x);\r
+ }\r
+ setColor(parentOf(x), BLACK);\r
+ setColor(parentOf(parentOf(x)), RED);\r
+ rotateLeft(parentOf(parentOf(x)));\r
+ }\r
+ }\r
+ }\r
+ root.color = BLACK;\r
+ }\r
+\r
+ /**\r
+ * Delete node p, and then rebalance the tree.\r
+ */\r
+ private void deleteEntry(Entry<K,V> p) {\r
+ modCount++;\r
+ size--;\r
+\r
+ // If strictly internal, copy successor's element to p and then make p\r
+ // point to successor.\r
+ if (p.left != null && p.right != null) {\r
+ Entry<K,V> s = successor(p);\r
+ p.key = s.key;\r
+ p.value = s.value;\r
+ p = s;\r
+ } // p has 2 children\r
+\r
+ // Start fixup at replacement node, if it exists.\r
+ Entry<K,V> replacement = (p.left != null ? p.left : p.right);\r
+\r
+ if (replacement != null) {\r
+ // Link replacement to parent\r
+ replacement.parent = p.parent;\r
+ if (p.parent == null)\r
+ root = replacement;\r
+ else if (p == p.parent.left)\r
+ p.parent.left = replacement;\r
+ else\r
+ p.parent.right = replacement;\r
+\r
+ // Null out links so they are OK to use by fixAfterDeletion.\r
+ p.left = p.right = p.parent = null;\r
+\r
+ // Fix replacement\r
+ if (p.color == BLACK)\r
+ fixAfterDeletion(replacement);\r
+ } else if (p.parent == null) { // return if we are the only node.\r
+ root = null;\r
+ } else { // No children. Use self as phantom replacement and unlink.\r
+ if (p.color == BLACK)\r
+ fixAfterDeletion(p);\r
+\r
+ if (p.parent != null) {\r
+ if (p == p.parent.left)\r
+ p.parent.left = null;\r
+ else if (p == p.parent.right)\r
+ p.parent.right = null;\r
+ p.parent = null;\r
+ }\r
+ }\r
+ }\r
+\r
+ /** From CLR */\r
+ private void fixAfterDeletion(Entry<K,V> x) {\r
+ while (x != root && colorOf(x) == BLACK) {\r
+ if (x == leftOf(parentOf(x))) {\r
+ Entry<K,V> sib = rightOf(parentOf(x));\r
+\r
+ if (colorOf(sib) == RED) {\r
+ setColor(sib, BLACK);\r
+ setColor(parentOf(x), RED);\r
+ rotateLeft(parentOf(x));\r
+ sib = rightOf(parentOf(x));\r
+ }\r
+\r
+ if (colorOf(leftOf(sib)) == BLACK &&\r
+ colorOf(rightOf(sib)) == BLACK) {\r
+ setColor(sib, RED);\r
+ x = parentOf(x);\r
+ } else {\r
+ if (colorOf(rightOf(sib)) == BLACK) {\r
+ setColor(leftOf(sib), BLACK);\r
+ setColor(sib, RED);\r
+ rotateRight(sib);\r
+ sib = rightOf(parentOf(x));\r
+ }\r
+ setColor(sib, colorOf(parentOf(x)));\r
+ setColor(parentOf(x), BLACK);\r
+ setColor(rightOf(sib), BLACK);\r
+ rotateLeft(parentOf(x));\r
+ x = root;\r
+ }\r
+ } else { // symmetric\r
+ Entry<K,V> sib = leftOf(parentOf(x));\r
+\r
+ if (colorOf(sib) == RED) {\r
+ setColor(sib, BLACK);\r
+ setColor(parentOf(x), RED);\r
+ rotateRight(parentOf(x));\r
+ sib = leftOf(parentOf(x));\r
+ }\r
+\r
+ if (colorOf(rightOf(sib)) == BLACK &&\r
+ colorOf(leftOf(sib)) == BLACK) {\r
+ setColor(sib, RED);\r
+ x = parentOf(x);\r
+ } else {\r
+ if (colorOf(leftOf(sib)) == BLACK) {\r
+ setColor(rightOf(sib), BLACK);\r
+ setColor(sib, RED);\r
+ rotateLeft(sib);\r
+ sib = leftOf(parentOf(x));\r
+ }\r
+ setColor(sib, colorOf(parentOf(x)));\r
+ setColor(parentOf(x), BLACK);\r
+ setColor(leftOf(sib), BLACK);\r
+ rotateRight(parentOf(x));\r
+ x = root;\r
+ }\r
+ }\r
+ }\r
+\r
+ setColor(x, BLACK);\r
+ }\r
+\r
+ private static final long serialVersionUID = 919286545866124006L;\r
+\r
+ /**\r
+ * Save the state of the {@code TreeMap7} instance to a stream (i.e.,\r
+ * serialize it).\r
+ *\r
+ * @serialData The <em>size</em> of the TreeMap7 (the number of key-value\r
+ * mappings) is emitted (int), followed by the key (Object)\r
+ * and value (Object) for each key-value mapping represented\r
+ * by the TreeMap7. The key-value mappings are emitted in\r
+ * key-order (as determined by the TreeMap7's Comparator,\r
+ * or by the keys' natural ordering if the TreeMap7 has no\r
+ * Comparator).\r
+ */\r
+ private void writeObject(java.io.ObjectOutputStream s)\r
+ throws java.io.IOException {\r
+ // Write out the Comparator and any hidden stuff\r
+ s.defaultWriteObject();\r
+\r
+ // Write out size (number of Mappings)\r
+ s.writeInt(size);\r
+\r
+ // Write out keys and values (alternating)\r
+ for (Iterator<Map.Entry<K,V>> i = entrySet().iterator(); i.hasNext(); ) {\r
+ Map.Entry<K,V> e = i.next();\r
+ s.writeObject(e.getKey());\r
+ s.writeObject(e.getValue());\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Reconstitute the {@code TreeMap7} instance from a stream (i.e.,\r
+ * deserialize it).\r
+ */\r
+ private void readObject(final java.io.ObjectInputStream s)\r
+ throws java.io.IOException, ClassNotFoundException {\r
+ // Read in the Comparator and any hidden stuff\r
+ s.defaultReadObject();\r
+\r
+ // Read in size\r
+ int size = s.readInt();\r
+\r
+ buildFromSorted(size, null, s, null);\r
+ }\r
+\r
+ /** Intended to be called only from TreeSet.readObject */\r
+ void readTreeSet(int size, java.io.ObjectInputStream s, V defaultVal)\r
+ throws java.io.IOException, ClassNotFoundException {\r
+ buildFromSorted(size, null, s, defaultVal);\r
+ }\r
+\r
+ /** Intended to be called only from TreeSet.addAll */\r
+ void addAllForTreeSet(SortedSet<? extends K> set, V defaultVal) {\r
+ try {\r
+ buildFromSorted(set.size(), set.iterator(), null, defaultVal);\r
+ } catch (java.io.IOException cannotHappen) {\r
+ } catch (ClassNotFoundException cannotHappen) {\r
+ }\r
+ }\r
+\r
+\r
+ /**\r
+ * Linear time tree building algorithm from sorted data. Can accept keys\r
+ * and/or values from iterator or stream. This leads to too many\r
+ * parameters, but seems better than alternatives. The four formats\r
+ * that this method accepts are:\r
+ *\r
+ * 1) An iterator of Map.Entries. (it != null, defaultVal == null).\r
+ * 2) An iterator of keys. (it != null, defaultVal != null).\r
+ * 3) A stream of alternating serialized keys and values.\r
+ * (it == null, defaultVal == null).\r
+ * 4) A stream of serialized keys. (it == null, defaultVal != null).\r
+ *\r
+ * It is assumed that the comparator of the TreeMap7 is already set prior\r
+ * to calling this method.\r
+ *\r
+ * @param size the number of keys (or key-value pairs) to be read from\r
+ * the iterator or stream\r
+ * @param it If non-null, new entries are created from entries\r
+ * or keys read from this iterator.\r
+ * @param str If non-null, new entries are created from keys and\r
+ * possibly values read from this stream in serialized form.\r
+ * Exactly one of it and str should be non-null.\r
+ * @param defaultVal if non-null, this default value is used for\r
+ * each value in the map. If null, each value is read from\r
+ * iterator or stream, as described above.\r
+ * @throws IOException propagated from stream reads. This cannot\r
+ * occur if str is null.\r
+ * @throws ClassNotFoundException propagated from readObject.\r
+ * This cannot occur if str is null.\r
+ */\r
+ private void buildFromSorted(int size, Iterator it,\r
+ java.io.ObjectInputStream str,\r
+ V defaultVal)\r
+ throws java.io.IOException, ClassNotFoundException {\r
+ this.size = size;\r
+ root = buildFromSorted(0, 0, size-1, computeRedLevel(size),\r
+ it, str, defaultVal);\r
+ }\r
+\r
+ /**\r
+ * Recursive "helper method" that does the real work of the\r
+ * previous method. Identically named parameters have\r
+ * identical definitions. Additional parameters are documented below.\r
+ * It is assumed that the comparator and size fields of the TreeMap7 are\r
+ * already set prior to calling this method. (It ignores both fields.)\r
+ *\r
+ * @param level the current level of tree. Initial call should be 0.\r
+ * @param lo the first element index of this subtree. Initial should be 0.\r
+ * @param hi the last element index of this subtree. Initial should be\r
+ * size-1.\r
+ * @param redLevel the level at which nodes should be red.\r
+ * Must be equal to computeRedLevel for tree of this size.\r
+ */\r
+ private final Entry<K,V> buildFromSorted(int level, int lo, int hi,\r
+ int redLevel,\r
+ Iterator it,\r
+ java.io.ObjectInputStream str,\r
+ V defaultVal)\r
+ throws java.io.IOException, ClassNotFoundException {\r
+ /*\r
+ * Strategy: The root is the middlemost element. To get to it, we\r
+ * have to first recursively construct the entire left subtree,\r
+ * so as to grab all of its elements. We can then proceed with right\r
+ * subtree.\r
+ *\r
+ * The lo and hi arguments are the minimum and maximum\r
+ * indices to pull out of the iterator or stream for current subtree.\r
+ * They are not actually indexed, we just proceed sequentially,\r
+ * ensuring that items are extracted in corresponding order.\r
+ */\r
+\r
+ if (hi < lo) return null;\r
+\r
+ int mid = (lo + hi) >>> 1;\r
+\r
+ Entry<K,V> left = null;\r
+ if (lo < mid)\r
+ left = buildFromSorted(level+1, lo, mid - 1, redLevel,\r
+ it, str, defaultVal);\r
+\r
+ // extract key and/or value from iterator or stream\r
+ K key;\r
+ V value;\r
+ if (it != null) {\r
+ if (defaultVal==null) {\r
+ Map.Entry<K,V> entry = (Map.Entry<K,V>)it.next();\r
+ key = entry.getKey();\r
+ value = entry.getValue();\r
+ } else {\r
+ key = (K)it.next();\r
+ value = defaultVal;\r
+ }\r
+ } else { // use stream\r
+ key = (K) str.readObject();\r
+ value = (defaultVal != null ? defaultVal : (V) str.readObject());\r
+ }\r
+\r
+ Entry<K,V> middle = new Entry<K,V>(key, value, null);\r
+\r
+ // color nodes in non-full bottommost level red\r
+ if (level == redLevel)\r
+ middle.color = RED;\r
+\r
+ if (left != null) {\r
+ middle.left = left;\r
+ left.parent = middle;\r
+ }\r
+\r
+ if (mid < hi) {\r
+ Entry<K,V> right = buildFromSorted(level+1, mid+1, hi, redLevel,\r
+ it, str, defaultVal);\r
+ middle.right = right;\r
+ right.parent = middle;\r
+ }\r
+\r
+ return middle;\r
+ }\r
+\r
+ /**\r
+ * Find the level down to which to assign all nodes BLACK. This is the\r
+ * last `full' level of the complete binary tree produced by\r
+ * buildTree. The remaining nodes are colored RED. (This makes a `nice'\r
+ * set of color assignments wrt future insertions.) This level number is\r
+ * computed by finding the number of splits needed to reach the zeroeth\r
+ * node. (The answer is ~lg(N), but in any case must be computed by same\r
+ * quick O(lg(N)) loop.)\r
+ */\r
+ private static int computeRedLevel(int sz) {\r
+ int level = 0;\r
+ for (int m = sz - 1; m >= 0; m = m / 2 - 1)\r
+ level++;\r
+ return level;\r
+ }\r
+}
\ No newline at end of file
--- /dev/null
+/*\r
+ * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.\r
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.\r
+ *\r
+ * This code is free software; you can redistribute it and/or modify it\r
+ * under the terms of the GNU General Public License version 2 only, as\r
+ * published by the Free Software Foundation. Oracle designates this\r
+ * particular file as subject to the "Classpath" exception as provided\r
+ * by Oracle in the LICENSE file that accompanied this code.\r
+ *\r
+ * This code is distributed in the hope that it will be useful, but WITHOUT\r
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License\r
+ * version 2 for more details (a copy is included in the LICENSE file that\r
+ * accompanied this code).\r
+ *\r
+ * You should have received a copy of the GNU General Public License version\r
+ * 2 along with this work; if not, write to the Free Software Foundation,\r
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.\r
+ *\r
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA\r
+ * or visit www.oracle.com if you need additional information or have any\r
+ * questions.\r
+ */\r
+\r
+package org.apache.poi.util.java7_util;\r
+\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.Comparator;\r
+import java.util.ConcurrentModificationException;\r
+import java.util.HashSet;\r
+import java.util.Iterator;\r
+import java.util.Map;\r
+import java.util.NoSuchElementException;\r
+import java.util.Set;\r
+import java.util.SortedSet;\r
+import java.util.TreeMap;\r
+\r
+/**\r
+ * A {@link NavigableSet7} implementation based on a {@link TreeMap}.\r
+ * The elements are ordered using their {@linkplain Comparable natural\r
+ * ordering}, or by a {@link Comparator} provided at set creation\r
+ * time, depending on which constructor is used.\r
+ *\r
+ * <p>This implementation provides guaranteed log(n) time cost for the basic\r
+ * operations ({@code add}, {@code remove} and {@code contains}).\r
+ *\r
+ * <p>Note that the ordering maintained by a set (whether or not an explicit\r
+ * comparator is provided) must be <i>consistent with equals</i> if it is to\r
+ * correctly implement the {@code Set} interface. (See {@code Comparable}\r
+ * or {@code Comparator} for a precise definition of <i>consistent with\r
+ * equals</i>.) This is so because the {@code Set} interface is defined in\r
+ * terms of the {@code equals} operation, but a {@code TreeSet7} instance\r
+ * performs all element comparisons using its {@code compareTo} (or\r
+ * {@code compare}) method, so two elements that are deemed equal by this method\r
+ * are, from the standpoint of the set, equal. The behavior of a set\r
+ * <i>is</i> well-defined even if its ordering is inconsistent with equals; it\r
+ * just fails to obey the general contract of the {@code Set} interface.\r
+ *\r
+ * <p><strong>Note that this implementation is not synchronized.</strong>\r
+ * If multiple threads access a tree set concurrently, and at least one\r
+ * of the threads modifies the set, it <i>must</i> be synchronized\r
+ * externally. This is typically accomplished by synchronizing on some\r
+ * object that naturally encapsulates the set.\r
+ * If no such object exists, the set should be "wrapped" using the\r
+ * {@link Collections#synchronizedSortedSet Collections.synchronizedSortedSet}\r
+ * method. This is best done at creation time, to prevent accidental\r
+ * unsynchronized access to the set: <pre>\r
+ * SortedSet s = Collections.synchronizedSortedSet(new TreeSet7(...));</pre>\r
+ *\r
+ * <p>The iterators returned by this class's {@code iterator} method are\r
+ * <i>fail-fast</i>: if the set is modified at any time after the iterator is\r
+ * created, in any way except through the iterator's own {@code remove}\r
+ * method, the iterator will throw a {@link ConcurrentModificationException}.\r
+ * Thus, in the face of concurrent modification, the iterator fails quickly\r
+ * and cleanly, rather than risking arbitrary, non-deterministic behavior at\r
+ * an undetermined time in the future.\r
+ *\r
+ * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed\r
+ * as it is, generally speaking, impossible to make any hard guarantees in the\r
+ * presence of unsynchronized concurrent modification. Fail-fast iterators\r
+ * throw {@code ConcurrentModificationException} on a best-effort basis.\r
+ * Therefore, it would be wrong to write a program that depended on this\r
+ * exception for its correctness: <i>the fail-fast behavior of iterators\r
+ * should be used only to detect bugs.</i>\r
+ *\r
+ * <p>This class is a member of the\r
+ * <a href="{@docRoot}/../technotes/guides/collections/index.html">\r
+ * Java Collections Framework</a>.\r
+ *\r
+ * @param <E> the type of elements maintained by this set\r
+ *\r
+ * @author Josh Bloch\r
+ * @see Collection\r
+ * @see Set\r
+ * @see HashSet\r
+ * @see Comparable\r
+ * @see Comparator\r
+ * @see TreeMap\r
+ * @since 1.2\r
+ */\r
+\r
+public class TreeSet7<E> extends AbstractSet7<E>\r
+ implements NavigableSet7<E>, Cloneable, java.io.Serializable\r
+{\r
+ /**\r
+ * The backing map.\r
+ */\r
+ private transient NavigableMap7<E,Object> m;\r
+\r
+ // Dummy value to associate with an Object in the backing Map\r
+ private static final Object PRESENT = new Object();\r
+\r
+ /**\r
+ * Constructs a set backed by the specified navigable map.\r
+ */\r
+ TreeSet7(NavigableMap7<E,Object> m) {\r
+ this.m = m;\r
+ }\r
+\r
+ /**\r
+ * Constructs a new, empty tree set, sorted according to the\r
+ * natural ordering of its elements. All elements inserted into\r
+ * the set must implement the {@link Comparable} interface.\r
+ * Furthermore, all such elements must be <i>mutually\r
+ * comparable</i>: {@code e1.compareTo(e2)} must not throw a\r
+ * {@code ClassCastException} for any elements {@code e1} and\r
+ * {@code e2} in the set. If the user attempts to add an element\r
+ * to the set that violates this constraint (for example, the user\r
+ * attempts to add a string element to a set whose elements are\r
+ * integers), the {@code add} call will throw a\r
+ * {@code ClassCastException}.\r
+ */\r
+ public TreeSet7() {\r
+ this(new TreeMap7<E,Object>());\r
+ }\r
+\r
+ /**\r
+ * Constructs a new, empty tree set, sorted according to the specified\r
+ * comparator. All elements inserted into the set must be <i>mutually\r
+ * comparable</i> by the specified comparator: {@code comparator.compare(e1,\r
+ * e2)} must not throw a {@code ClassCastException} for any elements\r
+ * {@code e1} and {@code e2} in the set. If the user attempts to add\r
+ * an element to the set that violates this constraint, the\r
+ * {@code add} call will throw a {@code ClassCastException}.\r
+ *\r
+ * @param comparator the comparator that will be used to order this set.\r
+ * If {@code null}, the {@linkplain Comparable natural\r
+ * ordering} of the elements will be used.\r
+ */\r
+ public TreeSet7(Comparator<? super E> comparator) {\r
+ this(new TreeMap7<E,Object>(comparator));\r
+ }\r
+\r
+ /**\r
+ * Constructs a new tree set containing the elements in the specified\r
+ * collection, sorted according to the <i>natural ordering</i> of its\r
+ * elements. All elements inserted into the set must implement the\r
+ * {@link Comparable} interface. Furthermore, all such elements must be\r
+ * <i>mutually comparable</i>: {@code e1.compareTo(e2)} must not throw a\r
+ * {@code ClassCastException} for any elements {@code e1} and\r
+ * {@code e2} in the set.\r
+ *\r
+ * @param c collection whose elements will comprise the new set\r
+ * @throws ClassCastException if the elements in {@code c} are\r
+ * not {@link Comparable}, or are not mutually comparable\r
+ * @throws NullPointerException if the specified collection is null\r
+ */\r
+ public TreeSet7(Collection<? extends E> c) {\r
+ this();\r
+ addAll(c);\r
+ }\r
+\r
+ /**\r
+ * Constructs a new tree set containing the same elements and\r
+ * using the same ordering as the specified sorted set.\r
+ *\r
+ * @param s sorted set whose elements will comprise the new set\r
+ * @throws NullPointerException if the specified sorted set is null\r
+ */\r
+ public TreeSet7(SortedSet<E> s) {\r
+ this(s.comparator());\r
+ addAll(s);\r
+ }\r
+\r
+ /**\r
+ * Returns an iterator over the elements in this set in ascending order.\r
+ *\r
+ * @return an iterator over the elements in this set in ascending order\r
+ */\r
+ public Iterator<E> iterator() {\r
+ return m.navigableKeySet().iterator();\r
+ }\r
+\r
+ /**\r
+ * Returns an iterator over the elements in this set in descending order.\r
+ *\r
+ * @return an iterator over the elements in this set in descending order\r
+ * @since 1.6\r
+ */\r
+ public Iterator<E> descendingIterator() {\r
+ return m.descendingKeySet().iterator();\r
+ }\r
+\r
+ /**\r
+ * @since 1.6\r
+ */\r
+ public NavigableSet7<E> descendingSet() {\r
+ return new TreeSet7<E>(m.descendingMap());\r
+ }\r
+\r
+ /**\r
+ * Returns the number of elements in this set (its cardinality).\r
+ *\r
+ * @return the number of elements in this set (its cardinality)\r
+ */\r
+ public int size() {\r
+ return m.size();\r
+ }\r
+\r
+ /**\r
+ * Returns {@code true} if this set contains no elements.\r
+ *\r
+ * @return {@code true} if this set contains no elements\r
+ */\r
+ public boolean isEmpty() {\r
+ return m.isEmpty();\r
+ }\r
+\r
+ /**\r
+ * Returns {@code true} if this set contains the specified element.\r
+ * More formally, returns {@code true} if and only if this set\r
+ * contains an element {@code e} such that\r
+ * <tt>(o==null ? e==null : o.equals(e))</tt>.\r
+ *\r
+ * @param o object to be checked for containment in this set\r
+ * @return {@code true} if this set contains the specified element\r
+ * @throws ClassCastException if the specified object cannot be compared\r
+ * with the elements currently in the set\r
+ * @throws NullPointerException if the specified element is null\r
+ * and this set uses natural ordering, or its comparator\r
+ * does not permit null elements\r
+ */\r
+ public boolean contains(Object o) {\r
+ return m.containsKey(o);\r
+ }\r
+\r
+ /**\r
+ * Adds the specified element to this set if it is not already present.\r
+ * More formally, adds the specified element {@code e} to this set if\r
+ * the set contains no element {@code e2} such that\r
+ * <tt>(e==null ? e2==null : e.equals(e2))</tt>.\r
+ * If this set already contains the element, the call leaves the set\r
+ * unchanged and returns {@code false}.\r
+ *\r
+ * @param e element to be added to this set\r
+ * @return {@code true} if this set did not already contain the specified\r
+ * element\r
+ * @throws ClassCastException if the specified object cannot be compared\r
+ * with the elements currently in this set\r
+ * @throws NullPointerException if the specified element is null\r
+ * and this set uses natural ordering, or its comparator\r
+ * does not permit null elements\r
+ */\r
+ public boolean add(E e) {\r
+ return m.put(e, PRESENT)==null;\r
+ }\r
+\r
+ /**\r
+ * Removes the specified element from this set if it is present.\r
+ * More formally, removes an element {@code e} such that\r
+ * <tt>(o==null ? e==null : o.equals(e))</tt>,\r
+ * if this set contains such an element. Returns {@code true} if\r
+ * this set contained the element (or equivalently, if this set\r
+ * changed as a result of the call). (This set will not contain the\r
+ * element once the call returns.)\r
+ *\r
+ * @param o object to be removed from this set, if present\r
+ * @return {@code true} if this set contained the specified element\r
+ * @throws ClassCastException if the specified object cannot be compared\r
+ * with the elements currently in this set\r
+ * @throws NullPointerException if the specified element is null\r
+ * and this set uses natural ordering, or its comparator\r
+ * does not permit null elements\r
+ */\r
+ public boolean remove(Object o) {\r
+ return m.remove(o)==PRESENT;\r
+ }\r
+\r
+ /**\r
+ * Removes all of the elements from this set.\r
+ * The set will be empty after this call returns.\r
+ */\r
+ public void clear() {\r
+ m.clear();\r
+ }\r
+\r
+ /**\r
+ * Adds all of the elements in the specified collection to this set.\r
+ *\r
+ * @param c collection containing elements to be added to this set\r
+ * @return {@code true} if this set changed as a result of the call\r
+ * @throws ClassCastException if the elements provided cannot be compared\r
+ * with the elements currently in the set\r
+ * @throws NullPointerException if the specified collection is null or\r
+ * if any element is null and this set uses natural ordering, or\r
+ * its comparator does not permit null elements\r
+ */\r
+ public boolean addAll(Collection<? extends E> c) {\r
+ // Use linear-time version if applicable\r
+ if (m.size()==0 && c.size() > 0 &&\r
+ c instanceof SortedSet &&\r
+ m instanceof TreeMap7) {\r
+ SortedSet<? extends E> set = (SortedSet<? extends E>) c;\r
+ TreeMap7<E,Object> map = (TreeMap7<E, Object>) m;\r
+ @SuppressWarnings("unchecked")\r
+ Comparator<? super E> cc = (Comparator<? super E>) set.comparator();\r
+ Comparator<? super E> mc = map.comparator();\r
+ if (cc==mc || (cc != null && cc.equals(mc))) {\r
+ map.addAllForTreeSet(set, PRESENT);\r
+ return true;\r
+ }\r
+ }\r
+ return super.addAll(c);\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if {@code fromElement} or {@code toElement}\r
+ * is null and this set uses natural ordering, or its comparator\r
+ * does not permit null elements\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+ * @since 1.6\r
+ */\r
+ public NavigableSet7<E> subSet(E fromElement, boolean fromInclusive,\r
+ E toElement, boolean toInclusive) {\r
+ return new TreeSet7<E>(m.subMap(fromElement, fromInclusive,\r
+ toElement, toInclusive));\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if {@code toElement} is null and\r
+ * this set uses natural ordering, or its comparator does\r
+ * not permit null elements\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+ * @since 1.6\r
+ */\r
+ public NavigableSet7<E> headSet(E toElement, boolean inclusive) {\r
+ return new TreeSet7<E>(m.headMap(toElement, inclusive));\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if {@code fromElement} is null and\r
+ * this set uses natural ordering, or its comparator does\r
+ * not permit null elements\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+ * @since 1.6\r
+ */\r
+ public NavigableSet7<E> tailSet(E fromElement, boolean inclusive) {\r
+ return new TreeSet7<E>(m.tailMap(fromElement, inclusive));\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if {@code fromElement} or\r
+ * {@code toElement} is null and this set uses natural ordering,\r
+ * or its comparator does not permit null elements\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+ */\r
+ public SortedSet<E> subSet(E fromElement, E toElement) {\r
+ return subSet(fromElement, true, toElement, false);\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if {@code toElement} is null\r
+ * and this set uses natural ordering, or its comparator does\r
+ * not permit null elements\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+ */\r
+ public SortedSet<E> headSet(E toElement) {\r
+ return headSet(toElement, false);\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if {@code fromElement} is null\r
+ * and this set uses natural ordering, or its comparator does\r
+ * not permit null elements\r
+ * @throws IllegalArgumentException {@inheritDoc}\r
+ */\r
+ public SortedSet<E> tailSet(E fromElement) {\r
+ return tailSet(fromElement, true);\r
+ }\r
+\r
+ public Comparator<? super E> comparator() {\r
+ return m.comparator();\r
+ }\r
+\r
+ /**\r
+ * @throws NoSuchElementException {@inheritDoc}\r
+ */\r
+ public E first() {\r
+ return m.firstKey();\r
+ }\r
+\r
+ /**\r
+ * @throws NoSuchElementException {@inheritDoc}\r
+ */\r
+ public E last() {\r
+ return m.lastKey();\r
+ }\r
+\r
+ // NavigableSet API methods\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if the specified element is null\r
+ * and this set uses natural ordering, or its comparator\r
+ * does not permit null elements\r
+ * @since 1.6\r
+ */\r
+ public E lower(E e) {\r
+ return m.lowerKey(e);\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if the specified element is null\r
+ * and this set uses natural ordering, or its comparator\r
+ * does not permit null elements\r
+ * @since 1.6\r
+ */\r
+ public E floor(E e) {\r
+ return m.floorKey(e);\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if the specified element is null\r
+ * and this set uses natural ordering, or its comparator\r
+ * does not permit null elements\r
+ * @since 1.6\r
+ */\r
+ public E ceiling(E e) {\r
+ return m.ceilingKey(e);\r
+ }\r
+\r
+ /**\r
+ * @throws ClassCastException {@inheritDoc}\r
+ * @throws NullPointerException if the specified element is null\r
+ * and this set uses natural ordering, or its comparator\r
+ * does not permit null elements\r
+ * @since 1.6\r
+ */\r
+ public E higher(E e) {\r
+ return m.higherKey(e);\r
+ }\r
+\r
+ /**\r
+ * @since 1.6\r
+ */\r
+ public E pollFirst() {\r
+ Map.Entry<E,?> e = m.pollFirstEntry();\r
+ return (e == null) ? null : e.getKey();\r
+ }\r
+\r
+ /**\r
+ * @since 1.6\r
+ */\r
+ public E pollLast() {\r
+ Map.Entry<E,?> e = m.pollLastEntry();\r
+ return (e == null) ? null : e.getKey();\r
+ }\r
+\r
+ /**\r
+ * Returns a shallow copy of this {@code TreeSet7} instance. (The elements\r
+ * themselves are not cloned.)\r
+ *\r
+ * @return a shallow copy of this set\r
+ */\r
+ @SuppressWarnings("unchecked")\r
+ public Object clone() {\r
+ TreeSet7<E> clone = null;\r
+ try {\r
+ clone = (TreeSet7<E>) super.clone();\r
+ } catch (CloneNotSupportedException e) {\r
+ throw new InternalError();\r
+ }\r
+\r
+ clone.m = new TreeMap7<E,Object>(m);\r
+ return clone;\r
+ }\r
+\r
+ /**\r
+ * Save the state of the {@code TreeSet7} instance to a stream (that is,\r
+ * serialize it).\r
+ *\r
+ * @serialData Emits the comparator used to order this set, or\r
+ * {@code null} if it obeys its elements' natural ordering\r
+ * (Object), followed by the size of the set (the number of\r
+ * elements it contains) (int), followed by all of its\r
+ * elements (each an Object) in order (as determined by the\r
+ * set's Comparator, or by the elements' natural ordering if\r
+ * the set has no Comparator).\r
+ */\r
+ private void writeObject(java.io.ObjectOutputStream s)\r
+ throws java.io.IOException {\r
+ // Write out any hidden stuff\r
+ s.defaultWriteObject();\r
+\r
+ // Write out Comparator\r
+ s.writeObject(m.comparator());\r
+\r
+ // Write out size\r
+ s.writeInt(m.size());\r
+\r
+ // Write out all elements in the proper order.\r
+ for (E e : m.keySet())\r
+ s.writeObject(e);\r
+ }\r
+\r
+ /**\r
+ * Reconstitute the {@code TreeSet7} instance from a stream (that is,\r
+ * deserialize it).\r
+ */\r
+ private void readObject(java.io.ObjectInputStream s)\r
+ throws java.io.IOException, ClassNotFoundException {\r
+ // Read in any hidden stuff\r
+ s.defaultReadObject();\r
+\r
+ // Read in Comparator\r
+ @SuppressWarnings("unchecked")\r
+ Comparator<? super E> c = (Comparator<? super E>) s.readObject();\r
+\r
+ // Create backing TreeMap\r
+ TreeMap7<E,Object> tm;\r
+ if (c==null)\r
+ tm = new TreeMap7<E,Object>();\r
+ else\r
+ tm = new TreeMap7<E,Object>(c);\r
+ m = tm;\r
+\r
+ // Read in size\r
+ int size = s.readInt();\r
+\r
+ tm.readTreeSet(size, s, PRESENT);\r
+ }\r
+\r
+ private static final long serialVersionUID = -2479143000061671589L;\r
+}
\ No newline at end of file
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
-import java.util.TreeSet;
import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.util.java7_util.TreeSet7;
import org.apache.poi.xssf.util.CTColComparator;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
*/
private void sweepCleanColumns(CTCols cols, CTCol[] flattenedColsArray, CTCol overrideColumn) {
List<CTCol> flattenedCols = new ArrayList<CTCol>(Arrays.asList(flattenedColsArray));
- TreeSet<CTCol> currentElements = new TreeSet<CTCol>(new CTColByMaxComparator());
+ TreeSet7<CTCol> currentElements = new TreeSet7<CTCol>(new CTColByMaxComparator());
ListIterator<CTCol> flIter = flattenedCols.listIterator();
CTCol haveOverrideColumn = null;
long lastMaxIndex = 0;