com.bagdemir.eboda.validator.PoolSizeValidator.java Source code

Java tutorial

Introduction

Here is the source code for com.bagdemir.eboda.validator.PoolSizeValidator.java

Source

/*
 *   Copyright (C) 2013 "Erhan Bagdemir"
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.bagdemir.eboda.validator;

import org.apache.commons.lang.StringUtils;

public class PoolSizeValidator implements Validator<String, String> {

    @Override
    public ValidationResult<String> validate(final String input) {

        final ValidationResult<String> validationResult = new ValidationResult();

        validationResult.setInput(input);

        if (StringUtils.isBlank(input)) {
            registerError(validationResult, "Pool size must be an integer value, but it was null.");
        } else {
            validatePoolSize(input, validationResult);
        }

        return validationResult;
    }

    private void validatePoolSize(final String input, final ValidationResult<String> validationResult) {
        try {
            int poolsize = Integer.parseInt(input);
            if (poolsize < 1 || poolsize > 100) {
                registerError(validationResult, String.format("Use pool size in range [1-100].", input));
            } else {
                validationResult.setInput(input);
            }
        } catch (NumberFormatException exception) {
            registerError(validationResult, String.format("Pool size must be an integer value.", input));
        }
    }

    private void registerError(final ValidationResult<String> validationResult, final String message) {
        validationResult.setValid(false);
        validationResult.setError(message);
    }
}