1 46 47 package org.jfree.data; 48 49 import java.io.Serializable ; 50 import java.lang.reflect.Method ; 51 import java.lang.reflect.Modifier ; 52 import java.util.ArrayList ; 53 import java.util.Collection ; 54 import java.util.HashMap ; 55 import java.util.Iterator ; 56 import java.util.List ; 57 import java.util.Map ; 58 59 import org.jfree.util.ObjectUtilities; 60 import org.jfree.util.PublicCloneable; 61 62 65 public class KeyToGroupMap implements Cloneable , PublicCloneable, Serializable { 66 67 68 private static final long serialVersionUID = -2228169345475318082L; 69 70 71 private Comparable defaultGroup; 72 73 74 private List groups; 75 76 77 private Map keyToGroupMap; 78 79 82 public KeyToGroupMap() { 83 this("Default Group"); 84 } 85 86 91 public KeyToGroupMap(Comparable defaultGroup) { 92 if (defaultGroup == null) { 93 throw new IllegalArgumentException ("Null 'defaultGroup' argument."); 94 } 95 this.defaultGroup = defaultGroup; 96 this.groups = new ArrayList (); 97 this.keyToGroupMap = new HashMap (); 98 } 99 100 105 public int getGroupCount() { 106 return this.groups.size() + 1; 107 } 108 109 116 public List getGroups() { 117 List result = new ArrayList (); 118 result.add(this.defaultGroup); 119 Iterator iterator = this.groups.iterator(); 120 while (iterator.hasNext()) { 121 Comparable group = (Comparable ) iterator.next(); 122 if (!result.contains(group)) { 123 result.add(group); 124 } 125 } 126 return result; 127 } 128 129 137 public int getGroupIndex(Comparable group) { 138 int result = this.groups.indexOf(group); 139 if (result < 0) { 140 if (this.defaultGroup.equals(group)) { 141 result = 0; 142 } 143 } 144 else { 145 result = result + 1; 146 } 147 return result; 148 } 149 150 158 public Comparable getGroup(Comparable key) { 159 if (key == null) { 160 throw new IllegalArgumentException ("Null 'key' argument."); 161 } 162 Comparable result = this.defaultGroup; 163 Comparable group = (Comparable ) this.keyToGroupMap.get(key); 164 if (group != null) { 165 result = group; 166 } 167 return result; 168 } 169 170 177 public void mapKeyToGroup(Comparable key, Comparable group) { 178 if (key == null) { 179 throw new IllegalArgumentException ("Null 'key' argument."); 180 } 181 Comparable currentGroup = getGroup(key); 182 if (!currentGroup.equals(this.defaultGroup)) { 183 if (!currentGroup.equals(group)) { 184 int count = getKeyCount(currentGroup); 185 if (count == 1) { 186 this.groups.remove(currentGroup); 187 } 188 } 189 } 190 if (group == null) { 191 this.keyToGroupMap.remove(key); 192 } 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 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 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.equal(this.defaultGroup, that.defaultGroup)) { 243 return false; 244 } 245 if (!this.keyToGroupMap.equals(that.keyToGroupMap)) { 246 return false; 247 } 248 return true; 249 } 250 251 259 public Object clone() throws CloneNotSupportedException { 260 KeyToGroupMap result = (KeyToGroupMap) super.clone(); 261 result.defaultGroup 262 = (Comparable ) KeyToGroupMap.clone(this.defaultGroup); 263 result.groups = (List ) KeyToGroupMap.clone(this.groups); 264 result.keyToGroupMap = (Map ) KeyToGroupMap.clone(this.keyToGroupMap); 265 return result; 266 } 267 268 275 private static Object clone(Object object) { 276 if (object == null) { 277 return null; 278 } 279 Class c = object.getClass(); 280 Object result = null; 281 try { 282 Method m = c.getMethod("clone", (Class []) null); 283 if (Modifier.isPublic(m.getModifiers())) { 284 try { 285 result = m.invoke(object, (Object []) null); 286 } 287 catch (Exception e) { 288 e.printStackTrace(); 289 } 290 } 291 } 292 catch (NoSuchMethodException e) { 293 result = object; 294 } 295 return result; 296 } 297 298 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 } 319 catch (Exception e) { 320 throw new CloneNotSupportedException ("Exception."); 321 } 322 } 323 return result; 324 } 325 326 } 327 | Popular Tags |