001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.lang3.mutable;
019
020 import java.io.Serializable;
021
022 /**
023 * A mutable <code>Object</code> wrapper.
024 *
025 * @since 2.1
026 * @author Apache Software Foundation
027 * @version $Id: MutableObject.java 965162 2010-07-18 05:53:30Z bayard $
028 */
029 public class MutableObject<T> implements Mutable<T>, Serializable {
030
031 /**
032 * Required for serialization support.
033 *
034 * @see java.io.Serializable
035 */
036 private static final long serialVersionUID = 86241875189L;
037
038 /** The mutable value. */
039 private T value;
040
041 /**
042 * Constructs a new MutableObject with the default value of <code>null</code>.
043 */
044 public MutableObject() {
045 super();
046 }
047
048 /**
049 * Constructs a new MutableObject with the specified value.
050 *
051 * @param value the initial value to store
052 */
053 public MutableObject(T value) {
054 super();
055 this.value = value;
056 }
057
058 //-----------------------------------------------------------------------
059 /**
060 * Gets the value.
061 *
062 * @return the value, may be null
063 */
064 public T getValue() {
065 return this.value;
066 }
067
068 /**
069 * Sets the value.
070 *
071 * @param value the value to set
072 */
073 public void setValue(T value) {
074 this.value = value;
075 }
076
077 //-----------------------------------------------------------------------
078 /**
079 * <p>
080 * Compares this object against the specified object. The result is <code>true</code> if and only if the argument
081 * is not <code>null</code> and is a <code>MutableObject</code> object that contains the same <code>T</code>
082 * value as this object.
083 * </p>
084 *
085 * @param obj the object to compare with, <code>null</code> returns <code>false</code>
086 * @return <code>true</code> if the objects are the same;
087 * <code>true</code> if the objects have equivalent <code>value</code> fields;
088 * <code>false</code> otherwise.
089 */
090 @SuppressWarnings("unchecked")
091 @Override
092 public boolean equals(Object obj) {
093 if (obj == null) {
094 return false;
095 }
096 if (this == obj) {
097 return true;
098 }
099 if (this.getClass() == obj.getClass()) {
100 MutableObject<T> that = (MutableObject<T>) obj;
101 return this.value.equals(that.value);
102 }
103 else {
104 return false;
105 }
106 }
107
108 /**
109 * Returns the value's hash code or <code>0</code> if the value is <code>null</code>.
110 *
111 * @return the value's hash code or <code>0</code> if the value is <code>null</code>.
112 */
113 @Override
114 public int hashCode() {
115 return value == null ? 0 : value.hashCode();
116 }
117
118 //-----------------------------------------------------------------------
119 /**
120 * Returns the String value of this mutable.
121 *
122 * @return the mutable value as a string
123 */
124 @Override
125 public String toString() {
126 return value == null ? "null" : value.toString();
127 }
128
129 }