Skip to content

Commit 321c589

Browse files
committed
Implement a functional API for workspaces to be closed
1 parent 38ed82b commit 321c589

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

java-compiler-testing/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<parent>
2323
<groupId>io.github.ascopes.jct</groupId>
2424
<artifactId>java-compiler-testing-parent</artifactId>
25-
<version>3.1.1-SNAPSHOT</version>
25+
<version>3.2.0-SNAPSHOT</version>
2626
<relativePath>../pom.xml</relativePath>
2727
</parent>
2828

java-compiler-testing/src/main/java/io/github/ascopes/jct/workspaces/Workspace.java

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,24 @@
7272
* <p>A simple example of usage of this interface would be the following:
7373
*
7474
* <pre><code>
75-
* try (Workspace workspace = Workspaces.newWorkspace(PathStrategy.TEMP_DIRECTORIES)) {
76-
* workspace
77-
* .createSourcePathPackage()
78-
* .copyContentsFrom("src", "test", "resources", "test-data");
75+
* try (Workspace workspace = Workspaces.newWorkspace()) {
76+
* workspace
77+
* .createSourcePathPackage()
78+
* .copyContentsFrom("src", "test", "resources", "test-data");
7979
*
80-
* var compilation = someCompiler.compile(workspace);
80+
* var compilation = someCompiler.compile(workspace);
8181
*
82-
* assertThat(compilation).isSuccessful();
83-
* }
82+
* assertThat(compilation).isSuccessful();
83+
* }
84+
* </code></pre>
85+
*
86+
* <p>As of 3.2.0, you can use a functional version of the above instead if this is more
87+
* suitable for your use-case:
88+
*
89+
* <pre><code>
90+
* Workspaces.newWorkspace().use(workspace -> {
91+
* ...
92+
* });
8493
* </code></pre>
8594
*
8695
* <p>Remember that files that are created as the result of a compilation can be queried via
@@ -834,4 +843,50 @@ default List<? extends PathRoot> getModulePathModule(String moduleName) {
834843
default Map<String, List<? extends PathRoot>> getModulePathModules() {
835844
return getModules(StandardLocation.MODULE_PATH);
836845
}
846+
847+
///
848+
/// Functional APIs.
849+
///
850+
851+
/**
852+
* Functional equivalent of consuming this object with a try-with-resources.
853+
*
854+
* <p>This workspace will be {@link #close() closed} upon completion of this method or upon
855+
* an exception being raised.
856+
*
857+
* @param consumer the consumer to pass this object to.
858+
* @param <T> the exception that the consumer can throw, or {@link RuntimeException}
859+
* if no checked exception is thrown.
860+
* @throws UncheckedIOException if the closure fails after the consumer is called.
861+
* @throws T the checked exception that the consumer can throw.
862+
*/
863+
default <T extends Throwable> void use(ThrowingWorkspaceConsumer<T> consumer) throws T {
864+
try {
865+
consumer.accept(this);
866+
} finally {
867+
close();
868+
}
869+
}
870+
871+
/**
872+
* A consumer functional interface that consumes a workspace and
873+
* can throw a checked exception.
874+
*
875+
* @param <T> the exception type that can be thrown, or {@link RuntimeException}
876+
* if no checked exception is thrown.
877+
* @author Ashley Scopes
878+
* @since 3.2.0
879+
*/
880+
@API(since = "3.2.0", status = Status.STABLE)
881+
public interface ThrowingWorkspaceConsumer<T extends Throwable> {
882+
883+
/**
884+
* Consume a workspace.
885+
*
886+
* @param workspace the workspace.
887+
* @throws T the checked exception that can be thrown.
888+
*/
889+
void accept(Workspace workspace) throws T;
890+
}
837891
}
892+

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
<groupId>io.github.ascopes.jct</groupId>
2323
<artifactId>java-compiler-testing-parent</artifactId>
24-
<version>3.1.1-SNAPSHOT</version>
24+
<version>3.2.0-SNAPSHOT</version>
2525
<packaging>pom</packaging>
2626

2727
<name>Java Compiler Testing parent project</name>

0 commit comments

Comments
 (0)