All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ehoffman.test.aspects.Validator Maven / Gradle / Ivy

There is a newer version: 1.5.0
Show newest version
package org.ehoffman.test.aspects;

import java.io.Closeable;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Used by broken advice to determine if the supplied parameters are valid.
 * 
 * @author rex
 */
public abstract class Validator implements Closeable {

    private final ConcurrentHashMap users = new ConcurrentHashMap<>();
    private final ConcurrentHashMap issues = new ConcurrentHashMap<>();

    protected abstract boolean validUser(String username);

    protected abstract boolean validIssue(String issue);

    public String getError(final String username, final String issue) {
        if (!users.containsKey(username)) {
            users.putIfAbsent(username, validUser(username));
        }
        if (!issues.containsKey(issue)) {
            issues.putIfAbsent(issue, validIssue(issue));
        }
        final StringBuffer errors = new StringBuffer();
        if (!users.get(username)) {
            errors.append("Username '" + username + "' is not a valid user");
        }
        if (!issues.get(issue)) {
            if (errors.length() > 0) {
                errors.append(" and ");
            }
            errors.append("Issue '" + issue + "' is not found in tracker");
        }
        if (errors.length() > 0) {
            return errors.toString();
        }
        return null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy