1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.databoard.type;
14 import java.util.Set;
\r
16 import org.simantics.databoard.accessor.error.ReferenceException;
\r
17 import org.simantics.databoard.accessor.reference.ChildReference;
\r
18 import org.simantics.databoard.accessor.reference.IndexReference;
\r
19 import org.simantics.databoard.accessor.reference.KeyReference;
\r
20 import org.simantics.databoard.accessor.reference.LabelReference;
\r
21 import org.simantics.databoard.accessor.reference.NameReference;
\r
22 import org.simantics.databoard.util.IdentityPair;
\r
23 import org.simantics.databoard.util.ObjectUtils;
\r
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";
\r
30 public Datatype keyType;
31 public Datatype valueType;
\r
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() {
\r
49 public Datatype getComponentType(int index) {
\r
50 if (index==0) return keyType;
\r
51 if (index==1) return valueType;
\r
52 throw new IllegalArgumentException();
\r
56 public Datatype getComponentType(ChildReference path) {
\r
57 if (path==null) return this;
\r
58 if (path instanceof IndexReference) {
\r
59 IndexReference ir = (IndexReference) path;
\r
60 if (ir.index==0) return keyType.getComponentType(path.childReference);
\r
61 if (ir.index==1) return valueType.getComponentType(path.childReference);
\r
63 if (path instanceof LabelReference) {
\r
64 LabelReference lr = (LabelReference) path;
\r
65 if (lr.label.equals("0") || lr.label.equals("key")) return keyType.getComponentType(path.childReference);
\r
66 if (lr.label.equals("1") || lr.label.equals("value")) return valueType.getComponentType(path.childReference);
\r
68 if (path instanceof NameReference) {
\r
69 NameReference nr = (NameReference) path;
\r
70 if (nr.name.equals("key")) return keyType.getComponentType(path.childReference);
\r
71 if (nr.name.equals("value")) return valueType.getComponentType(path.childReference);
\r
73 throw new IllegalArgumentException();
\r
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;
\r
89 if ( !hasEqualMetadata(obj) ) return false;
\r
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")
\r
114 public <T extends Datatype> T getChildType(ChildReference reference) throws ReferenceException {
\r
115 if (reference==null) return (T) this;
\r
117 if (reference instanceof LabelReference) {
\r
118 // LabelReference lr = (LabelReference) reference;
\r
119 return keyType.getChildType(reference.getChildReference());
\r
120 } else if (reference instanceof KeyReference) {
\r
121 // KeyReference ref = (KeyReference) reference;
\r
122 return keyType.getChildType(reference.getChildReference());
\r
124 throw new ReferenceException(reference.getClass().getName()+" is not a reference of a map");
\r