Source Code Cross Referenced for KeyToGroupMap.java in  » Chart » jfreechart » org » jfree » data » 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 » Chart » jfreechart » org.jfree.data 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* ===========================================================
002:         * JFreeChart : a free chart library for the Java(tm) platform
003:         * ===========================================================
004:         *
005:         * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006:         *
007:         * Project Info:  http://www.jfree.org/jfreechart/index.html
008:         *
009:         * This library is free software; you can redistribute it and/or modify it 
010:         * under the terms of the GNU Lesser General Public License as published by 
011:         * the Free Software Foundation; either version 2.1 of the License, or 
012:         * (at your option) any later version.
013:         *
014:         * This library is distributed in the hope that it will be useful, but 
015:         * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016:         * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017:         * License for more details.
018:         *
019:         * You should have received a copy of the GNU Lesser General Public
020:         * License along with this library; if not, write to the Free Software
021:         * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022:         * USA.  
023:         *
024:         * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025:         * in the United States and other countries.]
026:         *
027:         * ------------------
028:         * KeyToGroupMap.java
029:         * ------------------
030:         * (C) Copyright 2004, 2005, by Object Refinery Limited and Contributors.
031:         *
032:         * Original Author:  David Gilbert (for Object Refinery Limited);
033:         * Contributor(s):   -;
034:         *
035:         * $Id: KeyToGroupMap.java,v 1.7.2.2 2005/10/25 21:29:13 mungady Exp $
036:         *
037:         * Changes
038:         * -------
039:         * 29-Apr-2004 : Version 1 (DG);
040:         * 07-Jul-2004 : Added a group list to ensure group index is consistent, fixed 
041:         *               cloning problem (DG);
042:         * 18-Aug-2005 : Added casts in clone() method to suppress 1.5 compiler 
043:         *               warnings - see patch 1260587 (DG);
044:         * 
045:         */
046:
047:        package org.jfree.data;
048:
049:        import java.io.Serializable;
050:        import java.lang.reflect.Method;
051:        import java.lang.reflect.Modifier;
052:        import java.util.ArrayList;
053:        import java.util.Collection;
054:        import java.util.HashMap;
055:        import java.util.Iterator;
056:        import java.util.List;
057:        import java.util.Map;
058:
059:        import org.jfree.util.ObjectUtilities;
060:        import org.jfree.util.PublicCloneable;
061:
062:        /**
063:         * A class that maps keys (instances of <code>Comparable</code>) to groups.
064:         */
065:        public class KeyToGroupMap implements  Cloneable, PublicCloneable,
066:                Serializable {
067:
068:            /** For serialization. */
069:            private static final long serialVersionUID = -2228169345475318082L;
070:
071:            /** The default group. */
072:            private Comparable defaultGroup;
073:
074:            /** The groups. */
075:            private List groups;
076:
077:            /** A mapping between keys and groups. */
078:            private Map keyToGroupMap;
079:
080:            /**
081:             * Creates a new map with a default group named 'Default Group'.
082:             */
083:            public KeyToGroupMap() {
084:                this ("Default Group");
085:            }
086:
087:            /**
088:             * Creates a new map with the specified default group.
089:             * 
090:             * @param defaultGroup  the default group (<code>null</code> not permitted).
091:             */
092:            public KeyToGroupMap(Comparable defaultGroup) {
093:                if (defaultGroup == null) {
094:                    throw new IllegalArgumentException(
095:                            "Null 'defaultGroup' argument.");
096:                }
097:                this .defaultGroup = defaultGroup;
098:                this .groups = new ArrayList();
099:                this .keyToGroupMap = new HashMap();
100:            }
101:
102:            /**
103:             * Returns the number of groups in the map.
104:             * 
105:             * @return The number of groups in the map.
106:             */
107:            public int getGroupCount() {
108:                return this .groups.size() + 1;
109:            }
110:
111:            /**
112:             * Returns a list of the groups (always including the default group) in the 
113:             * map.  The returned list is independent of the map, so altering the list 
114:             * will have no effect.
115:             * 
116:             * @return The groups (never <code>null</code>).
117:             */
118:            public List getGroups() {
119:                List result = new ArrayList();
120:                result.add(this .defaultGroup);
121:                Iterator iterator = this .groups.iterator();
122:                while (iterator.hasNext()) {
123:                    Comparable group = (Comparable) iterator.next();
124:                    if (!result.contains(group)) {
125:                        result.add(group);
126:                    }
127:                }
128:                return result;
129:            }
130:
131:            /**
132:             * Returns the index for the group.
133:             * 
134:             * @param group  the group.
135:             * 
136:             * @return The group index (or -1 if the group is not represented within 
137:             *         the map).
138:             */
139:            public int getGroupIndex(Comparable group) {
140:                int result = this .groups.indexOf(group);
141:                if (result < 0) {
142:                    if (this .defaultGroup.equals(group)) {
143:                        result = 0;
144:                    }
145:                } else {
146:                    result = result + 1;
147:                }
148:                return result;
149:            }
150:
151:            /**
152:             * Returns the group that a key is mapped to.
153:             * 
154:             * @param key  the key (<code>null</code> not permitted).
155:             * 
156:             * @return The group (never <code>null</code>, returns the default group if
157:             *         there is no mapping for the specified key).
158:             */
159:            public Comparable getGroup(Comparable key) {
160:                if (key == null) {
161:                    throw new IllegalArgumentException("Null 'key' argument.");
162:                }
163:                Comparable result = this .defaultGroup;
164:                Comparable group = (Comparable) this .keyToGroupMap.get(key);
165:                if (group != null) {
166:                    result = group;
167:                }
168:                return result;
169:            }
170:
171:            /**
172:             * Maps a key to a group.
173:             * 
174:             * @param key  the key (<code>null</code> not permitted).
175:             * @param group  the group (<code>null</code> permitted, clears any 
176:             *               existing mapping).
177:             */
178:            public void mapKeyToGroup(Comparable key, Comparable group) {
179:                if (key == null) {
180:                    throw new IllegalArgumentException("Null 'key' argument.");
181:                }
182:                Comparable currentGroup = getGroup(key);
183:                if (!currentGroup.equals(this .defaultGroup)) {
184:                    if (!currentGroup.equals(group)) {
185:                        int count = getKeyCount(currentGroup);
186:                        if (count == 1) {
187:                            this .groups.remove(currentGroup);
188:                        }
189:                    }
190:                }
191:                if (group == null) {
192:                    this .keyToGroupMap.remove(key);
193:                } else {
194:                    if (!this .groups.contains(group)) {
195:                        if (!this .defaultGroup.equals(group)) {
196:                            this .groups.add(group);
197:                        }
198:                    }
199:                    this .keyToGroupMap.put(key, group);
200:                }
201:            }
202:
203:            /**
204:             * Returns the number of keys mapped to the specified group.  This method 
205:             * won't always return an accurate result for the default group, since 
206:             * explicit mappings are not required for this group.
207:             * 
208:             * @param group  the group (<code>null</code> not permitted).
209:             * 
210:             * @return The key count.
211:             */
212:            public int getKeyCount(Comparable group) {
213:                if (group == null) {
214:                    throw new IllegalArgumentException("Null 'group' argument.");
215:                }
216:                int result = 0;
217:                Iterator iterator = this .keyToGroupMap.values().iterator();
218:                while (iterator.hasNext()) {
219:                    Comparable g = (Comparable) iterator.next();
220:                    if (group.equals(g)) {
221:                        result++;
222:                    }
223:                }
224:                return result;
225:            }
226:
227:            /**
228:             * Tests the map for equality against an arbitrary object.
229:             * 
230:             * @param obj  the object to test against (<code>null</code> permitted).
231:             * 
232:             * @return A boolean.
233:             */
234:            public boolean equals(Object obj) {
235:                if (obj == this ) {
236:                    return true;
237:                }
238:                if (!(obj instanceof  KeyToGroupMap)) {
239:                    return false;
240:                }
241:                KeyToGroupMap that = (KeyToGroupMap) obj;
242:                if (!ObjectUtilities
243:                        .equal(this .defaultGroup, that.defaultGroup)) {
244:                    return false;
245:                }
246:                if (!this .keyToGroupMap.equals(that.keyToGroupMap)) {
247:                    return false;
248:                }
249:                return true;
250:            }
251:
252:            /**
253:             * Returns a clone of the map.
254:             * 
255:             * @return A clone.
256:             * 
257:             * @throws CloneNotSupportedException  if there is a problem cloning the
258:             *                                     map.
259:             */
260:            public Object clone() throws CloneNotSupportedException {
261:                KeyToGroupMap result = (KeyToGroupMap) super .clone();
262:                result.defaultGroup = (Comparable) KeyToGroupMap
263:                        .clone(this .defaultGroup);
264:                result.groups = (List) KeyToGroupMap.clone(this .groups);
265:                result.keyToGroupMap = (Map) KeyToGroupMap
266:                        .clone(this .keyToGroupMap);
267:                return result;
268:            }
269:
270:            /**
271:             * Attempts to clone the specified object using reflection.
272:             * 
273:             * @param object  the object (<code>null</code> permitted).
274:             * 
275:             * @return The cloned object, or the original object if cloning failed.
276:             */
277:            private static Object clone(Object object) {
278:                if (object == null) {
279:                    return null;
280:                }
281:                Class c = object.getClass();
282:                Object result = null;
283:                try {
284:                    Method m = c.getMethod("clone", (Class[]) null);
285:                    if (Modifier.isPublic(m.getModifiers())) {
286:                        try {
287:                            result = m.invoke(object, (Object[]) null);
288:                        } catch (Exception e) {
289:                            e.printStackTrace();
290:                        }
291:                    }
292:                } catch (NoSuchMethodException e) {
293:                    result = object;
294:                }
295:                return result;
296:            }
297:
298:            /**
299:             * Returns a clone of the list.
300:             * 
301:             * @param list  the list.
302:             * 
303:             * @return A clone of the list.
304:             * 
305:             * @throws CloneNotSupportedException if the list could not be cloned.
306:             */
307:            private static Collection clone(Collection list)
308:                    throws CloneNotSupportedException {
309:                Collection result = null;
310:                if (list != null) {
311:                    try {
312:                        List clone = (List) list.getClass().newInstance();
313:                        Iterator iterator = list.iterator();
314:                        while (iterator.hasNext()) {
315:                            clone.add(KeyToGroupMap.clone(iterator.next()));
316:                        }
317:                        result = clone;
318:                    } catch (Exception e) {
319:                        throw new CloneNotSupportedException("Exception.");
320:                    }
321:                }
322:                return result;
323:            }
324:
325:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.