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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.common.procedure.guarded;
\r
14 import java.util.concurrent.atomic.AtomicBoolean;
\r
15 import java.util.concurrent.atomic.AtomicInteger;
\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
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
29 * @author Tuukka Lehtonen
\r
33 public class GuardedAsyncProcedureWrapper<T> extends AsyncProcedureAdapter<T> {
\r
35 private final AsyncProcedure<T> procedure;
\r
36 private final AtomicBoolean onceGuard = new AtomicBoolean(false);
\r
37 private final AtomicInteger resultCounter;
\r
39 public GuardedAsyncProcedureWrapper(AsyncProcedure<T> procedure, int expectedResults) {
\r
40 this.procedure = procedure;
\r
41 this.resultCounter = new AtomicInteger(expectedResults);
\r
45 public void exception(AsyncReadGraph graph, Throwable t) {
\r
46 if (onceGuard.compareAndSet(false, true)) {
\r
47 procedure.exception(graph, t);
\r
52 public void execute(AsyncReadGraph graph, T result) {
\r
53 int count = resultCounter.decrementAndGet();
\r
55 if (onceGuard.compareAndSet(false, true)) {
\r
56 procedure.execute(graph, result);
\r
58 } else if (count < 0) {
\r
59 System.out.println(this + ": execute invoked more than expected (count=" + count + ", procedure=" + procedure + ")");
\r