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     * @version $Id: MutableObject.java 1088899 2011-04-05 05:31:27Z bayard $
027     */
028    public 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(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        public T getValue() {
064            return this.value;
065        }
066    
067        /**
068         * Sets the value.
069         * 
070         * @param value  the value to set
071         */
072        public void setValue(T value) {
073            this.value = value;
074        }
075    
076        //-----------------------------------------------------------------------
077        /**
078         * <p>
079         * Compares this object against the specified object. The result is <code>true</code> if and only if the argument
080         * is not <code>null</code> and is a <code>MutableObject</code> object that contains the same <code>T</code>
081         * value as this object.
082         * </p>
083         * 
084         * @param obj  the object to compare with, <code>null</code> returns <code>false</code>
085         * @return  <code>true</code> if the objects are the same;
086         *          <code>true</code> if the objects have equivalent <code>value</code> fields;
087         *          <code>false</code> otherwise.
088         */
089        @Override
090        public boolean equals(Object obj) {
091            if (obj == null) {
092                return false;
093            }
094            if (this == obj) {
095                return true;
096            }
097            if (this.getClass() == obj.getClass()) {
098                MutableObject<?> that = (MutableObject<?>) obj;
099                return this.value.equals(that.value);
100            } else {
101                return false;
102            }
103        }
104    
105        /**
106         * Returns the value's hash code or <code>0</code> if the value is <code>null</code>.
107         * 
108         * @return the value's hash code or <code>0</code> if the value is <code>null</code>.
109         */
110        @Override
111        public int hashCode() {
112            return value == null ? 0 : value.hashCode();
113        }
114    
115        //-----------------------------------------------------------------------
116        /**
117         * Returns the String value of this mutable.
118         * 
119         * @return the mutable value as a string
120         */
121        @Override
122        public String toString() {
123            return value == null ? "null" : value.toString();
124        }
125    
126    }