KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > mutable > MutableObject


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

16
17 package org.apache.commons.lang.mutable;
18
19 import java.io.Serializable JavaDoc;
20
21 /**
22  * A mutable <code>Object</code> wrapper.
23  *
24  * @since 2.1
25  * @version $Id: MutableObject.java 161243 2005-04-14 04:30:28Z ggregory $
26  */

27 public class MutableObject implements Mutable, Serializable JavaDoc {
28
29     /** Serialization lock. */
30     private static final long serialVersionUID = 86241875189L;
31
32     /** The mutable value. */
33     private Object JavaDoc value;
34
35     /**
36      * Constructs a new MutableObject with the default value of <code>null</code>.
37      */

38     public MutableObject() {
39         super();
40     }
41
42     /**
43      * Constructs a new MutableObject with the specified value.
44      *
45      * @param value
46      * a value.
47      */

48     public MutableObject(Object JavaDoc value) {
49         super();
50         this.value = value;
51     }
52
53     //-----------------------------------------------------------------------
54
/**
55      * Gets the value.
56      *
57      * @return the value
58      */

59     public Object JavaDoc getValue() {
60         return this.value;
61     }
62
63     /**
64      * Sets the value.
65      *
66      * @param value
67      * the value to set
68      */

69     public void setValue(Object JavaDoc value) {
70         this.value = value;
71     }
72
73     //-----------------------------------------------------------------------
74
/**
75      * Compares this object against the specified object. The result is <code>true</code> if and only if the argument
76      * is not <code>null</code> and is a <code>MutableObject</code> object that contains the same <code>Object</code>
77      * value as this object.
78      *
79      * @param obj
80      * the object to compare with.
81      * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
82      */

83     public boolean equals(Object JavaDoc obj) {
84         if (obj instanceof MutableObject) {
85             Object JavaDoc other = ((MutableObject) obj).value;
86             return value == other || (value != null && value.equals(other));
87         }
88         return false;
89     }
90
91     /**
92      * Returns the value's hash code or <code>0</code> if the value is <code>null</code>.
93      *
94      * @return the value's hash code or <code>0</code> if the value is <code>null</code>.
95      */

96     public int hashCode() {
97         return value == null ? 0 : value.hashCode();
98     }
99
100     /**
101      * Returns the String value of this mutable.
102      *
103      * @return the mutable value as a string
104      */

105     public String JavaDoc toString() {
106         return value == null ? "null" : value.toString();
107     }
108
109 }
110
Popular Tags