com.github.daddo.validation.validator.Util.java Source code

Java tutorial

Introduction

Here is the source code for com.github.daddo.validation.validator.Util.java

Source

/**
 * Copyright 2015 Daniele Monesi
 * 
 * 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.daddo.validation.validator;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.reflect.FieldUtils;

/**
 * 
 * @author Daniele Monesi - monesidn AT gmail.com
 */
public abstract class Util {

    public static Object accessPropertyByName(Object bean, String propertyName) throws PropertyAccessException {
        try {
            return PropertyUtils.getProperty(bean, propertyName);
        } catch (IllegalAccessException ex) {
            throw new PropertyAccessException(ex);
        } catch (InvocationTargetException ex) {
            throw new PropertyAccessException(ex);
        } catch (NoSuchMethodException ex) {
            throw new PropertyAccessException(ex);
        }
    }

    public static Object accessStaticFieldByName(Class<?> clasz, String fieldName) {
        try {
            Field field = FieldUtils.getField(clasz, fieldName);
            return FieldUtils.readStaticField(field);
        } catch (IllegalAccessException ex) {
            throw new StaticFieldAccessException("Unable to access field " + clasz.getName() + "#" + fieldName, ex);
        } catch (IllegalArgumentException ex) {
            throw new StaticFieldAccessException("Unable to access field " + clasz.getName() + "#" + fieldName, ex);
        }
    }

    static boolean isEmpty(Object object) {
        if (object == null)
            return true;

        if (object instanceof String)
            return ((String) object).isEmpty();

        if (object instanceof Collection<?>)
            return ((Collection<?>) object).isEmpty();

        throw new UnexpectedTypeException(object.getClass(), String.class, Collection.class);
    }

    static Boolean safeCastToBoolean(Object object) {
        if (object == null)
            return null;

        if (object instanceof Boolean)
            return (Boolean) object;

        throw new UnexpectedTypeException(object.getClass(), Boolean.class);
    }

    static Calendar safeCastToCalendar(Object object) {

        if (object == null)
            return null;

        if (object instanceof Calendar)
            return (Calendar) object;

        if (object instanceof Date) {
            Calendar result = Calendar.getInstance();
            result.setTime((Date) object);
            return result;
        }

        if (object instanceof Number) {
            Calendar result = Calendar.getInstance();
            result.setTimeInMillis(((Number) object).longValue());
            return result;
        }

        throw new UnexpectedTypeException(object.getClass(), Calendar.class, Date.class, Number.class);
    }

}