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