1 16 package org.apache.commons.collections.map; 17 18 import java.io.Serializable ; 19 import java.util.AbstractSet ; 20 import java.util.Collection ; 21 import java.util.Collections ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.NoSuchElementException ; 25 import java.util.Set ; 26 27 import org.apache.commons.collections.BoundedMap; 28 import org.apache.commons.collections.KeyValue; 29 import org.apache.commons.collections.MapIterator; 30 import org.apache.commons.collections.OrderedMap; 31 import org.apache.commons.collections.OrderedMapIterator; 32 import org.apache.commons.collections.ResettableIterator; 33 import org.apache.commons.collections.iterators.SingletonIterator; 34 import org.apache.commons.collections.keyvalue.TiedMapEntry; 35 36 61 public class SingletonMap 62 implements OrderedMap, BoundedMap, KeyValue, Serializable , Cloneable { 63 64 65 private static final long serialVersionUID = -8931271118676803261L; 66 67 68 private final Object key; 69 70 private Object value; 71 72 75 public SingletonMap() { 76 super(); 77 this.key = null; 78 } 79 80 86 public SingletonMap(Object key, Object value) { 87 super(); 88 this.key = key; 89 this.value = value; 90 } 91 92 97 public SingletonMap(KeyValue keyValue) { 98 super(); 99 this.key = keyValue.getKey(); 100 this.value = keyValue.getValue(); 101 } 102 103 108 public SingletonMap(Map.Entry entry) { 109 super(); 110 this.key = entry.getKey(); 111 this.value = entry.getValue(); 112 } 113 114 121 public SingletonMap(Map map) { 122 super(); 123 if (map.size() != 1) { 124 throw new IllegalArgumentException ("The map size must be 1"); 125 } 126 Map.Entry entry = (Map.Entry ) map.entrySet().iterator().next(); 127 this.key = entry.getKey(); 128 this.value = entry.getValue(); 129 } 130 131 138 public Object getKey() { 139 return key; 140 } 141 142 147 public Object getValue() { 148 return value; 149 } 150 151 157 public Object setValue(Object value) { 158 Object old = this.value; 159 this.value = value; 160 return old; 161 } 162 163 170 public boolean isFull() { 171 return true; 172 } 173 174 179 public int maxSize() { 180 return 1; 181 } 182 183 191 public Object get(Object key) { 192 if (isEqualKey(key)) { 193 return value; 194 } 195 return null; 196 } 197 198 203 public int size() { 204 return 1; 205 } 206 207 212 public boolean isEmpty() { 213 return false; 214 } 215 216 223 public boolean containsKey(Object key) { 224 return (isEqualKey(key)); 225 } 226 227 233 public boolean containsValue(Object value) { 234 return (isEqualValue(value)); 235 } 236 237 249 public Object put(Object key, Object value) { 250 if (isEqualKey(key)) { 251 return setValue(value); 252 } 253 throw new IllegalArgumentException ("Cannot put new key/value pair - Map is fixed size singleton"); 254 } 255 256 267 public void putAll(Map map) { 268 switch (map.size()) { 269 case 0: 270 return; 271 272 case 1: 273 Map.Entry entry = (Map.Entry ) map.entrySet().iterator().next(); 274 put(entry.getKey(), entry.getValue()); 275 return; 276 277 default: 278 throw new IllegalArgumentException ("The map size must be 0 or 1"); 279 } 280 } 281 282 289 public Object remove(Object key) { 290 throw new UnsupportedOperationException (); 291 } 292 293 296 public void clear() { 297 throw new UnsupportedOperationException (); 298 } 299 300 308 public Set entrySet() { 309 Map.Entry entry = new TiedMapEntry(this, getKey()); 310 return Collections.singleton(entry); 311 } 312 313 320 public Set keySet() { 321 return Collections.singleton(key); 322 } 323 324 331 public Collection values() { 332 return new SingletonValues(this); 333 } 334 335 347 public MapIterator mapIterator() { 348 return new SingletonMapIterator(this); 349 } 350 351 361 public OrderedMapIterator orderedMapIterator() { 362 return new SingletonMapIterator(this); 363 } 364 365 370 public Object firstKey() { 371 return getKey(); 372 } 373 374 379 public Object lastKey() { 380 return getKey(); 381 } 382 383 389 public Object nextKey(Object key) { 390 return null; 391 } 392 393 399 public Object previousKey(Object key) { 400 return null; 401 } 402 403 410 protected boolean isEqualKey(Object key) { 411 return (key == null ? getKey() == null : key.equals(getKey())); 412 } 413 414 420 protected boolean isEqualValue(Object value) { 421 return (value == null ? getValue() == null : value.equals(getValue())); 422 } 423 424 428 static class SingletonMapIterator implements OrderedMapIterator, ResettableIterator { 429 private final SingletonMap parent; 430 private boolean hasNext = true; 431 private boolean canGetSet = false; 432 433 SingletonMapIterator(SingletonMap parent) { 434 super(); 435 this.parent = parent; 436 } 437 438 public boolean hasNext() { 439 return hasNext; 440 } 441 442 public Object next() { 443 if (hasNext == false) { 444 throw new NoSuchElementException (AbstractHashedMap.NO_NEXT_ENTRY); 445 } 446 hasNext = false; 447 canGetSet = true; 448 return parent.getKey(); 449 } 450 451 public boolean hasPrevious() { 452 return (hasNext == false); 453 } 454 455 public Object previous() { 456 if (hasNext == true) { 457 throw new NoSuchElementException (AbstractHashedMap.NO_PREVIOUS_ENTRY); 458 } 459 hasNext = true; 460 return parent.getKey(); 461 } 462 463 public void remove() { 464 throw new UnsupportedOperationException (); 465 } 466 467 public Object getKey() { 468 if (canGetSet == false) { 469 throw new IllegalStateException (AbstractHashedMap.GETKEY_INVALID); 470 } 471 return parent.getKey(); 472 } 473 474 public Object getValue() { 475 if (canGetSet == false) { 476 throw new IllegalStateException (AbstractHashedMap.GETVALUE_INVALID); 477 } 478 return parent.getValue(); 479 } 480 481 public Object setValue(Object value) { 482 if (canGetSet == false) { 483 throw new IllegalStateException (AbstractHashedMap.SETVALUE_INVALID); 484 } 485 return parent.setValue(value); 486 } 487 488 public void reset() { 489 hasNext = true; 490 } 491 492 public String toString() { 493 if (hasNext) { 494 return "Iterator[]"; 495 } else { 496 return "Iterator[" + getKey() + "=" + getValue() + "]"; 497 } 498 } 499 } 500 501 505 static class SingletonValues extends AbstractSet implements Serializable { 506 private static final long serialVersionUID = -3689524741863047872L; 507 private final SingletonMap parent; 508 509 SingletonValues(SingletonMap parent) { 510 super(); 511 this.parent = parent; 512 } 513 514 public int size() { 515 return 1; 516 } 517 public boolean isEmpty() { 518 return false; 519 } 520 public boolean contains(Object object) { 521 return parent.containsValue(object); 522 } 523 public void clear() { 524 throw new UnsupportedOperationException (); 525 } 526 public Iterator iterator() { 527 return new SingletonIterator(parent.getValue(), false); 528 } 529 } 530 531 537 public Object clone() { 538 try { 539 SingletonMap cloned = (SingletonMap) super.clone(); 540 return cloned; 541 } catch (CloneNotSupportedException ex) { 542 throw new InternalError (); 543 } 544 } 545 546 552 public boolean equals(Object obj) { 553 if (obj == this) { 554 return true; 555 } 556 if (obj instanceof Map == false) { 557 return false; 558 } 559 Map other = (Map ) obj; 560 if (other.size() != 1) { 561 return false; 562 } 563 Map.Entry entry = (Map.Entry ) other.entrySet().iterator().next(); 564 return isEqualKey(entry.getKey()) && isEqualValue(entry.getValue()); 565 } 566 567 572 public int hashCode() { 573 return (getKey() == null ? 0 : getKey().hashCode()) ^ 574 (getValue() == null ? 0 : getValue().hashCode()); 575 } 576 577 582 public String toString() { 583 return new StringBuffer (128) 584 .append('{') 585 .append((getKey() == this ? "(this Map)" : getKey())) 586 .append('=') 587 .append((getValue() == this ? "(this Map)" : getValue())) 588 .append('}') 589 .toString(); 590 } 591 592 } 593 | Popular Tags |