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

org.zodiac.sdk.json.util.NumberUtil Maven / Gradle / Ivy

There is a newer version: 1.5.17
Show newest version
package org.zodiac.sdk.json.util;

public abstract class NumberUtil {

    private NumberUtil() {
    }

    public static int indexOf(byte[] container, int coff, int clen, int val) {
        byte bval = (byte)(val);
        int lmt = coff + clen;
        for (int idx = coff; idx != lmt; idx++) {
            if (container[idx] == bval)
                return idx;
        }
        return -1;
    }

    public static int indexOf(byte[] container, int coff, int clen, byte[] seq, int soff, int slen) {
        if (slen == 0)
            return -1;
        int clmt = coff + clen;
        while (coff != clmt) {
            int off1 = coff;
            coff = indexOf(container, coff, clen, seq[soff]);
            if (coff == -1)
                return -1;
            clen -= (coff - off1);
            if (clen < slen)
                return -1;
            boolean found = true;
            for (int idx = 0; idx != slen; idx++) {
                if (container[coff + idx] != seq[soff + idx]) {
                    found = false;
                    break;
                }
            }
            if (found)
                return coff;
            coff++;
            clen--;
        }
        return -1;
    }

    public static boolean isNumber(String str) {
        return isNumberDo(str, true);
    }

    public static boolean isIntegerNumber(String str) {
        return isNumberDo(str, true);
    }

    public static Object toIntgerNumber(Object value, Object defaultValue) throws Exception {
        if (null == value)
            return defaultValue;
        Class clz = value.getClass();
        try {
            if (Integer.class.isAssignableFrom(clz) || Integer.TYPE == clz) {
                return Integer.parseInt(value.toString());
            } else if (Long.class.isAssignableFrom(clz) || Long.TYPE == clz) {
                return Long.parseLong(value.toString());
            } else {
                throw new Exception("Unsupport type " + value);
            }
        } catch (Exception e) {
            throw e;
        }
    }

    private static boolean isNumberDo(String str, boolean incDot) {
        if (str != null && str.length() != 0) {
            char[] chars = str.toCharArray();
            int l = chars.length;

            int start = chars[0] != '-' && chars[0] != '+' ? 0 : 1;
            boolean hasDot = false;

            for (int i = start; i < l; ++i) {
                int ch = chars[i];

                if(incDot) {
                    if (ch == 46) {
                        if (hasDot) {
                            return false;
                        } else {
                            hasDot = true;
                            continue;
                        }
                    }
                }

                if (!Character.isDigit(ch)) {
                    return false;
                }
            }

            return true;
        } else {
            return false;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy