Make BulkChange auto-closeable

so that it can be used as below:

    try (BulkChange bc = new BulkChange(o)) {
       ...
       bc.commit();
    }
This commit is contained in:
Kohsuke Kawaguchi committed 2016-06-28 13:56:32 -07:00
1 parent 533156c7c4
commit 5b4dbb3970
5 files changed
+20 -45

No files matched your search

+11 -17
View File
@@ -25,6 +25,7 @@ package hudson;
import hudson.model.Saveable;
import java.io.Closeable;
import java.io.IOException;
/**
@@ -35,24 +36,9 @@ import java.io.IOException;
* The usage of {@link BulkChange} needs to follow a specific closure-like pattern, namely:
*
* <pre>
* BulkChange bc = new BulkChange(someObject);
* try {
* ... make changes to 'someObject'
* } finally {
* bc.commit();
* }
* </pre>
*
* <p>
* ... or if you'd like to avoid saving when something bad happens:
*
* <pre>
* BulkChange bc = new BulkChange(someObject);
* try {
* try (BulkChange bc = new BulkChange(someObject)) {
* ... make changes to 'someObject'
* bc.commit();
* } finally {
* bc.abort();
* }
* </pre>
*
@@ -82,7 +68,7 @@ import java.io.IOException;
* @author Kohsuke Kawaguchi
* @since 1.249
*/
public class BulkChange {
public class BulkChange implements Closeable {
private final Saveable saveable;
public final Exception allocator;
private final BulkChange parent;
@@ -112,6 +98,14 @@ public class BulkChange {
saveable.save();
}
/**
* Alias for {@link #abort()} to make {@link BulkChange} auto-closeable.
*/
@Override
public void close() {
abort();
}
/**
* Exits the scope of {@link BulkChange} without saving the changes.
*