Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

close specific method. #14

Merged
merged 2 commits into from Aug 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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