View Javadoc

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