⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringtools.java

📁 Open DMT GPS server source code
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                    BigInteger bigLong = parseBigInteger(num, null);                    if (bigLong != null) {                        return bigLong.intValue();                    }                }            }        }        return dft;    }        /**    *** Return true if the specified String contains a valid int value    *** @param data  The String to test    *** @param strict True to test for a strict int value (ie. does not contain    ***               any other superfluous trailing characters), false to allow for     ***               other non-critical trailing characters.    *** @return True if the specified String contains a valid int value    **/    public static boolean isInt(String data, boolean strict)    {        if (StringTools.isBlank(data)) {            return false;        } else {            FilterNumber fn = new FilterNumber(data, Integer.class);            return fn.isValid(strict);        }    }    // ------------------------------------------------------------------------    /**    *** Parse the specified object into a short value    *** @param data  The object to parse    *** @param dft   The default short value if unable to parse the specified object    *** @return The parsed short value    **/    public static int parseShort(Object data, short dft)    {        if (data == null) {            return dft;        } else        if (data instanceof Number) {            return ((Number)data).shortValue();        } else {            return StringTools.parseShort(data.toString(), dft);        }    }    /**    *** Parse the specified String into a short value    *** @param data  The String to parse    *** @param dft   The default short value if unable to parse the specified object    *** @return The parsed short value    **/    public static short parseShort(String data, short dft)    {        return StringTools.parseShort(new FilterNumber(data, Short.class), dft);    }        /**    *** Parse the specified FilterNumber into a short value    *** @param num  The FilterNumber to parse    *** @param dft  The default short value if unable to parse the specified object    *** @return The parsed short value    **/    public static short parseShort(FilterNumber num, short dft)    {        if ((num != null) && num.supportsType(Short.class)) {            if (num.isHex()) {                return (short)StringTools.parseHexLong(num.getValueString(), dft);            } else {                try {                    return Short.parseShort(num.getValueString());                } catch (NumberFormatException nfe) {                    // Since 'FilterNumber' makes sure that only digits are parsed,                    // this likely means that the specified digit string is too large                    // for this required data type.  Our last ditch effort is to                     // attempt to convert it to a BigInteger and extract the lower                    // number of bits to match our data type.                    BigInteger bigLong = parseBigInteger(num, null);                    if (bigLong != null) {                        return bigLong.shortValue();                    }                }            }        }        return dft;    }        /**    *** Return true if the specified String contains a valid short value    *** @param data  The String to test    *** @param strict True to test for a strict short value (ie. does not contain    ***               any other superfluous trailing characters), false to allow for     ***               other non-critical trailing characters.    *** @return True if the specified String contains a valid short value    **/    public static boolean isShort(String data, boolean strict)    {        if (StringTools.isBlank(data)) {            return false;        } else {            FilterNumber fn = new FilterNumber(data, Short.class);            return fn.isValid(strict);        }    }    // ------------------------------------------------------------------------    /**    *** Parse the specified String into a BigInteger value    *** @param data  The String to parse    *** @param dft   The default BigInteger value if unable to parse the specified object    *** @return The parsed BigInteger value    **/    public static BigInteger parseBigInteger(String data, BigInteger dft)    {        return StringTools.parseBigInteger(new FilterNumber(data, BigInteger.class), dft);    }        /**    *** Parse the specified FilterNumber into a BigInteger value    *** @param num  The FilterNumber to parse    *** @param dft  The default BigInteger value if unable to parse the specified object    *** @return The parsed BigInteger value    **/    public static BigInteger parseBigInteger(FilterNumber num, BigInteger dft)    {        if ((num != null) && num.supportsType(BigInteger.class)) {            if (num.isHex()) {                try {                    return new BigInteger(num.getHexBytes());                } catch (NumberFormatException nfe) {                    // ignore (not likely to occur)                }                return null;            } else {                try {                    return new BigInteger(num.getValueString());                } catch (NumberFormatException nfe) {                    // ignore (not likely to occur)                }            }        }        return dft;    }        // ------------------------------------------------------------------------        /**    *** Class used to parse numeric values    **/    public static class FilterNumber    {                private String   inpStr = null;        private Class<?> type   = null;        private boolean  isHex  = false;                private String numStr = null;        private int startPos  = -1;        private int endPos    = -1;                public FilterNumber(String val, Class<?> type) {                         /* null string */            if (val == null) { // null string                //Print.logDebug("'null' value");                return;            }                        /* skip initial whitespace */            int s = 0;            while ((s < val.length()) && Character.isWhitespace(val.charAt(s))) { s++; }            if (s == val.length()) { // empty string                //Print.logDebug("empty value");                return;            }            String v = val; // (val != null)? val.trim() : "";            int vlen = v.length();                        /* hex number */            boolean hex = false;            if (((s + 1) < vlen) && (v.charAt(s) == '0') && (Character.toLowerCase(v.charAt(s + 1)) == 'x')) {                // we will be parsing a hex value                hex = true;                s += 2; // skip "0x"            }                        /* plus sign? */            if (!hex && (s < vlen) && (v.charAt(s) == '+')) {                // skip over prefixing '+'                s++;            }            /* negative number */            int ps, p = (!hex && (s < vlen) && (v.charAt(s) == '-'))? (s + 1) : s;                                    /* skip initial digits */            if (hex) {                // scan until end of hex digits                for (ps = p; (p < vlen) && ("0123456789ABCDEF".indexOf(Character.toUpperCase(v.charAt(p))) >= 0);) { p++; }            } else {                // scan until end of decimal digits                for (ps = p; (p < vlen) && Character.isDigit(v.charAt(p));) { p++; }            }            boolean foundDigit = (p > ps);                        /* end of digits? */            String num;            if ((p >= vlen)                || Long.class.isAssignableFrom(type)                || Integer.class.isAssignableFrom(type)                || Short.class.isAssignableFrom(type)                || Byte.class.isAssignableFrom(type)                || BigInteger.class.isAssignableFrom(type)                ) {                // end of String or Long/Integer/Short/Byte/BigInteger                num = v.substring(s, p);            } else            if (v.charAt(p) != '.') {                // Double/Float, but doesn't contain decimal                num = v.substring(s, p);            } else {                // Double/Float, decimal digits                p++; // skip '.'                for (ps = p; (p < vlen) && Character.isDigit(v.charAt(p));) { p++; }                if (p > ps) { foundDigit = true; }                num = v.substring(s, p);            }                        /* set instance vars */            if (foundDigit) {                this.isHex      = hex;                this.inpStr     = val;                this.type       = type;                this.numStr     = num;                this.startPos   = s;                this.endPos     = p;            }                    }                public <T> boolean supportsType(Class<T> ct) {            if ((this.numStr != null) && (this.type != null)) {                if (this.type.isAssignableFrom(ct)) {                    return true; // quick check (Double/Float/BigInteger/Long/Integer/Byte)                } else                if (Short.class.isAssignableFrom(this.type)) {                    return this.supportsType(Byte.class);                } else                if (Integer.class.isAssignableFrom(this.type)) {                    return this.supportsType(Short.class);                } else                if (Long.class.isAssignableFrom(this.type)) {                    return this.supportsType(Integer.class);                } else                if (BigInteger.class.isAssignableFrom(this.type)) {                    return this.supportsType(Long.class);                } else                if (Float.class.isAssignableFrom(this.type)) {                    return this.supportsType(BigInteger.class);                } else                if (Double.class.isAssignableFrom(this.type)) {                    return this.supportsType(Float.class);                } else {                    return false;                }            } else {                return false;            }        }                public String getInputString() {            return this.inpStr;        }                public Class getClassType() {            return this.type;        }                public String getClassTypeName() {            if (this.type != null) {                String cn = this.type.getName();                if (cn.startsWith("java.lang.")) {                    return cn.substring("java.lang.".length());                } else                if (cn.startsWith("java.math.")) {                    return cn.substring("java.math.".length());                } else {                    return cn;                }            } else {                return "null";            }        }                public boolean isHex() {            return this.isHex;        }                public String getValueString() {            return this.numStr;        }                public boolean isValid(boolean strict) {            if (this.getValueString() == null) {                return false;            } else            if (!strict) {                // don't care about trailing characters                return true;            } else {                // must not have any trailing characters                return (this.getInputString().length() == this.getEnd());            }        }        public byte[] getHexBytes() {            if (this.isHex) {                return StringTools.parseHex(this.getValueString(), new byte[0]);            } else {                // not tested yet                return (new BigInteger(this.getValueString())).toByteArray();            }        }                public int getStart() {            return this.startPos;        }                public int getEnd() {            return this.endPos;        }                public int getLength() {            return (this.endPos - this.startPos);        }                public Number toNumber(Number dft) {            if ((this.numStr != null) && (this.type != null)) {                try {                    if (Byte.class.equals(this.type)) {                        return new Byte(this.numStr);                    } else                    if (Short.class.equals(this.type)) {                        return new Short(this.numStr);                    } else                    if (Integer.class.equals(this.type)) {                        return new Integer(this.numStr);                    } else                    if (Long.class.equals(this.type)) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -