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

org.xlcloud.validation.Validations Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * 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 org.xlcloud.validation;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.xlcloud.rest.exception.ValidationException;
import org.xlcloud.rest.exception.ValidationException.ValidationFailureType;

/**
 * TODO Documentation
 * 
 * @author Tomek Adamczewski, AMG.net
 */
public class Validations {

    private I validatedObject;

    private String objectDescriptor;

    private Validations( I object ) {
        this.validatedObject = object;
        if (object != null) {
            this.objectDescriptor = object.toString();
        }
    }

    public static  Validations validateThat(I object) {
        return new Validations(object);
    }

    public Validations as(String objectDescriptor) {
        if (StringUtils.isNotBlank(objectDescriptor)) {
            this.objectDescriptor = objectDescriptor;
        }
        return this;
    }

    public Validations isNotNull() {
        if (validatedObject == null) {
            throw new ValidationException("validated resource [" + objectDescriptor + "] cannot be null",
                    ValidationFailureType.GENERIC_IS_NULL, objectDescriptor);
        }
        return this;
    }

    public Validations is(Class clazz) {
        isNotNull();
        if (clazz.isAssignableFrom(validatedObject.getClass())) {
            return this;
        }
        throw new ValidationException("validated resource [" + objectDescriptor + "] of type [" + validatedObject.toString()
                + "] was supposed to be of type" + clazz,
                ValidationFailureType.GENERIC_IS_NOT_INSTANCE, objectDescriptor);
    }

    public Validations isNotBlank() {
        is(String.class);
        String stringVal = (String) validatedObject;
        if (StringUtils.isNotBlank(stringVal)) {
            return this;
        }
        throw new ValidationException("validated string [" + objectDescriptor + "] with value [" + stringVal + "] cannot be blank",
                ValidationFailureType.GENERIC_IS_BLANK, objectDescriptor);
    }

    public Validations isNull() {
        if (validatedObject != null) {
            throw new ValidationException("validated resource [" + objectDescriptor + "] must be null",
                    ValidationFailureType.GENERIC_IS_NOT_NULL, objectDescriptor);
        }
        return this;
    }

    public Validations isEqualTo(I expected) {
        if (!ObjectUtils.equals(validatedObject, expected)) {
            throw new ValidationException("validated resource [" + objectDescriptor + "] is not equal to " + expected,
                    ValidationFailureType.GENERIC_NOT_EXPECTED_VALUE, objectDescriptor);
        } else {
            return this;
        }
    }

    public Validations isNotEqualTo(I expected) {
        if (ObjectUtils.equals(validatedObject, expected)) {
            throw new ValidationException("validated resource [" + objectDescriptor + "] is equal to " + expected,
                    ValidationFailureType.GENERIC_NOT_EXPECTED_VALUE, objectDescriptor);
        } else {
            return this;
        }
    }

}