X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Ftype%2FRecordType.java;h=8133fde16142216c3a23a054d3bba198d3a050e5;hp=1fc4c7ab5b50b7b9ec82d19ec9255bc13c9e7f05;hb=refs%2Fchanges%2F38%2F238%2F2;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/type/RecordType.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/type/RecordType.java index 1fc4c7ab5..8133fde16 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/type/RecordType.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/type/RecordType.java @@ -1,70 +1,70 @@ -/******************************************************************************* - * Copyright (c) 2010 Association for Decentralized Information Management in - * Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ +/******************************************************************************* + * Copyright (c) 2010 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ package org.simantics.databoard.type; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.StringTokenizer; - -import org.simantics.databoard.accessor.error.ReferenceException; -import org.simantics.databoard.accessor.reference.ChildReference; -import org.simantics.databoard.accessor.reference.IndexReference; -import org.simantics.databoard.accessor.reference.LabelReference; -import org.simantics.databoard.accessor.reference.NameReference; -import org.simantics.databoard.annotations.Referable; -import org.simantics.databoard.binding.error.DatatypeConstructionException; -import org.simantics.databoard.util.IdentityPair; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.StringTokenizer; + +import org.simantics.databoard.accessor.error.ReferenceException; +import org.simantics.databoard.accessor.reference.ChildReference; +import org.simantics.databoard.accessor.reference.IndexReference; +import org.simantics.databoard.accessor.reference.LabelReference; +import org.simantics.databoard.accessor.reference.NameReference; +import org.simantics.databoard.annotations.Referable; +import org.simantics.databoard.binding.error.DatatypeConstructionException; +import org.simantics.databoard.util.IdentityPair; public @Referable class RecordType extends Datatype { - - public static final Datatype VOID_TYPE = new RecordType(false); - - public static final String KEY_REFERABLE = "referable"; // "false"/"true" - - // Key to identifier(s) index - public static final String KEY_IDENTIFIER = "identifier"; // "0" "0,1" "n[,m]" - - public static final Component[] NO_COMPONENTS = new Component[0]; - Component[] components = NO_COMPONENTS; - - /** Indices to identifiers of this record. This field is filled on request */ - private transient int[] identifiersIndices; - - /** Identifier type */ + public static final Datatype VOID_TYPE = new RecordType(false); + + public static final String KEY_REFERABLE = "referable"; // "false"/"true" + + // Key to identifier(s) index + public static final String KEY_IDENTIFIER = "identifier"; // "0" "0,1" "n[,m]" + + public static final Component[] NO_COMPONENTS = new Component[0]; + + Component[] components = NO_COMPONENTS; + + /** Indices to identifiers of this record. This field is filled on request */ + private transient int[] identifiersIndices; + + /** Identifier type */ private transient Datatype identifierType; public RecordType() { - setReferable(false); + setReferable(false); } public RecordType(boolean referable, Component...components) { - this.components = components; - setReferable(referable); + this.components = components; + setReferable(referable); } - public boolean isReferable() { + public boolean isReferable() { String str = metadata.get( KEY_REFERABLE ); return str!=null && str.equals( Boolean.TRUE.toString() ); - } - - public void setReferable( boolean referable ) { - if ( !referable ) - { - metadata.remove(KEY_REFERABLE); - } else { - metadata.put(KEY_REFERABLE, Boolean.toString(referable)); - } + } + + public void setReferable( boolean referable ) { + if ( !referable ) + { + metadata.remove(KEY_REFERABLE); + } else { + metadata.put(KEY_REFERABLE, Boolean.toString(referable)); + } } @Override @@ -81,30 +81,30 @@ public @Referable class RecordType extends Datatype { public void setComponents(Component[] components) { this.components = components; } - - public void mergeRecord(RecordType src) - throws DatatypeConstructionException - { - int ci = src.getComponentCount(); - for (int i=0; i0) System.arraycopy(components, 0, newComponents, 0, index); - if (index0) System.arraycopy(components, 0, newComponents, 0, index); + if (index> compareHistory) { - if ( this==obj ) return true; - if ( !hasEqualMetadata(obj) ) return false; + if ( this==obj ) return true; + if ( !hasEqualMetadata(obj) ) return false; if (obj instanceof RecordType == false) return false; RecordType other = (RecordType) obj; @@ -175,7 +175,7 @@ public @Referable class RecordType extends Datatype { for (Component c : components) hash = hash*13 + 7 * c.name.hashCode() /*+ 3*c.type.hashCode()*/; return hash; - } + } @Override public void accept(Visitor1 v, Object obj) { @@ -200,243 +200,243 @@ public @Referable class RecordType extends Datatype { if (!getComponent(i).name.equals(Integer.toString(i))) return false; } return true; - } - - /** - * Get component type by index - * @param index index - * @return componenet type or null if index was invalid - */ - @Override - public Datatype getComponentType(int index) { - if (index<0||index>=components.length) return null; - return components[index].type; - } - - @Override - public Datatype getComponentType(ChildReference path) throws IllegalArgumentException { - if (path==null) return this; - if (path instanceof IndexReference) { - IndexReference ir = (IndexReference) path; - return components[ir.index].type.getComponentType(path.childReference); - } - if (path instanceof NameReference) { - NameReference nr = (NameReference) path; - return getComponent( nr.name ).type.getComponentType(path.childReference); - } - if (path instanceof LabelReference) { - LabelReference lr = (LabelReference) path; - try { - Integer i = new Integer(lr.label); - return getComponent( i ).type.getComponentType(path.childReference); - } catch (NumberFormatException nfe) { - return getComponent( lr.label ).type.getComponentType(path.childReference); - } - } - throw new IllegalArgumentException(); - } - - public boolean hasComponent(String fieldName) { - for (int i=0; inull if one does not exist - */ - public Integer getComponentIndex(String fieldName) { - for (int i=0; inull - */ - public Datatype getComponentType(String fieldName) { - int index = getComponentIndex2(fieldName); - if (index<0) return null; - return components[index].type; - } - - /** - * Get component by name. - * - * @param fieldName component name - * @return component or null if one does not exist - */ - public Component getComponent(String fieldName) { - for (Component c : components) - if (c.name.equals(fieldName)) return c; - return null; - } - - /** - * Get component by index. - * - * @param index component index - * @return component or null if one does not exist - */ - public Component getComponent(int index) { - if (index<0||index>=components.length) return null; - return components[index]; - } - - public Component[] getComponents() { - return components; - } - - /** - * Get an array of indices that describe which fields compose the identifier - * of this record - * - * @return indices - */ - public int[] getIdentifiers() { - String ids = metadata.get( KEY_IDENTIFIER ); - if (ids == null) { - identifiersIndices = new int[0]; - } else { - // Parse - StringTokenizer st = new StringTokenizer(ids, ","); - - int[] indices = new int[ st.countTokens() ]; - for (int i=0; i0) sb.append(','); - sb.append( Integer.toString(indices[i]) ); - } - - String str = sb.toString(); - if ( str.isEmpty() ) { - metadata.remove( KEY_IDENTIFIER ); - } else { - metadata.put( KEY_IDENTIFIER, str ); - } - } - - /** - * Set which fields compose the identifier of this record - * @param indices - */ - public void setIdentifiers(List indices) - { - int[] indices2 = new int[indices.size()]; - for (int i=0; i T getChildType( ChildReference reference ) throws ReferenceException - { - if (reference==null) return (T) this; - - if (reference instanceof LabelReference) { - LabelReference lr = (LabelReference) reference; - String fieldName = lr.label; - int index = getComponentIndex2(fieldName); - if (index<0) throw new ReferenceException("RecordType doesn't have field by name \""+fieldName+"\""); - return components[index].type.getChildType(reference.childReference); - } - - if (reference instanceof IndexReference) { - IndexReference ref = (IndexReference) reference; - int index = ref.getIndex(); - if ( index<0 || index>=components.length ) new ReferenceException("RecordType doesn't have field at index+"+index); - return components[index].type.getChildType(reference.childReference); - } - - if (reference instanceof NameReference) { - NameReference lr = (NameReference) reference; - String fieldName = lr.name; - int index = getComponentIndex2(fieldName); - if (index<0) throw new ReferenceException("RecordType doesn't have field by name \""+fieldName+"\""); - return components[index].type.getChildType(reference.childReference); - } - - throw new ReferenceException(reference.getClass()+" is not a subreference of RecordType"); - + } + + /** + * Get component type by index + * @param index index + * @return componenet type or null if index was invalid + */ + @Override + public Datatype getComponentType(int index) { + if (index<0||index>=components.length) return null; + return components[index].type; + } + + @Override + public Datatype getComponentType(ChildReference path) throws IllegalArgumentException { + if (path==null) return this; + if (path instanceof IndexReference) { + IndexReference ir = (IndexReference) path; + return components[ir.index].type.getComponentType(path.childReference); + } + if (path instanceof NameReference) { + NameReference nr = (NameReference) path; + return getComponent( nr.name ).type.getComponentType(path.childReference); + } + if (path instanceof LabelReference) { + LabelReference lr = (LabelReference) path; + try { + Integer i = new Integer(lr.label); + return getComponent( i ).type.getComponentType(path.childReference); + } catch (NumberFormatException nfe) { + return getComponent( lr.label ).type.getComponentType(path.childReference); + } + } + throw new IllegalArgumentException(); + } + + public boolean hasComponent(String fieldName) { + for (int i=0; inull if one does not exist + */ + public Integer getComponentIndex(String fieldName) { + for (int i=0; inull + */ + public Datatype getComponentType(String fieldName) { + int index = getComponentIndex2(fieldName); + if (index<0) return null; + return components[index].type; + } + + /** + * Get component by name. + * + * @param fieldName component name + * @return component or null if one does not exist + */ + public Component getComponent(String fieldName) { + for (Component c : components) + if (c.name.equals(fieldName)) return c; + return null; + } + + /** + * Get component by index. + * + * @param index component index + * @return component or null if one does not exist + */ + public Component getComponent(int index) { + if (index<0||index>=components.length) return null; + return components[index]; + } + + public Component[] getComponents() { + return components; + } + + /** + * Get an array of indices that describe which fields compose the identifier + * of this record + * + * @return indices + */ + public int[] getIdentifiers() { + String ids = metadata.get( KEY_IDENTIFIER ); + if (ids == null) { + identifiersIndices = new int[0]; + } else { + // Parse + StringTokenizer st = new StringTokenizer(ids, ","); + + int[] indices = new int[ st.countTokens() ]; + for (int i=0; i0) sb.append(','); + sb.append( Integer.toString(indices[i]) ); + } + + String str = sb.toString(); + if ( str.isEmpty() ) { + metadata.remove( KEY_IDENTIFIER ); + } else { + metadata.put( KEY_IDENTIFIER, str ); + } + } + + /** + * Set which fields compose the identifier of this record + * @param indices + */ + public void setIdentifiers(List indices) + { + int[] indices2 = new int[indices.size()]; + for (int i=0; i T getChildType( ChildReference reference ) throws ReferenceException + { + if (reference==null) return (T) this; + + if (reference instanceof LabelReference) { + LabelReference lr = (LabelReference) reference; + String fieldName = lr.label; + int index = getComponentIndex2(fieldName); + if (index<0) throw new ReferenceException("RecordType doesn't have field by name \""+fieldName+"\""); + return components[index].type.getChildType(reference.childReference); + } + + if (reference instanceof IndexReference) { + IndexReference ref = (IndexReference) reference; + int index = ref.getIndex(); + if ( index<0 || index>=components.length ) new ReferenceException("RecordType doesn't have field at index+"+index); + return components[index].type.getChildType(reference.childReference); + } + + if (reference instanceof NameReference) { + NameReference lr = (NameReference) reference; + String fieldName = lr.name; + int index = getComponentIndex2(fieldName); + if (index<0) throw new ReferenceException("RecordType doesn't have field by name \""+fieldName+"\""); + return components[index].type.getChildType(reference.childReference); + } + + throw new ReferenceException(reference.getClass()+" is not a subreference of RecordType"); + } }