X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2Fimpl%2FOptionalBindingDefault.java;fp=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Fbinding%2Fimpl%2FOptionalBindingDefault.java;h=8af15ff3c6e25365a4faa8c6229b08bb4e68d2e2;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/OptionalBindingDefault.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/OptionalBindingDefault.java new file mode 100644 index 000000000..8af15ff3c --- /dev/null +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/OptionalBindingDefault.java @@ -0,0 +1,107 @@ +/******************************************************************************* + * 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.impl; + +import org.simantics.databoard.binding.Binding; +import org.simantics.databoard.binding.OptionalBinding; +import org.simantics.databoard.binding.error.BindingException; +import org.simantics.databoard.binding.error.UnsupportedOperationException; +import org.simantics.databoard.type.OptionalType; + +/** + * This implementation binds OptionalType to null / Object assignment. + * + * @author Toni Kalajainen + */ +public class OptionalBindingDefault extends OptionalBinding { + + public OptionalBindingDefault(Binding componentBinding) { + super(componentBinding); + } + + public OptionalBindingDefault(OptionalType type, Binding componentBinding) { + super(type, componentBinding); + } + + /** + * Create result with no value + * + * @return no value + */ + public Object createNoValue() { + return null; + } + + /** + * Create result with a value + * + * @param value + * @return argument that contains a value + */ + public Object createValue(Object value) + throws BindingException { + // Only this implementation, sub-classes may have another behaviour + if (value==null) throw new BindingException("Cannot bind null as a value"); + return value; + } + + /** + * Tests whether arg contains a value + * + * @param arg + * @return true if arg contained a value + */ + public boolean hasValue(Object arg) { + return arg!=null; + } + + /** + * Get the non-null value, the arg did not contain a value, + * BindingException is thrown. + * + * @param arg argument that contains a value + * @return the composite value + * @throws BindingException + */ + public Object getValue(Object arg) + throws BindingException { + if (arg == null) throw new BindingException("Optional value ("+arg+") does not contain value."); + return arg; + } + + public void setValue(Object optional, Object componentValue) + throws BindingException { + if (componentValue==optional) return; + throw new UnsupportedOperationException("Cannot set new value to container of java.lang.Object"); + } + + public void setNoValue(Object optional) + throws BindingException + { + if (optional==null) return; + throw new UnsupportedOperationException("Cannot remove value from container of java.lang.Object"); + } + + @Override + public boolean isInstance(Object obj) { + if (obj==null) return true; + return componentBinding.isInstance(obj); + } + + @Override + public boolean isImmutable() { + return true; + } + + +} +