]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/type/MapType.java
Added addFirst/After/Before + remove SCL functions for Ordered Sets
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / type / MapType.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.type;
13
14 import java.util.Set;
15
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;
24
25 public class MapType extends Datatype {
26
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";
29         
30         public Datatype keyType;
31         public Datatype valueType;
32         
33         public MapType() {              
34         }
35         
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;
41         }
42         
43     @Override
44     public int getComponentCount() {
45         return 2;
46     }
47     
48     @Override
49     public Datatype getComponentType(int index) {
50         if (index==0) return keyType;
51         if (index==1) return valueType;
52         throw new IllegalArgumentException();
53     }
54     
55     @Override
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);
62         }
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);
67         }
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);
72         }
73         throw new IllegalArgumentException();
74     }   
75         @Override
76         public void accept(Visitor1 v, Object obj) {
77             v.visit(this, obj);        
78         }
79
80         @Override
81         public <T> T accept(Visitor<T> v) {
82             return v.visit(this);
83         }
84
85         @Override
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;
92                 
93                 return keyType.deepEquals(other.keyType, compareHistory) && 
94                         valueType.deepEquals(other.valueType, compareHistory);
95         }
96         
97         @Override
98         protected void collectSubtypes(Set<Datatype> subtypes, Set<Datatype> recursiveSubtypes) {
99                 keyType.collectSubtypes(subtypes, recursiveSubtypes);
100                 valueType.collectSubtypes(subtypes, recursiveSubtypes);
101         }
102         
103         @Override
104         public int hashCode() {
105                 if (keyType==this) return 0;
106                 if (valueType==this) return 0;
107                 return 0x232ae +
108                         ObjectUtils.hashCode(keyType) * 13 +
109                         ObjectUtils.hashCode(valueType) * 17;
110         }
111
112         @SuppressWarnings("unchecked")
113         @Override
114         public <T extends Datatype> T getChildType(ChildReference reference) throws ReferenceException {
115                 if (reference==null) return (T) this;
116                 
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());
123                 } 
124                 throw new ReferenceException(reference.getClass().getName()+" is not a reference of a map");    
125         }
126         
127 }
128