]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/type/OptionalType.java
Added addFirst/After/Before + remove SCL functions for Ordered Sets
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / type / OptionalType.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.ComponentReference;
19 import org.simantics.databoard.accessor.reference.IndexReference;
20 import org.simantics.databoard.accessor.reference.KeyReference;
21 import org.simantics.databoard.accessor.reference.LabelReference;
22 import org.simantics.databoard.accessor.reference.NameReference;
23 import org.simantics.databoard.util.IdentityPair;
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     }
38     
39     @Override
40     public int getComponentCount() {
41         return 1;
42     }
43     
44     @Override
45     public Datatype getComponentType(int index) {
46         if (index!=0) throw new IllegalArgumentException();
47         return componentType;
48     }
49     
50     @Override
51     public Datatype getComponentType(ChildReference path) {
52         if (path==null) return this;
53         if (path instanceof KeyReference) throw new IllegalArgumentException("KeyReference is not supported in OptionalType"); 
54         if (path instanceof NameReference) throw new IllegalArgumentException("NameReference is not supported in OptionalType"); 
55         if (path instanceof IndexReference && ((IndexReference) path).index!=0) throw new IllegalArgumentException("Index out of bounds");
56         if (path instanceof LabelReference && !((LabelReference) path).label.equals("v")) throw new IllegalArgumentException("Unknown label");
57         return componentType.getComponentType(path.childReference);
58     }    
59     
60         @Override
61         protected void collectSubtypes(Set<Datatype> subtypes,
62                 Set<Datatype> recursiveSubtypes) {
63                 componentType.collectSubtypes(subtypes, recursiveSubtypes);
64         }
65     
66     @Override
67     public int hashCode() {
68         return componentType.hashCode() + metadataHashCode();
69     }
70     
71     @Override
72     protected boolean deepEquals(Object obj, Set<IdentityPair<Datatype, Datatype>> compareHistory) {
73                 if (this==obj) return true;
74                 if (obj instanceof OptionalType == false) return false;
75                 if ( !hasEqualMetadata(obj) ) return false;
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
90         @SuppressWarnings("unchecked")
91         @Override
92         public <T extends Datatype> T getChildType(ChildReference reference) throws ReferenceException {
93                 if (reference==null) return (T) this;
94
95                 if (reference instanceof LabelReference) {
96                         LabelReference lr = (LabelReference) reference;
97                         if (lr.label.equals("o")) {
98                                 return componentType.getChildType(reference.getChildReference());
99                         }                       
100                 }
101                 
102                 if (reference instanceof ComponentReference) {
103                         return componentType.getChildType(reference.getChildReference());
104                 } 
105                 
106                 throw new ReferenceException(reference.getClass()+" is not a reference of OptionalType");               
107         }
108         
109 }