KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > i18n > client > impl > ConstantMap


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.i18n.client.impl;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24
25 /**
26  * Read only Map used when returning <code>Constants</code> maps. Preserves
27  * order. ConstantMap should only be created or modified by GWT, as constant
28  * maps are constructed using a very stereotyped algorithm, which allows
29  * <code>ConstantMap</code> to maintain order with very little code. In
30  * specific, no elements are every removed from them and all elements are added
31  * before the first user operation.
32  */

33 public class ConstantMap extends HashMap JavaDoc {
34
35   private static class DummyMapEntry implements Map.Entry JavaDoc {
36     private final Object JavaDoc key;
37
38     private final Object JavaDoc value;
39
40     DummyMapEntry(Object JavaDoc key, Object JavaDoc value) {
41       this.key = key;
42       this.value = value;
43     }
44
45     public Object JavaDoc getKey() {
46       return key;
47     }
48
49     public Object JavaDoc getValue() {
50       return value;
51     }
52
53     public Object JavaDoc setValue(Object JavaDoc arg0) {
54       throw new UnsupportedOperationException JavaDoc();
55     }
56   }
57
58   private class OrderedConstantSet extends ArrayList JavaDoc implements Set JavaDoc {
59     private class ImmutableIterator implements Iterator JavaDoc {
60       private final Iterator JavaDoc base;
61
62       ImmutableIterator(Iterator JavaDoc base) {
63         this.base = base;
64       }
65
66       public boolean hasNext() {
67         return base.hasNext();
68       }
69
70       public Object JavaDoc next() {
71         return base.next();
72       }
73
74       public void remove() {
75         throw new UnsupportedOperationException JavaDoc("Immutable set");
76       }
77     }
78
79     public void clear() {
80       throw new UnsupportedOperationException JavaDoc("Immutable set");
81     }
82
83     public Iterator JavaDoc iterator() {
84       Iterator JavaDoc base = super.iterator();
85       return new ImmutableIterator(base);
86     }
87   }
88
89   private OrderedConstantSet entries;
90
91   private final OrderedConstantSet keys = new OrderedConstantSet();
92
93   private OrderedConstantSet values;
94
95   public void clear() {
96     throw unsupported("clear");
97   }
98
99   public Set JavaDoc entrySet() {
100     if (entries == null) {
101       entries = new OrderedConstantSet();
102       for (int i = 0; i < keys.size(); i++) {
103         Object JavaDoc key = keys.get(i);
104         Object JavaDoc value = get(key);
105         entries.add(new DummyMapEntry(key, value));
106       }
107     }
108     return entries;
109   }
110
111   public Set JavaDoc keySet() {
112     return keys;
113   }
114
115   public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
116     // We may want to find a more efficient implementation later.
117
boolean exists = keys.contains(key);
118     if (!exists) {
119       keys.add(key);
120     }
121     return super.put(key, value);
122   }
123
124   public Object JavaDoc remove(Object JavaDoc key) {
125     throw unsupported("remove");
126   }
127
128   public Collection JavaDoc values() {
129     if (values == null) {
130       values = new OrderedConstantSet();
131       for (int i = 0; i < keys.size(); i++) {
132         Object JavaDoc element = keys.get(i);
133         values.add(this.get(element));
134       }
135     }
136     return values;
137   }
138
139   private UnsupportedOperationException JavaDoc unsupported(String JavaDoc operation) {
140     return new UnsupportedOperationException JavaDoc(operation
141         + " not supported on a constant map");
142   }
143 }
144
Popular Tags