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