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