283 /**
284 * Returns the current subject.
285 *
286 * <p> The current subject is installed by the {@link #callAs} method.
287 * When {@code callAs(subject, action)} is called, {@code action} is
288 * executed with {@code subject} as its current subject which can be
289 * retrieved by this method. After {@code action} is finished, the current
290 * subject is reset to its previous value. The current
291 * subject is {@code null} before the first call of {@code callAs()}.
292 *
293 * <p> This method returns the
294 * {@code Subject} bound to the period of the execution of the current
295 * thread.
296 *
297 * @return the current subject, or {@code null} if a current subject is
298 * not installed or the current subject is set to {@code null}.
299 * @see #callAs(Subject, Callable)
300 * @since 18
301 */
302 public static Subject current() {
303 return SCOPED_SUBJECT.orElse(null);
304 }
305
306 /**
307 * Executes a {@code Callable} with {@code subject} as the
308 * current subject.
309 *
310 * <p> This method launches {@code action} and binds {@code subject} to the
311 * period of its execution.
312 *
313 * @param subject the {@code Subject} that the specified {@code action}
314 * will run as. This parameter may be {@code null}.
315 * @param action the code to be run with {@code subject} as its current
316 * subject. Must not be {@code null}.
317 * @param <T> the type of value returned by the {@code call} method
318 * of {@code action}
319 * @return the value returned by the {@code call} method of {@code action}
320 * @throws NullPointerException if {@code action} is {@code null}
321 * @throws CompletionException if {@code action.call()} throws an exception.
322 * The cause of the {@code CompletionException} is set to the exception
323 * thrown by {@code action.call()}.
|
283 /**
284 * Returns the current subject.
285 *
286 * <p> The current subject is installed by the {@link #callAs} method.
287 * When {@code callAs(subject, action)} is called, {@code action} is
288 * executed with {@code subject} as its current subject which can be
289 * retrieved by this method. After {@code action} is finished, the current
290 * subject is reset to its previous value. The current
291 * subject is {@code null} before the first call of {@code callAs()}.
292 *
293 * <p> This method returns the
294 * {@code Subject} bound to the period of the execution of the current
295 * thread.
296 *
297 * @return the current subject, or {@code null} if a current subject is
298 * not installed or the current subject is set to {@code null}.
299 * @see #callAs(Subject, Callable)
300 * @since 18
301 */
302 public static Subject current() {
303 return SCOPED_SUBJECT.isBound() ? SCOPED_SUBJECT.get() : null;
304 }
305
306 /**
307 * Executes a {@code Callable} with {@code subject} as the
308 * current subject.
309 *
310 * <p> This method launches {@code action} and binds {@code subject} to the
311 * period of its execution.
312 *
313 * @param subject the {@code Subject} that the specified {@code action}
314 * will run as. This parameter may be {@code null}.
315 * @param action the code to be run with {@code subject} as its current
316 * subject. Must not be {@code null}.
317 * @param <T> the type of value returned by the {@code call} method
318 * of {@code action}
319 * @return the value returned by the {@code call} method of {@code action}
320 * @throws NullPointerException if {@code action} is {@code null}
321 * @throws CompletionException if {@code action.call()} throws an exception.
322 * The cause of the {@code CompletionException} is set to the exception
323 * thrown by {@code action.call()}.
|