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   * @version $Id: MutableObject.java 1562971 2014-01-30 21:23:18Z ggregory $
28   */
29  public class MutableObject<T> implements Mutable<T>, Serializable {
30  
31      /**
32       * Required for serialization support.
33       * 
34       * @see java.io.Serializable
35       */
36      private static final long serialVersionUID = 86241875189L;
37  
38      /** The mutable value. */
39      private T value;
40  
41      /**
42       * Constructs a new MutableObject with the default value of <code>null</code>.
43       */
44      public MutableObject() {
45          super();
46      }
47  
48      /**
49       * Constructs a new MutableObject with the specified value.
50       * 
51       * @param value  the initial value to store
52       */
53      public MutableObject(final T value) {
54          super();
55          this.value = value;
56      }
57  
58      //-----------------------------------------------------------------------
59      /**
60       * Gets the value.
61       * 
62       * @return the value, may be null
63       */
64      @Override
65      public T getValue() {
66          return this.value;
67      }
68  
69      /**
70       * Sets the value.
71       * 
72       * @param value  the value to set
73       */
74      @Override
75      public void setValue(final T value) {
76          this.value = value;
77      }
78  
79      //-----------------------------------------------------------------------
80      /**
81       * <p>
82       * Compares this object against the specified object. The result is <code>true</code> if and only if the argument
83       * is not <code>null</code> and is a <code>MutableObject</code> object that contains the same <code>T</code>
84       * value as this object.
85       * </p>
86       * 
87       * @param obj  the object to compare with, <code>null</code> returns <code>false</code>
88       * @return  <code>true</code> if the objects are the same;
89       *          <code>true</code> if the objects have equivalent <code>value</code> fields;
90       *          <code>false</code> otherwise.
91       */
92      @Override
93      public boolean equals(final Object obj) {
94          if (obj == null) {
95              return false;
96          }
97          if (this == obj) {
98              return true;
99          }
100         if (this.getClass() == obj.getClass()) {
101             final MutableObject<?> that = (MutableObject<?>) obj;
102             return this.value.equals(that.value);
103         }
104         return false;
105     }
106 
107     /**
108      * Returns the value's hash code or <code>0</code> if the value is <code>null</code>.
109      * 
110      * @return the value's hash code or <code>0</code> if the value is <code>null</code>.
111      */
112     @Override
113     public int hashCode() {
114         return value == null ? 0 : value.hashCode();
115     }
116 
117     //-----------------------------------------------------------------------
118     /**
119      * Returns the String value of this mutable.
120      * 
121      * @return the mutable value as a string
122      */
123     @Override
124     public String toString() {
125         return value == null ? "null" : value.toString();
126     }
127 
128 }