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

Java tutorial

Introduction

Here is the source code for com.github.nakamurakj.validator.Validators.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 static org.apache.commons.lang3.StringUtils.*;

import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.validator.routines.DateValidator;

/**
 * Validator
 */
public final class Validators {

    /** ? */
    private static final Boolean[] BOOLEAN_VALUES = { Boolean.TRUE, Boolean.FALSE };
    /** yes/no */
    private static final String[] YES_OR_NO_VALUES = { "yes", "no" };

    /** ?? */
    private static final String KATAKANA_REGEX = "^[\u30A0-\u30FF]+$";
    /** ??? */
    private static final String HALF_KATAKANA_REGEX = "^[\uFF65-\uFF9F]+$";
    /** ???? */
    private static final String HIRAGANA_REGEX = "^[\u3040-\u309F]+$";;
    /** ??? */
    /* ??????(http://lightmaterial.blogspot.jp/2007/10/blog-post_14.html) */
    private final static String TEL_NO_REGEX = "(?!^(090|080|070|050))(?=^\\d{2,5}?-\\d{1,4}?-\\d{4}$)[\\d-]{12}|(?=^(090|080|070|050)-\\d{4}-\\d{4}$)[\\d-]{13}";
    /** ??? */
    private final static String ZIP_CODE_REGEX = "[0-9]{3}+-[0-9]{4}+";
    /** ??? */
    private final static String HALF_NUMBER_REGEX = "^[0-9]*$";

    private static final String SPACE = " ";
    private static final String EM_SPACE = "";
    private static final String EM_LONG_MARKS = "";

    /**
     * private constractors
     */
    private Validators() {
        // ignore
    }

    /**
     * ???
     *
     * @param string 
     * @param min ??
     * @param max ?
     * @return ??<code>true</code>
     */
    public static boolean isNumberString(String string, int min, int max) {
        if (string != null && string.length() >= min && string.length() <= max) {
            try {
                long value = Long.parseLong(string);
                return Long.MIN_VALUE <= value && value <= Long.MAX_VALUE;
            } catch (NumberFormatException e) {
                // ignore
            }
        }
        return false;
    }

    /**
     * ????
     *
     * @param string 
     * @return ???<code>true</code>
     */
    public static boolean isHalfNumber(String string) {
        return isPatternMatch(string, HALF_NUMBER_REGEX);
    }

    /**
     * ????
     *
     * @param tel ?
     * @return ????<code>true</code>
     */
    public static boolean isTelNo(String tel, String regex) {
        if (regex == null) {
            return isPatternMatch(tel, TEL_NO_REGEX);
        }
        return isPatternMatch(tel, regex);
    }

    /**
     * ????
     *
     * @param zip ?
     * @return ????<code>true</code>
     */
    public static boolean isZipCode(String zip, String regex) {
        if (regex == null) {
            return isPatternMatch(zip, ZIP_CODE_REGEX);
        }
        return isPatternMatch(zip, regex);
    }

    /**
     * ????????
     *
     * @param string 
     * @param regex ??
     * @return ?????????<code>true</code>
     */
    public static boolean isPatternMatch(String string, String regex) {
        if (string != null && regex != null) {
            Matcher matcher = Pattern.compile(regex).matcher(string);
            return matcher.matches();
        }
        return false;
    }

    /**
     * ??
     *
     * @param string 
     * @return ???<code>true</code>
     */
    public static boolean isBoolean(String string) {
        if (string != null) {
            for (Boolean value : BOOLEAN_VALUES) {
                if (value.toString().equals(string.toLowerCase())) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * yes/no?
     *
     * @param string 
     * @return yes/no??<code>true</code>
     */
    public static boolean isYesOrNo(String string) {
        if (string != null) {
            for (String value : YES_OR_NO_VALUES) {
                if (value.toLowerCase().equals(string.toLowerCase())) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Charset?
     *
     * @param string 
     * @return ??<code>true</code>
     */
    public static boolean isCharsetSupported(String string) {
        try {
            return Charset.isSupported(string);
        } catch (IllegalArgumentException e) {
            return false;
        }
    }

    /**
     * ?
     *
     * @param string 
     * @param pattern 
     * @return ??<code>true</code>
     */
    public static boolean isDateFormat(String string, String pattern) {
        if (string != null && pattern != null) {
            return DateValidator.getInstance().validate(string, pattern) != null;
        }
        return false;
    }

    /**
     * ??
     *
     * @param string 
     * @return ???<code>true</code>
     */
    public static boolean isHalfKatakana(String string) {
        return isPatternMatch(string, HALF_KATAKANA_REGEX);
    }

    /**
     * ?
     *
     * @param string 
     * @param space ("")?
     * @param halfSpace ??
     * @param longMarks ("")?
     * @return ??<code>true</code>
     */
    public static boolean isKatakana(String string, boolean space, boolean halfSpace, boolean longMarks) {
        if (string != null) {
            String check = removeMatcher(string, space, halfSpace, longMarks);
            return isPatternMatch(check, KATAKANA_REGEX);
        }
        return false;
    }

    /**
     * ????
     *
     * @param string 
     * @param space ("")?
     * @param halfSpace ??
     * @param longMarks ("")?
     * @return ?????<code>true</code>
     */
    public static boolean isHiragana(String string, boolean space, boolean halfSpace, boolean longMarks) {
        if (string != null) {
            String check = removeMatcher(string, space, halfSpace, longMarks);
            return isPatternMatch(check, HIRAGANA_REGEX);
        }
        return false;
    }

    /**
     * ?????????????
     *
     * @param string 
     * @param space ???
     * @param halfSpace ????
     * @param longMarks ???
     * @return ??
     */
    private static String removeMatcher(String string, boolean space, boolean halfSpace, boolean longMarks) {
        String check = string;
        if (space) {
            // ????
            check = replace(check, EM_SPACE, EMPTY);
        }
        if (halfSpace) {
            // ?????
            check = replace(check, SPACE, EMPTY);
        }
        if (longMarks) {
            // ???
            check = replace(check, EM_LONG_MARKS, EMPTY);
        }
        return check;
    }

}