Java Char Create toCharacter(Object o)

Here you can find the source of toCharacter(Object o)

Description

to Character

License

LGPL

Declaration

public static Character toCharacter(Object o) 

Method Source Code

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

public class Main {
    public static Character toCharacter(Object o) {
        if (o == null)
            return null;

        if (o.getClass() == Character.class)
            return (Character) o;

        if (o.getClass() == Boolean.class)
            return booleanToChar((Boolean) o);
        if (o.getClass() == Byte.class)
            return (char) ((Byte) o).byteValue();
        if (o.getClass() == Short.class)
            return shortToCharacter((Short) o);
        if (o.getClass() == Integer.class)
            return intToCharacter((Integer) o);
        if (o.getClass() == Long.class)
            return longToCharacter((Long) o);
        if (o.getClass() == Float.class)
            return floatToCharacter((Float) o);
        if (o.getClass() == Double.class)
            return doubleToCharacter((Double) o);
        if (o.getClass() == String.class)
            return stringToCharacter((String) o);

        return null;
    }/*from  w  w  w .j  a  v a2 s .com*/

    public static char booleanToChar(boolean b) {
        return b ? (char) 1 : 0;
    }

    public static Character shortToCharacter(short h) {
        if (h >= Character.MIN_VALUE && h <= Character.MAX_VALUE) {
            return (char) h;
        }
        return null;
    }

    public static Character intToCharacter(int i) {
        if (i >= Character.MIN_VALUE && i <= Character.MAX_VALUE) {
            return (char) i;
        }
        return null;
    }

    public static Character longToCharacter(long l) {
        if (l >= Character.MIN_VALUE && l <= Character.MAX_VALUE) {
            return (char) l;
        }
        return null;
    }

    public static Character floatToCharacter(float f) {
        if (f >= Character.MIN_VALUE && f <= Character.MAX_VALUE) {
            return (char) f;
        }
        return null;
    }

    public static Character doubleToCharacter(double d) {
        if (d >= Character.MIN_VALUE && d <= Character.MAX_VALUE) {
            return (char) d;
        }
        return null;
    }

    public static Character stringToCharacter(String s) {
        if (s == null || s.length() != 1)
            return null;
        try {
            return s.charAt(0);
        } catch (IndexOutOfBoundsException e) {
            return null;
        }
    }
}

Related

  1. toChar(String string)
  2. toChar(String unicode)
  3. toCharA(byte[] data)
  4. toCharacter(final String value)
  5. toCharacter(final String value)
  6. toCharacter(Object o)
  7. toCharacter(Object obj)
  8. toCharacter(Object object)
  9. toCharacter(Object value)