com.discovery.darchrow.util.Validator.java Source code

Java tutorial

Introduction

Here is the source code for com.discovery.darchrow.util.Validator.java

Source

/*
 * Copyright (C) 2008 feilong
 *
 * 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.discovery.darchrow.util;

import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;

/**
 * <b></b>,?nullEmpty.
 * 
 * <ol>
 * <li>{@link #isNullOrEmpty(Object)} ?null</li>
 * <li>{@link #isNotNullOrEmpty(Object)}??null?</li>
 * </ol>
 * 
 * <h3>empty,//:</h3>
 * 
 * <blockquote>
 * <ol>
 * <li>{@link Collection}, {@link Collection#isEmpty()};</li>
 * <li>{@link Map}, {@link Map#isEmpty()};</li>
 * <li>{@link String}, {@link String#trim()}{@code .length()<=0};</li>
 * <li>{@link Enumeration}, {@link Enumeration#hasMoreElements()};</li>
 * <li>{@link Iterator}, {@link Iterator#hasNext()};</li>
 * <li><code>Object[]</code>,length==0;:?primitive ,instanceof Object[];</li>
 * <li><code>byte[]</code>,length==0;</li>
 * <li><code>boolean[]</code>,length==0;</li>
 * <li><code>char[]</code>,length==0;</li>
 * <li><code>int[]</code>,length==0;</li>
 * <li><code>short[]</code>,length==0;</li>
 * <li><code>float[]</code>,length==0;</li>
 * <li><code>double[]</code>,length==0;</li>
 * </ol>
 * </blockquote>
 * 
 * @author feilong
 * @version 1.0.0 Sep 2, 2010 8:35:28 PM
 * @version 1.0.1 2012-9-23 21:34 rename method,isNullOrEmptyisNull
 * @version 1.0.7 2014-5-22 15:57 add {@link #arrayIsNullOrEmpty(Object)}
 * @see String#trim()
 * @see Map#isEmpty()
 * @see Collection#isEmpty()
 * @see Enumeration#hasMoreElements()
 * @see Iterator#hasNext()
 * @since 1.0.0
 */
public final class Validator {

    /** Don't let anyone instantiate this class. */
    private Validator() {
        //AssertionError?. ?????. ???.
        //see Effective Java 2nd
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    /**
     * ?NullEmpty
     * 
     * <h3>empty,//:</h3>
     * 
     * <blockquote>
     * <ol>
     * <li>{@link Collection}, {@link Collection#isEmpty()};</li>
     * <li>{@link Map}, {@link Map#isEmpty()};</li>
     * <li>{@link String}, {@link String#trim()}{@code .length()<=0};</li>
     * <li>{@link Enumeration}, {@link Enumeration#hasMoreElements()};</li>
     * <li>{@link Iterator}, {@link Iterator#hasNext()};</li>
     * <li><code>Object[]</code>,length==0;:?primitive ,instanceof Object[];</li>
     * <li><code>byte[]</code>,length==0;</li>
     * <li><code>boolean[]</code>,length==0;</li>
     * <li><code>char[]</code>,length==0;</li>
     * <li><code>int[]</code>,length==0;</li>
     * <li><code>short[]</code>,length==0;</li>
     * <li><code>float[]</code>,length==0;</li>
     * <li><code>double[]</code>,length==0;</li>
     * </ol>
     * </blockquote>
     * 
     * @param value
     *            ?Collection,Map,String,Enumeration,Iterator,?
     * @return null,true<br>
     *         emptytrue<br>
     *         false<br>
     *         ?,?empty,false
     * @see org.apache.commons.collections.CollectionUtils#isEmpty(Collection)
     * @see org.apache.commons.collections.CollectionUtils#isNotEmpty(Collection)
     * @see org.apache.commons.collections.MapUtils#isEmpty(Map)
     * @see org.apache.commons.collections.MapUtils#isNotEmpty(Map)
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(byte[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(boolean[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(char[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(int[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(long[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(short[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(float[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(double[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(Object[])
     * @see org.apache.commons.lang.StringUtils#isBlank(String)
     * @see org.apache.commons.lang.StringUtils#isEmpty(String)
     */
    public static final boolean isNullOrEmpty(Object value) {
        if (null == value) {
            return true;
        }
        // *****************************************************************************

        // 
        if (value instanceof String) {// , 
            return value.toString().trim().length() <= 0;
        }

        // ?
        if (value instanceof Collection) {
            return ((Collection<?>) value).isEmpty();
        }

        // map
        if (value instanceof Map) {
            return ((Map<?, ?>) value).isEmpty();
        }

        // 
        if (value instanceof Enumeration) {
            return !((Enumeration<?>) value).hasMoreElements();
        }

        // Iterator
        if (value instanceof Iterator) {
            return !((Iterator<?>) value).hasNext();
        }

        boolean arrayFlag = arrayIsNullOrEmpty(value);
        if (arrayFlag) {
            return true;
        }
        // ?
        return false;
    }

    /**
     * ??NullEmpty, !{@link #isNullOrEmpty(Object)}  <br>
     * 
     * <h3>empty,//:</h3>
     * 
     * <blockquote>
     * <ol>
     * <li>{@link Collection}, {@link Collection#isEmpty()};</li>
     * <li>{@link Map}, {@link Map#isEmpty()};</li>
     * <li>{@link String}, {@link String#trim()}{@code .length()<=0};</li>
     * <li>{@link Enumeration}, {@link Enumeration#hasMoreElements()};</li>
     * <li>{@link Iterator}, {@link Iterator#hasNext()};</li>
     * <li><code>Object[]</code>,length==0;:?primitive ,instanceof Object[];</li>
     * <li><code>byte[]</code>,length==0;</li>
     * <li><code>boolean[]</code>,length==0;</li>
     * <li><code>char[]</code>,length==0;</li>
     * <li><code>int[]</code>,length==0;</li>
     * <li><code>short[]</code>,length==0;</li>
     * <li><code>float[]</code>,length==0;</li>
     * <li><code>double[]</code>,length==0;</li>
     * </ol>
     * </blockquote>
     * 
     * @param value
     *            ?Collection,Map,String,Enumeration,Iterator,?
     * @return null,false<br>
     *         false<br>
     *         true<br>
     *         ?,?empty,true
     * @see org.apache.commons.collections.CollectionUtils#isEmpty(Collection)
     * @see org.apache.commons.collections.CollectionUtils#isNotEmpty(Collection)
     * @see org.apache.commons.collections.MapUtils#isEmpty(Map)
     * @see org.apache.commons.collections.MapUtils#isNotEmpty(Map)
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(byte[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(boolean[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(char[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(int[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(long[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(short[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(float[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(double[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(Object[])
     * @see org.apache.commons.lang.StringUtils#isBlank(String)
     * @see org.apache.commons.lang.StringUtils#isEmpty(String)
     */
    public static final boolean isNotNullOrEmpty(Object value) {
        return !isNullOrEmpty(value);
    }

    /**
     *  ?, primitive .
     * 
     * @param value
     *            ?
     *            <ul>
     *            <li>Object[] </li>
     *            <li>byte[]</li>
     *            <li>boolean[]</li>
     *            <li>char[]</li>
     *            <li>int[]</li>
     *            <li>long[]</li>
     *            <li>short[]</li>
     *            <li>float[]</li>
     *            <li>double[]</li>
     *            </ul>
     * @return ( primitive),length==0;<br>
     *         ? false
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(byte[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(boolean[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(char[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(int[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(long[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(short[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(float[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(double[])
     * @see org.apache.commons.lang.ArrayUtils#isEmpty(Object[])
     * @since 1.0.7
     */
    private static boolean arrayIsNullOrEmpty(Object value) {
        // ***********************************************************
        //  Integer/String...User. instanceof Object[]
        if (value instanceof Object[]) {
            return ((Object[]) value).length == 0;
        }

        // ***********************************************************
        // primitive ints
        if (value instanceof int[]) {
            return ((int[]) value).length == 0;
        }

        // primitive long
        if (value instanceof long[]) {
            return ((long[]) value).length == 0;
        }

        // primitive float
        if (value instanceof float[]) {
            return ((float[]) value).length == 0;
        }

        // primitive double
        if (value instanceof double[]) {
            return ((double[]) value).length == 0;
        }

        // primitive char
        if (value instanceof char[]) {
            return ((char[]) value).length == 0;
        }

        // primitive boolean
        if (value instanceof boolean[]) {
            return ((boolean[]) value).length == 0;
        }

        // primitive byte
        if (value instanceof byte[]) {
            return ((byte[]) value).length == 0;
        }

        // primitive short
        if (value instanceof short[]) {
            return ((short[]) value).length == 0;
        }
        return false;
    }
}