18
18
import db .TerminatedTransactionException ;
19
19
import db .Transaction ;
20
20
import ghidra .framework .store .LockException ;
21
+ import utility .function .ExceptionalCallback ;
22
+ import utility .function .ExceptionalSupplier ;
21
23
22
24
/**
23
25
* <code>UndoableDomainObject</code> extends a domain object to provide transaction
@@ -48,6 +50,72 @@ public interface UndoableDomainObject extends DomainObject, Undoable {
48
50
*/
49
51
public Transaction openTransaction (String description ) throws IllegalStateException ;
50
52
53
+ /**
54
+ * Performs the given callback inside of a transaction. Use this method in place of the more
55
+ * verbose try/catch/finally semantics.
56
+ * <p>
57
+ * <pre>
58
+ * program.withTransaction("My Description", () -> {
59
+ * // ... Do something
60
+ * });
61
+ * </pre>
62
+ *
63
+ * <p>
64
+ * Note: the transaction created by this method will always be committed when the call is
65
+ * finished. If you need the ability to abort transactions, then you need to use the other
66
+ * methods on this interface.
67
+ *
68
+ * @param description brief description of transaction
69
+ * @param callback the callback that will be called inside of a transaction
70
+ * @throws E any exception that may be thrown in the given callback
71
+ */
72
+ public default <E extends Exception > void withTransaction (String description ,
73
+ ExceptionalCallback <E > callback ) throws E {
74
+ int id = startTransaction (description );
75
+ try {
76
+ callback .call ();
77
+ }
78
+ finally {
79
+ endTransaction (id , true );
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Calls the given supplier inside of a transaction. Use this method in place of the more
85
+ * verbose try/catch/finally semantics.
86
+ * <p>
87
+ * <pre>
88
+ * program.withTransaction("My Description", () -> {
89
+ * // ... Do something
90
+ * return result;
91
+ * });
92
+ * </pre>
93
+ * <p>
94
+ * If you do not need to supply a result, then use
95
+ * {@link #withTransaction(String, ExceptionalCallback)} instead.
96
+ *
97
+ * @param <E> the exception that may be thrown from this method
98
+ * @param <T> the type of result returned by the supplier
99
+ * @param description brief description of transaction
100
+ * @param supplier the supplier that will be called inside of a transaction
101
+ * @return the result returned by the supplier
102
+ * @throws E any exception that may be thrown in the given callback
103
+ */
104
+ public default <E extends Exception , T > T withTransaction (String description ,
105
+ ExceptionalSupplier <T , E > supplier ) throws E {
106
+ T t = null ;
107
+ boolean success = false ;
108
+ int id = startTransaction (description );
109
+ try {
110
+ t = supplier .get ();
111
+ success = true ;
112
+ }
113
+ finally {
114
+ endTransaction (id , success );
115
+ }
116
+ return t ;
117
+ }
118
+
51
119
/**
52
120
* Start a new transaction in order to make changes to this domain object.
53
121
* All changes must be made in the context of a transaction.
@@ -90,8 +158,8 @@ public interface UndoableDomainObject extends DomainObject, Undoable {
90
158
public TransactionInfo getCurrentTransactionInfo ();
91
159
92
160
/**
93
- * Returns true if the last transaction was terminated externally from the action that
94
- * started it.
161
+ * Returns true if the last transaction was terminated from the action that started it.
162
+ * @return true if the last transaction was terminated from the action that started it.
95
163
*/
96
164
public boolean hasTerminatedTransaction ();
97
165
@@ -108,7 +176,7 @@ public interface UndoableDomainObject extends DomainObject, Undoable {
108
176
* using a shared transaction manager. If either or both is already shared,
109
177
* a transition to a single shared transaction manager will be
110
178
* performed.
111
- * @param domainObj
179
+ * @param domainObj the domain object
112
180
* @throws LockException if lock or open transaction is active on either
113
181
* this or the specified domain object
114
182
*/
0 commit comments