]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/accessor/java/JavaLong.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / accessor / java / JavaLong.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.accessor.java;
13
14 import org.simantics.databoard.Bindings;
15 import org.simantics.databoard.accessor.Accessor;
16 import org.simantics.databoard.accessor.LongAccessor;
17 import org.simantics.databoard.accessor.error.AccessorConstructionException;
18 import org.simantics.databoard.accessor.error.AccessorException;
19 import org.simantics.databoard.accessor.error.ReferenceException;
20 import org.simantics.databoard.accessor.event.Event;
21 import org.simantics.databoard.accessor.event.ValueAssigned;
22 import org.simantics.databoard.accessor.impl.AccessorParams;
23 import org.simantics.databoard.accessor.impl.ListenerEntry;
24 import org.simantics.databoard.accessor.interestset.LongInterestSet;
25 import org.simantics.databoard.accessor.reference.ChildReference;
26 import org.simantics.databoard.binding.ArrayBinding;
27 import org.simantics.databoard.binding.Binding;
28 import org.simantics.databoard.binding.LongBinding;
29 import org.simantics.databoard.binding.RecordBinding;
30 import org.simantics.databoard.binding.VariantBinding;
31 import org.simantics.databoard.binding.error.BindingException;
32 import org.simantics.databoard.binding.mutable.ContainerOptionalBinding;
33 import org.simantics.databoard.type.LongType;
34
35 public class JavaLong extends JavaObject implements LongAccessor {
36         
37         public JavaLong(Accessor parent, LongBinding binding, Object object, AccessorParams params) {
38                 super(parent, binding, object, params);
39         }
40         
41         public LongBinding getBinding() {
42                 return (LongBinding) binding;
43         }
44
45         @Override
46         public LongType type() {
47                 return getBinding().type();
48         }
49
50         @Override
51         public long getValue() throws AccessorException {
52                 readLock();
53                 try {
54                         return getBinding().getValue_(object);
55                 } catch (BindingException e) {
56                         throw new AccessorException(e);
57                 } finally {
58                         readUnlock();
59                 }
60         }
61         
62         @Override
63         public void setValue(long newValue) throws AccessorException {
64                 if (newValue == getValue()) return;
65                 writeLock();
66                 try {
67                         if (binding.isImmutable() && parent!=null && parent instanceof JavaArray) {
68                                 JavaObject jo = (JavaObject) parent;
69                                 ArrayBinding ab = (ArrayBinding) jo.binding;
70                                 Object nv = getBinding().create(newValue);
71                                 ab.set(jo.object, (Integer)keyInParent, nv);
72                                 this.object = nv;
73                         } else if (binding.isImmutable() && parent!=null && parent instanceof JavaRecord) {
74                                 JavaObject jo = (JavaObject) parent;
75                                 RecordBinding rb = (RecordBinding) jo.binding;
76                                 Object nv = getBinding().create(newValue);
77                                 rb.setComponent(jo.object, (Integer)keyInParent, nv);
78                                 this.object = nv;
79                         } else if (binding.isImmutable() && parent!=null && parent instanceof JavaVariant) {
80                                 JavaObject jo = (JavaObject) parent;
81                                 VariantBinding vb = (VariantBinding) jo.binding;
82                                 Object nv = getBinding().create(newValue);
83                                 vb.setContent(jo.object, getBinding(), nv);
84                                 this.object = nv;
85                         } else if (binding.isImmutable() && parent!=null && parent instanceof JavaOptional) {
86                                 // Plain assign
87                                 JavaOptional jo = (JavaOptional) parent;
88                                 ContainerOptionalBinding vb = (ContainerOptionalBinding) jo.binding;
89                                 vb.setValue(jo.object, newValue);
90                                 this.object = newValue;
91                         } else {
92                                 // Set value
93                                 getBinding().setValue(object, newValue);
94                         }
95
96                         
97                         // Notify
98                         ListenerEntry le = listeners;
99                         while (le!=null) {
100                                 LongInterestSet is = le.getInterestSet();
101                                 if (is.inNotifications()) {
102                                         Event e = new ValueAssigned( Bindings.LONG, is.inValues() ? newValue : null );
103                                         emitEvent(le, e);
104                                 }
105                                 le = le.next;
106                         }
107                         
108                 } catch (BindingException e) {
109                         throw new AccessorException(e);
110                 } finally {
111                         writeUnlock();
112                 }
113         }
114
115         @Override
116         public void setValue(Binding binding, Object newValue) throws AccessorException {
117                 try {
118                         long nv = ((LongBinding)binding).getValue_(newValue);
119                         setValue(nv);
120                 } catch (BindingException e) {
121                         throw new AccessorException(e);
122                 }
123         }
124
125         @SuppressWarnings("unchecked")
126         @Override
127         public <T extends Accessor> T getComponent(ChildReference reference)
128         throws AccessorConstructionException {
129                 if (reference==null) return (T) this;           
130                 throw new ReferenceException(reference.getClass()+" is not a subreference of LongType");        
131         }
132
133         @Override
134         Event applyLocal(Event e, boolean makeRollback) throws AccessorException {
135                 Event rollback = makeRollback ? new ValueAssigned( Bindings.LONG, getValue() ) : null;          
136                 if (e instanceof ValueAssigned) {
137                         ValueAssigned va = (ValueAssigned) e;
138                         if (va.newValue == null) throw new AccessorException("Long value expected, got null");                  
139                         setValue(va.newValue.getBinding(), va.newValue.getValue());
140                         return rollback;
141                 } else {
142                         throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Long");                 
143                 }
144         }
145
146
147 }
148