View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang.mutable;
19  
20  import java.io.Serializable;
21  
22  /**
23   * A mutable <code>Object</code> wrapper.
24   * 
25   * @since 2.1
26   * @version $Id: MutableObject.java 437554 2006-08-28 06:21:41Z bayard $
27   */
28  public class MutableObject implements Mutable, Serializable {
29  
30      /**
31       * Required for serialization support.
32       * 
33       * @see java.io.Serializable
34       */
35      private static final long serialVersionUID = 86241875189L;
36  
37      /** The mutable value. */
38      private Object value;
39  
40      /**
41       * Constructs a new MutableObject with the default value of <code>null</code>.
42       */
43      public MutableObject() {
44          super();
45      }
46  
47      /**
48       * Constructs a new MutableObject with the specified value.
49       * 
50       * @param value
51       *            a value.
52       */
53      public MutableObject(Object value) {
54          super();
55          this.value = value;
56      }
57  
58      //-----------------------------------------------------------------------
59      /**
60       * Gets the value.
61       * 
62       * @return the value
63       */
64      public Object getValue() {
65          return this.value;
66      }
67  
68      /**
69       * Sets the value.
70       * 
71       * @param value
72       *            the value to set
73       */
74      public void setValue(Object value) {
75          this.value = value;
76      }
77  
78      //-----------------------------------------------------------------------
79      /**
80       * Compares this object against the specified object. The result is <code>true</code> if and only if the argument
81       * is not <code>null</code> and is a <code>MutableObject</code> object that contains the same <code>Object</code>
82       * value as this object.
83       * 
84       * @param obj
85       *            the object to compare with.
86       * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
87       */
88      public boolean equals(Object obj) {
89          if (obj instanceof MutableObject) {
90              Object other = ((MutableObject) obj).value;
91              return value == other || (value != null && value.equals(other));
92          }
93          return false;
94      }
95  
96      /**
97       * Returns the value's hash code or <code>0</code> if the value is <code>null</code>.
98       * 
99       * @return the value's hash code or <code>0</code> if the value is <code>null</code>.
100      */
101     public int hashCode() {
102         return value == null ? 0 : value.hashCode();
103     }
104 
105     /**
106      * Returns the String value of this mutable.
107      * 
108      * @return the mutable value as a string
109      */
110     public String toString() {
111         return value == null ? "null" : value.toString();
112     }
113 
114 }