Source code: org/sablecc/sablecc/TypedHashMap.java
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2 * This file is part of SableCC. *
3 * See the file "LICENSE" for copyright information and the *
4 * terms and conditions for copying, distribution and *
5 * modification of SableCC. *
6 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
7
8 package org.sablecc.sablecc;
9
10 import java.util.*;
11
12 public class TypedHashMap extends HashMap
13 {
14 private Cast keyCast;
15 private Cast valueCast;
16 private Set entries;
17
18 public TypedHashMap()
19 {
20 super();
21
22 keyCast = NoCast.instance;
23 valueCast = NoCast.instance;
24 }
25
26 public TypedHashMap(int initialCapacity, Cast keyCast, Cast valueCast)
27 {
28 super(initialCapacity);
29
30 this.keyCast = keyCast;
31 this.valueCast = valueCast;
32 }
33
34 public TypedHashMap(Map map)
35 {
36 super();
37
38 keyCast = NoCast.instance;
39 valueCast = NoCast.instance;
40
41 Map.Entry[] entries = (Map.Entry[]) map.entrySet().toArray(new Map.Entry[0]);
42 for(int i = 0; i < entries.length; i++)
43 {
44 this.put(entries[i].getKey(),entries[i].getValue());
45 }
46
47 }
48
49 public TypedHashMap(Cast keyCast, Cast valueCast)
50 {
51 super();
52
53 this.keyCast = keyCast;
54 this.valueCast = valueCast;
55 }
56
57 public Object clone()
58 {
59 return new TypedHashMap(this, keyCast, valueCast);
60 }
61
62 public TypedHashMap(Map map, Cast keyCast, Cast valueCast)
63 {
64 super();
65
66 this.keyCast = keyCast;
67 this.valueCast = valueCast;
68
69 Map.Entry[] entries = (Map.Entry[]) map.entrySet().toArray(new Map.Entry[0]);
70 for(int i = 0; i < entries.length; i++)
71 {
72 this.put(entries[i].getKey(),entries[i].getValue());
73 }
74
75 }
76
77 public Cast getKeyCast()
78 {
79 return keyCast;
80 }
81
82 public Cast getValueCast()
83 {
84 return valueCast;
85 }
86
87 public Set entrySet()
88 {
89 if(entries == null)
90 {
91 entries = new EntrySet(super.entrySet());
92 }
93
94 return entries;
95 }
96
97 public Object put(Object key, Object value)
98 {
99 return super.put(keyCast.cast(key), valueCast.cast(value));
100 }
101
102 private class EntrySet extends AbstractSet
103 {
104 private Set set
105 ;
106
107 EntrySet(Set set
108 )
109 {
110 this.set = set
111 ;
112 }
113
114 public int size()
115 {
116 return set.size();
117 }
118
119 public Iterator iterator()
120 {
121 return new EntryIterator(set.iterator());
122 }
123 }
124
125 private class EntryIterator implements Iterator
126 {
127 private Iterator iterator;
128
129 EntryIterator(Iterator iterator)
130 {
131 this.iterator = iterator;
132 }
133
134 public boolean hasNext()
135 {
136 return iterator.hasNext();
137 }
138
139 public Object next()
140 {
141 return new TypedEntry((Map.Entry) iterator.next());
142 }
143
144 public void remove
145 ()
146 {
147 iterator.remove();
148 }
149 }
150
151 private class TypedEntry implements Map.Entry
152 {
153 private Map.Entry entry;
154
155 TypedEntry(Map.Entry entry)
156 {
157 this.entry = entry;
158 }
159
160 public Object getKey()
161 {
162 return entry.getKey();
163 }
164
165 public Object getValue()
166 {
167 return entry.getValue();
168 }
169
170 public Object setValue(Object value)
171 {
172 return entry.setValue(valueCast.cast(value));
173 }
174 }
175 }