This project has retired. For details please refer to its Attic page.
MapEntry xref
View Javadoc

1   /*
2    * $Id: MapEntry.java 1049711 2010-12-15 21:12:00Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.tiles.request.collection;
22  
23  
24  import java.util.Map;
25  
26  
27  /**
28   * <p>Map.Entry implementation that can be constructed to either be read-only
29   * or not.</p>
30   *
31   * @version $Rev: 1049711 $ $Date: 2010-12-16 08:12:00 +1100 (Thu, 16 Dec 2010) $
32   * @param <K> The key type.
33   * @param <V> The value type.
34   */
35  
36  public class MapEntry<K, V> implements Map.Entry<K, V> {
37  
38  
39      /**
40       * <p>The entry key.</p>
41       */
42      private K key;
43  
44      /**
45       * <p>The entry value.</p>
46       */
47      private V value;
48  
49      /**
50       * <p>Whether the entry can be modified.</p>
51       */
52      private boolean modifiable = false;
53  
54  
55      /**
56       * <p>Creates a map entry that can either allow modifications or not.</p>
57       *
58       * @param key        The entry key
59       * @param value      The entry value
60       * @param modifiable Whether the entry should allow modification or not
61       */
62      public MapEntry(K key, V value, boolean modifiable) {
63          this.key = key;
64          this.value = value;
65          this.modifiable = modifiable;
66      }
67  
68  
69      /**
70       * <p>Gets the entry key.</p>
71       *
72       * @return The entry key
73       */
74      public K getKey() {
75          return key;
76      }
77  
78  
79      /**
80       * <p>Gets the entry value.</p>
81       *
82       * @return The entry key
83       */
84      public V getValue() {
85          return value;
86      }
87  
88  
89      /**
90       * <p>Sets the entry value if the entry can be modified.</p>
91       *
92       * @param val The new value
93       * @return The old entry value
94       * @throws UnsupportedOperationException If the entry cannot be modified
95       */
96      public V setValue(V val) {
97          if (modifiable) {
98              V oldVal = this.value;
99              this.value = val;
100             return oldVal;
101         }
102         throw new UnsupportedOperationException("The map entry is not modifiable");
103     }
104 
105 
106     /**
107      * <p>Determines if this entry is equal to the passed object.</p>
108      *
109      * @param o The object to test
110      * @return True if equal, else false
111      */
112     @Override
113     @SuppressWarnings("unchecked")
114     public boolean equals(Object o) {
115         if (o != null && o instanceof Map.Entry) {
116             Map.Entry<K, V> entry = (Map.Entry<K, V>) o;
117             return (this.getKey() == null ? entry.getKey() == null : this
118                     .getKey().equals(entry.getKey()))
119                     && (this.getValue() == null ? entry.getValue() == null
120                             : this.getValue().equals(entry.getValue()));
121         }
122         return false;
123     }
124 
125 
126     /**
127      * <p>Returns the hashcode for this entry.</p>
128      *
129      * @return The and'ed hashcode of the key and value
130      */
131     @Override
132     public int hashCode() {
133         return (this.getKey() == null ? 0 : this.getKey().hashCode())
134             ^ (this.getValue() == null ? 0 : this.getValue().hashCode());
135     }
136 }