Example usage for java.lang Character valueOf

List of usage examples for java.lang Character valueOf

Introduction

In this page you can find the example usage for java.lang Character valueOf.

Prototype

@HotSpotIntrinsicCandidate
public static Character valueOf(char c) 

Source Link

Document

Returns a Character instance representing the specified char value.

Usage

From source file:cat.albirar.framework.dynabean.impl.DynaBeanImpl.java

/**
 * Test if the value is for a primitive type and return an object representation with default (0) value. If value is
 * null and the type is primitive, return a representation of default value for the primitive corresponding type.
 * //w  ww . j a va 2s . co m
 * @param value the value, can be null
 * @param pb The property bean descriptor
 * @return The value or the default value representation for the primitive type (0)
 */
private Object nullSafeValue(Object value, DynaBeanPropertyDescriptor pb) {
    if (!pb.isPrimitive()) {
        return value;
    }
    if (pb.getPropertyType().getName().equals("byte")) {
        return (value == null ? Byte.valueOf((byte) 0) : (Byte) value);
    }
    if (pb.getPropertyType().getName().equals("short")) {
        return (value == null ? Short.valueOf((short) 0) : (Short) value);
    }
    if (pb.getPropertyType().getName().equals("int")) {
        return (value == null ? Integer.valueOf(0) : (Integer) value);
    }
    if (pb.getPropertyType().getName().equals("long")) {
        return (value == null ? Long.valueOf(0L) : (Long) value);
    }
    if (pb.getPropertyType().getName().equals("float")) {
        return (value == null ? Float.valueOf(0F) : (Float) value);
    }
    if (pb.getPropertyType().getName().equals("double")) {
        return (value == null ? Double.valueOf(0D) : (Double) value);
    }
    if (pb.getPropertyType().getName().equals("char")) {
        return (value == null ? Character.valueOf('\u0000') : (Character) value);
    }
    return (value == null ? Boolean.FALSE : (Boolean) value);
}

From source file:org.yamj.core.tools.web.HTMLTools.java

public static String decodeHtml(String source) {
    if (null == source || 0 == source.length()) {
        return source;
    }//  w  w w . j av  a2s .  c o m

    int currentIndex = 0;
    int delimiterStartIndex;
    int delimiterEndIndex;

    StringBuilder result = null;

    while (currentIndex <= source.length()) {
        delimiterStartIndex = source.indexOf('&', currentIndex);
        if (delimiterStartIndex != -1) {
            delimiterEndIndex = source.indexOf(';', delimiterStartIndex + 1);
            if (delimiterEndIndex != -1) {
                // ensure that the string builder is setup correctly
                if (null == result) {
                    result = new StringBuilder();
                }

                // add the text that leads up to this match
                if (delimiterStartIndex > currentIndex) {
                    result.append(source.substring(currentIndex, delimiterStartIndex));
                }

                // add the decoded entity
                String entity = source.substring(delimiterStartIndex, delimiterEndIndex + 1);

                currentIndex = delimiterEndIndex + 1;

                // try to decoded numeric entities
                if (entity.charAt(1) == '#') {
                    int start = 2;
                    int radix = 10;
                    // check if the number is hexadecimal
                    if (entity.charAt(2) == 'X' || entity.charAt(2) == 'x') {
                        start++;
                        radix = 16;
                    }
                    try {
                        Character c = Character.valueOf(
                                (char) Integer.parseInt(entity.substring(start, entity.length() - 1), radix));
                        result.append(c);
                    } // when the number of the entity can't be parsed, add the entity as-is
                    catch (NumberFormatException error) {
                        result.append(entity);
                    }
                } else {
                    // try to decode the entity as a literal
                    Character decoded = HTML_DECODE_MAP.get(entity);
                    if (decoded != null) {
                        result.append(decoded);
                    } // if there was no match, add the entity as-is
                    else {
                        result.append(entity);
                    }
                }
            } else {
                break;
            }
        } else {
            break;
        }
    }

    if (null == result) {
        return source;
    } else if (currentIndex < source.length()) {
        result.append(source.substring(currentIndex));
    }

    return result.toString();
}

From source file:com.zimbra.common.util.HttpUtil.java

/**
 * urlEscape method will encode '?' and '&', so make sure
 * the passed in String does not have query string in it.
 * Or call urlEscape on each segment and append query
 * String afterwards. Deviates slightly from RFC 3986 to
 * include "<" and ">"/*w  w  w  .  j a  va  2s.  com*/
 *
 * from RFC 3986:
 *
 * pchar       = unreserved / pct-encoded / sub-delims / ":" / "@"
 *
 * sub-delims  = "!" / "$" / "&" / "'" / "(" / ")"
 *                   / "*" / "+" / "," / ";" / "="
 *
 * unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"
 *
 */
public static String urlEscape(String str) {
    // rfc 2396 url escape.
    StringBuilder buf = null;
    for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        String escaped = null;
        if (c < 0x7F)
            escaped = sUrlEscapeMap.get(c);

        if (escaped != null || c >= 0x7F) {
            if (buf == null) {
                buf = new StringBuilder();
                buf.append(str.substring(0, i));
            }
            if (escaped != null)
                buf.append(escaped);
            else {
                try {
                    byte[] raw = Character.valueOf(c).toString().getBytes("UTF-8");
                    for (byte b : raw) {
                        int unsignedB = b & 0xFF; // byte is signed
                        buf.append("%").append(Integer.toHexString(unsignedB).toUpperCase());
                    }
                } catch (IOException e) {
                    buf.append(c);
                }
            }
        } else if (buf != null) {
            buf.append(c);
        }
    }
    if (buf != null)
        return buf.toString();
    return str;
}

From source file:gov.nih.nci.cagrid.sdk4query.processor.PublicDataCQL2ParameterizedHQL.java

private java.lang.Object valueToObject(String className, String value) throws QueryProcessingException {
    LOG.debug("Converting \"" + value + "\" to object of type " + className);
    if (className.equals(String.class.getName())) {
        return value;
    }//from   ww w .  java  2 s .  c  om
    if (className.equals(Integer.class.getName())) {
        return Integer.valueOf(value);
    }
    if (className.equals(Long.class.getName())) {
        return Long.valueOf(value);
    }
    if (className.equals(Double.class.getName())) {
        return Double.valueOf(value);
    }
    if (className.equals(Float.class.getName())) {
        return Float.valueOf(value);
    }
    if (className.equals(Boolean.class.getName())) {
        return Boolean.valueOf(value);
    }
    if (className.equals(Character.class.getName())) {
        if (value.length() == 1) {
            return Character.valueOf(value.charAt(0));
        } else {
            throw new QueryProcessingException(
                    "The value \"" + value + "\" of length " + value.length() + " is not a valid character");
        }
    }
    if (className.equals(Date.class.getName())) {
        // try time, then dateTime, then just date
        List<SimpleDateFormat> formats = new ArrayList<SimpleDateFormat>(3);
        formats.add(new SimpleDateFormat("HH:mm:ss"));
        formats.add(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"));
        formats.add(new SimpleDateFormat("yyyy-MM-dd"));

        Date date = null;
        Iterator<SimpleDateFormat> formatIter = formats.iterator();
        while (date == null && formatIter.hasNext()) {
            SimpleDateFormat formatter = formatIter.next();
            try {
                date = formatter.parse(value);
            } catch (ParseException ex) {
                LOG.debug(value + " was not parsable by pattern " + formatter.toPattern());
            }
        }
        if (date == null) {
            throw new QueryProcessingException("Unable to parse date value \"" + value + "\"");
        }

        return date;
    }

    throw new QueryProcessingException("No conversion for type " + className);
}

From source file:org.plasma.sdo.helper.DataConverter.java

public Object fromCharacter(Type targetType, char value) {
    DataType targetDataType = DataType.valueOf(targetType.getName());
    switch (targetDataType) {
    case Character:
        return Character.valueOf(value);
    case String:
        return String.valueOf(value);
    default:// ww w.j a v  a  2 s . c o m
        throw new InvalidDataConversionException(targetDataType, DataType.Character, value);
    }
}

From source file:com.glaf.core.util.StringTools.java

public static String formatLine(String sourceString, int length) {
    if (sourceString == null) {
        return "";
    }/*w  w  w  .  ja  v  a  2  s  .  com*/
    if (sourceString.length() <= length) {
        StringBuffer buffer = new StringBuffer(sourceString.length() + 10);
        int k = length - sourceString.length();
        for (int j = 0; j < k; j++) {
            buffer.append(sourceString).append("&nbsp;");
        }
        sourceString = buffer.toString();
        return sourceString;
    }
    char[] sourceChrs = sourceString.toCharArray();
    char[] distinChrs = new char[length];

    for (int i = 0; i < length; i++) {
        if (i >= sourceChrs.length) {
            return sourceString;
        }
        Character chr = Character.valueOf(sourceChrs[i]);
        if (chr.charValue() <= 202 && chr.charValue() >= 8) {
            distinChrs[i] = chr.charValue();
        } else {
            distinChrs[i] = chr.charValue();
            length--;
        }
    }
    return new String(distinChrs) + "";
}

From source file:net.solarnetwork.node.io.serial.rxtx.SerialPortConnection.java

private String asciiDebugValue(byte[] data) {
    if (data == null || data.length < 1) {
        return "";
    }//w w  w. j  ava  2s  .  co m
    StringBuilder buf = new StringBuilder();
    buf.append(Hex.encodeHex(data)).append(" (");
    for (byte b : data) {
        if (b >= 32 && b < 126) {
            buf.append(Character.valueOf((char) b));
        } else {
            buf.append('~');
        }
    }
    buf.append(")");
    return buf.toString();
}

From source file:org.apache.hadoop.hbase.io.HbaseObjectWritable.java

/**
 * Read a {@link Writable}, {@link String}, primitive type, or an array of
 * the preceding./*from w ww  . j av a2s.c o  m*/
 * @param in
 * @param objectWritable
 * @param conf
 * @return the object
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public static Object readObject(DataInput in, HbaseObjectWritable objectWritable, Configuration conf)
        throws IOException {
    Class<?> declaredClass = CODE_TO_CLASS.get(WritableUtils.readVInt(in));
    Object instance;
    if (declaredClass.isPrimitive()) { // primitive types
        if (declaredClass == Boolean.TYPE) { // boolean
            instance = Boolean.valueOf(in.readBoolean());
        } else if (declaredClass == Character.TYPE) { // char
            instance = Character.valueOf(in.readChar());
        } else if (declaredClass == Byte.TYPE) { // byte
            instance = Byte.valueOf(in.readByte());
        } else if (declaredClass == Short.TYPE) { // short
            instance = Short.valueOf(in.readShort());
        } else if (declaredClass == Integer.TYPE) { // int
            instance = Integer.valueOf(in.readInt());
        } else if (declaredClass == Long.TYPE) { // long
            instance = Long.valueOf(in.readLong());
        } else if (declaredClass == Float.TYPE) { // float
            instance = Float.valueOf(in.readFloat());
        } else if (declaredClass == Double.TYPE) { // double
            instance = Double.valueOf(in.readDouble());
        } else if (declaredClass == Void.TYPE) { // void
            instance = null;
        } else {
            throw new IllegalArgumentException("Not a primitive: " + declaredClass);
        }
    } else if (declaredClass.isArray()) { // array
        if (declaredClass.equals(byte[].class)) {
            instance = Bytes.readByteArray(in);
        } else if (declaredClass.equals(Result[].class)) {
            instance = Result.readArray(in);
        } else {
            int length = in.readInt();
            instance = Array.newInstance(declaredClass.getComponentType(), length);
            for (int i = 0; i < length; i++) {
                Array.set(instance, i, readObject(in, conf));
            }
        }
    } else if (declaredClass.equals(Array.class)) { //an array not declared in CLASS_TO_CODE
        Class<?> componentType = readClass(conf, in);
        int length = in.readInt();
        instance = Array.newInstance(componentType, length);
        for (int i = 0; i < length; i++) {
            Array.set(instance, i, readObject(in, conf));
        }
    } else if (List.class.isAssignableFrom(declaredClass)) { // List
        int length = in.readInt();
        instance = new ArrayList(length);
        for (int i = 0; i < length; i++) {
            ((ArrayList) instance).add(readObject(in, conf));
        }
    } else if (declaredClass == String.class) { // String
        instance = Text.readString(in);
    } else if (declaredClass.isEnum()) { // enum
        instance = Enum.valueOf((Class<? extends Enum>) declaredClass, Text.readString(in));
    } else if (declaredClass == Message.class) {
        String className = Text.readString(in);
        try {
            declaredClass = getClassByName(conf, className);
            instance = tryInstantiateProtobuf(declaredClass, in);
        } catch (ClassNotFoundException e) {
            LOG.error("Can't find class " + className, e);
            throw new IOException("Can't find class " + className, e);
        }
    } else { // Writable or Serializable
        Class instanceClass = null;
        int b = (byte) WritableUtils.readVInt(in);
        if (b == NOT_ENCODED) {
            String className = Text.readString(in);
            try {
                instanceClass = getClassByName(conf, className);
            } catch (ClassNotFoundException e) {
                LOG.error("Can't find class " + className, e);
                throw new IOException("Can't find class " + className, e);
            }
        } else {
            instanceClass = CODE_TO_CLASS.get(b);
        }
        if (Writable.class.isAssignableFrom(instanceClass)) {
            Writable writable = WritableFactories.newInstance(instanceClass, conf);
            try {
                writable.readFields(in);
            } catch (Exception e) {
                LOG.error("Error in readFields", e);
                throw new IOException("Error in readFields", e);
            }
            instance = writable;
            if (instanceClass == NullInstance.class) { // null
                declaredClass = ((NullInstance) instance).declaredClass;
                instance = null;
            }
        } else {
            int length = in.readInt();
            byte[] objectBytes = new byte[length];
            in.readFully(objectBytes);
            ByteArrayInputStream bis = null;
            ObjectInputStream ois = null;
            try {
                bis = new ByteArrayInputStream(objectBytes);
                ois = new ObjectInputStream(bis);
                instance = ois.readObject();
            } catch (ClassNotFoundException e) {
                LOG.error("Class not found when attempting to deserialize object", e);
                throw new IOException("Class not found when attempting to " + "deserialize object", e);
            } finally {
                if (bis != null)
                    bis.close();
                if (ois != null)
                    ois.close();
            }
        }
    }
    if (objectWritable != null) { // store values
        objectWritable.declaredClass = declaredClass;
        objectWritable.instance = instance;
    }
    return instance;
}

From source file:org.apache.hadoop.hbase.security.access.HbaseObjectWritableFor96Migration.java

/**
 * Read a {@link Writable}, {@link String}, primitive type, or an array of
 * the preceding./*from w w  w. ja v  a  2 s. c  o  m*/
 * @param in
 * @param objectWritable
 * @param conf
 * @return the object
 * @throws IOException
 */
@SuppressWarnings("unchecked")
static Object readObject(DataInput in, HbaseObjectWritableFor96Migration objectWritable, Configuration conf)
        throws IOException {
    Class<?> declaredClass = CODE_TO_CLASS.get(WritableUtils.readVInt(in));
    Object instance;
    if (declaredClass.isPrimitive()) { // primitive types
        if (declaredClass == Boolean.TYPE) { // boolean
            instance = Boolean.valueOf(in.readBoolean());
        } else if (declaredClass == Character.TYPE) { // char
            instance = Character.valueOf(in.readChar());
        } else if (declaredClass == Byte.TYPE) { // byte
            instance = Byte.valueOf(in.readByte());
        } else if (declaredClass == Short.TYPE) { // short
            instance = Short.valueOf(in.readShort());
        } else if (declaredClass == Integer.TYPE) { // int
            instance = Integer.valueOf(in.readInt());
        } else if (declaredClass == Long.TYPE) { // long
            instance = Long.valueOf(in.readLong());
        } else if (declaredClass == Float.TYPE) { // float
            instance = Float.valueOf(in.readFloat());
        } else if (declaredClass == Double.TYPE) { // double
            instance = Double.valueOf(in.readDouble());
        } else if (declaredClass == Void.TYPE) { // void
            instance = null;
        } else {
            throw new IllegalArgumentException("Not a primitive: " + declaredClass);
        }
    } else if (declaredClass.isArray()) { // array
        if (declaredClass.equals(byte[].class)) {
            instance = Bytes.readByteArray(in);
        } else {
            int length = in.readInt();
            instance = Array.newInstance(declaredClass.getComponentType(), length);
            for (int i = 0; i < length; i++) {
                Array.set(instance, i, readObject(in, conf));
            }
        }
    } else if (declaredClass.equals(Array.class)) { //an array not declared in CLASS_TO_CODE
        Class<?> componentType = readClass(conf, in);
        int length = in.readInt();
        instance = Array.newInstance(componentType, length);
        for (int i = 0; i < length; i++) {
            Array.set(instance, i, readObject(in, conf));
        }
    } else if (List.class.isAssignableFrom(declaredClass)) { // List
        int length = in.readInt();
        instance = new ArrayList(length);
        for (int i = 0; i < length; i++) {
            ((ArrayList) instance).add(readObject(in, conf));
        }
    } else if (declaredClass == String.class) { // String
        instance = Text.readString(in);
    } else if (declaredClass.isEnum()) { // enum
        instance = Enum.valueOf((Class<? extends Enum>) declaredClass, Text.readString(in));
    } else if (declaredClass == Message.class) {
        String className = Text.readString(in);
        try {
            declaredClass = getClassByName(conf, className);
            instance = tryInstantiateProtobuf(declaredClass, in);
        } catch (ClassNotFoundException e) {
            LOG.error("Can't find class " + className, e);
            throw new IOException("Can't find class " + className, e);
        }
    } else if (Scan.class.isAssignableFrom(declaredClass)) {
        int length = in.readInt();
        byte[] scanBytes = new byte[length];
        in.readFully(scanBytes);
        ClientProtos.Scan.Builder scanProto = ClientProtos.Scan.newBuilder();
        instance = ProtobufUtil.toScan(scanProto.mergeFrom(scanBytes).build());
    } else { // Writable or Serializable
        Class instanceClass = null;
        int b = (byte) WritableUtils.readVInt(in);
        if (b == NOT_ENCODED) {
            String className = Text.readString(in);
            try {
                instanceClass = getClassByName(conf, className);
            } catch (ClassNotFoundException e) {
                LOG.error("Can't find class " + className, e);
                throw new IOException("Can't find class " + className, e);
            }
        } else {
            instanceClass = CODE_TO_CLASS.get(b);
        }
        if (Writable.class.isAssignableFrom(instanceClass)) {
            Writable writable = WritableFactories.newInstance(instanceClass, conf);
            try {
                writable.readFields(in);
            } catch (Exception e) {
                LOG.error("Error in readFields", e);
                throw new IOException("Error in readFields", e);
            }
            instance = writable;
            if (instanceClass == NullInstance.class) { // null
                declaredClass = ((NullInstance) instance).declaredClass;
                instance = null;
            }
        } else {
            int length = in.readInt();
            byte[] objectBytes = new byte[length];
            in.readFully(objectBytes);
            ByteArrayInputStream bis = null;
            ObjectInputStream ois = null;
            try {
                bis = new ByteArrayInputStream(objectBytes);
                ois = new ObjectInputStream(bis);
                instance = ois.readObject();
            } catch (ClassNotFoundException e) {
                LOG.error("Class not found when attempting to deserialize object", e);
                throw new IOException("Class not found when attempting to " + "deserialize object", e);
            } finally {
                if (bis != null)
                    bis.close();
                if (ois != null)
                    ois.close();
            }
        }
    }
    if (objectWritable != null) { // store values
        objectWritable.declaredClass = declaredClass;
        objectWritable.instance = instance;
    }
    return instance;
}

From source file:net.solarnetwork.node.io.rxtx.SerialPortSupport.java

protected final String asciiDebugValue(byte[] data) {
    if (data == null || data.length < 1) {
        return "";
    }//from   w  w w. j a  v a2  s  . c  o m
    StringBuilder buf = new StringBuilder();
    buf.append(Hex.encodeHex(data)).append(" (");
    for (byte b : data) {
        if (b >= 32 && b < 126) {
            buf.append(Character.valueOf((char) b));
        } else {
            buf.append('~');
        }
    }
    buf.append(")");
    return buf.toString();
}