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