]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/reference/KeyReference.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / reference / KeyReference.java
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\r
4  *  All rights reserved. This program and the accompanying materials\r
5  *  are made available under the terms of the Eclipse Public License v1.0\r
6  *  which accompanies this distribution, and is available at\r
7  *  http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard.accessor.reference;
13
14 import org.simantics.databoard.Bindings;\r
15 import org.simantics.databoard.adapter.Adapter;\r
16 import org.simantics.databoard.binding.Binding;\r
17 import org.simantics.databoard.binding.mutable.Variant;\r
18 import org.simantics.databoard.util.URIUtil;\r
19
20 /**
21  * Map Value
22  * "<key>/"
23  */
24 public class KeyReference extends ChildReference {
25 \r
26         static Adapter VARIANT_TO_STRING_ADAPTER = Bindings.getAdapterUnchecked(Bindings.VARIANT, Bindings.STR_VARIANT);\r
27         
28         public Variant key;
29         \r
30         public KeyReference() {\r
31         }\r
32         
33         public KeyReference(Binding keyBinding, Object key) {
34                 this.key = new Variant(keyBinding, key);
35         }
36         
37         public KeyReference(Variant key) {
38                 this.key = key.clone();
39         }
40         
41         public KeyReference(Variant key, ChildReference child) {
42                 super(child);
43                 this.key = key.clone();
44         }
45         
46         @Override\r
47         public String toString(boolean labelReference) {\r
48                 String strKey = (String) VARIANT_TO_STRING_ADAPTER.adaptUnchecked(key);\r
49                 return labelReference ? URIUtil.encodeURI( strKey ) : "k-"+strKey;\r
50         }\r
51
52         @Override
53         public ChildReference clone() {
54                 return new KeyReference(key.clone(), childReference==null ? null : childReference.clone());
55         }       
56         \r
57         @Override\r
58         public boolean equals(Object obj) {\r
59                 if (obj instanceof KeyReference == false) return false;\r
60                 KeyReference other = (KeyReference) obj;\r
61                 if (!other.key.equals(key)) return false;\r
62                 if (other.hasChildReference() != hasChildReference()) return false;\r
63                 if (hasChildReference() && !other.childReference.equals(childReference)) return false;\r
64                 return true;\r
65         }\r
66         \r
67         @Override\r
68         public int hashCode() {\r
69                 int hash = 61468996 + 37 * key.hashCode();\r
70                 if (hasChildReference()) hash = 31*hash + childReference.hashCode();            \r
71                 return hash;\r
72         }\r
73         \r
74         @Override\r
75         public String toString() {\r
76                 return "["+key+"]";\r
77         }\r
78         
79 }
80