KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > util > MapCopy


1 package org.jboss.cache.util;
2
3 import java.io.IOException JavaDoc;
4 import java.io.Serializable JavaDoc;
5 import java.util.AbstractMap JavaDoc;
6 import java.util.AbstractSet JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.Set JavaDoc;
10
11 /**
12  * Contains a fixed array of read-only map entries, from a copy of an existing map.
13  * This class is more lightweight for places where the copied map will just be iterated over.
14  * <p/>
15  * This map is strictly read-only, and map modification methods (as well as modifications over iterators) will throw
16  * {@link UnsupportedOperationException}s.
17  */

18 public class MapCopy<K, V> extends AbstractMap JavaDoc implements Serializable JavaDoc
19 {
20
21    private static final long serialVersionUID = -958813082188242956L;
22
23    private Map.Entry JavaDoc<K, V> data[];
24
25    private transient Set JavaDoc<Map.Entry JavaDoc<K, V>> entrySet;
26
27    /**
28     * Copies the supplied map to an internal array.
29     */

30    public MapCopy(Map JavaDoc<K, V> m)
31    {
32       data = new Map.Entry JavaDoc[m.size()];
33       int i = 0;
34       for (Map.Entry JavaDoc<K, V> me : m.entrySet())
35       {
36          if (me == null)
37             throw new NullPointerException JavaDoc();
38          data[i++] = new SimpleEntry<K, V>(me);
39       }
40       init();
41    }
42
43    private void init()
44    {
45       this.entrySet = new AbstractSet JavaDoc<Map.Entry JavaDoc<K, V>>()
46       {
47          public int size()
48          {
49             return data.length;
50          }
51
52          public Iterator JavaDoc<Map.Entry JavaDoc<K, V>> iterator()
53          {
54             return new EntryIterator();
55          }
56       };
57    }
58
59    private class EntryIterator implements Iterator JavaDoc<Entry<K, V>>
60    {
61       private int index;
62
63       public boolean hasNext()
64       {
65          return index < data.length;
66       }
67
68       public Entry<K, V> next()
69       {
70          return data[index++];
71       }
72
73       public void remove()
74       {
75          throw new UnsupportedOperationException JavaDoc();
76       }
77    }
78
79    /**
80     * Where is Java 1.6?
81     */

82    private static class SimpleEntry<K, V> implements Map.Entry JavaDoc<K, V>, Serializable JavaDoc
83    {
84       private static final long serialVersionUID = -6092752114794052323L;
85
86       private K key;
87
88       private V value;
89
90       public SimpleEntry(Entry<K, V> me)
91       {
92          key = me.getKey();
93          value = me.getValue();
94       }
95
96       public K getKey()
97       {
98          return key;
99       }
100
101       public V getValue()
102       {
103          return value;
104       }
105
106       public V setValue(V arg0)
107       {
108          throw new UnsupportedOperationException JavaDoc();
109       }
110
111       @Override JavaDoc
112       public boolean equals(Object JavaDoc o)
113       {
114          Map.Entry JavaDoc e2 = (Map.Entry JavaDoc) o;
115          return (getKey() == null ? e2.getKey() == null : getKey().equals(e2.getKey()))
116                  && (getValue() == null ? e2.getValue() == null : getValue().equals(e2.getValue()));
117       }
118
119       @Override JavaDoc
120       public int hashCode()
121       {
122          return (getKey() == null ? 0 : getKey().hashCode()) ^
123                  (getValue() == null ? 0 : getValue().hashCode());
124       }
125
126       @Override JavaDoc
127       public String JavaDoc toString()
128       {
129          return key + "=" + value;
130       }
131    }
132
133    @Override JavaDoc
134    public Set JavaDoc entrySet()
135    {
136       return entrySet;
137    }
138
139    @Override JavaDoc
140    public int size()
141    {
142       return data.length;
143    }
144
145    private void readObject(java.io.ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc
146    {
147       in.defaultReadObject();
148       init();
149    }
150
151 }
152
Popular Tags