1 |
/* AbstractMap.java -- Abstract implementation of most of Map |
/* AbstractMap.java -- Abstract implementation of most of Map |
2 |
Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. |
Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
518 |
{ |
{ |
519 |
return o == null ? 0 : o.hashCode(); |
return o == null ? 0 : o.hashCode(); |
520 |
} |
} |
521 |
|
|
522 |
|
/** |
523 |
|
* A class which implements Map.Entry. It is shared by HashMap, TreeMap, |
524 |
|
* Hashtable, and Collections. It is not specified by the JDK, but makes |
525 |
|
* life much easier. |
526 |
|
* |
527 |
|
* @author Jon Zeppieri |
528 |
|
* @author Eric Blake <ebb9@email.byu.edu> |
529 |
|
*/ |
530 |
|
static class BasicMapEntry implements Entry |
531 |
|
{ |
532 |
|
/** |
533 |
|
* The key. Package visible for direct manipulation. |
534 |
|
*/ |
535 |
|
Object key; |
536 |
|
|
537 |
|
/** |
538 |
|
* The value. Package visible for direct manipulation. |
539 |
|
*/ |
540 |
|
Object value; |
541 |
|
|
542 |
|
/** |
543 |
|
* Basic constructor initializes the fields. |
544 |
|
* @param newKey the key |
545 |
|
* @param newValue the value |
546 |
|
*/ |
547 |
|
BasicMapEntry(Object newKey, Object newValue) |
548 |
|
{ |
549 |
|
key = newKey; |
550 |
|
value = newValue; |
551 |
|
} |
552 |
|
|
553 |
|
/** |
554 |
|
* Compares the specified object with this entry. Returns true only if |
555 |
|
* the object is a mapping of identical key and value. In other words, |
556 |
|
* this must be: |
557 |
|
* <pre> |
558 |
|
* (o instanceof Map.Entry) && |
559 |
|
* (getKey() == null ? ((HashMap) o).getKey() == null |
560 |
|
* : getKey().equals(((HashMap) o).getKey())) && |
561 |
|
* (getValue() == null ? ((HashMap) o).getValue() == null |
562 |
|
* : getValue().equals(((HashMap) o).getValue())) |
563 |
|
* </pre> |
564 |
|
* |
565 |
|
* @param o the object to compare |
566 |
|
* @return true if it is equal |
567 |
|
*/ |
568 |
|
public final boolean equals(Object o) |
569 |
|
{ |
570 |
|
if (! (o instanceof Map.Entry)) |
571 |
|
return false; |
572 |
|
// Optimize for our own entries. |
573 |
|
if (o instanceof BasicMapEntry) |
574 |
|
{ |
575 |
|
BasicMapEntry e = (BasicMapEntry) o; |
576 |
|
return (AbstractMap.equals(key, e.key) |
577 |
|
&& AbstractMap.equals(value, e.value)); |
578 |
|
} |
579 |
|
Map.Entry e = (Map.Entry) o; |
580 |
|
return (AbstractMap.equals(key, e.getKey()) |
581 |
|
&& AbstractMap.equals(value, e.getValue())); |
582 |
|
} |
583 |
|
|
584 |
|
/** |
585 |
|
* Get the key corresponding to this entry. |
586 |
|
* |
587 |
|
* @return the key |
588 |
|
*/ |
589 |
|
public final Object getKey() |
590 |
|
{ |
591 |
|
return key; |
592 |
|
} |
593 |
|
|
594 |
|
/** |
595 |
|
* Get the value corresponding to this entry. If you already called |
596 |
|
* Iterator.remove(), the behavior undefined, but in this case it works. |
597 |
|
* |
598 |
|
* @return the value |
599 |
|
*/ |
600 |
|
public final Object getValue() |
601 |
|
{ |
602 |
|
return value; |
603 |
|
} |
604 |
|
|
605 |
|
/** |
606 |
|
* Returns the hash code of the entry. This is defined as the exclusive-or |
607 |
|
* of the hashcodes of the key and value (using 0 for null). In other |
608 |
|
* words, this must be: |
609 |
|
* <pre> |
610 |
|
* (getKey() == null ? 0 : getKey().hashCode()) ^ |
611 |
|
* (getValue() == null ? 0 : getValue().hashCode()) |
612 |
|
* </pre> |
613 |
|
* |
614 |
|
* @return the hash code |
615 |
|
*/ |
616 |
|
public final int hashCode() |
617 |
|
{ |
618 |
|
return (AbstractMap.hashCode(key) ^ AbstractMap.hashCode(value)); |
619 |
|
} |
620 |
|
|
621 |
|
/** |
622 |
|
* Replaces the value with the specified object. This writes through |
623 |
|
* to the map, unless you have already called Iterator.remove(). It |
624 |
|
* may be overridden to restrict a null value. |
625 |
|
* |
626 |
|
* @param newVal the new value to store |
627 |
|
* @return the old value |
628 |
|
* @throws NullPointerException if the map forbids null values |
629 |
|
*/ |
630 |
|
public Object setValue(Object newVal) |
631 |
|
{ |
632 |
|
Object r = value; |
633 |
|
value = newVal; |
634 |
|
return r; |
635 |
|
} |
636 |
|
|
637 |
|
/** |
638 |
|
* This provides a string representation of the entry. It is of the form |
639 |
|
* "key=value", where string concatenation is used on key and value. |
640 |
|
* |
641 |
|
* @return the string representation |
642 |
|
*/ |
643 |
|
public final String toString() |
644 |
|
{ |
645 |
|
return key + "=" + value; |
646 |
|
} |
647 |
|
} // class BasicMapEntry |
648 |
} |
} |