Source Code Cross Referenced for ShortKeyMapToMapAdapter.java in  » Development » PCJ » bak » pcj » adapter » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » PCJ » bak.pcj.adapter 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Primitive Collections for Java.
003:         *  Copyright (C) 2003  Søren Bak
004:         *
005:         *  This library is free software; you can redistribute it and/or
006:         *  modify it under the terms of the GNU Lesser General Public
007:         *  License as published by the Free Software Foundation; either
008:         *  version 2.1 of the License, or (at your option) any later version.
009:         *
010:         *  This library is distributed in the hope that it will be useful,
011:         *  but WITHOUT ANY WARRANTY; without even the implied warranty of
012:         *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013:         *  Lesser General Public License for more details.
014:         *
015:         *  You should have received a copy of the GNU Lesser General Public
016:         *  License along with this library; if not, write to the Free Software
017:         *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018:         */
019:        package bak.pcj.adapter;
020:
021:        import bak.pcj.map.ShortKeyMap;
022:        import bak.pcj.map.ShortKeyMapIterator;
023:        import bak.pcj.hash.DefaultShortHashFunction;
024:        import bak.pcj.util.Exceptions;
025:
026:        import java.util.Iterator;
027:        import java.util.Collection;
028:        import java.util.AbstractSet;
029:        import java.util.Map;
030:        import java.util.Set;
031:
032:        /**
033:         *  This class represents adapters of primitive maps from
034:         *  short values to objects to Java Collections
035:         *  Framework maps. The adapter is implemented as a wrapper 
036:         *  around a primitive map. Thus, 
037:         *  changes to the underlying map are reflected by this
038:         *  map and vice versa.
039:         *
040:         *  @see        ShortKeyMap
041:         *  @see        java.util.Map
042:         *
043:         *  @author     Søren Bak
044:         *  @version    1.1     20-08-2003 23:03
045:         *  @since      1.0
046:         */
047:        public class ShortKeyMapToMapAdapter implements  Map {
048:
049:            /** The underlying primitive map. */
050:            protected ShortKeyMap map;
051:
052:            /**
053:             *  Creates a new adaption of a primitive map of short
054:             *  keys to a Java Collections Framework map.
055:             *
056:             *  @param      map
057:             *              the underlying primitive map.
058:             *
059:             *  @throws     NullPointerException
060:             *              if <tt>map</tt> is <tt>null</tt>.
061:             */
062:            public ShortKeyMapToMapAdapter(ShortKeyMap map) {
063:                if (map == null)
064:                    Exceptions.nullArgument("map");
065:                this .map = map;
066:            }
067:
068:            /**
069:             *  Clears this map. The underlying map is cleared.
070:             *
071:             *  @throws     UnsupportedOperationException
072:             *              if the operation is not supported by the
073:             *              underlying map.
074:             */
075:            public void clear() {
076:                map.clear();
077:            }
078:
079:            /**
080:             *  Indicates whether this map contains a mapping from a specified
081:             *  key. This is so, only if the underlying collection contains
082:             *  the unwrapped key.
083:             *
084:             *  @param      key
085:             *              the key to test for.
086:             *
087:             *  @return     <tt>true</tt> if this map contains a mapping from
088:             *              the specified key; returns <tt>false</tt>
089:             *              otherwise.
090:             *
091:             *  @throws     NullPointerException
092:             *              if <tt>key</tt> is <tt>null</tt>.
093:             *
094:             *  @throws     ClassCastException
095:             *              if <tt>key</tt> is not of class {@link Short Short}.
096:             */
097:            public boolean containsKey(Object key) {
098:                return map.containsKey(((Short) key).shortValue());
099:            }
100:
101:            /**
102:             *  Indicates whether this map contains a mapping to a specified
103:             *  value. 
104:             *
105:             *  @param      value
106:             *              the value to test for.
107:             *
108:             *  @return     <tt>true</tt> if this map contains at least one
109:             *              mapping to the specified value; returns
110:             *              <tt>false</tt> otherwise.
111:             */
112:            public boolean containsValue(Object value) {
113:                return map.containsValue(value);
114:            }
115:
116:            /**
117:             *  Returns a set view of the entries of this map. The returned
118:             *  set is a view, so changes to this map are reflected by the
119:             *  returned set and vice versa. All elements of the returned
120:             *  set implements {@link java.util.Map.Entry java.util.Map.Entry}.
121:             *
122:             *  @return     a set view of the entries of this map.
123:             */
124:            public Set entrySet() {
125:                return new EntrySet();
126:            }
127:
128:            /**
129:             *  Indicates whether this map is equal to some object.
130:             *
131:             *  @param      obj
132:             *              the object with which to compare this map.
133:             *
134:             *  @return     <tt>true</tt> if this map is equal to the
135:             *              specified object; returns <tt>false</tt>
136:             *              otherwise.
137:             */
138:            public boolean equals(Object obj) {
139:                if (!(obj instanceof  Map))
140:                    return false;
141:                Map m = (Map) obj;
142:                if (m.size() != map.size())
143:                    return false;
144:                Iterator i = m.entrySet().iterator();
145:                while (i.hasNext()) {
146:                    Map.Entry e = (Map.Entry) i.next();
147:                    if (e.getKey() == null)
148:                        return false;
149:                    if (e.getValue() == null)
150:                        return false;
151:                    if (!get(e.getKey()).equals(e.getValue()))
152:                        return false;
153:                }
154:                return true;
155:            }
156:
157:            /**
158:             *  Maps a specified key to a value. Returns <tt>null</tt>
159:             *  if no mapping exists for the specified key.
160:             *
161:             *  @param      key
162:             *              the key to map to a value.
163:             *
164:             *  @return     the value that the specified key maps to, or
165:             *              <tt>null</tt>, if no such mapping exists.
166:             *
167:             *  @throws     NullPointerException
168:             *              if <tt>key</tt> is <tt>null</tt>.
169:             *
170:             *  @throws     ClassCastException
171:             *              if <tt>key</tt> is not of class {@link Short Short}.
172:             */
173:            public Object get(Object key) {
174:                short k = ((Short) key).shortValue();
175:                return map.get(k);
176:            }
177:
178:            /**
179:             *  Returns a hash code value for this map. The hash code
180:             *  returned is that of the underlying map.
181:             *
182:             *  @return     a hash code value for this map.
183:             */
184:            public int hashCode() {
185:                return map.hashCode();
186:            }
187:
188:            /**
189:             *  Indicates whether this map is empty.
190:             *
191:             *  @return     <tt>true</tt> if this map is empty; returns
192:             *              <tt>false</tt> otherwise.
193:             */
194:            public boolean isEmpty() {
195:                return map.isEmpty();
196:            }
197:
198:            /**
199:             *  Returns a set view of the keys of this map. Removals from the
200:             *  returned set removes the corresponding entries in this map.
201:             *  Changes to the map are reflected in the set. All elements
202:             *  if the returned set is of class {@link Short Short}.
203:             *
204:             *  @return     a set view of the keys of this map.
205:             */
206:            public Set keySet() {
207:                return new ShortSetToSetAdapter(map.keySet());
208:            }
209:
210:            /**
211:             *  Adds a mapping from a specified key to a specified value to
212:             *  this map. If a mapping already exists for the specified key
213:             *  it is overwritten by the new mapping. The mapping is
214:             *  added to the underlying map.
215:             *
216:             *  @param      key
217:             *              the key of the mapping to add to this map.
218:             *
219:             *  @param      value
220:             *              the value of the mapping to add to this map.
221:             *
222:             *  @return     the old value if a
223:             *              mapping from the specified key already existed
224:             *              in this map; returns <tt>null</tt> otherwise.
225:             *
226:             *  @throws     UnsupportedOperationException
227:             *              if the operation is not supported by this map.
228:             *
229:             *  @throws     NullPointerException
230:             *              if <tt>key</tt> is <tt>null</tt>;
231:             *              if <tt>value</tt> is <tt>null</tt>.
232:             *
233:             *  @throws     ClassCastException
234:             *              if <tt>key</tt> is not of class {@link Short Short}.
235:             */
236:            public Object put(Object key, Object value) {
237:                short k = ((Short) key).shortValue();
238:                return map.put(k, value);
239:            }
240:
241:            /**
242:             *  Adds all mappings from a specified map to this map. Any
243:             *  existing mappings whose keys collide with a new mapping is
244:             *  overwritten by the new mapping. The mappings are
245:             *  added to the underlying map.
246:             *
247:             *  @param      map
248:             *              the map whose mappings to add to this map.
249:             *
250:             *  @throws     NullPointerException
251:             *              if <tt>map</tt> is <tt>null</tt>.
252:             *
253:             *  @throws     UnsupportedOperationException
254:             *              if the operation is not supported by this map.
255:             *
256:             *  @throws     NullPointerException
257:             *              if a key in <tt>map</tt> is <tt>null</tt>.
258:             *
259:             *  @throws     ClassCastException
260:             *              if a key in <tt>map</tt> is not of class {@link Short Short}.
261:             */
262:            public void putAll(Map map) {
263:                Iterator i = map.entrySet().iterator();
264:                while (i.hasNext()) {
265:                    Map.Entry e = (Map.Entry) i.next();
266:                    put(e.getKey(), e.getValue());
267:                }
268:            }
269:
270:            /**
271:             *  Removes the mapping from a specified key from this map.
272:             *  The mapping is removed from the underlying map.
273:             *
274:             *  @param      key
275:             *              the key whose mapping to remove from this map.
276:             *
277:             *  @return     the old value if a
278:             *              mapping from the specified key already existed
279:             *              in this map; returns <tt>null</tt> otherwise.
280:             *
281:             *  @throws     UnsupportedOperationException
282:             *              if the operation is not supported by the 
283:             *              underlying map.
284:             */
285:            public Object remove(Object key) {
286:                if (key == null)
287:                    return null;
288:                if (!(key instanceof  Short))
289:                    return null;
290:                short k = ((Short) key).shortValue();
291:                return map.remove(k);
292:            }
293:
294:            /**
295:             *  Returns the size of this map. The size is defined as the
296:             *  number of mappings from keys to values. The size is that
297:             *  of the underlying map.
298:             *
299:             *  @return     the size of this map.
300:             */
301:            public int size() {
302:                return map.size();
303:            }
304:
305:            /**
306:             *  Returns a collection view of the values in this map. The
307:             *  collection is not modifiable, but changes to the map are
308:             *  reflected in the collection. 
309:             *
310:             *  @return     a collection view of the values in this map.
311:             */
312:            public Collection values() {
313:                return map.values();
314:            }
315:
316:            class EntrySet extends AbstractSet {
317:
318:                public Iterator iterator() {
319:                    return new Iterator() {
320:                        ShortKeyMapIterator i = map.entries();
321:
322:                        public boolean hasNext() {
323:                            return i.hasNext();
324:                        }
325:
326:                        public Object next() {
327:                            i.next();
328:                            return new Entry(i.getKey(), i.getValue());
329:                        }
330:
331:                        public void remove() {
332:                            i.remove();
333:                        }
334:                    };
335:                }
336:
337:                public boolean add(Object obj) {
338:                    Map.Entry e = (Map.Entry) obj;
339:                    if (contains(e))
340:                        return false;
341:                    put(e.getKey(), e.getValue());
342:                    return true;
343:                }
344:
345:                public int size() {
346:                    return map.size();
347:                }
348:            }
349:
350:            class Entry implements  Map.Entry {
351:                Short key;
352:                Object value;
353:
354:                Entry(short key, Object value) {
355:                    this .key = new Short(key);
356:                    this .value = value;
357:                }
358:
359:                public Object getKey() {
360:                    return key;
361:                }
362:
363:                public Object getValue() {
364:                    return value;
365:                }
366:
367:                public Object setValue(Object value) {
368:                    return put(key, value);
369:                }
370:
371:                public int hashCode() {
372:                    return DefaultShortHashFunction.INSTANCE.hash(key
373:                            .shortValue())
374:                            ^ value.hashCode();
375:                }
376:
377:                public boolean equals(Object obj) {
378:                    if (!(obj instanceof  Map.Entry))
379:                        return false;
380:                    Map.Entry e = (Map.Entry) obj;
381:                    return key.equals(e.getKey()) && value.equals(e.getValue());
382:                }
383:            }
384:
385:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.