|
72 | 72 | * <p>A simple example of usage of this interface would be the following: |
73 | 73 | * |
74 | 74 | * <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"); |
79 | 79 | * |
80 | | - * var compilation = someCompiler.compile(workspace); |
| 80 | + * var compilation = someCompiler.compile(workspace); |
81 | 81 | * |
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 | + * }); |
84 | 93 | * </code></pre> |
85 | 94 | * |
86 | 95 | * <p>Remember that files that are created as the result of a compilation can be queried via |
@@ -834,4 +843,52 @@ default List<? extends PathRoot> getModulePathModule(String moduleName) { |
834 | 843 | default Map<String, List<? extends PathRoot>> getModulePathModules() { |
835 | 844 | return getModules(StandardLocation.MODULE_PATH); |
836 | 845 | } |
| 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 | + * @since 3.2.0 |
| 863 | + */ |
| 864 | + @API(since = "3.2.0", status = Status.STABLE) |
| 865 | + default <T extends Throwable> void use(ThrowingWorkspaceConsumer<T> consumer) throws T { |
| 866 | + try { |
| 867 | + consumer.accept(this); |
| 868 | + } finally { |
| 869 | + close(); |
| 870 | + } |
| 871 | + } |
| 872 | + |
| 873 | + /** |
| 874 | + * A consumer functional interface that consumes a workspace and |
| 875 | + * can throw a checked exception. |
| 876 | + * |
| 877 | + * @param <T> the exception type that can be thrown, or {@link RuntimeException} |
| 878 | + * if no checked exception is thrown. |
| 879 | + * @author Ashley Scopes |
| 880 | + * @since 3.2.0 |
| 881 | + */ |
| 882 | + @API(since = "3.2.0", status = Status.STABLE) |
| 883 | + public interface ThrowingWorkspaceConsumer<T extends Throwable> { |
| 884 | + |
| 885 | + /** |
| 886 | + * Consume a workspace. |
| 887 | + * |
| 888 | + * @param workspace the workspace. |
| 889 | + * @throws T the checked exception that can be thrown. |
| 890 | + */ |
| 891 | + void accept(Workspace workspace) throws T; |
| 892 | + } |
837 | 893 | } |
| 894 | + |
0 commit comments