001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.collections.keyvalue; 018 019 import java.io.Serializable; 020 import java.util.Map; 021 022 import org.apache.commons.collections.KeyValue; 023 024 /** 025 * A {@link java.util.Map.Entry Map.Entry} tied to a map underneath. 026 * <p> 027 * This can be used to enable a map entry to make changes on the underlying 028 * map, however this will probably mess up any iterators. 029 * 030 * @since Commons Collections 3.0 031 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 032 * 033 * @author Stephen Colebourne 034 */ 035 public class TiedMapEntry implements Map.Entry, KeyValue, Serializable { 036 037 /** Serialization version */ 038 private static final long serialVersionUID = -8453869361373831205L; 039 040 /** The map underlying the entry/iterator */ 041 private final Map map; 042 /** The key */ 043 private final Object key; 044 045 /** 046 * Constructs a new entry with the given Map and key. 047 * 048 * @param map the map 049 * @param key the key 050 */ 051 public TiedMapEntry(Map map, Object key) { 052 super(); 053 this.map = map; 054 this.key = key; 055 } 056 057 // Map.Entry interface 058 //------------------------------------------------------------------------- 059 /** 060 * Gets the key of this entry 061 * 062 * @return the key 063 */ 064 public Object getKey() { 065 return key; 066 } 067 068 /** 069 * Gets the value of this entry direct from the map. 070 * 071 * @return the value 072 */ 073 public Object getValue() { 074 return map.get(key); 075 } 076 077 /** 078 * Sets the value associated with the key direct onto the map. 079 * 080 * @param value the new value 081 * @return the old value 082 * @throws IllegalArgumentException if the value is set to this map entry 083 */ 084 public Object setValue(Object value) { 085 if (value == this) { 086 throw new IllegalArgumentException("Cannot set value to this map entry"); 087 } 088 return map.put(key, value); 089 } 090 091 /** 092 * Compares this <code>Map.Entry</code> with another <code>Map.Entry</code>. 093 * <p> 094 * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)} 095 * 096 * @param obj the object to compare to 097 * @return true if equal key and value 098 */ 099 public boolean equals(Object obj) { 100 if (obj == this) { 101 return true; 102 } 103 if (obj instanceof Map.Entry == false) { 104 return false; 105 } 106 Map.Entry other = (Map.Entry) obj; 107 Object value = getValue(); 108 return 109 (key == null ? other.getKey() == null : key.equals(other.getKey())) && 110 (value == null ? other.getValue() == null : value.equals(other.getValue())); 111 } 112 113 /** 114 * Gets a hashCode compatible with the equals method. 115 * <p> 116 * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()} 117 * 118 * @return a suitable hash code 119 */ 120 public int hashCode() { 121 Object value = getValue(); 122 return (getKey() == null ? 0 : getKey().hashCode()) ^ 123 (value == null ? 0 : value.hashCode()); 124 } 125 126 /** 127 * Gets a string version of the entry. 128 * 129 * @return entry as a string 130 */ 131 public String toString() { 132 return getKey() + "=" + getValue(); 133 } 134 135 }