Source Code Cross Referenced for MapToObjectKeyLongMapAdapter.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) 2002, 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.Adapter;
022:        import bak.pcj.LongIterator;
023:        import bak.pcj.LongCollection;
024:        import bak.pcj.map.ObjectKeyLongMap;
025:        import bak.pcj.map.AbstractObjectKeyLongMap;
026:        import bak.pcj.map.ObjectKeyLongMapIterator;
027:        import bak.pcj.map.MapDefaults;
028:        import bak.pcj.map.NoSuchMappingException;
029:        import bak.pcj.set.LongSet;
030:        import bak.pcj.util.Exceptions;
031:
032:        import java.util.Map;
033:        import java.util.Set;
034:        import java.util.Iterator;
035:
036:        /**
037:         *  This class represents adaptions of Java Collections Framework
038:         *  maps to primitive maps from object values to long values.
039:         *  The adapter is implemented as a wrapper around the map. 
040:         *  Thus, changes to the underlying map are reflected by this
041:         *  map and vice versa.
042:         *
043:         *  <p>
044:         *  Adapters from JCF maps to primitive map will
045:         *  fail if the JCF collection contains <tt>null</tt> values or
046:         *  values of the wrong class. However, adapters are not fast
047:         *  failing in the case that the underlying map should
048:         *  contain illegal keys or values. To implement fast failure would require
049:         *  every operation to check every key and value of the underlying
050:         *  map before doing anything. Instead validation methods
051:         *  are provided. They can be called using the assertion facility
052:         *  in the client code:
053:         *  <pre>
054:         *      MapToObjectKeyLongMapAdapter s;
055:         *      ...
056:         *      <b>assert</b> s.validate();
057:         *  </pre>
058:         *  or by letting the adapter throw an exception on illegal values:
059:         *  <pre>
060:         *      MapToObjectKeyLongMapAdapter s;
061:         *      ...
062:         *      s.evalidate();  // Throws an exception on illegal values
063:         *  </pre>
064:         *  Either way, validation must be invoked directly by the client
065:         *  code.
066:         *
067:         *  @author     S&oslash;ren Bak
068:         *  @version    1.1     21-08-2003 19:12
069:         *  @since      1.1
070:         */
071:        public class MapToObjectKeyLongMapAdapter extends
072:                AbstractObjectKeyLongMap implements  ObjectKeyLongMap {
073:
074:            /** The underlying map. */
075:            protected Map map;
076:
077:            /** The value corresponding to the last key found by containsKey(). */
078:            protected Long lastValue;
079:
080:            /**
081:             *  Creates a new adaption to a map from object
082:             *  values to long values.
083:             *
084:             *  @param      map
085:             *              the underlying map. This map must
086:             *              consist of
087:             *              values of class
088:             *              {@link Long Long}. Otherwise a
089:             *              {@link ClassCastException ClassCastException}
090:             *              will be thrown by some methods.
091:             *
092:             *  @throws     NullPointerException
093:             *              if <tt>map</tt> is <tt>null</tt>.
094:             */
095:            public MapToObjectKeyLongMapAdapter(Map map) {
096:                if (map == null)
097:                    Exceptions.nullArgument("map");
098:                this .map = map;
099:                lastValue = null;
100:            }
101:
102:            /**
103:             *  Creates a new adaption to a map from object
104:             *  values to long values. The map to adapt is optionally validated.
105:             *
106:             *  @param      map
107:             *              the underlying map. This map must
108:             *              consist of 
109:             *              values of class
110:             *              {@link Long Long}. Otherwise a
111:             *              {@link ClassCastException ClassCastException}
112:             *              will be thrown by some methods.
113:             *
114:             *  @param      validate
115:             *              indicates whether <tt>map</tt> should
116:             *              be checked for illegal values.
117:             *
118:             *  @throws     NullPointerException
119:             *              if <tt>map</tt> is <tt>null</tt>.
120:             *
121:             *  @throws     IllegalStateException
122:             *              if <tt>validate</tt> is <tt>true</tt> and
123:             *              <tt>map</tt> contains a <tt>null</tt> value,
124:             *              or a value that is not of class
125:             *              {@link Long Long}.
126:             */
127:            public MapToObjectKeyLongMapAdapter(Map map, boolean validate) {
128:                if (map == null)
129:                    Exceptions.nullArgument("map");
130:                this .map = map;
131:                lastValue = null;
132:                if (validate)
133:                    evalidate();
134:            }
135:
136:            public void clear() {
137:                map.clear();
138:            }
139:
140:            public boolean containsKey(Object key) {
141:                lastValue = (Long) map.get(key);
142:                return lastValue != null;
143:            }
144:
145:            public boolean containsValue(long value) {
146:                return map.containsValue(new Long(value));
147:            }
148:
149:            public ObjectKeyLongMapIterator entries() {
150:                return new ObjectKeyLongMapIterator() {
151:                    Iterator i = map.entrySet().iterator();
152:                    Map.Entry lastEntry = null;
153:
154:                    public boolean hasNext() {
155:                        return i.hasNext();
156:                    }
157:
158:                    public void next() {
159:                        lastEntry = (Map.Entry) i.next();
160:                    }
161:
162:                    public Object getKey() {
163:                        if (lastEntry == null)
164:                            Exceptions.noElementToGet();
165:                        return lastEntry.getKey();
166:                    }
167:
168:                    public long getValue() {
169:                        if (lastEntry == null)
170:                            Exceptions.noElementToGet();
171:                        return ((Long) lastEntry.getValue()).longValue();
172:                    }
173:
174:                    public void remove() {
175:                        i.remove();
176:                        lastEntry = null;
177:                    }
178:                };
179:            }
180:
181:            public long get(Object key) {
182:                Long value = (Long) map.get(key);
183:                return value == null ? MapDefaults.defaultLong() : value
184:                        .longValue();
185:            }
186:
187:            public Set keySet() {
188:                return map.keySet();
189:            }
190:
191:            public long lget() {
192:                if (lastValue == null)
193:                    Exceptions.noLastElement();
194:                return lastValue.longValue();
195:            }
196:
197:            public long put(Object key, long value) {
198:                Long oldValue = (Long) map.put(key, new Long(value));
199:                return oldValue == null ? MapDefaults.defaultLong() : oldValue
200:                        .longValue();
201:            }
202:
203:            public long remove(Object key) {
204:                Long value = (Long) map.remove(key);
205:                return value == null ? MapDefaults.defaultLong() : value
206:                        .longValue();
207:            }
208:
209:            public int size() {
210:                return map.size();
211:            }
212:
213:            public LongCollection values() {
214:                return new CollectionToLongCollectionAdapter(map.values());
215:            }
216:
217:            public long tget(Object key) {
218:                Long value = (Long) map.get(key);
219:                if (value == null)
220:                    Exceptions.noSuchMapping(key);
221:                return value.longValue();
222:            }
223:
224:            /**
225:             *  Indicates whether the underlying map is valid for
226:             *  this adapter. For the underlying map to be valid it
227:             *  can contain no <tt>null</tt>
228:             *  values and only {@link Long Long} values.
229:             *
230:             *  @return     <tt>true</tt> if the underlying map is
231:             *              valid; returns <tt>false</tt> otherwise.
232:             */
233:            public boolean validate() {
234:                return Adapter.isObjectKeyLongAdaptable(map);
235:            }
236:
237:            /**
238:             *  Validates the map underlying this adapter and throws
239:             *  an exception if it is invalid. For the underlying map to be valid it
240:             *  can contain no <tt>null</tt>
241:             *  values and only {@link Long Long} values.
242:             *
243:             *  @throws     IllegalStateException
244:             *              if the underlying map is invalid.
245:             */
246:            public void evalidate() {
247:                if (!validate())
248:                    Exceptions.cannotAdapt("map");
249:            }
250:
251:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.