Java Object to Byte castToByte(Object value)

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

Description

cast To Byte

License

Apache License

Declaration

public static final Byte castToByte(Object value) 

Method Source Code

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

public class Main {
    public static final Byte castToByte(Object value) {
        if (value == null) {
            return null;
        }/*ww  w .  j a  v a2s .  co m*/

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

        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 Byte.parseByte(strVal);
        }

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

Related

  1. castByte(Object o)
  2. castBytes(Object o)
  3. castToByte(Object val)
  4. castToByte(Object value)