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 */
028public class MutableObject<T> implements Mutable<T>, Serializable {
029
030    /**
031     * Required for serialization support.
032     * 
033     * @see java.io.Serializable
034     */
035    private static final long serialVersionUID = 86241875189L;
036
037    /** The mutable value. */
038    private T value;
039
040    /**
041     * Constructs a new MutableObject with the default value of <code>null</code>.
042     */
043    public MutableObject() {
044        super();
045    }
046
047    /**
048     * Constructs a new MutableObject with the specified value.
049     * 
050     * @param value  the initial value to store
051     */
052    public MutableObject(final T value) {
053        super();
054        this.value = value;
055    }
056
057    //-----------------------------------------------------------------------
058    /**
059     * Gets the value.
060     * 
061     * @return the value, may be null
062     */
063    @Override
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    @Override
074    public void setValue(final T value) {
075        this.value = value;
076    }
077
078    //-----------------------------------------------------------------------
079    /**
080     * <p>
081     * Compares this object against the specified object. The result is <code>true</code> if and only if the argument
082     * is not <code>null</code> and is a <code>MutableObject</code> object that contains the same <code>T</code>
083     * value as this object.
084     * </p>
085     * 
086     * @param obj  the object to compare with, <code>null</code> returns <code>false</code>
087     * @return  <code>true</code> if the objects are the same;
088     *          <code>true</code> if the objects have equivalent <code>value</code> fields;
089     *          <code>false</code> otherwise.
090     */
091    @Override
092    public boolean equals(final 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            final MutableObject<?> that = (MutableObject<?>) obj;
101            return this.value.equals(that.value);
102        }
103        return false;
104    }
105
106    /**
107     * Returns the value's hash code or <code>0</code> if the value is <code>null</code>.
108     * 
109     * @return the value's hash code or <code>0</code> if the value is <code>null</code>.
110     */
111    @Override
112    public int hashCode() {
113        return value == null ? 0 : value.hashCode();
114    }
115
116    //-----------------------------------------------------------------------
117    /**
118     * Returns the String value of this mutable.
119     * 
120     * @return the mutable value as a string
121     */
122    @Override
123    public String toString() {
124        return value == null ? "null" : value.toString();
125    }
126
127}