ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ConcurrentSkipListMap.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/ConcurrentSkipListMap.java (file contents):
Revision 1.42 by jsr166, Sat Sep 10 20:11:53 2005 UTC vs.
Revision 1.43 by dl, Sun Nov 20 15:38:08 2005 UTC

# Line 2599 | Line 2599 | public class ConcurrentSkipListMap<K,V>
2599  
2600      }
2601  
2602 +    /**
2603 +     * Custom Entry class used by Entry iterators that relays
2604 +     * setValue changes to the underlying map. For explanation,
2605 +     * see similar construction in ConcurrentHashMap.
2606 +     */
2607 +    final class WriteThroughEntry extends AbstractMap.SimpleEntry<K,V> {
2608 +        WriteThroughEntry(K k, Object v) {
2609 +            super(k, (V)v);
2610 +        }
2611 +        public V setValue(V value) {
2612 +            if (value == null) throw new NullPointerException();
2613 +            V v = super.setValue(value);
2614 +            ConcurrentSkipListMap.this.put(getKey(), value);
2615 +            return v;
2616 +        }
2617 +    }
2618 +
2619      final class ValueIterator extends Iter implements Iterator<V> {
2620          ValueIterator() {
2621              initAscending();
# Line 2674 | Line 2691 | public class ConcurrentSkipListMap<K,V>
2691          }
2692      }
2693  
2694 <    /**
2678 <     * Entry iterators use the same trick as in ConcurrentHashMap and
2679 <     * elsewhere of using the iterator itself to represent entries,
2680 <     * thus avoiding having to create entry objects in next().
2681 <     */
2682 <    abstract class EntryIter extends Iter implements Map.Entry<K,V> {
2683 <        /** Cache of last value returned */
2684 <        Object lastValue;
2685 <
2686 <        EntryIter() {
2687 <        }
2688 <
2689 <        public K getKey() {
2690 <            Node<K,V> l = last;
2691 <            if (l == null)
2692 <                throw new IllegalStateException();
2693 <            return l.key;
2694 <        }
2695 <
2696 <        public V getValue() {
2697 <            Object v = lastValue;
2698 <            if (last == null || v == null)
2699 <                throw new IllegalStateException();
2700 <            return (V)v;
2701 <        }
2702 <
2703 <        public V setValue(V value) {
2704 <            throw new UnsupportedOperationException();
2705 <        }
2706 <
2707 <        public boolean equals(Object o) {
2708 <            // If not acting as entry, just use default.
2709 <            if (last == null)
2710 <                return super.equals(o);
2711 <            if (!(o instanceof Map.Entry))
2712 <                return false;
2713 <            Map.Entry e = (Map.Entry)o;
2714 <            return (getKey().equals(e.getKey()) &&
2715 <                    getValue().equals(e.getValue()));
2716 <        }
2717 <
2718 <        public int hashCode() {
2719 <            // If not acting as entry, just use default.
2720 <            if (last == null)
2721 <                return super.hashCode();
2722 <            return getKey().hashCode() ^ getValue().hashCode();
2723 <        }
2724 <
2725 <        public String toString() {
2726 <            // If not acting as entry, just use default.
2727 <            if (last == null)
2728 <                return super.toString();
2729 <            return getKey() + "=" + getValue();
2730 <        }
2731 <    }
2732 <
2733 <    final class EntryIterator extends EntryIter
2734 <        implements Iterator<Map.Entry<K,V>> {
2694 >    final class EntryIterator extends Iter implements Iterator<Map.Entry<K,V>> {
2695          EntryIterator() {
2696              initAscending();
2697          }
2698          public Map.Entry<K,V> next() {
2699 <            lastValue = nextValue;
2699 >            Node<K,V> n = next;
2700              ascend();
2701 <            return this;
2701 >            return new WriteThroughEntry(n.key, n.value);
2702          }
2703      }
2704  
2705 <    final class SubMapEntryIterator extends EntryIter
2746 <        implements Iterator<Map.Entry<K,V>> {
2705 >    final class SubMapEntryIterator extends Iter implements Iterator<Map.Entry<K,V>> {
2706          final K fence;
2707          SubMapEntryIterator(K least, K fence) {
2708              initAscending(least, fence);
# Line 2751 | Line 2710 | public class ConcurrentSkipListMap<K,V>
2710          }
2711  
2712          public Map.Entry<K,V> next() {
2713 <            lastValue = nextValue;
2713 >            Node<K,V> n = next;
2714              ascend(fence);
2715 <            return this;
2715 >            return new WriteThroughEntry(n.key, n.value);
2716          }
2717      }
2718  
2719 <    final class DescendingEntryIterator extends EntryIter
2761 <        implements Iterator<Map.Entry<K,V>>  {
2719 >    final class DescendingEntryIterator extends Iter implements Iterator<Map.Entry<K,V>>  {
2720          DescendingEntryIterator() {
2721              initDescending();
2722          }
2723          public Map.Entry<K,V> next() {
2724 <            lastValue = nextValue;
2724 >            Node<K,V> n = next;
2725              descend();
2726 <            return this;
2726 >            return new WriteThroughEntry(n.key, n.value);
2727          }
2728      }
2729  
2730 <    final class DescendingSubMapEntryIterator extends EntryIter
2773 <        implements Iterator<Map.Entry<K,V>>  {
2730 >    final class DescendingSubMapEntryIterator extends Iter implements Iterator<Map.Entry<K,V>>  {
2731          final K least;
2732          DescendingSubMapEntryIterator(K least, K fence) {
2733              initDescending(least, fence);
# Line 2778 | Line 2735 | public class ConcurrentSkipListMap<K,V>
2735          }
2736  
2737          public Map.Entry<K,V> next() {
2738 <            lastValue = nextValue;
2738 >            Node<K,V> n = next;
2739              descend(least);
2740 <            return this;
2740 >            return new WriteThroughEntry(n.key, n.value);
2741          }
2742      }
2743  
# Line 2836 | Line 2793 | public class ConcurrentSkipListMap<K,V>
2793          public void clear() {
2794              ConcurrentSkipListMap.this.clear();
2795          }
2839        public Object[] toArray() {
2840            Collection<K> c = new ArrayList<K>();
2841            for (Iterator<K> i = iterator(); i.hasNext(); )
2842                c.add(i.next());
2843            return c.toArray();
2844        }
2845        public <T> T[] toArray(T[] a) {
2846            Collection<K> c = new ArrayList<K>();
2847            for (Iterator<K> i = iterator(); i.hasNext(); )
2848                c.add(i.next());
2849            return c.toArray(a);
2850        }
2796      }
2797  
2798      class DescendingKeySet extends KeySet {
# Line 2872 | Line 2817 | public class ConcurrentSkipListMap<K,V>
2817          public void clear() {
2818              ConcurrentSkipListMap.this.clear();
2819          }
2875        public Object[] toArray() {
2876            Collection<V> c = new ArrayList<V>();
2877            for (Iterator<V> i = iterator(); i.hasNext(); )
2878                c.add(i.next());
2879            return c.toArray();
2880        }
2881        public <T> T[] toArray(T[] a) {
2882            Collection<V> c = new ArrayList<V>();
2883            for (Iterator<V> i = iterator(); i.hasNext(); )
2884                c.add(i.next());
2885            return c.toArray(a);
2886        }
2820      }
2821  
2822      class EntrySet extends AbstractSet<Map.Entry<K,V>> {
# Line 2913 | Line 2846 | public class ConcurrentSkipListMap<K,V>
2846          public void clear() {
2847              ConcurrentSkipListMap.this.clear();
2848          }
2916        public Object[] toArray() {
2917            Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>();
2918            for (Map.Entry<K,V> e : this)
2919                c.add(new AbstractMap.SimpleEntry<K,V>(e.getKey(),
2920                                                       e.getValue()));
2921            return c.toArray();
2922        }
2923        public <T> T[] toArray(T[] a) {
2924            Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>();
2925            for (Map.Entry<K,V> e : this)
2926                c.add(new AbstractMap.SimpleEntry<K,V>(e.getKey(),
2927                                                       e.getValue()));
2928            return c.toArray(a);
2929        }
2849      }
2850  
2851      class DescendingEntrySet extends EntrySet {
# Line 3261 | Line 3180 | public class ConcurrentSkipListMap<K,V>
3180              public boolean contains(Object k) {
3181                  return ConcurrentSkipListSubMap.this.containsKey(k);
3182              }
3264            public Object[] toArray() {
3265                Collection<K> c = new ArrayList<K>();
3266                for (Iterator<K> i = iterator(); i.hasNext(); )
3267                    c.add(i.next());
3268                return c.toArray();
3269            }
3270            public <T> T[] toArray(T[] a) {
3271                Collection<K> c = new ArrayList<K>();
3272                for (Iterator<K> i = iterator(); i.hasNext(); )
3273                    c.add(i.next());
3274                return c.toArray(a);
3275            }
3183          }
3184  
3185          public Set<K> descendingKeySet() {
# Line 3304 | Line 3211 | public class ConcurrentSkipListMap<K,V>
3211              public boolean contains(Object v) {
3212                  return ConcurrentSkipListSubMap.this.containsValue(v);
3213              }
3307            public Object[] toArray() {
3308                Collection<V> c = new ArrayList<V>();
3309                for (Iterator<V> i = iterator(); i.hasNext(); )
3310                    c.add(i.next());
3311                return c.toArray();
3312            }
3313            public <T> T[] toArray(T[] a) {
3314                Collection<V> c = new ArrayList<V>();
3315                for (Iterator<V> i = iterator(); i.hasNext(); )
3316                    c.add(i.next());
3317                return c.toArray(a);
3318            }
3214          }
3215  
3216          public Set<Map.Entry<K,V>> entrySet() {
# Line 3352 | Line 3247 | public class ConcurrentSkipListMap<K,V>
3247                      return false;
3248                  return m.remove(key, e.getValue());
3249              }
3355            public Object[] toArray() {
3356                Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>();
3357                for (Map.Entry<K,V> e : this)
3358                    c.add(new AbstractMap.SimpleEntry<K,V>(e.getKey(),
3359                                                           e.getValue()));
3360                return c.toArray();
3361            }
3362            public <T> T[] toArray(T[] a) {
3363                Collection<Map.Entry<K,V>> c = new ArrayList<Map.Entry<K,V>>();
3364                for (Map.Entry<K,V> e : this)
3365                    c.add(new AbstractMap.SimpleEntry<K,V>(e.getKey(),
3366                                                           e.getValue()));
3367                return c.toArray(a);
3368            }
3250          }
3251  
3252          public Set<Map.Entry<K,V>> descendingEntrySet() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines