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
018package org.apache.commons.lang3.mutable;
019
020import java.io.Serializable;
021
022/**
023 * A mutable <code>Object</code> wrapper.
024 * 
025 * @param <T> the type to set and get 
026 * @since 2.1
027 * @version $Id: MutableObject.java 1562971 2014-01-30 21:23:18Z ggregory $
028 */
029public 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(final 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    @Override
065    public T getValue() {
066        return this.value;
067    }
068
069    /**
070     * Sets the value.
071     * 
072     * @param value  the value to set
073     */
074    @Override
075    public void setValue(final T value) {
076        this.value = value;
077    }
078
079    //-----------------------------------------------------------------------
080    /**
081     * <p>
082     * Compares this object against the specified object. The result is <code>true</code> if and only if the argument
083     * is not <code>null</code> and is a <code>MutableObject</code> object that contains the same <code>T</code>
084     * value as this object.
085     * </p>
086     * 
087     * @param obj  the object to compare with, <code>null</code> returns <code>false</code>
088     * @return  <code>true</code> if the objects are the same;
089     *          <code>true</code> if the objects have equivalent <code>value</code> fields;
090     *          <code>false</code> otherwise.
091     */
092    @Override
093    public boolean equals(final Object obj) {
094        if (obj == null) {
095            return false;
096        }
097        if (this == obj) {
098            return true;
099        }
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}