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.type;
16 import org.simantics.databoard.accessor.error.ReferenceException;
17 import org.simantics.databoard.accessor.reference.ChildReference;
18 import org.simantics.databoard.accessor.reference.IndexReference;
19 import org.simantics.databoard.accessor.reference.KeyReference;
20 import org.simantics.databoard.accessor.reference.LabelReference;
21 import org.simantics.databoard.accessor.reference.NameReference;
22 import org.simantics.databoard.util.IdentityPair;
23 import org.simantics.databoard.util.ObjectUtils;
25 public class MapType extends Datatype {
27 /** Key to describe if map is to be serialized ordered, the value is boolean value "false"/"true" */
28 public static final String KEY_ORDERED = "ordered";
30 public Datatype keyType;
31 public Datatype valueType;
36 public MapType(Datatype keyType, Datatype valueType) {
37 if (keyType==null || valueType==null)
38 throw new IllegalArgumentException("Argument must not be null");
39 this.keyType = keyType;
40 this.valueType = valueType;
44 public int getComponentCount() {
49 public Datatype getComponentType(int index) {
50 if (index==0) return keyType;
51 if (index==1) return valueType;
52 throw new IllegalArgumentException();
56 public Datatype getComponentType(ChildReference path) {
57 if (path==null) return this;
58 if (path instanceof IndexReference) {
59 IndexReference ir = (IndexReference) path;
60 if (ir.index==0) return keyType.getComponentType(path.childReference);
61 if (ir.index==1) return valueType.getComponentType(path.childReference);
63 if (path instanceof LabelReference) {
64 LabelReference lr = (LabelReference) path;
65 if (lr.label.equals("0") || lr.label.equals("key")) return keyType.getComponentType(path.childReference);
66 if (lr.label.equals("1") || lr.label.equals("value")) return valueType.getComponentType(path.childReference);
68 if (path instanceof NameReference) {
69 NameReference nr = (NameReference) path;
70 if (nr.name.equals("key")) return keyType.getComponentType(path.childReference);
71 if (nr.name.equals("value")) return valueType.getComponentType(path.childReference);
73 throw new IllegalArgumentException();
76 public void accept(Visitor1 v, Object obj) {
81 public <T> T accept(Visitor<T> v) {
86 protected boolean deepEquals(Object obj,
87 Set<IdentityPair<Datatype, Datatype>> compareHistory) {
88 if (this==obj) return true;
89 if ( !hasEqualMetadata(obj) ) return false;
90 if (obj instanceof MapType == false) return false;
91 MapType other = (MapType) obj;
93 return keyType.deepEquals(other.keyType, compareHistory) &&
94 valueType.deepEquals(other.valueType, compareHistory);
98 protected void collectSubtypes(Set<Datatype> subtypes, Set<Datatype> recursiveSubtypes) {
99 keyType.collectSubtypes(subtypes, recursiveSubtypes);
100 valueType.collectSubtypes(subtypes, recursiveSubtypes);
104 public int hashCode() {
105 if (keyType==this) return 0;
106 if (valueType==this) return 0;
108 ObjectUtils.hashCode(keyType) * 13 +
109 ObjectUtils.hashCode(valueType) * 17;
112 @SuppressWarnings("unchecked")
114 public <T extends Datatype> T getChildType(ChildReference reference) throws ReferenceException {
115 if (reference==null) return (T) this;
117 if (reference instanceof LabelReference) {
118 // LabelReference lr = (LabelReference) reference;
119 return keyType.getChildType(reference.getChildReference());
120 } else if (reference instanceof KeyReference) {
121 // KeyReference ref = (KeyReference) reference;
122 return keyType.getChildType(reference.getChildReference());
124 throw new ReferenceException(reference.getClass().getName()+" is not a reference of a map");