1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.accessor.reference;
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;
24 public class KeyReference extends ChildReference {
26 static Adapter VARIANT_TO_STRING_ADAPTER = Bindings.getAdapterUnchecked(Bindings.VARIANT, Bindings.STR_VARIANT);
30 public KeyReference() {
33 public KeyReference(Binding keyBinding, Object key) {
34 this.key = new Variant(keyBinding, key);
37 public KeyReference(Variant key) {
38 this.key = key.clone();
41 public KeyReference(Variant key, ChildReference child) {
43 this.key = key.clone();
47 public String toString(boolean labelReference) {
48 String strKey = (String) VARIANT_TO_STRING_ADAPTER.adaptUnchecked(key);
49 return labelReference ? URIUtil.encodeURI( strKey ) : "k-"+strKey;
53 public ChildReference clone() {
54 return new KeyReference(key.clone(), childReference==null ? null : childReference.clone());
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;
68 public int hashCode() {
69 int hash = 61468996 + 37 * key.hashCode();
70 if (hasChildReference()) hash = 31*hash + childReference.hashCode();
75 public String toString() {