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.exception.ManyObjectsForFunctionalRelationException;
19 import org.simantics.db.procedure.AsyncProcedure;
21 final public class SingleFunctionalOrNullProcedure<Result> extends AsyncMultiProcedureAdapter<Result> {
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);
29 public SingleFunctionalOrNullProcedure(String location, AsyncProcedure<Result> procedure) {
31 this.location = location;
32 this.procedure = procedure;
37 public void execute(AsyncReadGraph graph, Result result) {
41 if(found.compareAndSet(false, true)) {
42 procedure.execute(graph, result);
44 if(exception.compareAndSet(false, true)) {
45 procedure.exception(graph, new ManyObjectsForFunctionalRelationException(location + " -> " + procedure));
52 public void finished(AsyncReadGraph graph) {
58 if(found.compareAndSet(false, true)) {
59 procedure.execute(graph, null);
65 public void exception(AsyncReadGraph graph, Throwable t) {
67 procedure.exception(graph, t);
72 public String toString() {
73 return "SingleFunctionalOrNullProcedure -> " + procedure;