    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 *      https://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;
    022import java.util.concurrent.atomic.AtomicReference;
    023
    024/**
    025 * A mutable {@link Object} wrapper.
    026 * <p>
    027 * This class was created before the introduction of {@link AtomicReference}.
    028 * </p>
    029 *
    030 * @param <T> the type to set and get.
    031 * @see AtomicReference
    032 * @since 2.1
    033 */
    034public class MutableObject<T> implements Mutable<T>, Serializable {
    035
    036    /**
    037     * Required for serialization support.
    038     *
    039     * @see java.io.Serializable
    040     */
    041    private static final long serialVersionUID = 86241875189L;
    042
    043    /** The mutable value. */
    044    private T value;
    045
    046    /**
    047     * Constructs a new MutableObject with the default value of {@code null}.
    048     */
    049    public MutableObject() {
    050    }
    051
    052    /**
    053     * Constructs a new MutableObject with the specified value.
    054     *
    055     * @param value  the initial value to store.
    056     */
    057    public MutableObject(final T value) {
    058        this.value = value;
    059    }
    060
    061    /**
    062     * Compares this object against the specified object. The result is {@code true} if and only if the argument
    063     * is not {@code null} and is a {@link MutableObject} object that contains the same {@link T}
    064     * value as this object.
    065     *
    066     * @param obj  the object to compare with, {@code null} returns {@code false}.
    067     * @return  {@code true} if the objects are the same;
    068     *          {@code true} if the objects have equivalent {@code value} fields;
    069     *          {@code false} otherwise.
    070     */
    071    @Override
    072    public boolean equals(final Object obj) {
    073        if (obj == null) {
    074            return false;
    075        }
    076        if (this == obj) {
    077            return true;
    078        }
    079        if (this.getClass() == obj.getClass()) {
    080            final MutableObject<?> that = (MutableObject<?>) obj;
    081            return Objects.equals(this.value, that.value);
    082        }
    083        return false;
    084    }
    085
    086    /**
    087     * Gets the value.
    088     *
    089     * @return the value, may be null.
    090     * @deprecated Use {@link #get()}.
    091     */
    092    @Deprecated
    093    @Override
    094    public T getValue() {
    095        return this.value;
    096    }
    097
    098    /**
    099     * Returns the value's hash code or {@code 0} if the value is {@code null}.
    100     *
    101     * @return the value's hash code or {@code 0} if the value is {@code null}.
    102     */
    103    @Override
    104    public int hashCode() {
    105        return Objects.hashCode(value);
    106    }
    107
    108    /**
    109     * Sets the value.
    110     *
    111     * @param value  the value to set.
    112     */
    113    @Override
    114    public void setValue(final T value) {
    115        this.value = value;
    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 Objects.toString(value);
    126    }
    127
    128}