1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.common.procedure.single.wrapper;
14 import java.util.concurrent.atomic.AtomicBoolean;
16 import org.simantics.db.AsyncReadGraph;
17 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;
18 import org.simantics.db.common.utils.Logger;
19 import org.simantics.db.procedure.AsyncProcedure;
21 final public class SingleOrNullProcedure<Result> extends AsyncMultiProcedureAdapter<Result> {
23 private Result result = null;
24 final AsyncProcedure<Result> procedure;
25 final AtomicBoolean found = new AtomicBoolean(false);
26 final AtomicBoolean done = new AtomicBoolean(false);
28 public SingleOrNullProcedure(AsyncProcedure<Result> procedure) {
29 this.procedure = procedure;
33 public void finished(AsyncReadGraph graph) {
35 // Shall fire exactly once!
36 if(done.compareAndSet(false, true)) {
38 procedure.execute(graph, result);
39 } catch (Throwable t) {
40 Logger.defaultLogError(t);
47 public void execute(AsyncReadGraph graph, Result result) {
49 if(found.compareAndSet(false, true)) {
52 // Shall fire exactly once!
53 if(done.compareAndSet(false, true)) {
55 procedure.execute(graph, null);
56 } catch (Throwable t) {
57 Logger.defaultLogError(t);
66 public void exception(AsyncReadGraph graph, Throwable t) {
67 // Shall fire exactly once!
68 if(done.compareAndSet(false, true)) {
70 procedure.exception(graph, t);
71 } catch (Throwable t2) {
72 Logger.defaultLogError(t2);
78 public String toString() {
79 return "SingleOrNullProcedure -> " + procedure;