]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/procedure/single/wrapper/SingleOrErrorProcedure.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / procedure / single / wrapper / SingleOrErrorProcedure.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 java.util.concurrent.atomic.AtomicBoolean;\r
15 \r
16 import org.simantics.db.AsyncReadGraph;\r
17 import org.simantics.db.common.procedure.adapter.AsyncMultiProcedureAdapter;\r
18 import org.simantics.db.common.utils.Logger;\r
19 import org.simantics.db.exception.NoSingleResultException;\r
20 import org.simantics.db.procedure.AsyncProcedure;\r
21 \r
22 final public class SingleOrErrorProcedure<Result> extends AsyncMultiProcedureAdapter<Result> {\r
23     \r
24         private Result result = null;\r
25         final AsyncProcedure<Result> procedure;\r
26     final AtomicBoolean found = new AtomicBoolean(false);\r
27     final AtomicBoolean done = new AtomicBoolean(false);\r
28     \r
29     public SingleOrErrorProcedure(AsyncProcedure<Result> procedure) {\r
30         this.procedure = procedure;\r
31     }\r
32         \r
33     @Override\r
34     public void finished(AsyncReadGraph graph) {\r
35         \r
36         // Shall fire exactly once!\r
37         if(done.compareAndSet(false, true)) {\r
38                 try {\r
39                         if(result == null) {\r
40                                         procedure.exception(graph, new NoSingleResultException("No items " + procedure));\r
41                         } else {\r
42                                 procedure.execute(graph, result);\r
43                         }\r
44                 } catch (Throwable t) {\r
45                         Logger.defaultLogError(t);\r
46                 }\r
47         }\r
48         \r
49     }\r
50     \r
51         public void execute(AsyncReadGraph graph, Result result) {\r
52 \r
53                 if(found.compareAndSet(false, true)) {\r
54                         this.result = result;\r
55                 } else {\r
56                         // Shall fire exactly once!\r
57                         if(done.compareAndSet(false, true)) {\r
58                                 try {\r
59                                         procedure.exception(graph, new NoSingleResultException("Multiple items " + this.result + " and " + result));\r
60                                 } catch (Throwable t) {\r
61                                 Logger.defaultLogError(t);\r
62                                 }\r
63                         }\r
64                 }\r
65 \r
66         }\r
67 \r
68         public void exception(AsyncReadGraph graph, Throwable t) {\r
69                 // Shall fire exactly once!\r
70                 if(done.compareAndSet(false, true)) {\r
71                         try {\r
72                                 procedure.exception(graph, t);\r
73                         } catch (Throwable t2) {\r
74                         Logger.defaultLogError(t2);\r
75                         }\r
76                 }\r
77         }\r
78     \r
79     \r
80     @Override\r
81     public String toString() {\r
82         return "SingleOrErrorProcedure -> " + procedure;\r
83     }\r
84 \r
85 }\r