]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/guarded/GuardedAsyncProcedureWrapper.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / procedure / guarded / GuardedAsyncProcedureWrapper.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.guarded;\r
13 \r
14 import java.util.concurrent.atomic.AtomicBoolean;\r
15 import java.util.concurrent.atomic.AtomicInteger;\r
16 \r
17 import org.simantics.db.AsyncReadGraph;\r
18 import org.simantics.db.common.procedure.adapter.AsyncProcedureAdapter;\r
19 import org.simantics.db.procedure.AsyncProcedure;\r
20 import org.simantics.db.procedure.Procedure;\r
21 \r
22 /**\r
23  * An asynchronous database callback procedure that is guarded against multiple\r
24  * invocations to {@link Procedure#execute(Object)} and\r
25  * {@link Procedure#exception(Throwable)}. Only the first invocation of is\r
26  * delegated or the Nth invocation to {@link Procedure#execute(Object)} where\r
27  * Nth is <code>expectedResults</code>.\r
28  * \r
29  * @author Tuukka Lehtonen\r
30  * \r
31  * @param <T>\r
32  */\r
33 public class GuardedAsyncProcedureWrapper<T> extends AsyncProcedureAdapter<T> {\r
34 \r
35     private final AsyncProcedure<T> procedure;\r
36     private final AtomicBoolean     onceGuard = new AtomicBoolean(false);\r
37     private final AtomicInteger     resultCounter;\r
38 \r
39     public GuardedAsyncProcedureWrapper(AsyncProcedure<T> procedure, int expectedResults) {\r
40         this.procedure = procedure;\r
41         this.resultCounter = new AtomicInteger(expectedResults);\r
42     }\r
43 \r
44     @Override\r
45     public void exception(AsyncReadGraph graph, Throwable t) {\r
46         if (onceGuard.compareAndSet(false, true)) {\r
47             procedure.exception(graph, t);\r
48         }\r
49     }\r
50 \r
51     @Override\r
52     public void execute(AsyncReadGraph graph, T result) {\r
53         int count = resultCounter.decrementAndGet();\r
54         if (count == 0) {\r
55             if (onceGuard.compareAndSet(false, true)) {\r
56                 procedure.execute(graph, result);\r
57             }\r
58         } else if (count < 0) {\r
59             System.out.println(this + ": execute invoked more than expected (count=" + count + ", procedure=" + procedure + ")");\r
60         }\r
61     }\r
62 \r
63 }\r