]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/single/wrapper/SingleFunctionalOrNullProcedure.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / procedure / single / wrapper / SingleFunctionalOrNullProcedure.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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.db.common.procedure.single.wrapper;
13
14 import java.util.concurrent.atomic.AtomicBoolean;
15
16 import org.simantics.db.AsyncReadGraph;
17 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
18 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
19 import org.simantics.db.procedure.AsyncProcedure;
20
21 final public class SingleFunctionalOrNullProcedure<Result> extends AsyncMultiProcedureAdapter<Result> {
22     
23         final String location;
24         final AsyncProcedure<Result> procedure;
25     final AtomicBoolean found = new AtomicBoolean(false);
26     final AtomicBoolean guard = new AtomicBoolean(false);
27     final AtomicBoolean exception = new AtomicBoolean(false);
28     
29     public SingleFunctionalOrNullProcedure(String location, AsyncProcedure<Result> procedure) {
30         
31         this.location = location;
32         this.procedure = procedure;
33         
34     }
35     
36     @Override
37     public void execute(AsyncReadGraph graph, Result result) {
38         
39         assert(!guard.get());
40
41         if(found.compareAndSet(false, true)) {
42             procedure.execute(graph, result);
43         } else {
44             if(exception.compareAndSet(false, true)) {
45                 procedure.exception(graph, new ManyObjectsForFunctionalRelationException(location + " -> " + procedure));
46             }
47         }
48         
49     }
50         
51     @Override
52     public void finished(AsyncReadGraph graph) {
53
54         assert(!guard.get());
55         
56         guard.set(true);
57         
58         if(found.compareAndSet(false, true)) {
59             procedure.execute(graph, null);
60         }
61         
62     }
63     
64     @Override
65     public void exception(AsyncReadGraph graph, Throwable t) {
66         
67         procedure.exception(graph, t);
68         
69     }
70     
71     @Override
72     public String toString() {
73         return "SingleFunctionalOrNullProcedure -> " + procedure;
74     }
75
76 }