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

@ackasaber

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.");
        }
    }
}

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