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 * https://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 import java.util.concurrent.atomic.AtomicReference;
23
24 /**
25 * A mutable {@link Object} wrapper.
26 * <p>
27 * This class was created before the introduction of the the {@link java.util.concurrent.atomic} package and the {@link AtomicReference} class.
28 * </p>
29 *
30 * @param <T> the type to set and get.
31 * @see AtomicReference
32 * @since 2.1
33 */
34 public class MutableObject<T> implements Mutable<T>, Serializable {
35
36 /**
37 * Required for serialization support.
38 *
39 * @see java.io.Serializable
40 */
41 private static final long serialVersionUID = 86241875189L;
42
43 /** The mutable value. */
44 private T value;
45
46 /**
47 * Constructs a new MutableObject with the default value of {@code null}.
48 */
49 public MutableObject() {
50 }
51
52 /**
53 * Constructs a new MutableObject with the specified value.
54 *
55 * @param value the initial value to store.
56 */
57 public MutableObject(final T value) {
58 this.value = value;
59 }
60
61 /**
62 * Compares this object against the specified object. The result is {@code true} if and only if the argument
63 * is not {@code null} and is a {@link MutableObject} object that contains the same {@link T}
64 * value as this object.
65 *
66 * @param obj the object to compare with, {@code null} returns {@code false}.
67 * @return {@code true} if the objects are the same;
68 * {@code true} if the objects have equivalent {@code value} fields;
69 * {@code false} otherwise.
70 */
71 @Override
72 public boolean equals(final Object obj) {
73 if (obj == null) {
74 return false;
75 }
76 if (this == obj) {
77 return true;
78 }
79 if (this.getClass() == obj.getClass()) {
80 final MutableObject<?> that = (MutableObject<?>) obj;
81 return Objects.equals(this.value, that.value);
82 }
83 return false;
84 }
85
86 /**
87 * Gets the value.
88 *
89 * @return the value, may be null.
90 * @deprecated Use {@link #get()}.
91 */
92 @Deprecated
93 @Override
94 public T getValue() {
95 return this.value;
96 }
97
98 /**
99 * Returns the value's hash code or {@code 0} if the value is {@code null}.
100 *
101 * @return the value's hash code or {@code 0} if the value is {@code null}.
102 */
103 @Override
104 public int hashCode() {
105 return Objects.hashCode(value);
106 }
107
108 /**
109 * Sets the value.
110 *
111 * @param value the value to set.
112 */
113 @Override
114 public void setValue(final T value) {
115 this.value = value;
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 Objects.toString(value);
126 }
127
128 }