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.lang3.mutable;
19  
20  import java.io.Serializable;
21  
22  /**
23   * A mutable <code>Object</code> wrapper.
24   * 
25   * @param <T> the type to set and get 
26   * @since 2.1
27   */
28  public class MutableObject<T> implements Mutable<T>, 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 T 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  the initial value to store
51       */
52      public MutableObject(final T value) {
53          super();
54          this.value = value;
55      }
56  
57      //-----------------------------------------------------------------------
58      /**
59       * Gets the value.
60       * 
61       * @return the value, may be null
62       */
63      @Override
64      public T getValue() {
65          return this.value;
66      }
67  
68      /**
69       * Sets the value.
70       * 
71       * @param value  the value to set
72       */
73      @Override
74      public void setValue(final T value) {
75          this.value = value;
76      }
77  
78      //-----------------------------------------------------------------------
79      /**
80       * <p>
81       * Compares this object against the specified object. The result is <code>true</code> if and only if the argument
82       * is not <code>null</code> and is a <code>MutableObject</code> object that contains the same <code>T</code>
83       * value as this object.
84       * </p>
85       * 
86       * @param obj  the object to compare with, <code>null</code> returns <code>false</code>
87       * @return  <code>true</code> if the objects are the same;
88       *          <code>true</code> if the objects have equivalent <code>value</code> fields;
89       *          <code>false</code> otherwise.
90       */
91      @Override
92      public boolean equals(final Object obj) {
93          if (obj == null) {
94              return false;
95          }
96          if (this == obj) {
97              return true;
98          }
99          if (this.getClass() == obj.getClass()) {
100             final MutableObject<?> that = (MutableObject<?>) obj;
101             return this.value.equals(that.value);
102         }
103         return false;
104     }
105 
106     /**
107      * Returns the value's hash code or <code>0</code> if the value is <code>null</code>.
108      * 
109      * @return the value's hash code or <code>0</code> if the value is <code>null</code>.
110      */
111     @Override
112     public int hashCode() {
113         return value == null ? 0 : value.hashCode();
114     }
115 
116     //-----------------------------------------------------------------------
117     /**
118      * Returns the String value of this mutable.
119      * 
120      * @return the mutable value as a string
121      */
122     @Override
123     public String toString() {
124         return value == null ? "null" : value.toString();
125     }
126 
127 }