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.lang.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 905636 2010-02-02 14:03:32Z niallp $
028     */
029    public class MutableObject implements Mutable, 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 Object 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(Object 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 Object 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(Object value) {
074            this.value = value;
075        }
076    
077        //-----------------------------------------------------------------------
078        /**
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>Object</code>
081         * value as this object.
082         * 
083         * @param obj  the object to compare with, null returns false
084         * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
085         */
086        public boolean equals(Object obj) {
087            if (obj instanceof MutableObject) {
088                Object other = ((MutableObject) obj).value;
089                return value == other || (value != null && value.equals(other));
090            }
091            return false;
092        }
093    
094        /**
095         * Returns the value's hash code or <code>0</code> if the value is <code>null</code>.
096         * 
097         * @return the value's hash code or <code>0</code> if the value is <code>null</code>.
098         */
099        public int hashCode() {
100            return value == null ? 0 : value.hashCode();
101        }
102    
103        //-----------------------------------------------------------------------
104        /**
105         * Returns the String value of this mutable.
106         * 
107         * @return the mutable value as a string
108         */
109        public String toString() {
110            return value == null ? "null" : value.toString();
111        }
112    
113    }