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; 021 022/** 023 * A mutable {@code Object} wrapper. 024 * 025 * @param <T> the type to set and get 026 * @since 2.1 027 */ 028public class MutableObject<T> implements Mutable<T>, Serializable { 029 030 /** 031 * Required for serialization support. 032 * 033 * @see java.io.Serializable 034 */ 035 private static final long serialVersionUID = 86241875189L; 036 037 /** The mutable value. */ 038 private T value; 039 040 /** 041 * Constructs a new MutableObject with the default value of {@code null}. 042 */ 043 public MutableObject() { 044 } 045 046 /** 047 * Constructs a new MutableObject with the specified value. 048 * 049 * @param value the initial value to store 050 */ 051 public MutableObject(final T value) { 052 this.value = value; 053 } 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 /** 078 * <p> 079 * Compares this object against the specified object. The result is {@code true} if and only if the argument 080 * is not {@code null} and is a {@code MutableObject} object that contains the same {@code T} 081 * value as this object. 082 * </p> 083 * 084 * @param obj the object to compare with, {@code null} returns {@code false} 085 * @return {@code true} if the objects are the same; 086 * {@code true} if the objects have equivalent {@code value} fields; 087 * {@code false} otherwise. 088 */ 089 @Override 090 public boolean equals(final Object obj) { 091 if (obj == null) { 092 return false; 093 } 094 if (this == obj) { 095 return true; 096 } 097 if (this.getClass() == obj.getClass()) { 098 final MutableObject<?> that = (MutableObject<?>) obj; 099 return this.value.equals(that.value); 100 } 101 return false; 102 } 103 104 /** 105 * Returns the value's hash code or {@code 0} if the value is {@code null}. 106 * 107 * @return the value's hash code or {@code 0} if the value is {@code null}. 108 */ 109 @Override 110 public int hashCode() { 111 return value == null ? 0 : value.hashCode(); 112 } 113 114 //----------------------------------------------------------------------- 115 /** 116 * Returns the String value of this mutable. 117 * 118 * @return the mutable value as a string 119 */ 120 @Override 121 public String toString() { 122 return value == null ? "null" : value.toString(); 123 } 124 125}