@@ -106,7 +106,7 @@ synchronized void addOutput(String s) {
106106 final ConcurrentMap <Integer , RequestInfo > activeRequests = new ConcurrentHashMap <>();
107107
108108 /** The function to be called after each {@link WorkRequest} is read. */
109- private final BiFunction < List < String >, PrintWriter , Integer > callback ;
109+ private final WorkRequestCallback callback ;
110110
111111 /** This worker's stderr. */
112112 private final PrintStream stderr ;
@@ -129,6 +129,7 @@ synchronized void addOutput(String s) {
129129 * @param messageProcessor Object responsible for parsing {@code WorkRequest}s from the server and
130130 * writing {@code WorkResponses} to the server.
131131 */
132+ @ Deprecated
132133 public WorkRequestHandler (
133134 BiFunction <List <String >, PrintWriter , Integer > callback ,
134135 PrintStream stderr ,
@@ -163,23 +164,69 @@ public WorkRequestHandler(
163164 /**
164165 * Creates a {@code WorkRequestHandler} that will call {@code callback} for each WorkRequest
165166 * received. Only used for the Builder.
167+ *
168+ * @deprecated Use WorkRequestHandlerBuilder instead.
166169 */
170+ @ Deprecated
167171 private WorkRequestHandler (
168172 BiFunction <List <String >, PrintWriter , Integer > callback ,
169173 PrintStream stderr ,
170174 WorkerMessageProcessor messageProcessor ,
171175 Duration cpuUsageBeforeGc ,
172176 BiConsumer <Integer , Thread > cancelCallback ) {
177+ this (
178+ new WorkRequestCallback ((request , pw ) -> callback .apply (request .getArgumentsList (), pw )),
179+ stderr ,
180+ messageProcessor ,
181+ cpuUsageBeforeGc ,
182+ cancelCallback );
183+ }
184+
185+ /**
186+ * Creates a {@code WorkRequestHandler} that will call {@code callback} for each WorkRequest
187+ * received. Only used for the Builder.
188+ *
189+ * @param callback WorkRequestCallback object with Callback method for executing a single
190+ * WorkRequest in a thread. The first argument to {@code callback} is the WorkRequest, the
191+ * second is where all error messages and other user-oriented messages should be written to.
192+ * The callback must return an exit code indicating success (zero) or failure (nonzero).
193+ */
194+ private WorkRequestHandler (
195+ WorkRequestCallback callback ,
196+ PrintStream stderr ,
197+ WorkerMessageProcessor messageProcessor ,
198+ Duration cpuUsageBeforeGc ,
199+ BiConsumer <Integer , Thread > cancelCallback ) {
173200 this .callback = callback ;
174201 this .stderr = stderr ;
175202 this .messageProcessor = messageProcessor ;
176203 this .gcScheduler = new CpuTimeBasedGcScheduler (cpuUsageBeforeGc );
177204 this .cancelCallback = cancelCallback ;
178205 }
179206
207+ /** A wrapper class for the callback BiFunction */
208+ public static class WorkRequestCallback {
209+
210+ /**
211+ * Callback method for executing a single WorkRequest in a thread. The first argument to {@code
212+ * callback} is the WorkRequest, the second is where all error messages and other user-oriented
213+ * messages should be written to. The callback must return an exit code indicating success
214+ * (zero) or failure (nonzero).
215+ */
216+ private final BiFunction <WorkRequest , PrintWriter , Integer > callback ;
217+
218+ public WorkRequestCallback (BiFunction <WorkRequest , PrintWriter , Integer > callback ) {
219+ this .callback = callback ;
220+ }
221+
222+ public Integer apply (WorkRequest workRequest , PrintWriter printWriter ) {
223+ return callback .apply (workRequest , printWriter );
224+ }
225+ }
226+
180227 /** Builder class for WorkRequestHandler. Required parameters are passed to the constructor. */
181228 public static class WorkRequestHandlerBuilder {
182- private final BiFunction < List < String >, PrintWriter , Integer > callback ;
229+ private final WorkRequestCallback callback ;
183230 private final PrintStream stderr ;
184231 private final WorkerMessageProcessor messageProcessor ;
185232 private Duration cpuUsageBeforeGc = Duration .ZERO ;
@@ -195,11 +242,32 @@ public static class WorkRequestHandlerBuilder {
195242 * @param stderr Stream that log messages should be written to, typically the process' stderr.
196243 * @param messageProcessor Object responsible for parsing {@code WorkRequest}s from the server
197244 * and writing {@code WorkResponses} to the server.
245+ * @deprecated use WorkRequestHandlerBuilder with WorkRequestCallback instead
198246 */
247+ @ Deprecated
199248 public WorkRequestHandlerBuilder (
200249 BiFunction <List <String >, PrintWriter , Integer > callback ,
201250 PrintStream stderr ,
202251 WorkerMessageProcessor messageProcessor ) {
252+ this (
253+ new WorkRequestCallback ((request , pw ) -> callback .apply (request .getArgumentsList (), pw )),
254+ stderr ,
255+ messageProcessor );
256+ }
257+
258+ /**
259+ * Creates a {@code WorkRequestHandlerBuilder}.
260+ *
261+ * @param callback WorkRequestCallback object with Callback method for executing a single
262+ * WorkRequest in a thread. The first argument to {@code callback} is the WorkRequest, the
263+ * second is where all error messages and other user-oriented messages should be written to.
264+ * The callback must return an exit code indicating success (zero) or failure (nonzero).
265+ * @param stderr Stream that log messages should be written to, typically the process' stderr.
266+ * @param messageProcessor Object responsible for parsing {@code WorkRequest}s from the server
267+ * and writing {@code WorkResponses} to the server.
268+ */
269+ public WorkRequestHandlerBuilder (
270+ WorkRequestCallback callback , PrintStream stderr , WorkerMessageProcessor messageProcessor ) {
203271 this .callback = callback ;
204272 this .stderr = stderr ;
205273 this .messageProcessor = messageProcessor ;
@@ -287,7 +355,7 @@ void respondToRequest(WorkRequest request, RequestInfo requestInfo) throws IOExc
287355 PrintWriter pw = new PrintWriter (sw )) {
288356 int exitCode ;
289357 try {
290- exitCode = callback .apply (request . getArgumentsList () , pw );
358+ exitCode = callback .apply (request , pw );
291359 } catch (RuntimeException e ) {
292360 e .printStackTrace (pw );
293361 exitCode = 1 ;
0 commit comments