    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     * Gets the value.
    058     *
    059     * @return the value, may be null
    060     */
    061    @Override
    062    public T getValue() {
    063        return this.value;
    064    }
    065
    066    /**
    067     * Sets the value.
    068     *
    069     * @param value  the value to set
    070     */
    071    @Override
    072    public void setValue(final T value) {
    073        this.value = value;
    074    }
    075
    076    /**
    077     * Compares this object against the specified object. The result is {@code true} if and only if the argument
    078     * is not {@code null} and is a {@link MutableObject} object that contains the same {@link T}
    079     * value as this object.
    080     *
    081     * @param obj  the object to compare with, {@code null} returns {@code false}
    082     * @return  {@code true} if the objects are the same;
    083     *          {@code true} if the objects have equivalent {@code value} fields;
    084     *          {@code false} otherwise.
    085     */
    086    @Override
    087    public boolean equals(final Object obj) {
    088        if (obj == null) {
    089            return false;
    090        }
    091        if (this == obj) {
    092            return true;
    093        }
    094        if (this.getClass() == obj.getClass()) {
    095            final MutableObject<?> that = (MutableObject<?>) obj;
    096            return Objects.equals(this.value, that.value);
    097        }
    098        return false;
    099    }
    100
    101    /**
    102     * Returns the value's hash code or {@code 0} if the value is {@code null}.
    103     *
    104     * @return the value's hash code or {@code 0} if the value is {@code null}.
    105     */
    106    @Override
    107    public int hashCode() {
    108        return Objects.hashCode(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}