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 018 package org.apache.commons.lang.mutable; 019 020 import java.io.Serializable; 021 022 /** 023 * A mutable <code>Object</code> wrapper. 024 * 025 * @since 2.1 026 * @version $Id: MutableObject.java 437554 2006-08-28 06:21:41Z bayard $ 027 */ 028 public class MutableObject implements Mutable, 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 Object value; 039 040 /** 041 * Constructs a new MutableObject with the default value of <code>null</code>. 042 */ 043 public MutableObject() { 044 super(); 045 } 046 047 /** 048 * Constructs a new MutableObject with the specified value. 049 * 050 * @param value 051 * a value. 052 */ 053 public MutableObject(Object value) { 054 super(); 055 this.value = value; 056 } 057 058 //----------------------------------------------------------------------- 059 /** 060 * Gets the value. 061 * 062 * @return the value 063 */ 064 public Object getValue() { 065 return this.value; 066 } 067 068 /** 069 * Sets the value. 070 * 071 * @param value 072 * the value to set 073 */ 074 public void setValue(Object value) { 075 this.value = value; 076 } 077 078 //----------------------------------------------------------------------- 079 /** 080 * Compares this object against the specified object. The result is <code>true</code> if and only if the argument 081 * is not <code>null</code> and is a <code>MutableObject</code> object that contains the same <code>Object</code> 082 * value as this object. 083 * 084 * @param obj 085 * the object to compare with. 086 * @return <code>true</code> if the objects are the same; <code>false</code> otherwise. 087 */ 088 public boolean equals(Object obj) { 089 if (obj instanceof MutableObject) { 090 Object other = ((MutableObject) obj).value; 091 return value == other || (value != null && value.equals(other)); 092 } 093 return false; 094 } 095 096 /** 097 * Returns the value's hash code or <code>0</code> if the value is <code>null</code>. 098 * 099 * @return the value's hash code or <code>0</code> if the value is <code>null</code>. 100 */ 101 public int hashCode() { 102 return value == null ? 0 : value.hashCode(); 103 } 104 105 /** 106 * Returns the String value of this mutable. 107 * 108 * @return the mutable value as a string 109 */ 110 public String toString() { 111 return value == null ? "null" : value.toString(); 112 } 113 114 }