X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2FRecordBinding.java;h=1937d6631f4618e8cf40f0dd63ea453f40d5f524;hb=refs%2Fchanges%2F10%2F1010%2F2;hp=416abf8290846ba64dfbef960ccc5f41d89982e2;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/RecordBinding.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/RecordBinding.java
index 416abf829..1937d6631 100644
--- a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/RecordBinding.java
+++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/RecordBinding.java
@@ -1,33 +1,33 @@
-/*******************************************************************************
- * 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.binding;
-import java.util.HashSet;
-import java.util.IdentityHashMap;
-import java.util.Set;
-
-import org.simantics.databoard.Bindings;
-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.adapter.AdaptException;
-import org.simantics.databoard.binding.error.BindingException;
-import org.simantics.databoard.binding.error.RuntimeBindingException;
-import org.simantics.databoard.binding.impl.BindingPrintContext;
-import org.simantics.databoard.type.Component;
-import org.simantics.databoard.type.RecordType;
-import org.simantics.databoard.util.IdentityHashSet;
-import org.simantics.databoard.util.IdentityPair;
+import java.util.HashSet;
+import java.util.IdentityHashMap;
+import java.util.Set;
+
+import org.simantics.databoard.Bindings;
+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.adapter.AdaptException;
+import org.simantics.databoard.binding.error.BindingException;
+import org.simantics.databoard.binding.error.RuntimeBindingException;
+import org.simantics.databoard.binding.impl.BindingPrintContext;
+import org.simantics.databoard.type.Component;
+import org.simantics.databoard.type.RecordType;
+import org.simantics.databoard.util.IdentityHashSet;
+import org.simantics.databoard.util.IdentityPair;
/**
@@ -40,27 +40,27 @@ import org.simantics.databoard.util.IdentityPair;
public abstract class RecordBinding extends Binding {
public Binding[] componentBindings;
-
- /**
- * Get binding by field name
- * @param fieldName
- * @return binding or null
- */
- public Binding getComponentBinding(String fieldName) {
- int fieldIndex = type().getComponentIndex2(fieldName);
- if (fieldIndex<0) return null;
- return getComponentBinding(fieldIndex);
- }
-
- public Object getComponentObject(Object obj, String fieldName) throws BindingException {
- int fieldIndex = type().getComponentIndex2(fieldName);
- if (fieldIndex<0) return null;
- return getComponent(obj, fieldIndex);
- }
-
- public int getComponentIndex(String fieldName) {
- return type().getComponentIndex2(fieldName);
- }
+
+ /**
+ * Get binding by field name
+ * @param fieldName
+ * @return binding or null
+ */
+ public Binding getComponentBinding(String fieldName) {
+ int fieldIndex = type().getComponentIndex2(fieldName);
+ if (fieldIndex<0) return null;
+ return getComponentBinding(fieldIndex);
+ }
+
+ public Object getComponentObject(Object obj, String fieldName) throws BindingException {
+ int fieldIndex = type().getComponentIndex2(fieldName);
+ if (fieldIndex<0) return null;
+ return getComponent(obj, fieldIndex);
+ }
+
+ public int getComponentIndex(String fieldName) {
+ return type().getComponentIndex2(fieldName);
+ }
public Binding getComponentBinding(int fieldIndex) {
return componentBindings[fieldIndex];
@@ -108,38 +108,38 @@ public abstract class RecordBinding extends Binding {
public abstract void setComponents(Object obj, Object ... value) throws BindingException;
public abstract void setComponent(Object obj, int index, Object value) throws BindingException;
-
- public void setComponent(Object obj, String fieldName, Object value) throws BindingException
- {
- int fieldIndex = type().getComponentIndex2(fieldName);
- if ( fieldIndex<0 ) throw new BindingException("Field "+fieldName+" does not exist.");
- setComponent(obj, fieldIndex, value);
- }
-
- @Override
- public void readFrom(Binding srcBinding, Object src, Object dst)
- throws BindingException {
- RecordBinding sb = (RecordBinding) srcBinding;
- int len = getComponentCount();
- if (sb.getComponentCount()!=len) throw new BindingException("field count mismatch");
- try {
- for (int i=0; i= 0) {
- ctx.b.append("/");
- ctx.b.append((char)('a' + val));
- }
- }
- }
- else {
- toStringAux(value, ctx);
- }
+
+ protected void toStringAux(Object value, BindingPrintContext ctx) throws BindingException {
+ ctx.b.append('{');
+ Component[] components = type().getComponents();
+ boolean first = true;
+ for(int i=0;i= 0) {
+ ctx.b.append("/");
+ ctx.b.append((char)('a' + val));
+ }
+ }
+ }
+ else {
+ toStringAux(value, ctx);
+ }
+ }
+
+ @Override
+ public Binding getComponentBinding(ChildReference path) throws IllegalArgumentException {
+ if (path==null) return this;
+ if (path instanceof IndexReference) {
+ IndexReference ir = (IndexReference) path;
+ return componentBindings[ir.index].getComponentBinding(path.childReference);
+ }
+ if (path instanceof NameReference) {
+ NameReference nr = (NameReference) path;
+ return getComponentBinding( nr.name ).getComponentBinding(path.childReference);
+ }
+ if (path instanceof LabelReference) {
+ LabelReference lr = (LabelReference) path;
+ try {
+ Integer i = new Integer(lr.label);
+ return getComponentBinding( i ).getComponentBinding(path.childReference);
+ } catch (NumberFormatException nfe) {
+ return getComponentBinding( lr.label ).getComponentBinding(path.childReference);
+ }
+ }
+ throw new IllegalArgumentException();
+ }
+
+ @Override
+ public boolean isImmutable() {
+ return getComponentCount() == 0;
+ }
+
+ public void setBoolean(Object r, int index, boolean z) throws BindingException
+ {
+ setComponent(r, index, ((BooleanBinding)componentBindings[index]).create(z));
+ }
+
+ public boolean getBoolean(Object r, int index) throws BindingException
+ {
+ return ((BooleanBinding)componentBindings[index]).getValue_( getComponent(r, index) );
+ }
+
+ public void setByte(Object r, int index, byte x) throws BindingException
+ {
+ setComponent(r, index, ((ByteBinding)componentBindings[index]).create(x));
+ }
+
+ public byte getByte(Object r, int index) throws BindingException
+ {
+ return ((ByteBinding)componentBindings[index]).getValue_( getComponent(r, index) );
+ }
+
+ public void setInt(Object r, int index, int x) throws BindingException
+ {
+ setComponent(r, index, ((IntegerBinding)componentBindings[index]).create(x));
+ }
+
+ public int getInt(Object r, int index) throws BindingException
+ {
+ return ((IntegerBinding)componentBindings[index]).getValue_( getComponent(r, index) );
+ }
+
+ public void setLong(Object r, int index, long x) throws BindingException
+ {
+ setComponent(r, index, ((LongBinding)componentBindings[index]).create(x));
+ }
+
+ public long getLong(Object r, int index) throws BindingException
+ {
+ return ((LongBinding)componentBindings[index]).getValue_( getComponent(r, index) );
+ }
+
+ public void setFloat(Object r, int index, float x) throws BindingException
+ {
+ setComponent(r, index, ((FloatBinding)componentBindings[index]).create(x));
+ }
+
+ public float getFloat(Object r, int index) throws BindingException
+ {
+ return ((FloatBinding)componentBindings[index]).getValue_( getComponent(r, index) );
+ }
+
+ public void setDouble(Object r, int index, double x) throws BindingException
+ {
+ setComponent(r, index, ((DoubleBinding)componentBindings[index]).create(x));
+ }
+
+ public double getDouble(Object r, int index) throws BindingException
+ {
+ return ((DoubleBinding)componentBindings[index]).getValue_( getComponent(r, index) );
+ }
+
+ @Override
+ protected boolean deepEquals(Object obj,
+ Set> compareHistory) {
+ RecordBinding o = (RecordBinding)obj;
+ if (!super.deepEquals( obj, compareHistory ))
+ return false;
+
+ if (componentBindings.length != o.componentBindings.length) return false;
+
+ for (int i = 0; i < componentBindings.length; i++)
+ if (!componentBindings[i].equals(o.componentBindings[i], compareHistory))
+ return false;
+
+ return true;
+ }
+
+ @Override
+ public int deepHashCode(IdentityHashMap