]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/reference/KeyReference.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/reference/KeyReference.java
new file mode 100644 (file)
index 0000000..b550df5
--- /dev/null
@@ -0,0 +1,80 @@
+/*******************************************************************************\r
+ *  Copyright (c) 2010 Association for Decentralized Information Management in\r
+ *  Industry THTH ry.\r
+ *  All rights reserved. This program and the accompanying materials\r
+ *  are made available under the terms of the Eclipse Public License v1.0\r
+ *  which accompanies this distribution, and is available at\r
+ *  http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ *  Contributors:\r
+ *      VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.databoard.accessor.reference;
+
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.adapter.Adapter;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.databoard.binding.mutable.Variant;\r
+import org.simantics.databoard.util.URIUtil;\r
+
+/**
+ * Map Value
+ * "<key>/"
+ */
+public class KeyReference extends ChildReference {
+\r
+       static Adapter VARIANT_TO_STRING_ADAPTER = Bindings.getAdapterUnchecked(Bindings.VARIANT, Bindings.STR_VARIANT);\r
+       
+       public Variant key;
+       \r
+       public KeyReference() {\r
+       }\r
+       
+       public KeyReference(Binding keyBinding, Object key) {
+               this.key = new Variant(keyBinding, key);
+       }
+       
+       public KeyReference(Variant key) {
+               this.key = key.clone();
+       }
+       
+       public KeyReference(Variant key, ChildReference child) {
+               super(child);
+               this.key = key.clone();
+       }
+       
+       @Override\r
+       public String toString(boolean labelReference) {\r
+               String strKey = (String) VARIANT_TO_STRING_ADAPTER.adaptUnchecked(key);\r
+               return labelReference ? URIUtil.encodeURI( strKey ) : "k-"+strKey;\r
+       }\r
+
+       @Override
+       public ChildReference clone() {
+               return new KeyReference(key.clone(), childReference==null ? null : childReference.clone());
+       }       
+       \r
+       @Override\r
+       public boolean equals(Object obj) {\r
+               if (obj instanceof KeyReference == false) return false;\r
+               KeyReference other = (KeyReference) obj;\r
+               if (!other.key.equals(key)) return false;\r
+               if (other.hasChildReference() != hasChildReference()) return false;\r
+               if (hasChildReference() && !other.childReference.equals(childReference)) return false;\r
+               return true;\r
+       }\r
+       \r
+       @Override\r
+       public int hashCode() {\r
+               int hash = 61468996 + 37 * key.hashCode();\r
+               if (hasChildReference()) hash = 31*hash + childReference.hashCode();            \r
+               return hash;\r
+       }\r
+       \r
+       @Override\r
+       public String toString() {\r
+               return "["+key+"]";\r
+       }\r
+       
+}
+