View Javadoc

1   /*
2      Copyright 2007 Ramon Servadei
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  package fulmine.util.reference;
17  
18  /**
19   * Provides a reference to a value. The reference is passed around and can
20   * 'point' to a new value via the {@link #set(Object)} method.
21   * 
22   * @author Ramon Servadei
23   */
24  public final class Value<T>
25  {
26      /** The value this instance wraps */
27      private T value;
28  
29      public Value()
30      {
31          super();
32      }
33  
34      public Value(T value)
35      {
36          super();
37          set(value);
38      }
39  
40      public T set(T value)
41      {
42          this.value = value;
43          return this.value;
44      }
45  
46      public T get()
47      {
48          return this.value;
49      }
50  
51      @Override
52      public String toString()
53      {
54          return this.value == null ? "null" : this.value.toString();
55      }
56  
57      @Override
58      public int hashCode()
59      {
60          final int prime = 31;
61          int result = 1;
62          result = prime * result + ((value == null) ? 0 : value.hashCode());
63          return result;
64      }
65  
66      @SuppressWarnings("unchecked")
67      @Override
68      public boolean equals(Object obj)
69      {
70          if (is.same(this, obj))
71          {
72              return true;
73          }
74          if (is.differentClass(this, obj))
75          {
76              return false;
77          }
78          final Value other = (Value) obj;
79          return is.eq(get(), other.get());
80      }
81  }