Skip to content

Commit

Permalink
close specific method. (#14)
Browse files Browse the repository at this point in the history
* close specific method.

* add caught.
  • Loading branch information
portlek committed Aug 17, 2022
1 parent 5496b6d commit cf4e85c
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/main/java/tr/com/infumia/terminable/CompositeTerminable.java
Expand Up @@ -58,6 +58,16 @@ default void closeUnchecked() {
}
}

/**
* closes the specific closeable object.
*
* @param closeable the closeable to close.
*
* @throws CompositeClosingException if something goes wrong when closing it.
*/
void closeSpecific(@NotNull AutoCloseable closeable)
throws CompositeClosingException;

/**
* binds the closeable.
*
Expand Down Expand Up @@ -128,6 +138,26 @@ public void close() throws CompositeClosingException {
}
}

@Override
public void closeSpecific(@NotNull final AutoCloseable closeable)
throws CompositeClosingException {
final var caught = new ArrayList<Exception>();
this.closeables.removeIf(c -> {
final var check = c.equals(closeable);
if (check) {
try {
c.close();
} catch (final Exception e) {
caught.add(e);
}
}
return check;
});
if (!caught.isEmpty()) {
throw new CompositeClosingException(caught);
}
}

@NotNull
@Override
public CompositeTerminable with(@NotNull final AutoCloseable closeable) {
Expand Down

0 comments on commit cf4e85c

Please sign in to comment.