Java Object to int castToInt(Object value)

Here you can find the source of castToInt(Object value)

Description

cast To Int

License

Apache License

Declaration

public static final Integer castToInt(Object value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static final Integer castToInt(Object value) {
        if (value == null) {
            return null;
        }// ww w .j  a  v a  2s .c o  m

        if (value instanceof Integer) {
            return (Integer) value;
        }

        if (value instanceof Number) {
            return ((Number) value).intValue();
        }

        if (value instanceof String) {
            String strVal = (String) value;

            if (strVal.length() == 0) {
                return null;
            }

            if ("null".equals(strVal) || "NULL".equals(strVal)) {
                return null;
            }

            if (strVal.trim().length() == 0) {
                return null;
            }

            return Integer.parseInt(strVal);
        }

        throw new NumberFormatException("can not cast to int, value : " + value);
    }
}

Related

  1. castInt(Object o)
  2. castInt(Object o)
  3. castToInt(double[] aDouble)
  4. castToInt(final long value)
  5. castToInteger(final String uid)
  6. castToInteger(Object object)
  7. castToInteger(Object value)
  8. castToIntegerArray(Object[] array)