]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/binding/impl/OptionalBindingDefault.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / binding / impl / OptionalBindingDefault.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
4  *  All rights reserved. This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License v1.0
6  *  which accompanies this distribution, and is available at
7  *  http://www.eclipse.org/legal/epl-v10.html
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.binding.impl;
13
14 import org.simantics.databoard.binding.Binding;
15 import org.simantics.databoard.binding.OptionalBinding;
16 import org.simantics.databoard.binding.error.BindingException;
17 import org.simantics.databoard.binding.error.UnsupportedOperationException;
18 import org.simantics.databoard.type.OptionalType;
19
20 /**
21  * This implementation binds OptionalType to null / Object assignment.
22  *
23  * @author Toni Kalajainen
24  */
25 public class OptionalBindingDefault extends OptionalBinding {
26
27     public OptionalBindingDefault(Binding componentBinding) {
28                 super(componentBinding);
29         }
30
31         public OptionalBindingDefault(OptionalType type, Binding componentBinding) {
32                 super(type, componentBinding);
33         }
34
35         /**
36      * Create result with no value
37      * 
38      * @return no value
39      */
40     public Object createNoValue() {
41         return null;
42     }
43     
44     /**
45      * Create result with a value
46      * 
47      * @param value
48      * @return argument that contains a value
49      */
50     public Object createValue(Object value) 
51     throws BindingException {
52         // Only this implementation, sub-classes may have another behaviour
53         if (value==null) throw new BindingException("Cannot bind null as a value");
54         return value;
55     }
56
57     /**
58      * Tests whether arg contains a value
59      * 
60      * @param arg 
61      * @return true if arg contained a value
62      */
63     public boolean hasValue(Object arg) {
64         return arg!=null;
65     }
66     
67     /**
68      * Get the non-null value, the arg did not contain a value,
69      * BindingException is thrown. 
70      *  
71      * @param arg argument that contains a value
72      * @return the composite value
73      * @throws BindingException
74      */
75     public Object getValue(Object arg) 
76     throws BindingException {
77         if (arg == null) throw new BindingException("Optional value ("+arg+") does not contain value.");
78         return arg;
79     }
80
81     public void setValue(Object optional, Object componentValue)
82     throws BindingException {
83         if (componentValue==optional) return;
84         throw new UnsupportedOperationException("Cannot set new value to container of java.lang.Object");
85     }
86     
87     public void setNoValue(Object optional)
88     throws BindingException
89     {           
90         if (optional==null) return;
91         throw new UnsupportedOperationException("Cannot remove value from container of java.lang.Object");
92     }
93
94         @Override
95         public boolean isInstance(Object obj) {
96                 if (obj==null) return true;
97                 return componentBinding.isInstance(obj);
98         }
99         
100         @Override
101         public boolean isImmutable() {
102                 return true;
103         }
104         
105         
106 }
107