`

commons-lang NumberUtilHelper

 
阅读更多
除了对字符串的复杂处理,对于数字,我们也是头大啊,类型,位移等等操作,如果写在程序里,无疑是天文数字,而工具类可以统一计算和管理

package org.ycl.commons.text;

import org.apache.commons.lang.StringUtils;

/**
 * 1. leftBit
 * <li>- 2^N, assign: this seat digit is one Auth</li>
 * 2. validAuth
 * <li>- X&N, assign: X' N seat digit has Auth</li>
 * 3. addAuth
 * <li>- X|N, assign: set X' N seat digit to 1</li>
 * 4. removeAuth
 * <li>- X^N, assign: set X' N seat digit to 0</li>
 * 5. toInt/toLong/toFloat/toDouble/toByte/toShort
 * <li>- null-letter safe Number convert</li>
 * 6. min/max
 * <li>- find array's min/max</li>
 * 7. isDigits/isNumber
 * <li>- judge string is digits/number</li>
 * 
 * NOTE:this is from my tool box
 * {@link org.apache.commons.lang.math.NumberUtils} 
 * 
 * @author e557400
 *
 */
public class NumberUtils {
 
	/**
	 * <<: move to left, fill 0 in right part
	 * this will be return 2^N
	 * 
	 * index
	 * 1         2
	 * 2         4
	 * 3         8
	 * ....
	 * 
	 * process
	 * 
	 * NUM: 1    BIT: 2^0==> 1
	 * OP:  <<   left
	 * NUM: 3
	 * EQ:  1000 ==> BIT: 2^3 ==> 8
	 * 
	 * NOTE: int is 32 bits
	 * exp int i=1, so i's 2 bits' source code:
	 * 00000000000000000000000000000001
	 * 
	 * @param index
	 * @return 2^index
	 */
	private static int leftBit(int index){
		return 1<<index;
	}
	
	/**
	 * &: only two is 1, then 1, others is 0.
	 * check whether the value's index bit is 1.
	 * 
	 * NUM: 6, 1
	 * leftBig: 1<<1 ==> 10
	 *          6 ==> 2^2+2^1 ==>110
	 * OP: 10&110==>10
	 * 
	 * @param value
	 * @param index
	 * @return
	 */
	public static boolean validAuth(int value, int index){
		return (leftBit(index)&value)==leftBit(index);
	}
	
	/**
	 * |: only two is 0, then 0, others is 1
	 * add value's index.
	 * 
	 * NUM: 6, 3
	 * leftBig: 1<<3 ==> 1000
	 *          6 ==> 2^2+2^1 ==>110
	 * OP: 1000|110==>1110 ==>2^3+6 = 14
	 * 
	 * @param value
	 * @param index
	 * @return
	 */
	public static int addAuth(int value, int index){
		return leftBit(index)|value;
	}
	
	/**
	 * ^: only two is same, then 0, others is 1
	 * 
     *NUM: 14, 3
	 * leftBig: 1<<3 ==> 1000
	 *          6 ==> 2^3+2^2+2^1 ==>1110
	 * OP: 1000^1110==>0110 ==>6
	 * 
	 * @param value
	 * @param index
	 * @return
	 */
	public static int removeAuth(int value, int index){
		if(validAuth(value,index)){
			return leftBit(index)^value;
		}else{
			return value;
		}
		
	}
	
	/**
     * <p>Convert a <code>String</code> to an <code>int</code>, returning
     * <code>zero</code> if the conversion fails.</p>
     *
     * <p>If the string is <code>null</code>, <code>zero</code> is returned.</p>
     *
     * <pre>
     *   NumberUtils.toInt(null) = 0
     *   NumberUtils.toInt("")   = 0
     *   NumberUtils.toInt("1")  = 1
     * </pre>
     *
     * @param str  the string to convert, may be null
     * @return the int represented by the string, or <code>zero</code> if
     *  conversion fails
     * @since 2.1
     */
    public static int toInt(String str) {
    	return org.apache.commons.lang.math.NumberUtils.toInt(str);
    }
    
    /**
     * <p>Returns the minimum value in an array.</p>
     * 
     * @param array  an array, must not be null or empty
     * @return the minimum value in the array
     * @throws IllegalArgumentException if <code>array</code> is <code>null</code>
     * @throws IllegalArgumentException if <code>array</code> is empty
     */
    public static long min(long[] array) {
        // Validates input
        if (array == null) {
            throw new IllegalArgumentException("The Array must not be null");
        } else if (array.length == 0) {
            throw new IllegalArgumentException("Array cannot be empty.");
        }
    
        // Finds and returns min
        long min = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }
    
        return min;
    }
    
    /**
     * <p>Checks whether the <code>String</code> contains only
     * digit characters.</p>
     *
     * <p><code>Null</code> and empty String will return
     * <code>false</code>.</p>
     *
     * @param str  the <code>String</code> to check
     * @return <code>true</code> if str contains only unicode numeric
     */
    public static boolean isDigits(String str) {
        if (StringUtils.isEmpty(str)) {
            return false;
        }
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }
    
    /**
     * <p>Checks whether the String a valid Java number.</p>
     *
     * <p>Valid numbers include hexadecimal marked with the <code>0x</code>
     * qualifier, scientific notation and numbers marked with a type
     * qualifier (e.g. 123L).</p>
     *
     * <p><code>Null</code> and empty String will return
     * <code>false</code>.</p>
     *
     * @param str  the <code>String</code> to check
     * @return <code>true</code> if the string is a correctly formatted number
     */
    public static boolean isNumber(String str) {
        if (StringUtils.isEmpty(str)) {
            return false;
        }
        char[] chars = str.toCharArray();
        int sz = chars.length;
        boolean hasExp = false;
        boolean hasDecPoint = false;
        boolean allowSigns = false;
        boolean foundDigit = false;
        // deal with any possible sign up front
        int start = (chars[0] == '-') ? 1 : 0;
        if (sz > start + 1) {
            if (chars[start] == '0' && chars[start + 1] == 'x') {
                int i = start + 2;
                if (i == sz) {
                    return false; // str == "0x"
                }
                // checking hex (it can't be anything else)
                for (; i < chars.length; i++) {
                    if ((chars[i] < '0' || chars[i] > '9')
                        && (chars[i] < 'a' || chars[i] > 'f')
                        && (chars[i] < 'A' || chars[i] > 'F')) {
                        return false;
                    }
                }
                return true;
            }
        }
        sz--; // don't want to loop to the last char, check it afterwords
              // for type qualifiers
        int i = start;
        // loop to the next to last char or to the last char if we need another digit to
        // make a valid number (e.g. chars[0..5] = "1234E")
        while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) {
            if (chars[i] >= '0' && chars[i] <= '9') {
                foundDigit = true;
                allowSigns = false;

            } else if (chars[i] == '.') {
                if (hasDecPoint || hasExp) {
                    // two decimal points or dec in exponent   
                    return false;
                }
                hasDecPoint = true;
            } else if (chars[i] == 'e' || chars[i] == 'E') {
                // we've already taken care of hex.
                if (hasExp) {
                    // two E's
                    return false;
                }
                if (!foundDigit) {
                    return false;
                }
                hasExp = true;
                allowSigns = true;
            } else if (chars[i] == '+' || chars[i] == '-') {
                if (!allowSigns) {
                    return false;
                }
                allowSigns = false;
                foundDigit = false; // we need a digit after the E
            } else {
                return false;
            }
            i++;
        }
        if (i < chars.length) {
            if (chars[i] >= '0' && chars[i] <= '9') {
                // no type qualifier, OK
                return true;
            }
            if (chars[i] == 'e' || chars[i] == 'E') {
                // can't have an E at the last byte
                return false;
            }
            if (chars[i] == '.') {
                if (hasDecPoint || hasExp) {
                    // two decimal points or dec in exponent
                    return false;
                }
                // single trailing decimal point after non-exponent is ok
                return foundDigit;
            }
            if (!allowSigns
                && (chars[i] == 'd'
                    || chars[i] == 'D'
                    || chars[i] == 'f'
                    || chars[i] == 'F')) {
                return foundDigit;
            }
            if (chars[i] == 'l'
                || chars[i] == 'L') {
                // not allowing L with an exponent
                return foundDigit && !hasExp;
            }
            // last character is illegal
            return false;
        }
        // allowSigns is true iff the val ends in 'E'
        // found digit it to make sure weird stuff like '.' and '1E-' doesn't pass
        return !allowSigns && foundDigit;
    }
	
	
	 public static void main(String args[]){
		 System.out.println(leftBit(1));
		 System.out.println(leftBit(2));
		 System.out.println(leftBit(3));
		 System.out.println(leftBit(4));
		 
		 System.out.println(addAuth(6,3)); 
		 
		 System.out.println(validAuth(6,1));
		 System.out.println(validAuth(6,2));
		 System.out.println(validAuth(14,3));
		 
		 System.out.println(removeAuth(6,3));
		 
		 System.out.println(org.apache.commons.lang.math.NumberUtils.isNumber(""));
		 System.out.println(org.apache.commons.lang.math.NumberUtils.isNumber(" "));
		 System.out.println(org.apache.commons.lang.math.NumberUtils.isNumber(null));
	 }
}

分享到:
评论

相关推荐

    commons-lang-2.6-API文档-中文版.zip

    赠送jar包:commons-lang-2.6.jar; 赠送原API文档:commons-lang-2.6-javadoc.jar; 赠送源代码:commons-lang-2.6-sources.jar; 包含翻译后的API文档:commons-lang-2.6-javadoc-API文档-中文(简体)版.zip ...

    commons-lang3-3.12.0-API文档-中文版.zip

    赠送jar包:commons-lang3-3.12.0.jar; 赠送原API文档:commons-lang3-3.12.0-javadoc.jar; 赠送源代码:commons-lang3-3.12.0-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.12.0.pom; 包含翻译后的API...

    commons-lang3-3.9-API文档-中文版.zip

    赠送jar包:commons-lang3-3.9.jar; 赠送原API文档:commons-lang3-3.9-javadoc.jar; 赠送源代码:commons-lang3-3.9-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.9.pom; 包含翻译后的API文档:...

    commons-lang3-3.12.0-API文档-中英对照版.zip

    赠送jar包:commons-lang3-3.12.0.jar; 赠送原API文档:commons-lang3-3.12.0-javadoc.jar; 赠送源代码:commons-lang3-3.12.0-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.12.0.pom; 包含翻译后的API...

    commons-lang3-3.10-API文档-中文版.zip

    赠送jar包:commons-lang3-3.10.jar; 赠送原API文档:commons-lang3-3.10-javadoc.jar; 赠送源代码:commons-lang3-3.10-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.10.pom; 包含翻译后的API文档:...

    commons-lang3-3.4-API文档-中文版.zip

    赠送jar包:commons-lang3-3.4.jar; 赠送原API文档:commons-lang3-3.4-javadoc.jar; 赠送源代码:commons-lang3-3.4-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.4.pom; 包含翻译后的API文档:...

    commons-lang3-3.7-API文档-中文版.zip

    赠送jar包:commons-lang3-3.7.jar; 赠送原API文档:commons-lang3-3.7-javadoc.jar; 赠送源代码:commons-lang3-3.7-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.7.pom; 包含翻译后的API文档:...

    commons-lang3-3.3.2-API文档-中文版.zip

    赠送jar包:commons-lang3-3.3.2.jar; 赠送原API文档:commons-lang3-3.3.2-javadoc.jar; 赠送源代码:commons-lang3-3.3.2-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.3.2.pom; 包含翻译后的API文档...

    commons-lang3-3.10-API文档-中英对照版.zip

    赠送jar包:commons-lang3-3.10.jar; 赠送原API文档:commons-lang3-3.10-javadoc.jar; 赠送源代码:commons-lang3-3.10-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.10.pom; 包含翻译后的API文档:...

    commons-lang-2.4-API文档-中文版.zip

    赠送jar包:commons-lang-2.4.jar; 赠送原API文档:commons-lang-2.4-javadoc.jar; 赠送源代码:commons-lang-2.4-sources.jar; 赠送Maven依赖信息文件:commons-lang-2.4.pom; 包含翻译后的API文档:commons-...

    commons-lang3-3.5-API文档-中文版.zip

    赠送jar包:commons-lang3-3.5.jar; 赠送原API文档:commons-lang3-3.5-javadoc.jar; 赠送源代码:commons-lang3-3.5-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.5.pom; 包含翻译后的API文档:...

    commons-lang3-3.7 和 commons-lang-2.6 合集

    本资源包含了两个不同版本的Lang包:`commons-lang3-3.7.jar` 和 `commons-lang-2.6.jar`。这两个版本分别代表了Lang项目在不同时间点的发展状态,它们各自具有不同的特性和功能,适用于不同需求的项目。 `commons-...

    java common-lang-commons-lang.jar

    commons-lang.jar是日常编程中必不可少的一个jar包,在日常java编程中,经常会使用到commons-lang.jar包,还没有准备commons-lang.jar包的用户可以前来下载使用,本站不仅为大家提供了commons-lang3-3.1.jar下载地址

    commons-lang-2.4.rar代码及jar文件

    这个"commons-lang-2.4.rar"压缩包包含了两个子文件,分别是"commons-lang-2.4-bin.zip"和"commons-lang-2.4-src.zip"。 "commons-lang-2.4-bin.zip"文件是编译后的二进制版本,包含了commons-lang库的JAR文件。这...

    commons-lang-2.6.jar包

    commons-lang-2.6.jar包commons-lang-2.6.jar包commons-lang-2.6.jar包commons-lang-2.6.jar包commons-lang-2.6.jar包加源码

    commons-lang3-3.8-API文档-中英对照版.zip

    赠送jar包:commons-lang3-3.8.jar; 赠送原API文档:commons-lang3-3.8-javadoc.jar; 赠送源代码:commons-lang3-3.8-sources.jar; 赠送Maven依赖信息文件:commons-lang3-3.8.pom; 包含翻译后的API文档:...

    commons-lang.rar

    commons-lang-1.0-b1.jar commons-lang-1.0.1.jar commons-lang-2.0.jar commons-lang-2.1-javadoc.jar commons-lang-2.1-sources.jar commons-lang-2.1.jar commons-lang-2.3.jar commons-lang-2.4-sources.jar ...

    开发工具 commons-lang3-3.4

    开发工具 commons-lang3-3.4开发工具 commons-lang3-3.4开发工具 commons-lang3-3.4开发工具 commons-lang3-3.4开发工具 commons-lang3-3.4开发工具 commons-lang3-3.4开发工具 commons-lang3-3.4开发工具 commons-...

    commons-lang-2.6-API文档-中英对照版.zip

    赠送jar包:commons-lang-2.6.jar; 赠送原API文档:commons-lang-2.6-javadoc.jar; 赠送源代码:commons-lang-2.6-sources.jar; 包含翻译后的API文档:commons-lang-2.6-javadoc-API文档-中文(简体)-英语-对照...

    commons-lang3-3.1.jar 附导入教程.rar

    commons-lang.jar是日常编程中必不可少的一个jar包,在日常java编程中,经常会使用到commons-lang.jar包,还没有准备commons-lang.jar包的用户可以前来下载使用,这里为大家提供了commons-lang3-3.1.jar

Global site tag (gtag.js) - Google Analytics