1/*2 * $Id: MapEntry.java 527536 2007-04-11 15:44:51Z apetrelli $3 *4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */21package org.apache.tiles.context;
222324import java.util.Map;
252627/***28 * <p>Map.Entry implementation that can be constructed to either be read-only29 * or not.</p>30 *31 * @version $Rev: 527536 $ $Date: 2007-04-11 17:44:51 +0200 (mer, 11 apr 2007) $32 * @param <K> The key type.33 * @param <V> The value type.34 */3536publicclass MapEntry<K, V> implements Map.Entry<K, V> {
373839/***40 * <p>The entry key.</p>41 */42private K key;
4344/***45 * <p>The entry value.</p>46 */47private V value;
4849/***50 * <p>Whether the entry can be modified.</p>51 */52privateboolean modifiable = false;
535455/***56 * <p>Creates a map entry that can either allow modifications or not.</p>57 *58 * @param key The entry key59 * @param value The entry value60 * @param modifiable Whether the entry should allow modification or not61 */62publicMapEntry(K key, V value, boolean modifiable) {
63this.key = key;
64this.value = value;
65this.modifiable = modifiable;
66 }
676869/***70 * <p>Gets the entry key.</p>71 *72 * @return The entry key73 */74public K getKey() {
75return key;
76 }
777879/***80 * <p>Gets the entry value.</p>81 *82 * @return The entry key83 */84public V getValue() {
85return value;
86 }
878889/***90 * <p>Sets the entry value if the entry can be modified.</p>91 *92 * @param val The new value93 * @return The old entry value94 * @throws UnsupportedOperationException If the entry cannot be modified95 */96public V setValue(V val) {
97if (modifiable) {
98 V oldVal = this.value;
99this.value = val;
100return oldVal;
101 } else {
102thrownew UnsupportedOperationException();
103 }
104 }
105106107/***108 * <p>Determines if this entry is equal to the passed object.</p>109 *110 * @param o The object to test111 * @return True if equal, else false112 */113 @SuppressWarnings("unchecked")
114publicboolean equals(Object o) {
115if (o != null && o instanceof Map.Entry) {
116 Map.Entry<K, V> entry = (Map.Entry<K, V>) o;
117return (this.getKey() == null ? entry.getKey() == null : this118 .getKey().equals(entry.getKey()))
119 && (this.getValue() == null ? entry.getValue() == null120 : this.getValue().equals(entry.getValue()));
121 }
122return false;
123 }
124125126/***127 * <p>Returns the hashcode for this entry.</p>128 *129 * @return The and'ed hashcode of the key and value130 */131publicint hashCode() {
132return (this.getKey() == null ? 0 : this.getKey().hashCode())
133 ^ (this.getValue() == null ? 0 : this.getValue().hashCode());
134 }
135 }