com.github.nakamurakj.validator.BeanValidator.java Source code

Java tutorial

Introduction

Here is the source code for com.github.nakamurakj.validator.BeanValidator.java

Source

/*
 * Copyright 2015 https://github.com/nakamurakj
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.nakamurakj.validator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.apache.commons.lang3.StringUtils;

/**
 * BeanValidator(@Valid?????Handler)
 */
public final class BeanValidator {

    /** {@code javax.validation.ValidatorFactory} */
    private static final ValidatorFactory FACTORY = Validation.buildDefaultValidatorFactory();

    /**
     * 
     */
    private BeanValidator() {
        // ignore
    }

    /**
     * {@code javax.validation.Validator}??
     *
     * @return {@code javax.validation.Validator}
     */
    public static Validator getValidator() {
        return FACTORY.getValidator();
    }

    /**
     * javax.validation????Bean?Validation?
     *
     * @param bean ?Bean
     * @return {@code ValidateMessage}?
     * @throws IllegalArgumentException 
     */
    public static <T> List<ValidateMessage<T>> validateBean(final T bean) throws IllegalArgumentException {
        if (bean == null) {
            throw new IllegalArgumentException("bean is null");
        }

        final Set<ConstraintViolation<T>> constraintViolations = getValidator().validate(bean);
        final int size = constraintViolations.size();
        final List<ValidateMessage<T>> messages = new ArrayList<ValidateMessage<T>>(size);
        if (size > 0) {
            final Iterator<ConstraintViolation<T>> ite = constraintViolations.iterator();
            while (ite.hasNext()) {
                messages.add(new ValidateMessage<T>(ite.next()));
            }
        }
        return messages;
    }

    /**
     * ?????
     *
     * @param <T> ?
     */
    public static final class ValidateMessage<T> {

        /**
         * {@link ConstraintViolation}
         */
        private final ConstraintViolation<T> constraintViolation;

        /** ??Bean */
        private final T rootBean;

        /** ??Bean??? */
        private final String rootBeanName;

        /** ??? */
        private final String targetProperty;

        /**  */
        private final String message;

        /**
         * 
         *
         * @param vio {@code ConstraintViolation}
         */
        private ValidateMessage(final ConstraintViolation<T> vio) {
            constraintViolation = vio;
            rootBean = vio.getRootBean();
            rootBeanName = vio.getRootBeanClass().getSimpleName();
            targetProperty = vio.getPropertyPath().toString();
            message = vio.getMessage();
        }

        /**
         * {@link ConstraintViolation}??
         *
         * @return constraintViolation {@link ConstraintViolation}
         */
        public ConstraintViolation<T> getConstraintViolation() {
            return constraintViolation;
        }

        /**
         * ??Bean??
         *
         * @return rootBean ??Bean
         */
        public T getRootBean() {
            return rootBean;
        }

        /**
         * ??Bean?????
         *
         * @return rootBeanName ??Bean???
         */
        public String getRootBeanName() {
            return rootBeanName;
        }

        /**
         * ?????
         *
         * @return targetProperty ???
         */
        public String getTargetProperty() {
            return targetProperty;
        }

        /**
         * ??
         *
         * @return message 
         */
        public String getMessage() {
            return message;
        }

        /**
         * ??
         *
         * @return 
         */
        public String getError() {
            return "Invalid" + StringUtils.capitalize(targetProperty);
        }

        /**
         * ??
         *
         * @return 
         */
        public String createErrorMessage() {
            return rootBeanName + "#" + targetProperty + "[" + message + "]";
        }
    }

}