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