001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2000,2008 Oracle. All rights reserved.
005: *
006: * $Id: MapEntryParameter.java,v 1.19.2.2 2008/01/07 15:14:06 cwl Exp $
007: */
008:
009: package com.sleepycat.collections;
010:
011: import java.util.Map;
012:
013: /**
014: * A simple <code>Map.Entry</code> implementation that can be used as in
015: * input parameter. Since a <code>MapEntryParameter</code> is not obtained
016: * from a map, it is not attached to any map in particular. To emphasize that
017: * changing this object does not change the map, the {@link #setValue} method
018: * always throws <code>UnsupportedOperationException</code>.
019: *
020: * <p><b>Warning:</b> Use of this interface violates the Java Collections
021: * interface contract since these state that <code>Map.Entry</code> objects
022: * should only be obtained from <code>Map.entrySet()</code> sets, while this
023: * class allows constructing them directly. However, it is useful for
024: * performing operations on an entry set such as add(), contains(), etc. For
025: * restrictions see {@link #getValue} and {@link #setValue}.</p>
026: *
027: * @author Mark Hayes
028: */
029: public class MapEntryParameter implements Map.Entry {
030:
031: private Object key;
032: private Object value;
033:
034: /**
035: * Creates a map entry with a given key and value.
036: *
037: * @param key is the key to use.
038: *
039: * @param value is the value to use.
040: */
041: public MapEntryParameter(Object key, Object value) {
042:
043: this .key = key;
044: this .value = value;
045: }
046:
047: /**
048: * Computes a hash code as specified by {@link
049: * java.util.Map.Entry#hashCode}.
050: *
051: * @return the computed hash code.
052: */
053: public int hashCode() {
054:
055: return ((key == null) ? 0 : key.hashCode())
056: ^ ((value == null) ? 0 : value.hashCode());
057: }
058:
059: /**
060: * Compares this entry to a given entry as specified by {@link
061: * java.util.Map.Entry#equals}.
062: *
063: * @return the computed hash code.
064: */
065: public boolean equals(Object other) {
066:
067: if (!(other instanceof Map.Entry)) {
068: return false;
069: }
070:
071: Map.Entry e = (Map.Entry) other;
072:
073: return ((key == null) ? (e.getKey() == null) : key.equals(e
074: .getKey()))
075: && ((value == null) ? (e.getValue() == null) : value
076: .equals(e.getValue()));
077: }
078:
079: /**
080: * Returns the key of this entry.
081: *
082: * @return the key of this entry.
083: */
084: public final Object getKey() {
085:
086: return key;
087: }
088:
089: /**
090: * Returns the value of this entry. Note that this will be the value
091: * passed to the constructor or the last value passed to {@link #setValue}.
092: * It will not reflect changes made to a Map.
093: *
094: * @return the value of this entry.
095: */
096: public final Object getValue() {
097:
098: return value;
099: }
100:
101: /**
102: * Always throws <code>UnsupportedOperationException</code> since this
103: * object is not attached to a map.
104: */
105: public Object setValue(Object newValue) {
106:
107: throw new UnsupportedOperationException();
108: }
109:
110: final void setValueInternal(Object newValue) {
111:
112: this .value = newValue;
113: }
114:
115: /**
116: * Converts the entry to a string representation for debugging.
117: *
118: * @return the string representation.
119: */
120: public String toString() {
121:
122: return "[key [" + key + "] value [" + value + ']';
123: }
124: }
|