1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.accessor.java;
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;
35 public class JavaLong extends JavaObject implements LongAccessor {
37 public JavaLong(Accessor parent, LongBinding binding, Object object, AccessorParams params) {
38 super(parent, binding, object, params);
41 public LongBinding getBinding() {
42 return (LongBinding) binding;
46 public LongType type() {
47 return getBinding().type();
51 public long getValue() throws AccessorException {
54 return getBinding().getValue_(object);
55 } catch (BindingException e) {
56 throw new AccessorException(e);
63 public void setValue(long newValue) throws AccessorException {
64 if (newValue == getValue()) return;
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);
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);
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);
85 } else if (binding.isImmutable() && parent!=null && parent instanceof JavaOptional) {
87 JavaOptional jo = (JavaOptional) parent;
88 ContainerOptionalBinding vb = (ContainerOptionalBinding) jo.binding;
89 vb.setValue(jo.object, newValue);
90 this.object = newValue;
93 getBinding().setValue(object, newValue);
98 ListenerEntry le = listeners;
100 LongInterestSet is = le.getInterestSet();
101 if (is.inNotifications()) {
102 Event e = new ValueAssigned( Bindings.LONG, is.inValues() ? newValue : null );
108 } catch (BindingException e) {
109 throw new AccessorException(e);
116 public void setValue(Binding binding, Object newValue) throws AccessorException {
118 long nv = ((LongBinding)binding).getValue_(newValue);
120 } catch (BindingException e) {
121 throw new AccessorException(e);
125 @SuppressWarnings("unchecked")
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");
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());
142 throw new AccessorException("Cannot apply "+e.getClass().getName()+" to Long");