1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.type;
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;
25 public class OptionalType extends Datatype {
26 public Datatype componentType;
28 public OptionalType() {
31 public OptionalType(Datatype componentType) {
32 this.componentType = componentType;
35 public Datatype getComponentType() {
40 public int getComponentCount() {
45 public Datatype getComponentType(int index) {
46 if (index!=0) throw new IllegalArgumentException();
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);
61 protected void collectSubtypes(Set<Datatype> subtypes,
62 Set<Datatype> recursiveSubtypes) {
63 componentType.collectSubtypes(subtypes, recursiveSubtypes);
67 public int hashCode() {
68 return componentType.hashCode() + metadataHashCode();
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);
81 public void accept(Visitor1 v, Object obj) {
86 public <T> T accept(Visitor<T> v) {
90 @SuppressWarnings("unchecked")
92 public <T extends Datatype> T getChildType(ChildReference reference) throws ReferenceException {
93 if (reference==null) return (T) this;
95 if (reference instanceof LabelReference) {
96 LabelReference lr = (LabelReference) reference;
97 if (lr.label.equals("o")) {
98 return componentType.getChildType(reference.getChildReference());
102 if (reference instanceof ComponentReference) {
103 return componentType.getChildType(reference.getChildReference());
106 throw new ReferenceException(reference.getClass()+" is not a reference of OptionalType");