/** * Provided consumer is executed just before resource is completed. * <p> * Consumers are executed in registration order. * * @param consumer a consumer which will be executed just before response is completed. * @throws AlreadyCompletedException if resource is already completed. */ synchronized void beforeComplete(Consumer<ResponseHeaders> consumer) { if (state == State.OPEN) { beforeCompleteConsumers.add(consumer); } else { throw new AlreadyCompletedException("Cannot accept new 'beforeComplete consumer'! Headers are sent."); } }
/** * If not completed then runs provided {@link Runnable}, otherwise throws {@link AlreadyCompletedException}. * * @param runnable to run. * @param exceptionMessage a detail message of potential {@link AlreadyCompletedException}. * @throws AlreadyCompletedException if resource is already completed. */ void runIfNotCompleted(Runnable runnable, String exceptionMessage) { // When resource is completed, then write-lock is locked. // It means then if cannot lock a read-lock then provided runnable should not be executed!. if (rwLock.readLock().tryLock()) { try { if (state != State.COMPLETED) { // Lock must be unlocked any-way runnable.run(); } else { throw new AlreadyCompletedException(exceptionMessage); } } finally { rwLock.readLock().unlock(); } } else { throw new AlreadyCompletedException(exceptionMessage); } }
/** * If not completed then executes provided {@link Supplier} and returns it's result, * otherwise throws {@link AlreadyCompletedException}. * * @param supplier to execute. * @param exceptionMessage a detail message of potential {@link AlreadyCompletedException}. * @throws AlreadyCompletedException if resource is already completed. */ <R> R supplyIfNotCompleted(Supplier<R> supplier, String exceptionMessage) { // When resource is completed, then write-lock is locked. // It means then if cannot lock a read-lock then provided runnable should not be executed!. if (rwLock.readLock().tryLock()) { try { if (state != State.COMPLETED) { // Lock must be unlocked any-way return supplier.get(); } else { throw new AlreadyCompletedException(exceptionMessage); } } finally { rwLock.readLock().unlock(); } } else { throw new AlreadyCompletedException(exceptionMessage); } }