001    /*
002     * Copyright (c) 2013 by Oli B.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express orimplied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     *
016     * (c)reated 29.11.2013 by Oli B. (boehm@javatux.de)
017     */
018    
019    package patterntesting.runtime.util;
020    
021    import java.util.Set;
022    
023    import javax.validation.*;
024    
025    import org.slf4j.*;
026    
027    /**
028     * This utility class is intended to check arguments. It is like the Assert
029     * class in Springframework and throws an {@link IllegalArgumentException} if
030     * the argument is not valid.
031     * <p>
032     * This class is abstract to avoid that it is instantiated. There are only
033     * static methods so there is no need to instantiate it. This is the same way
034     * like the Springframework does it.
035     * </p>
036     * <p>
037     * For more information about the use of bean validation have a look at <a href=
038     * "http://techpatches.blogspot.de/2013/11/bean-validation-hibernate-validator.html"
039     * >Bean Validation with Hibernate Validator framework</a>
040     * </p>.
041     *
042     * @author oliver (boehm@javatux.de)
043     * @since 1.4 (29.11.2013)
044     */
045    public abstract class AssertArg {
046    
047        private static final Logger log = LoggerFactory.getLogger(AssertArg.class);
048    
049        /**
050         * Checks if the argument is valid. If not (or if is null) an
051         * {@link IllegalArgumentException} will be thrown.
052         *
053         * @param validatable the validatable argument
054         */
055        public static void isValid(final Validator validatable) {
056            isValid(validatable, validatable);
057        }
058    
059        /**
060         * Checks if the argument is valid. If not (or if is null) an
061         * {@link IllegalArgumentException} will be thrown.
062         *
063         * @param argument the argument
064         * @param validator the validator
065         */
066        public static void isValid(final Object argument, final Validator validator) {
067            if (argument == null) {
068                throw new IllegalArgumentException("argument is null");
069            }
070            if (validator == null) {
071                log.debug("No validator given to validate {}.", argument);
072            } else {
073                Set<ConstraintViolation<Object>> violations = validator.validate(argument);
074                if ((violations != null) && (violations.size() > 0)) {
075                    throw new IllegalArgumentException(argument + " is invalid: " + violations);
076                }
077            }
078        }
079    
080    }
081