Java String Value Of valueOfInteger(final String s, int radix)

Here you can find the source of valueOfInteger(final String s, int radix)

Description

value Of Integer

License

Open Source License

Parameter

Parameter Description
s a parameter
radix a parameter

Declaration

public static final Integer valueOfInteger(final String s, int radix) 

Method Source Code

//package com.java2s;

public class Main {
    private static final int INT_MAX = -Integer.MAX_VALUE;
    private static final int INT_MIN1 = -Integer.MAX_VALUE / 10;
    private static final int INT_MIN2 = Integer.MIN_VALUE / 10;
    public static final String STR_EMP = "";
    private static final char CH_ZERO = '0';

    /**/*from ww w  .j  a v  a 2  s  .  c o m*/
     * 
     * @param s
     * @return
     */
    public static final Integer valueOfInteger(final String s) {
        return Integer.valueOf(parseInt(s));
    }

    /**
     * 
     * @param s
     * @param radix
     * @return
     */
    public static final Integer valueOfInteger(final String s, int radix) {
        return Integer.valueOf(parseInt(s));
    }

    /**
     * 
     * @param o
     * @return
     */
    public static final String valueOf(final Object o) {
        return (o instanceof String) ? (String) o : (o == null ? "null" : o.toString());
    }

    /**
     * 
     * @param s
     * @return
     */
    public static final int parseInt(final String s) {
        if (s == null) {
            throw new NumberFormatException("Null string ");
        }
        final int len = s.length();
        if (len == 0) {
            throw new NumberFormatException("Empty string ");
        }
        final int neg;
        int num = 0;

        final char ch = s.charAt(0);
        int d;
        if (ch == '-') {
            if (len == 1) {
                throw new NumberFormatException("Missing digits:  " + s);
            }
            neg = 1;
        } else {
            d = ch - CH_ZERO;
            if (d < 0 || d > 9) {
                throw new NumberFormatException("Not number :  " + s);
            }
            num = -d;
            neg = -1;

        }
        final int limitInt;
        final int multmax;
        if (neg == -1) {
            limitInt = INT_MAX;
            multmax = INT_MIN1;
        } else {
            limitInt = Integer.MIN_VALUE;
            multmax = INT_MIN2;
        }
        //
        int i = 1;
        while (i < len) {
            d = s.charAt(i++) - CH_ZERO;
            if (d < 0 || d > 9) {
                throw new NumberFormatException("Not number :  " + s);
            }
            if (num < multmax) {
                throw new NumberFormatException("Underflow number :  " + s);
            }
            num *= 10;
            if (num < (limitInt + d)) {
                throw new NumberFormatException("Overflow number :  " + s);
            }
            num -= d;
        }
        return neg * num;
    }

    /**
     * 
     * @param s
     * @param radix
     * @return
     */
    public static final int parseInt(final String s, final int radix) {
        if (s == null) {
            throw new NumberFormatException("Null string ");
        }
        final int len = s.length();
        if (len == 0) {
            throw new NumberFormatException("Empty string ");
        }
        final int neg;
        int num = 0;

        final char ch = s.charAt(0);
        int d;
        if (ch == '-') {
            if (len == 1) {
                throw new NumberFormatException("Missing digits:  " + s);
            }
            neg = 1;
        } else {
            d = ch - CH_ZERO;
            if (d > 9) {
                d = d - 16;
                if (d < 0 || d > 9) {
                    throw new NumberFormatException("Not number :  " + s);
                }
                num = -(d + 9);
                neg = -1;

            } else {
                if (d < 0 || d > 9) {
                    throw new NumberFormatException("Not number :  " + s);
                }
                num = -(d);
                neg = -1;

            }

        }
        final int limitInt;
        final int multmax;
        if (neg == -1) {
            limitInt = INT_MAX;
            multmax = INT_MIN1;
        } else {
            limitInt = Integer.MIN_VALUE;
            multmax = INT_MIN2;
        }
        int i = 1;
        while (i < len) {
            d = s.charAt(i++) - CH_ZERO;
            if (d > 9) {
                d = d - 16;
                if (d < 0 || d > 9) {
                    throw new NumberFormatException("Not number :  " + d);
                }
                if (num < multmax) {
                    throw new NumberFormatException("Underflow number :  " + s);
                }
                num *= radix;
                if (num < (limitInt + d)) {
                    throw new NumberFormatException("Overflow number :  " + s);
                }
                num -= (d + 9);

            } else {
                if (d < 0 || d > 9) {
                    throw new NumberFormatException("Not number :  " + d);
                }
                if (num < multmax) {
                    throw new NumberFormatException("Underflow number :  " + s);
                }
                num *= radix;
                if (num < (limitInt + d)) {
                    throw new NumberFormatException("Overflow number :  " + s);
                }
                num -= (d);
            }
        }
        return neg * num;
    }

    /**
     * 
     * @param objs
     * @return
     */
    public static String toString(final Object[] objs) {
        if (objs == null) {
            return STR_EMP;
        }
        final int len = objs.length;
        if (len == 0) {
            return STR_EMP;
        }
        final StringBuilder buf = new StringBuilder(len * 12);
        for (int i = 0; i < len - 1; i++) {
            buf.append(objs[i]).append(", ");
        }
        return buf.append(objs[len - 1]).toString();
    }
}

Related

  1. valueOfEnum(Class enumClass, String value, E defaultValue)
  2. valueOfEnum(final Class type, final String value)
  3. valueOfIC(Class enumType, String aName)
  4. valueOfIgnoreCase(Class cls, String value)
  5. valueOfIgnoreCase(Class enumType, String constantName)
  6. valueOfIpv4(String ip)
  7. valueOfLongToString(long[] values)
  8. valueOfNullSafe(final Class enumType, final String name)
  9. valueOfOrEmptyString(final Object object)