Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Make StoredSecret AutoCloseable #11

Description

@ackasaber

StoredSecret interface looks like a good candidate to be AutoCloseable. So instead of this abomination from the sample code

private void userLogin() {
    log.info("Authenticating a user");

    final StoredCredential enteredCredential = enterCredentials();
    StoredCredential storedCredential = null;

    try {
        // Save the credential to the store.
        storedCredential = credentialStorage.get(CREDENTIALS_KEY);

        if (storedCredential.equals(enteredCredential)) {
            log.info("User logged in successfully.");
        } else {
            log.info("Authentication failed.");
        }
    } finally {
        // clear password value
        enteredCredential.clear();

        if (storedCredential != null) {
            storedCredential.clear();
        }
    }
}

you could write this beauty

private void userLogin() {
    log.info("Authenticating a user");

    try (StoredCredential enteredCredential = enterCredentials();
         StoredCredential storedCredential = credentialStorage.get(CREDENTIALS_KEY)) {

        if (storedCredential.equals(enteredCredential)) {
            log.info("User logged in successfully.");
        } else {
            log.info("Authentication failed.");
        }
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions