-/*******************************************************************************\r
- * Copyright (c) 2010 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
+/*******************************************************************************
+ * 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;\r
-import java.util.IdentityHashMap;\r
-import java.util.Set;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.accessor.reference.ChildReference;\r
-import org.simantics.databoard.accessor.reference.IndexReference;\r
-import org.simantics.databoard.accessor.reference.LabelReference;\r
-import org.simantics.databoard.accessor.reference.NameReference;\r
-import org.simantics.databoard.adapter.AdaptException;\r
-import org.simantics.databoard.binding.error.BindingException;\r
-import org.simantics.databoard.binding.error.RuntimeBindingException;\r
-import org.simantics.databoard.binding.impl.BindingPrintContext;\r
-import org.simantics.databoard.type.Component;\r
-import org.simantics.databoard.type.RecordType;\r
-import org.simantics.databoard.util.IdentityHashSet;\r
-import org.simantics.databoard.util.IdentityPair;\r
+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;
/**
public abstract class RecordBinding extends Binding {
public Binding[] componentBindings;
- \r
- /**\r
- * Get binding by field name\r
- * @param fieldName\r
- * @return binding or <code>null</code>\r
- */\r
- public Binding getComponentBinding(String fieldName) {\r
- int fieldIndex = type().getComponentIndex2(fieldName);\r
- if (fieldIndex<0) return null;\r
- return getComponentBinding(fieldIndex);\r
- }\r
- \r
- public Object getComponentObject(Object obj, String fieldName) throws BindingException {\r
- int fieldIndex = type().getComponentIndex2(fieldName);\r
- if (fieldIndex<0) return null;\r
- return getComponent(obj, fieldIndex);\r
- }\r
- \r
- public int getComponentIndex(String fieldName) {\r
- return type().getComponentIndex2(fieldName);\r
- }\r
+
+ /**
+ * Get binding by field name
+ * @param fieldName
+ * @return binding or <code>null</code>
+ */
+ 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];
public abstract void setComponents(Object obj, Object ... value) throws BindingException;
public abstract void setComponent(Object obj, int index, Object value) throws BindingException;
-\r
- public void setComponent(Object obj, String fieldName, Object value) throws BindingException\r
- {\r
- int fieldIndex = type().getComponentIndex2(fieldName);\r
- if ( fieldIndex<0 ) throw new BindingException("Field "+fieldName+" does not exist.");\r
- setComponent(obj, fieldIndex, value);\r
- }\r
- \r
- @Override\r
- public void readFrom(Binding srcBinding, Object src, Object dst)\r
- throws BindingException {\r
- RecordBinding sb = (RecordBinding) srcBinding;\r
- int len = getComponentCount();\r
- if (sb.getComponentCount()!=len) throw new BindingException("field count mismatch");\r
- try {\r
- for (int i=0; i<len; i++) {\r
- Binding dcb = getComponentBinding(i);\r
- Binding scb = sb.getComponentBinding(i);\r
- Object sc = sb.getComponent(src, i);\r
- if (dcb.isImmutable()) {\r
- Object cv = Bindings.clone(sc, scb, dcb);\r
- setComponent(dst, i, cv);\r
- } else {\r
- Object v = getComponent(dst, i);\r
- v = dcb.readFromTry(scb, sc, v);\r
- setComponent(dst, i, v);\r
- }\r
- } \r
- } catch (AdaptException e) {\r
- throw new BindingException(e);\r
- }\r
- }\r
+
+ 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<len; i++) {
+ Binding dcb = getComponentBinding(i);
+ Binding scb = sb.getComponentBinding(i);
+ Object sc = sb.getComponent(src, i);
+ if (dcb.isImmutable()) {
+ Object cv = Bindings.clone(sc, scb, dcb);
+ setComponent(dst, i, cv);
+ } else {
+ Object v = getComponent(dst, i);
+ v = dcb.readFromTry(scb, sc, v);
+ setComponent(dst, i, v);
+ }
+ }
+ } catch (AdaptException e) {
+ throw new BindingException(e);
+ }
+ }
@Override
public void accept(Visitor1 v, Object obj) {
Object v1 = getComponent(o1, i);
Object v2 = getComponent(o2, i);
int dif = c.deepCompare(v1, v2, compareHistory);
- if (dif!=0)\r
+ if (dif!=0)
return dif;
}
return 0;
throw new RuntimeBindingException(e);
}
}
-\r
- protected void toStringAux(Object value, BindingPrintContext ctx) throws BindingException {\r
- ctx.b.append('{');\r
- Component[] components = type().getComponents();\r
- boolean first = true;\r
- for(int i=0;i<components.length;++i) {\r
- if(first)\r
- first = false;\r
- else {\r
- ctx.b.append(", ");\r
- if (!ctx.singleLine) ctx.b.append('\n');\r
- }\r
- Binding binding = getComponentBinding(i);\r
- Object cval = getComponent(value, i);\r
- if(binding instanceof OptionalBinding && \r
- !((OptionalBinding)binding).hasValue(cval))\r
- continue;\r
- Component component = components[i]; \r
- ctx.b.append(component.name);\r
- ctx.b.append(" = ");\r
- binding.toString(cval, ctx);\r
- }\r
- ctx.b.append('}');\r
- }\r
- \r
- @Override\r
- protected void toString(Object value, BindingPrintContext ctx) throws BindingException {\r
- if(type().isReferable()) {\r
- if(ctx.refs.contains(value)) {\r
- int val = ctx.refs.get(value);\r
- if(val < 0) {\r
- val = ctx.nameCount.value++;\r
- ctx.refs.put(value, val);\r
- }\r
- ctx.b.append((char)('a' + val));\r
- }\r
- else {\r
- ctx.refs.put(value, -1);\r
- toStringAux(value, ctx);\r
- int val = ctx.refs.remove(value);\r
- if(val >= 0) {\r
- ctx.b.append("/");\r
- ctx.b.append((char)('a' + val));\r
- }\r
- }\r
- }\r
- else {\r
- toStringAux(value, ctx);\r
- }\r
+
+ protected void toStringAux(Object value, BindingPrintContext ctx) throws BindingException {
+ ctx.b.append('{');
+ Component[] components = type().getComponents();
+ boolean first = true;
+ for(int i=0;i<components.length;++i) {
+ if(first)
+ first = false;
+ else {
+ ctx.b.append(", ");
+ if (!ctx.singleLine) ctx.b.append('\n');
+ }
+ Binding binding = getComponentBinding(i);
+ Object cval = getComponent(value, i);
+ if(binding instanceof OptionalBinding &&
+ !((OptionalBinding)binding).hasValue(cval))
+ continue;
+ Component component = components[i];
+ ctx.b.append(component.name);
+ ctx.b.append(" = ");
+ binding.toString(cval, ctx);
+ }
+ ctx.b.append('}');
+ }
+
+ @Override
+ protected void toString(Object value, BindingPrintContext ctx) throws BindingException {
+ if(type().isReferable()) {
+ if(ctx.refs.contains(value)) {
+ int val = ctx.refs.get(value);
+ if(val < 0) {
+ val = ctx.nameCount.value++;
+ ctx.refs.put(value, val);
+ }
+ ctx.b.append((char)('a' + val));
+ }
+ else {
+ ctx.refs.put(value, -1);
+ toStringAux(value, ctx);
+ int val = ctx.refs.remove(value);
+ if(val >= 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<IdentityPair<Binding, Binding>> 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<Object, Object> hashedObjects) {
+ int code = super.deepHashCode(hashedObjects);
+ for (int i = 0; i < componentBindings.length; i++)
+ code = 17 * code + componentBindings[i].hashCode(hashedObjects);
+ return code;
}
-\r
- @Override\r
- public Binding getComponentBinding(ChildReference path) throws IllegalArgumentException {\r
- if (path==null) return this;\r
- if (path instanceof IndexReference) {\r
- IndexReference ir = (IndexReference) path;\r
- return componentBindings[ir.index].getComponentBinding(path.childReference);\r
- }\r
- if (path instanceof NameReference) {\r
- NameReference nr = (NameReference) path;\r
- return getComponentBinding( nr.name ).getComponentBinding(path.childReference);\r
- }\r
- if (path instanceof LabelReference) {\r
- LabelReference lr = (LabelReference) path; \r
- try {\r
- Integer i = new Integer(lr.label);\r
- return getComponentBinding( i ).getComponentBinding(path.childReference);\r
- } catch (NumberFormatException nfe) {\r
- return getComponentBinding( lr.label ).getComponentBinding(path.childReference);\r
- }\r
- }\r
- throw new IllegalArgumentException();\r
- }\r
- \r
- @Override\r
- public boolean isImmutable() {\r
- return getComponentCount() == 0;\r
- }\r
- \r
- public void setBoolean(Object r, int index, boolean z) throws BindingException\r
- {\r
- setComponent(r, index, ((BooleanBinding)componentBindings[index]).create(z));\r
- }\r
- \r
- public boolean getBoolean(Object r, int index) throws BindingException\r
- {\r
- return ((BooleanBinding)componentBindings[index]).getValue_( getComponent(r, index) );\r
- }\r
- \r
- public void setByte(Object r, int index, byte x) throws BindingException\r
- {\r
- setComponent(r, index, ((ByteBinding)componentBindings[index]).create(x));\r
- }\r
- \r
- public byte getByte(Object r, int index) throws BindingException\r
- {\r
- return ((ByteBinding)componentBindings[index]).getValue_( getComponent(r, index) );\r
- }\r
-\r
- public void setInt(Object r, int index, int x) throws BindingException\r
- {\r
- setComponent(r, index, ((IntegerBinding)componentBindings[index]).create(x));\r
- }\r
- \r
- public int getInt(Object r, int index) throws BindingException\r
- {\r
- return ((IntegerBinding)componentBindings[index]).getValue_( getComponent(r, index) );\r
- }\r
- \r
- public void setLong(Object r, int index, long x) throws BindingException\r
- {\r
- setComponent(r, index, ((LongBinding)componentBindings[index]).create(x));\r
- }\r
- \r
- public long getLong(Object r, int index) throws BindingException\r
- {\r
- return ((LongBinding)componentBindings[index]).getValue_( getComponent(r, index) );\r
- }\r
- \r
- public void setFloat(Object r, int index, float x) throws BindingException\r
- {\r
- setComponent(r, index, ((FloatBinding)componentBindings[index]).create(x));\r
- }\r
- \r
- public float getFloat(Object r, int index) throws BindingException\r
- {\r
- return ((FloatBinding)componentBindings[index]).getValue_( getComponent(r, index) );\r
- }\r
- \r
- public void setDouble(Object r, int index, double x) throws BindingException\r
- {\r
- setComponent(r, index, ((DoubleBinding)componentBindings[index]).create(x));\r
- }\r
- \r
- public double getDouble(Object r, int index) throws BindingException\r
- {\r
- return ((DoubleBinding)componentBindings[index]).getValue_( getComponent(r, index) );\r
- }\r
-\r
- @Override\r
- protected boolean deepEquals(Object obj,\r
- Set<IdentityPair<Binding, Binding>> compareHistory) {\r
- RecordBinding o = (RecordBinding)obj;\r
- if (!super.deepEquals( obj, compareHistory ))\r
- return false;\r
- \r
- if (componentBindings.length != o.componentBindings.length) return false;\r
- \r
- for (int i = 0; i < componentBindings.length; i++)\r
- if (!componentBindings[i].equals(o.componentBindings[i], compareHistory))\r
- return false;\r
- \r
- return true;\r
- }\r
-\r
- @Override\r
- public int deepHashCode(IdentityHashMap<Object, Object> hashedObjects) {\r
- int code = super.deepHashCode(hashedObjects);\r
- for (int i = 0; i < componentBindings.length; i++)\r
- code = 17 * code + componentBindings[i].hashCode(hashedObjects);\r
- return code;\r
- }\r
}