Java Object to Short castToShort(Object value)

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

Description

cast To Short

License

Apache License

Declaration

public static final Short castToShort(Object value) 

Method Source Code

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

public class Main {
    public static final Short castToShort(Object value) {
        if (value == null) {
            return null;
        }//ww  w  .  j ava2s.c  o  m

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

        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 Short.parseShort(strVal);
        }

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

Related

  1. castShort(Object o)
  2. castShort(Object val)
  3. castShortToFloat(short[] x)
  4. castToShort(final int value)
  5. castToShort(final String uid)
  6. castToShort(Object value)