]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/single/wrapper/NullSingleOrNullProcedure.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / procedure / single / wrapper / NullSingleOrNullProcedure.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.common.procedure.single.wrapper;\r
13 \r
14 import org.simantics.db.AsyncReadGraph;\r
15 import org.simantics.db.common.utils.Logger;\r
16 import org.simantics.db.procedure.AsyncProcedure;\r
17 \r
18 final public class NullSingleOrNullProcedure<Result> {\r
19 \r
20         private Result result = null;\r
21         final AsyncProcedure<Result> procedure;\r
22         \r
23         boolean done = false;\r
24         boolean found = false;\r
25         int ready = 1;\r
26         \r
27 //      final AtomicBoolean done = new AtomicBoolean(false);\r
28 //      final AtomicBoolean found = new AtomicBoolean(false);\r
29 //      final AtomicInteger ready = new AtomicInteger(1);\r
30 \r
31         public NullSingleOrNullProcedure(AsyncProcedure<Result> procedure) {\r
32 \r
33                 this.procedure = procedure;\r
34 \r
35         }\r
36 \r
37         public void inc() {\r
38                 ready++;\r
39 //              ready.incrementAndGet();\r
40         }\r
41 \r
42         public void dec(AsyncReadGraph graph) {\r
43 \r
44                 if((--ready) == 0) {\r
45                         // Shall fire exactly once!\r
46                         if(!done) {\r
47                                 done = true;\r
48                                 try {\r
49                                         procedure.execute(graph, result);\r
50                                 } catch (Throwable t) {\r
51                                 Logger.defaultLogError(t);\r
52                                 }\r
53                         }\r
54                 }\r
55 \r
56         }\r
57 \r
58         public void offer(AsyncReadGraph graph, Result result) {\r
59 \r
60                 if(!found) {\r
61                         found = true;\r
62                         this.result = result;\r
63                 } else {\r
64                         // Shall fire exactly once!\r
65                         if(!done) {\r
66                                 done = true;\r
67                                 try {\r
68                                         procedure.execute(graph, null);\r
69                                 } catch (Throwable t) {\r
70                                 Logger.defaultLogError(t);\r
71                                 }\r
72                         }\r
73                 }\r
74 \r
75         }\r
76 \r
77         public void exception(AsyncReadGraph graph, Throwable t) {\r
78                 // Shall fire exactly once!\r
79                 if(!done) {\r
80                         done = true;\r
81                         try {\r
82                                 procedure.exception(graph, t);\r
83                         } catch (Throwable t2) {\r
84                         Logger.defaultLogError(t2);\r
85                         }\r
86                 }\r
87         }\r
88 \r
89 }\r