]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/type/MapType.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / type / MapType.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/type/MapType.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/type/MapType.java
new file mode 100644 (file)
index 0000000..4fede9c
--- /dev/null
@@ -0,0 +1,128 @@
+/*******************************************************************************\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.type;
+
+import java.util.Set;\r
+\r
+import org.simantics.databoard.accessor.error.ReferenceException;\r
+import org.simantics.databoard.accessor.reference.ChildReference;\r
+import org.simantics.databoard.accessor.reference.IndexReference;\r
+import org.simantics.databoard.accessor.reference.KeyReference;\r
+import org.simantics.databoard.accessor.reference.LabelReference;\r
+import org.simantics.databoard.accessor.reference.NameReference;\r
+import org.simantics.databoard.util.IdentityPair;\r
+import org.simantics.databoard.util.ObjectUtils;\r
+
+public class MapType extends Datatype {
+\r
+       /** Key to describe if map is to be serialized ordered, the value is boolean value "false"/"true" */
+       public static final String KEY_ORDERED = "ordered";\r
+       \r
+       public Datatype keyType;
+       public Datatype valueType;\r
+       
+       public MapType() {              
+       }
+       
+       public MapType(Datatype keyType, Datatype valueType) {
+               if (keyType==null || valueType==null)
+                       throw new IllegalArgumentException("Argument must not be null");
+               this.keyType = keyType;
+               this.valueType = valueType;
+       }
+       \r
+    @Override\r
+    public int getComponentCount() {\r
+       return 2;\r
+    }\r
+    \r
+    @Override\r
+    public Datatype getComponentType(int index) {\r
+       if (index==0) return keyType;\r
+       if (index==1) return valueType;\r
+       throw new IllegalArgumentException();\r
+    }\r
+    \r
+    @Override\r
+    public Datatype getComponentType(ChildReference path) {\r
+       if (path==null) return this;\r
+       if (path instanceof IndexReference) {\r
+               IndexReference ir = (IndexReference) path;\r
+               if (ir.index==0) return keyType.getComponentType(path.childReference);\r
+               if (ir.index==1) return valueType.getComponentType(path.childReference);\r
+       }\r
+       if (path instanceof LabelReference) {\r
+               LabelReference lr = (LabelReference) path;\r
+               if (lr.label.equals("0") || lr.label.equals("key")) return keyType.getComponentType(path.childReference);\r
+               if (lr.label.equals("1") || lr.label.equals("value")) return valueType.getComponentType(path.childReference);\r
+       }\r
+       if (path instanceof NameReference) {\r
+               NameReference nr = (NameReference) path;\r
+               if (nr.name.equals("key")) return keyType.getComponentType(path.childReference);\r
+               if (nr.name.equals("value")) return valueType.getComponentType(path.childReference);\r
+       }\r
+       throw new IllegalArgumentException();\r
+    }  
+       @Override
+       public void accept(Visitor1 v, Object obj) {
+           v.visit(this, obj);        
+       }
+
+       @Override
+       public <T> T accept(Visitor<T> v) {
+           return v.visit(this);
+       }
+
+       @Override
+       protected boolean deepEquals(Object obj,
+                       Set<IdentityPair<Datatype, Datatype>> compareHistory) {         
+               if (this==obj) return true;\r
+               if ( !hasEqualMetadata(obj) ) return false;\r
+               if (obj instanceof MapType == false) return false;
+               MapType other = (MapType) obj;
+               
+               return keyType.deepEquals(other.keyType, compareHistory) && 
+                       valueType.deepEquals(other.valueType, compareHistory);
+       }
+       
+       @Override
+       protected void collectSubtypes(Set<Datatype> subtypes, Set<Datatype> recursiveSubtypes) {
+               keyType.collectSubtypes(subtypes, recursiveSubtypes);
+               valueType.collectSubtypes(subtypes, recursiveSubtypes);
+       }
+       
+       @Override
+       public int hashCode() {
+               if (keyType==this) return 0;
+               if (valueType==this) return 0;
+               return 0x232ae +
+                       ObjectUtils.hashCode(keyType) * 13 +
+                       ObjectUtils.hashCode(valueType) * 17;
+       }
+\r
+       @SuppressWarnings("unchecked")\r
+       @Override\r
+       public <T extends Datatype> T getChildType(ChildReference reference) throws ReferenceException {\r
+               if (reference==null) return (T) this;\r
+               \r
+               if (reference instanceof LabelReference) {\r
+//                     LabelReference lr = (LabelReference) reference;\r
+                       return keyType.getChildType(reference.getChildReference());\r
+               } else if (reference instanceof KeyReference) {\r
+//                     KeyReference ref = (KeyReference) reference;\r
+                       return keyType.getChildType(reference.getChildReference());\r
+               } \r
+               throw new ReferenceException(reference.getClass().getName()+" is not a reference of a map");    \r
+       }\r
+       
+}
+