    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;
    021import java.util.Objects;
    022
    023/**
    024 * A mutable {@link Object} wrapper.
    025 *
    026 * @param <T> the type to set and get
    027 * @since 2.1
    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}.
    043     */
    044    public MutableObject() {
    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        this.value = value;
    054    }
    055
    056    /**
    057     * Compares this object against the specified object. The result is {@code true} if and only if the argument
    058     * is not {@code null} and is a {@link MutableObject} object that contains the same {@link T}
    059     * value as this object.
    060     *
    061     * @param obj  the object to compare with, {@code null} returns {@code false}
    062     * @return  {@code true} if the objects are the same;
    063     *          {@code true} if the objects have equivalent {@code value} fields;
    064     *          {@code false} otherwise.
    065     */
    066    @Override
    067    public boolean equals(final Object obj) {
    068        if (obj == null) {
    069            return false;
    070        }
    071        if (this == obj) {
    072            return true;
    073        }
    074        if (this.getClass() == obj.getClass()) {
    075            final MutableObject<?> that = (MutableObject<?>) obj;
    076            return Objects.equals(this.value, that.value);
    077        }
    078        return false;
    079    }
    080
    081    /**
    082     * Gets the value.
    083     *
    084     * @return the value, may be null
    085     */
    086    @Override
    087    public T getValue() {
    088        return this.value;
    089    }
    090
    091    /**
    092     * Returns the value's hash code or {@code 0} if the value is {@code null}.
    093     *
    094     * @return the value's hash code or {@code 0} if the value is {@code null}.
    095     */
    096    @Override
    097    public int hashCode() {
    098        return Objects.hashCode(value);
    099    }
    100
    101    /**
    102     * Sets the value.
    103     *
    104     * @param value  the value to set
    105     */
    106    @Override
    107    public void setValue(final T value) {
    108        this.value = value;
    109    }
    110
    111    /**
    112     * Returns the String value of this mutable.
    113     *
    114     * @return the mutable value as a string
    115     */
    116    @Override
    117    public String toString() {
    118        return Objects.toString(value);
    119    }
    120
    121}