]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/type/OptionalType.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / type / OptionalType.java
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\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
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard.type;
13
14 import java.util.Set;\r
15 \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.ComponentReference;\r
19 import org.simantics.databoard.accessor.reference.IndexReference;\r
20 import org.simantics.databoard.accessor.reference.KeyReference;\r
21 import org.simantics.databoard.accessor.reference.LabelReference;\r
22 import org.simantics.databoard.accessor.reference.NameReference;\r
23 import org.simantics.databoard.util.IdentityPair;\r
24
25 public class OptionalType extends Datatype {
26     public Datatype componentType;
27     
28     public OptionalType() {     
29     }
30     
31     public OptionalType(Datatype componentType) {
32         this.componentType = componentType;
33     }
34     
35     public Datatype getComponentType() {
36         return componentType;
37     }\r
38     \r
39     @Override\r
40     public int getComponentCount() {\r
41         return 1;\r
42     }\r
43     \r
44     @Override\r
45     public Datatype getComponentType(int index) {\r
46         if (index!=0) throw new IllegalArgumentException();\r
47         return componentType;\r
48     }\r
49     \r
50     @Override\r
51     public Datatype getComponentType(ChildReference path) {\r
52         if (path==null) return this;\r
53         if (path instanceof KeyReference) throw new IllegalArgumentException("KeyReference is not supported in OptionalType"); \r
54         if (path instanceof NameReference) throw new IllegalArgumentException("NameReference is not supported in OptionalType"); \r
55         if (path instanceof IndexReference && ((IndexReference) path).index!=0) throw new IllegalArgumentException("Index out of bounds");\r
56         if (path instanceof LabelReference && !((LabelReference) path).label.equals("v")) throw new IllegalArgumentException("Unknown label");\r
57         return componentType.getComponentType(path.childReference);\r
58     }    
59     
60         @Override
61         protected void collectSubtypes(Set<Datatype> subtypes,
62                 Set<Datatype> recursiveSubtypes) {
63                 componentType.collectSubtypes(subtypes, recursiveSubtypes);
64         }
65     
66     @Override\r
67     public int hashCode() {\r
68         return componentType.hashCode() + metadataHashCode();\r
69     }\r
70     \r
71     @Override
72     protected boolean deepEquals(Object obj, Set<IdentityPair<Datatype, Datatype>> compareHistory) {
73                 if (this==obj) return true;\r
74                 if (obj instanceof OptionalType == false) return false;\r
75                 if ( !hasEqualMetadata(obj) ) return false;\r
76                 OptionalType other = (OptionalType) obj;
77                 return componentType.deepEquals(other.componentType, compareHistory);
78     }     
79
80         @Override
81         public void accept(Visitor1 v, Object obj) {
82             v.visit(this, obj);        
83         }
84
85         @Override
86         public <T> T accept(Visitor<T> v) {
87             return v.visit(this);
88         }       
89 \r
90         @SuppressWarnings("unchecked")\r
91         @Override\r
92         public <T extends Datatype> T getChildType(ChildReference reference) throws ReferenceException {\r
93                 if (reference==null) return (T) this;\r
94 \r
95                 if (reference instanceof LabelReference) {\r
96                         LabelReference lr = (LabelReference) reference;\r
97                         if (lr.label.equals("o")) {\r
98                                 return componentType.getChildType(reference.getChildReference());\r
99                         }                       \r
100                 }\r
101                 \r
102                 if (reference instanceof ComponentReference) {\r
103                         return componentType.getChildType(reference.getChildReference());\r
104                 } \r
105                 \r
106                 throw new ReferenceException(reference.getClass()+" is not a reference of OptionalType");               \r
107         }\r
108         
109 }