]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.objmap2/src/org/simantics/objmap/graph/impl/RangeUpdateRequest.java
Thread safety changes in objmap2
[simantics/platform.git] / bundles / org.simantics.objmap2 / src / org / simantics / objmap / graph / impl / RangeUpdateRequest.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2013 Association for Decentralized Information Management
3  * in 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.objmap.graph.impl;
13
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.procedure.SyncListener;
17 import org.simantics.db.request.Read;
18
19 import org.simantics.objmap.exceptions.MappingException;
20 import org.simantics.objmap.forward.IForwardMapping;
21 import org.simantics.objmap.graph.impl.Link;
22
23
24 public class RangeUpdateRequest<Domain,Range> implements Read<Integer>, SyncListener<Integer> {
25
26     Link<Domain,Range> link;
27     /*
28      * Note that this map uses a read request that it has got from caller and 
29      * not the one that is used in updateRange. This is intentional.
30      */
31     IForwardMapping<Domain, Range> map; // map==null is used to flag that request is performed once
32     Mapping<Domain, Range> mapping; // mapping==null is used as a flag the request disposed
33     int counter;
34     
35     public RangeUpdateRequest(Link<Domain,Range> link, IForwardMapping<Domain,Range> map, Mapping<Domain,Range> mapping) {
36         this.link = link;
37         this.map = map;
38         this.mapping = mapping;
39         this.counter = 0;
40     }
41
42     @Override
43     public Integer perform(ReadGraph g) throws DatabaseException {
44         boolean changed = false;
45         if (map != null)
46             changed = link.type.checkChanges(g, map, link.domainElement, link.rangeElement);
47         else if (mapping != null)
48             changed = link.type.checkChanges(g, mapping, link.domainElement, link.rangeElement);
49         
50         return changed ? counter + 1 : counter;
51     }
52     
53     @Override
54     public void exception(ReadGraph graph, Throwable throwable)
55             throws DatabaseException {
56         if(throwable instanceof DatabaseException)
57             throw (DatabaseException)throwable;
58         else
59             throw new MappingException(throwable);
60     }
61
62     @Override
63     public void execute(ReadGraph graph, Integer result)
64             throws DatabaseException {
65         boolean changed = result != counter;
66         counter = result;
67         
68         if (map != null) {
69             if (changed)
70                 link.type.updateRange(graph, map, link.domainElement, link.rangeElement);
71             map = null;
72         }
73         else if (mapping != null && changed) {
74             mapping.domainModified(link);
75             mapping = null;
76         }
77     }
78
79     @Override
80     public boolean isDisposed() {
81         return mapping == null || link.removed || mapping.isDisposed();
82     }
83 }