MutableObject.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.lang3.mutable;

  18. import java.io.Serializable;
  19. import java.util.Objects;

  20. /**
  21.  * A mutable {@link Object} wrapper.
  22.  *
  23.  * @param <T> the type to set and get
  24.  * @since 2.1
  25.  */
  26. public class MutableObject<T> implements Mutable<T>, Serializable {

  27.     /**
  28.      * Required for serialization support.
  29.      *
  30.      * @see java.io.Serializable
  31.      */
  32.     private static final long serialVersionUID = 86241875189L;

  33.     /** The mutable value. */
  34.     private T value;

  35.     /**
  36.      * Constructs a new MutableObject with the default value of {@code null}.
  37.      */
  38.     public MutableObject() {
  39.     }

  40.     /**
  41.      * Constructs a new MutableObject with the specified value.
  42.      *
  43.      * @param value  the initial value to store
  44.      */
  45.     public MutableObject(final T value) {
  46.         this.value = value;
  47.     }

  48.     /**
  49.      * Compares this object against the specified object. The result is {@code true} if and only if the argument
  50.      * is not {@code null} and is a {@link MutableObject} object that contains the same {@link T}
  51.      * value as this object.
  52.      *
  53.      * @param obj  the object to compare with, {@code null} returns {@code false}
  54.      * @return  {@code true} if the objects are the same;
  55.      *          {@code true} if the objects have equivalent {@code value} fields;
  56.      *          {@code false} otherwise.
  57.      */
  58.     @Override
  59.     public boolean equals(final Object obj) {
  60.         if (obj == null) {
  61.             return false;
  62.         }
  63.         if (this == obj) {
  64.             return true;
  65.         }
  66.         if (this.getClass() == obj.getClass()) {
  67.             final MutableObject<?> that = (MutableObject<?>) obj;
  68.             return Objects.equals(this.value, that.value);
  69.         }
  70.         return false;
  71.     }

  72.     /**
  73.      * Gets the value.
  74.      *
  75.      * @return the value, may be null
  76.      */
  77.     @Override
  78.     public T getValue() {
  79.         return this.value;
  80.     }

  81.     /**
  82.      * Returns the value's hash code or {@code 0} if the value is {@code null}.
  83.      *
  84.      * @return the value's hash code or {@code 0} if the value is {@code null}.
  85.      */
  86.     @Override
  87.     public int hashCode() {
  88.         return Objects.hashCode(value);
  89.     }

  90.     /**
  91.      * Sets the value.
  92.      *
  93.      * @param value  the value to set
  94.      */
  95.     @Override
  96.     public void setValue(final T value) {
  97.         this.value = value;
  98.     }

  99.     /**
  100.      * Returns the String value of this mutable.
  101.      *
  102.      * @return the mutable value as a string
  103.      */
  104.     @Override
  105.     public String toString() {
  106.         return Objects.toString(value);
  107.     }

  108. }