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.chain.web;
018
019
020 import java.util.Map;
021
022
023 /**
024 * <p>Map.Entry implementation that can be constructed to either be read-only
025 * or not.</p>
026 *
027 * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $
028 */
029
030 public class MapEntry implements Map.Entry {
031
032
033 /**
034 * <p>The entry key.</p>
035 */
036 private Object key;
037
038 /**
039 * <p>The entry value.</p>
040 */
041 private Object value;
042
043 /**
044 * <p>Whether the entry can be modified.</p>
045 */
046 private boolean modifiable = false;
047
048
049 /**
050 * <p>Creates a map entry that can either allow modifications or not.</p>
051 *
052 * @param key The entry key
053 * @param value The entry value
054 * @param modifiable Whether the entry should allow modification or not
055 */
056 public MapEntry(Object key, Object value, boolean modifiable) {
057 this.key = key;
058 this.value = value;
059 this.modifiable = modifiable;
060 }
061
062
063 /**
064 * <p>Gets the entry key.</p>
065 *
066 * @return The entry key
067 */
068 public Object getKey() {
069 return key;
070 }
071
072
073 /**
074 * <p>Gets the entry value.</p>
075 *
076 * @return The entry key
077 */
078 public Object getValue() {
079 return value;
080 }
081
082
083 /**
084 * <p>Sets the entry value if the entry can be modified.</p>
085 *
086 * @param val The new value
087 * @return The old entry value
088 * @throws UnsupportedOperationException If the entry cannot be modified
089 */
090 public Object setValue(Object val) {
091 if (modifiable) {
092 Object oldVal = this.value;
093 this.value = val;
094 return oldVal;
095 } else {
096 throw new UnsupportedOperationException();
097 }
098 }
099
100
101 /**
102 * <p>Determines if this entry is equal to the passed object.</p>
103 *
104 * @param o The object to test
105 * @return True if equal, else false
106 */
107 public boolean equals(Object o) {
108 if (o != null && o instanceof Map.Entry) {
109 Map.Entry entry = (Map.Entry)o;
110 return (this.getKey() == null ?
111 entry.getKey() == null : this.getKey().equals(entry.getKey())) &&
112 (this.getValue() == null ?
113 entry.getValue() == null : this.getValue().equals(entry.getValue()));
114 }
115 return false;
116 }
117
118
119 /**
120 * <p>Returns the hashcode for this entry.</p>
121 *
122 * @return The and'ed hashcode of the key and value
123 */
124 public int hashCode() {
125 return (this.getKey() == null ? 0 : this.getKey().hashCode()) ^
126 (this.getValue() == null ? 0 : this.getValue().hashCode());
127 }
128 }