View Javadoc

1   package org.sourceforge.jemm.collections.internal;
2   
3   import java.io.Serializable;
4   
5   import org.sourceforge.jemm.client.shared.Encodable;
6   import org.sourceforge.jemm.client.shared.ValueEncoder;
7   import org.sourceforge.jemm.lifecycle.ValueVisitor;
8   import org.sourceforge.jemm.lifecycle.Visitable;
9   
10  /** 
11   * A encodable value that carries its hashcode outside itself.
12   * 
13   * @author Rory Graves
14   */
15  public class StoredValue implements Serializable,Encodable<StoredValue>,Visitable {
16  	private static final long serialVersionUID = 1L;
17  
18  	private final int hashCode;
19  	private final Object value;
20  		
21  	public StoredValue(Object value) {
22  		if(value == null) {
23  			this.hashCode = 0;
24  			this.value = null;
25  		} else {
26  			this.hashCode = value.hashCode();
27  			this.value = value; 			
28  		}
29  	}
30  	
31  	public StoredValue(int hashCode,Object value) {
32  		this.hashCode = hashCode;
33  		this.value = value;
34  	}
35  
36  	@Override
37  	public StoredValue encode(ValueEncoder encoder) {
38  		return new StoredValue(hashCode,encoder.encode(value));
39  	}
40  
41  	public int getHashCode() {
42  		return hashCode;
43  	}
44  
45  	public Object getValue() {
46  		return value;
47  	}
48  	
49  	@Override
50  	public int hashCode() {
51  		return hashCode;
52  	}
53  
54  	@Override
55  	public boolean equals(Object obj) {
56  		if (this == obj)
57  			return true;
58  		if (obj == null)
59  			return false;
60  		if (getClass() != obj.getClass())
61  			return false;
62  		StoredValue other = (StoredValue) obj;
63  		if (hashCode != other.hashCode)
64  			return false;
65  		if(value == null)
66  			return other.value == null;
67  		else
68  			return value.equals(other.value);
69  	}
70  	
71  	@Override public String toString() {
72  		return "StoredValue(" + hashCode + "," + value + ")";
73  	}
74  
75  	@Override
76  	public void visit(ValueVisitor visitor) {
77  		visitor.visit(value);
78  	}
79  
80  	public boolean isPotentialMatch(StoredValue otherValue) {
81  		return otherValue.hashCode == this.hashCode;
82  	}
83  }