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:it.unimi.dsi.util.Properties.java

public void addProperty(final String key, final char c) {
    super.addProperty(key, Character.valueOf(c));
}

From source file:com.magnet.android.mms.request.GenericResponseParserPrimitiveTest.java

@SmallTest
public void testGenericPrimitiveResponse() throws MobileException, IOException {
    GenericResponseParser<String> parser = new GenericResponseParser<String>(String.class, null,
            GenericRestConstants.CONTENT_TYPE_TEXT_PLAIN, null);
    String response = "\"Whatever is the deal\"";

    String result = (String) parser.parseResponse(response.getBytes());
    assertEquals("Whatever is the deal", result);

    result = (String) parser.parseResponse((byte[]) null);
    assertEquals(null, result);//w w w.j  a  va  2 s  .c  om

    GenericResponseParser<Character> cparser = new GenericResponseParser<Character>(char.class, null,
            GenericRestConstants.CONTENT_TYPE_TEXT_PLAIN, null);
    char cresponse = 'p';

    try {
        Character cresult = (Character) cparser.parseResponse(String.valueOf(cresponse).getBytes("UTF-8"));
        assertEquals(Character.valueOf(cresponse), cresult);
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    GenericResponseParser<Integer> nparser = new GenericResponseParser<Integer>(Integer.class, null,
            GenericRestConstants.CONTENT_TYPE_TEXT_PLAIN, null);
    String strResponse = Integer.toString(Integer.MIN_VALUE);

    Integer nresult = (Integer) nparser.parseResponse(strResponse.getBytes());
    assertEquals(Integer.MIN_VALUE, nresult.intValue());
}

From source file:org.apache.camel.dataformat.csv.CsvDataFormatTest.java

@Test
public void shouldDisableEscape() {
    CsvDataFormat dataFormat = new CsvDataFormat().setEscapeDisabled(true).setEscape('e');

    // Properly saved
    assertSame(CSVFormat.DEFAULT, dataFormat.getFormat());
    assertTrue(dataFormat.isEscapeDisabled());
    assertEquals(Character.valueOf('e'), dataFormat.getEscape());

    // Properly used
    assertNull(dataFormat.getActiveFormat().getEscapeCharacter());
}

From source file:it.unimi.dsi.util.Properties.java

public void setProperty(final String key, final char b) {
    super.setProperty(key, Character.valueOf(b));
}

From source file:com.edgenius.wiki.render.filter.BasePatternFilter.java

/**
 * For example, if RichEdit bolds a piece of text which is inside pure text, the converted
 * bold markup requires a special surrounding. Here detect this case, if required, return "%" filter markup.
 * @param node/*from w w w .  j a v a 2 s .c o  m*/
 * @return
 */
protected String getSeparatorFilter(HTMLNode node) {
    if (!isNeedSpecialSurrounding())
        return "";

    HTMLNode cursor;
    boolean needSeparator = false;

    //check leading character, if it is not special character, then this markup need separator filter
    cursor = node.previous();
    while (cursor != null) {
        if (cursor.isTextNode()) {
            String text = cursor.getText();
            if (!StringUtils.isEmpty(text)) {
                text = Character.valueOf(text.charAt(text.length() - 1)).toString();
                //See our issue http://bug.edgenius.com/issues/34
                //and SUN Java bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6337993
                try {
                    needSeparator = !FilterRegxConstants.BORDER_PATTERN.matcher(text).find();
                    break;
                } catch (StackOverflowError e) {
                    AuditLogger.error("StackOverflow Error in BasePatternFilter.getSeparatorFilter. Input["
                            + text + "]  Pattern [" + FilterRegxConstants.BORDER_PATTERN.pattern() + "]");
                } catch (Throwable e) {
                    AuditLogger.error("Unexpected error in BasePatternFilter.getSeparatorFilter. Input[" + text
                            + "]  Pattern [" + FilterRegxConstants.BORDER_PATTERN.pattern() + "]", e);
                }
            }
        } else {
            //visible tag, such as <img src="img.jgp"> (will conver to !img.jpg!), they will convert some Word, 
            //so it means a separator are required. For example, <img src="..smiley.gif"><img src="some-att.jpg"> 
            // will convert to %%:(%%!img.jpg!, rather than :(!img.jpg!
            if (RenderUtil.isVisibleTag(cursor)) {
                needSeparator = true;
                break;
            }
            //need check if it is BLOCKHTML
            if (RenderUtil.isBlockTag(cursor)) {
                needSeparator = false;
                break;
            }
        }
        cursor = cursor.previous();
    }
    if (!needSeparator) {
        if (node.getPair() != null)
            cursor = node.getPair().next();
        else
            //this is for sigle tag node, such as image <img> which does not need close tag
            cursor = node.next();
        while (cursor != null) {
            if (cursor.isTextNode()) {
                String text = cursor.getText();
                if (!StringUtils.isEmpty(text)) {
                    text = Character.valueOf(text.charAt(0)).toString();
                    //See our issue http://bug.edgenius.com/issues/34
                    //and SUN Java bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6337993
                    try {
                        needSeparator = !FilterRegxConstants.BORDER_PATTERN.matcher(text).find();
                        break;
                    } catch (StackOverflowError e) {
                        AuditLogger.error("StackOverflow Error in BasePatternFilter.getSeparatorFilter. Input["
                                + text + "]  Pattern [" + FilterRegxConstants.BORDER_PATTERN.pattern() + "]");
                    } catch (Throwable e) {
                        AuditLogger.error(
                                "Unexpected error in BasePatternFilter.getSeparatorFilter. Input[" + text
                                        + "]  Pattern [" + FilterRegxConstants.BORDER_PATTERN.pattern() + "]",
                                e);
                    }
                }
            } else {
                //visible tag, such as <img src="img.jgp"> (will conver to !img.jpg!), they will convert some Word, 
                //so it means a separator are required. For example, <img src="..smiley.gif"><img src="some-att.jpg"> 
                // will convert to %:(%!img.jpg!, rather than :(!img.jpg!
                if (RenderUtil.isVisibleTag(cursor)) {
                    needSeparator = true;
                    break;
                }
                //need check if it is BLOCKHTML
                if (RenderUtil.isBlockTag(cursor)) {
                    needSeparator = false;
                    break;
                }
            }
            cursor = cursor.next();
        }
    }
    if (needSeparator)
        return "%%";
    else
        return "";
}

From source file:termint.gui.vt.VTElement.java

@Override
public int hashCode() {
    return Character.valueOf(c).hashCode();
}

From source file:com.workday.autoparse.json.demo.InstanceUpdaterTest.java

@Test
public void testBoxedPrimitives() {
    TestObject testObject = new TestObject();
    testObject.myBoxedBoolean = false;/*w w  w .ja v a 2s . c  o m*/
    testObject.myBoxedByte = 1;
    testObject.myBoxedChar = 'a';
    testObject.myBoxedDouble = 1.1;
    testObject.myBoxedFloat = 1.1f;
    testObject.myBoxedInt = 1;
    testObject.myBoxedLong = 1L;
    testObject.myBoxedShort = 1;

    Map<String, Object> updates = new HashMap<>();
    updates.put("myBoxedBoolean", true);
    updates.put("myBoxedByte", (byte) 2);
    updates.put("myBoxedChar", 'b');
    updates.put("myBoxedDouble", 2.2);
    updates.put("myBoxedFloat", 2.2f);
    updates.put("myBoxedInt", 2);
    updates.put("myBoxedLong", 2L);
    updates.put("myBoxedShort", (short) 2);

    TestObject$$JsonObjectParser.INSTANCE.updateInstanceFromMap(testObject, updates, CONTEXT);

    assertEquals("myBoxedBoolean", true, testObject.myBoxedBoolean);
    assertEquals("myBoxedByte", Byte.valueOf((byte) 2), testObject.myBoxedByte);
    assertEquals("myBoxedChar", Character.valueOf('b'), testObject.myBoxedChar);
    assertEquals("myBoxedDouble", Double.valueOf(2.2), testObject.myBoxedDouble);
    assertEquals("myBoxedFloat", Float.valueOf(2.2f), testObject.myBoxedFloat);
    assertEquals("myBoxedInt", Integer.valueOf(2), testObject.myBoxedInt);
    assertEquals("myBoxedLong", Long.valueOf(2L), testObject.myBoxedLong);
    assertEquals("myBoxedShort", Short.valueOf((short) 2), testObject.myBoxedShort);
}

From source file:de.hybris.platform.test.HJMPTest.java

@Test
public void testWriteReadValues() {
    LOG.debug(">>> testWriteReadValues()");

    final Float floatValue = new Float(1234.5678f);
    /* conv-LOG */LOG.debug(">>> Float (" + floatValue + ") <<<");
    remote.setFloat(floatValue);// w  w  w.j a v a2s . co m
    assertEquals(floatValue, remote.getFloat());

    final Double doubleValue = new Double(2234.5678d);
    /* conv-LOG */LOG.debug(">>> Double (" + doubleValue + ") <<<");
    remote.setDouble(doubleValue);
    assertEquals(doubleValue, remote.getDouble());

    final Character characterValue = Character.valueOf('g');
    /* conv-LOG */LOG.debug(">>> Character (" + characterValue + ") <<<");
    remote.setCharacter(characterValue);
    assertEquals(characterValue, remote.getCharacter());

    final Integer integerValue = Integer.valueOf(3357);
    /* conv-LOG */LOG.debug(">>> Integer (" + integerValue + ") <<<");
    remote.setInteger(integerValue);
    assertEquals(integerValue, remote.getInteger());

    final Long longValue = Long.valueOf(4357L);
    /* conv-LOG */LOG.debug(">>> Long (" + longValue + ") <<<");
    remote.setLong(longValue);
    assertEquals(longValue, remote.getLong());

    final Calendar now = Utilities.getDefaultCalendar();
    now.set(Calendar.MILLISECOND, 0);

    /* conv-LOG */LOG.debug(">>> Date (" + now + ", ms=" + now.getTime() + ")<<<");
    remote.setDate(now.getTime());
    final java.util.Date got = remote.getDate();
    /* conv-LOG */LOG.debug(">>> found (" + got.getTime() + ")<<<");
    assertEquals(now.getTime(), got);

    // try to get 123,4567 as big decimal
    final BigDecimal bigDecimalValue = BigDecimal.valueOf(1234567, 4);
    /* conv-LOG */LOG.debug(">>> BigDecimal (" + bigDecimalValue + ")<<<");
    remote.setBigDecimal(bigDecimalValue);
    assertTrue(remote.getBigDecimal().compareTo(bigDecimalValue) == 0);

    final java.sql.Timestamp timestamp = new java.sql.Timestamp(now.getTime().getTime());
    /* conv-LOG */LOG.debug(">>> Timestamp (" + timestamp + ", ms=" + timestamp.getTime() + ")<<<");
    remote.setDate(timestamp);
    final java.util.Date got2 = remote.getDate();
    /* conv-LOG */LOG.debug(">>> found (" + got2.getTime() + ")<<<");
    assertEquals(now.getTime(), got2);

    final String str = "Alles wird gut!";
    /* conv-LOG */LOG.debug(">>> String (" + str + ")<<<");
    remote.setString(str);
    assertEquals(str, remote.getString());

    final String longstr = "Alles wird lange gut!";
    /* conv-LOG */LOG.debug(">>> Long String (" + longstr + ")<<<");
    remote.setLongString(longstr);
    assertEquals(longstr, remote.getLongString());

    //put 50000 chars into it
    String longstr2 = "";
    for (int i2 = 0; i2 < 5000; i2++) {
        longstr2 += "01234567890";
    }
    remote.setLongString(longstr2);
    assertEquals(longstr2, remote.getLongString());

    /*
     * remote.setString( "" ); assertEquals( "" , remote.getString() ); remote.setString( null ); assertNull(
     * remote.getString() ); remote.setLongString( "" ); assertEquals( "" , remote.getLongString() );
     * remote.setLongString( null ); assertNull( remote.getLongString() );
     */

    final Boolean booleanValue = Boolean.TRUE;
    /* conv-LOG */LOG.debug(">>> Boolean (" + booleanValue + ")<<<");
    remote.setBoolean(booleanValue);
    assertEquals(booleanValue, remote.getBoolean());

    final float floatValue2 = 5234.5678f;
    /* conv-LOG */LOG.debug(">>> float (" + floatValue2 + ") <<<");
    remote.setPrimitiveFloat(floatValue2);
    assertTrue(">>> float (" + floatValue2 + ") <<<", floatValue2 == remote.getPrimitiveFloat());

    final double doubleValue2 = 6234.5678d;
    /* conv-LOG */LOG.debug(">>> double (" + doubleValue2 + ") <<<");
    remote.setPrimitiveDouble(doubleValue2);
    assertTrue(">>> double (" + doubleValue2 + ") <<<", doubleValue2 == remote.getPrimitiveDouble());

    final int integerValue2 = 7357;
    /* conv-LOG */LOG.debug(">>> int (" + integerValue2 + ") <<<");
    remote.setPrimitiveInteger(integerValue2);
    assertTrue(integerValue2 == remote.getPrimitiveInteger());

    final long longValue2 = 8357L;
    /* conv-LOG */LOG.debug(">>> long (" + longValue2 + ") <<<");
    remote.setPrimitiveLong(longValue2);
    assertTrue(">>> long (" + longValue2 + ") <<<", longValue2 == remote.getPrimitiveLong());

    final byte byteValue = 123;
    /* conv-LOG */LOG.debug(">>> byte (" + byteValue + ") <<<");
    remote.setPrimitiveByte(byteValue);
    assertTrue(">>> byte (" + byteValue + ") <<<", byteValue == remote.getPrimitiveByte());

    final boolean booleanValue2 = true;
    /* conv-LOG */LOG.debug(">>> boolean (" + booleanValue2 + ") <<<");
    remote.setPrimitiveBoolean(booleanValue2);
    assertTrue(">>> boolean (" + booleanValue2 + ") <<<", booleanValue2 == remote.getPrimitiveBoolean());

    final char characterValue2 = 'g';
    /* conv-LOG */LOG.debug(">>> char (" + characterValue2 + ") <<<");
    remote.setPrimitiveChar(characterValue2);
    assertTrue(">>> char (" + characterValue2 + ") <<<", characterValue2 == remote.getPrimitiveChar());

    final ArrayList list = new ArrayList();
    LOG.debug(">>> Serializable with standard classes (" + list + ") <<<");
    remote.setSerializable(list);
    assertEquals(list, remote.getSerializable());

    if (!Config.isOracleUsed()) {
        try {
            final HashMap bigtest = new HashMap();
            LOG.debug(">>> Serializable with standard classes (big thing) <<<");
            final byte[] byteArray = new byte[100000];
            bigtest.put("test", byteArray);
            remote.setSerializable(bigtest);
            final Map longtestret = (Map) remote.getSerializable();
            assertTrue(longtestret.size() == 1);
            final byte[] byteArray2 = (byte[]) longtestret.get("test");
            assertTrue(byteArray2.length == 100000);
        } catch (final Exception e) {
            throw new JaloSystemException(e,
                    "Unable to write big serializable object, db: " + Config.getDatabase() + ".", 0);
        }
    }

    /* conv-LOG */LOG.debug(">>> Serializable (null) <<<");
    remote.setSerializable(null);
    assertNull(remote.getSerializable());

    //         try
    //         {
    //            final Dummy dummy = new Dummy();
    //            /*conv-LOG*/ LOG.debug(">>> Serializable with custom classes ("+dummy+") <<<");
    //            remote.setSerializable( dummy );
    //            final Serializable x = remote.getSerializable();
    //            assertTrue( dummy.equals( x ) || x == null );
    //            if( x == null )
    //               /*conv-LOG*/ LOG.debug(">>> ok, we're still unable of writing non-serverside classes to database : read NULL ");
    //         }
    //         catch( Exception e )
    //         {
    //            /*conv-LOG*/ LOG.debug(">>> ok, we're still unable of writing non-serverside classes to database : " + e );
    //         }

    //
    // search
    //

    try {
        /* conv-LOG */LOG.debug(">>> Search ( " + str + ", " + integerValue2 + " ) <<<");
        home.finderTest(str, integerValue2);
    } catch (final Exception e) {
        e.printStackTrace();
        fail("TestItem not found!");
    }

    //
    // 'null' tests
    //

    /* conv-LOG */LOG.debug(">>> String (null) <<<");
    remote.setString(null);
    assertNull(remote.getString());

    /* conv-LOG */LOG.debug(">>> Character (null) <<<");
    remote.setCharacter(null);
    assertNull(remote.getCharacter());

    /* conv-LOG */LOG.debug(">>> Integer (null) <<<");
    remote.setInteger(null);
    assertNull(remote.getInteger());

    /* conv-LOG */LOG.debug(">>> Date (null) <<<");
    remote.setDate(null);
    assertNull(remote.getDate());

    /* conv-LOG */LOG.debug(">>> Double (null) <<<");
    remote.setDouble(null);
    assertNull(remote.getDouble());

    /* conv-LOG */LOG.debug(">>> Float (null) <<<");
    remote.setFloat(null);
    assertNull(remote.getFloat());
}

From source file:org.alfresco.serializers.PropertiesTypeConverter.java

@SuppressWarnings("rawtypes")
private PropertiesTypeConverter() {
    ////from   www. j  a  va 2  s . c  o  m
    // From string
    //
    addConverter(String.class, Class.class, new TypeConverter.Converter<String, Class>() {
        public Class convert(String source) {
            try {
                return Class.forName(source);
            } catch (ClassNotFoundException e) {
                throw new TypeConversionException("Failed to convert string to class: " + source, e);
            }
        }
    });
    addConverter(String.class, Boolean.class, new TypeConverter.Converter<String, Boolean>() {
        public Boolean convert(String source) {
            return Boolean.valueOf(source);
        }
    });
    addConverter(String.class, Character.class, new TypeConverter.Converter<String, Character>() {
        public Character convert(String source) {
            if ((source == null) || (source.length() == 0)) {
                return null;
            }
            return Character.valueOf(source.charAt(0));
        }
    });
    addConverter(String.class, Number.class, new TypeConverter.Converter<String, Number>() {
        public Number convert(String source) {
            try {
                return DecimalFormat.getNumberInstance().parse(source);
            } catch (ParseException e) {
                throw new TypeConversionException("Failed to parse number " + source, e);
            }
        }
    });
    addConverter(String.class, Byte.class, new TypeConverter.Converter<String, Byte>() {
        public Byte convert(String source) {
            return Byte.valueOf(source);
        }
    });
    addConverter(String.class, Short.class, new TypeConverter.Converter<String, Short>() {
        public Short convert(String source) {
            return Short.valueOf(source);
        }
    });
    addConverter(String.class, Integer.class, new TypeConverter.Converter<String, Integer>() {
        public Integer convert(String source) {
            return Integer.valueOf(source);
        }
    });
    addConverter(String.class, Long.class, new TypeConverter.Converter<String, Long>() {
        public Long convert(String source) {
            return Long.valueOf(source);
        }
    });
    addConverter(String.class, Float.class, new TypeConverter.Converter<String, Float>() {
        public Float convert(String source) {
            return Float.valueOf(source);
        }
    });
    addConverter(String.class, Double.class, new TypeConverter.Converter<String, Double>() {
        public Double convert(String source) {
            return Double.valueOf(source);
        }
    });
    addConverter(String.class, BigInteger.class, new TypeConverter.Converter<String, BigInteger>() {
        public BigInteger convert(String source) {
            return new BigInteger(source);
        }
    });
    addConverter(String.class, BigDecimal.class, new TypeConverter.Converter<String, BigDecimal>() {
        public BigDecimal convert(String source) {
            return new BigDecimal(source);
        }
    });
    addConverter(JSON.class, BigDecimal.class, new TypeConverter.Converter<JSON, BigDecimal>() {
        public BigDecimal convert(JSON source) {
            String type = (String) source.get("t");
            if (type.equals("FIXED_POINT")) {
                String number = (String) source.get("n");
                Integer precision = (Integer) source.get("p");
                MathContext ctx = new MathContext(precision);
                return new BigDecimal(number, ctx);
            } else {
                throw new IllegalArgumentException(
                        "Invalid source object for conversion " + source + ", expected a Fixed Decimal object");
            }
        }
    });
    addConverter(String.class, Date.class, new TypeConverter.Converter<String, Date>() {
        public Date convert(String source) {
            try {
                Date date = ISO8601DateFormat.parse(source);
                return date;
            } catch (PlatformRuntimeException e) {
                throw new TypeConversionException("Failed to convert date " + source + " to string", e);
            } catch (AlfrescoRuntimeException e) {
                throw new TypeConversionException("Failed to convert date " + source + " to string", e);
            }
        }
    });
    addConverter(String.class, Duration.class, new TypeConverter.Converter<String, Duration>() {
        public Duration convert(String source) {
            return new Duration(source);
        }
    });
    addConverter(String.class, QName.class, new TypeConverter.Converter<String, QName>() {
        public QName convert(String source) {
            return QName.createQName(source);
        }
    });
    addConverter(JSON.class, QName.class, new TypeConverter.Converter<JSON, QName>() {
        public QName convert(JSON source) {
            String type = (String) source.get("t");
            if (type.equals("QNAME")) {
                String qname = (String) source.get("v");
                return QName.createQName(qname);
            } else {
                throw new IllegalArgumentException();
            }
        }
    });
    addConverter(String.class, ContentData.class, new TypeConverter.Converter<String, ContentData>() {
        public ContentData convert(String source) {
            return ContentData.createContentProperty(source);
        }
    });
    addConverter(String.class, NodeRef.class, new TypeConverter.Converter<String, NodeRef>() {
        public NodeRef convert(String source) {
            return new NodeRef(source);
        }
    });
    addConverter(JSON.class, NodeRef.class, new TypeConverter.Converter<JSON, NodeRef>() {
        public NodeRef convert(JSON source) {
            String type = (String) source.get("t");
            if (!type.equals("NODEREF")) {
                throw new IllegalArgumentException(
                        "Invalid source object for conversion " + source + ", expected a NodeRef object");
            }
            String protocol = (String) source.get("p");
            String storeId = (String) source.get("s");
            String id = (String) source.get("id");
            NodeRef nodeRef = new NodeRef(new StoreRef(protocol, storeId), id);
            return nodeRef;
        }
    });
    addConverter(String.class, StoreRef.class, new TypeConverter.Converter<String, StoreRef>() {
        public StoreRef convert(String source) {
            return new StoreRef(source);
        }
    });
    addConverter(JSON.class, StoreRef.class, new TypeConverter.Converter<JSON, StoreRef>() {
        public StoreRef convert(JSON source) {
            String type = (String) source.get("t");
            if (!type.equals("STOREREF")) {
                throw new IllegalArgumentException(
                        "Invalid source object for conversion " + source + ", expected a StoreRef object");
            }
            String protocol = (String) source.get("p");
            String storeId = (String) source.get("s");
            return new StoreRef(protocol, storeId);
        }
    });
    addConverter(String.class, ChildAssociationRef.class,
            new TypeConverter.Converter<String, ChildAssociationRef>() {
                public ChildAssociationRef convert(String source) {
                    return new ChildAssociationRef(source);
                }
            });
    addConverter(String.class, AssociationRef.class, new TypeConverter.Converter<String, AssociationRef>() {
        public AssociationRef convert(String source) {
            return new AssociationRef(source);
        }
    });
    addConverter(String.class, InputStream.class, new TypeConverter.Converter<String, InputStream>() {
        public InputStream convert(String source) {
            try {
                return new ByteArrayInputStream(source.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                throw new TypeConversionException("Encoding not supported", e);
            }
        }
    });
    addConverter(String.class, MLText.class, new TypeConverter.Converter<String, MLText>() {
        public MLText convert(String source) {
            return new MLText(source);
        }
    });
    addConverter(JSON.class, MLText.class, new TypeConverter.Converter<JSON, MLText>() {
        public MLText convert(JSON source) {
            String type = (String) source.get("t");
            if (!type.equals("MLTEXT")) {
                throw new IllegalArgumentException(
                        "Invalid source object for conversion " + source + ", expected a NodeRef object");
            }

            MLText mlText = new MLText();
            for (String languageTag : source.keySet()) {
                String text = (String) source.get(languageTag);
                Locale locale = Locale.forLanguageTag(languageTag);
                mlText.put(locale, text);
            }

            return mlText;
        }
    });
    //        addConverter(JSON.class, ContentDataWithId.class, new TypeConverter.Converter<JSON, ContentDataWithId>()
    //        {
    //            public ContentDataWithId convert(JSON source)
    //            {
    //                String type = (String)source.get("t");
    //                if(!type.equals("CONTENT_DATA_ID"))
    //                {
    //                    throw new IllegalArgumentException("Invalid source object for conversion "
    //                            + source 
    //                            + ", expected a ContentDataWithId object");
    //                }
    //                String contentUrl = (String)source.get("u");
    //                String mimeType = (String)source.get("m");
    //                Long size = (Long)source.get("s");
    //                String encoding = (String)source.get("e");
    //                String languageTag = (String)source.get("l");
    //                Long id = (Long)source.get("id");
    //                Locale locale = Locale.forLanguageTag(languageTag);
    //
    //                ContentData contentData = new ContentData(contentUrl, mimeType, size, encoding, locale);
    //                ContentDataWithId contentDataWithId = new ContentDataWithId(contentData, id);
    //                return contentDataWithId;
    //            }
    //        });
    addConverter(JSON.class, ContentData.class, new TypeConverter.Converter<JSON, ContentData>() {
        public ContentData convert(JSON source) {
            ContentData contentData = null;

            String type = (String) source.get("t");
            if (type.equals("CONTENT")) {
                String contentUrl = (String) source.get("u");
                String mimeType = (String) source.get("m");
                Long size = (Long) source.get("s");
                String encoding = (String) source.get("e");
                String languageTag = (String) source.get("l");
                Locale locale = Locale.forLanguageTag(languageTag);
                contentData = new ContentData(contentUrl, mimeType, size, encoding, locale);
            } else if (type.equals("CONTENT_DATA_ID")) {
                String contentUrl = (String) source.get("u");
                String mimeType = (String) source.get("m");
                Long size = (Long) source.get("s");
                String encoding = (String) source.get("e");
                String languageTag = (String) source.get("l");
                Locale locale = Locale.forLanguageTag(languageTag);
                contentData = new ContentData(contentUrl, mimeType, size, encoding, locale);
            } else {
                throw new IllegalArgumentException(
                        "Invalid source object for conversion " + source + ", expected a ContentData object");
            }

            return contentData;
        }
    });
    addConverter(JSON.class, String.class, new TypeConverter.Converter<JSON, String>() {
        public String convert(JSON source) {
            // TODO distinguish between different BasicDBObject representations e.g. for MLText, ...

            Set<String> languageTags = source.keySet();
            if (languageTags.size() == 0) {
                throw new IllegalArgumentException("Persisted MLText is invalid " + source);
            } else if (languageTags.size() > 1) {
                // TODO
                logger.warn("Persisted MLText has more than 1 locale " + source);
            }

            String languageTag = languageTags.iterator().next();
            String text = (String) source.get(languageTag);
            return text;
        }
    });
    addConverter(String.class, Locale.class, new TypeConverter.Converter<String, Locale>() {
        public Locale convert(String source) {
            return I18NUtil.parseLocale(source);
        }
    });
    addConverter(String.class, Period.class, new TypeConverter.Converter<String, Period>() {
        public Period convert(String source) {
            return new Period(source);
        }
    });
    addConverter(String.class, VersionNumber.class, new TypeConverter.Converter<String, VersionNumber>() {
        public VersionNumber convert(String source) {
            return new VersionNumber(source);
        }
    });

    //
    // From Locale
    //
    addConverter(Locale.class, String.class, new TypeConverter.Converter<Locale, String>() {
        public String convert(Locale source) {
            String localeStr = source.toString();
            if (localeStr.length() < 6) {
                localeStr += "_";
            }
            return localeStr;
        }
    });

    //
    // From VersionNumber
    //
    addConverter(VersionNumber.class, String.class, new TypeConverter.Converter<VersionNumber, String>() {
        public String convert(VersionNumber source) {
            return source.toString();
        }
    });

    //
    // From MLText
    //
    addConverter(MLText.class, String.class, new TypeConverter.Converter<MLText, String>() {
        public String convert(MLText source) {
            return source.getDefaultValue();
        }
    });

    addConverter(MLText.class, JSON.class, new TypeConverter.Converter<MLText, JSON>() {
        public JSON convert(MLText source) {
            JSON map = new JSON();
            map.put("t", "MLTEXT");
            for (Map.Entry<Locale, String> entry : source.entrySet()) {
                map.put(entry.getKey().toLanguageTag(), entry.getValue());
            }
            return map;
        }
    });

    //        addConverter(ContentDataWithId.class, JSON.class, new TypeConverter.Converter<ContentDataWithId, JSON>()
    //        {
    //            public JSON convert(ContentDataWithId source)
    //            {
    //                JSON map = new JSON();
    //
    //                String contentUrl = source.getContentUrl();
    //                Long id = source.getId();
    //                String languageTag = source.getLocale().toLanguageTag();
    //                String encoding = source.getEncoding();
    //                long size = source.getSize();
    //                String mimeType = source.getMimetype();
    //
    //                map.put("t", "CONTENT_DATA_ID");
    //                map.put("u", contentUrl);
    //                map.put("m", mimeType);
    //                map.put("s", size);
    //                map.put("e", encoding);
    //                map.put("l", languageTag);
    //                map.put("id", id);
    //                return map;
    //            }
    //        });

    addConverter(ContentData.class, JSON.class, new TypeConverter.Converter<ContentData, JSON>() {
        public JSON convert(ContentData source) {
            JSON map = new JSON();

            String contentUrl = source.getContentUrl();
            String languageTag = source.getLocale().toLanguageTag();
            String encoding = source.getEncoding();
            long size = source.getSize();
            String mimeType = source.getMimetype();

            map.put("t", "CONTENT_DATA");
            map.put("u", contentUrl);
            map.put("m", mimeType);
            map.put("s", size);
            map.put("e", encoding);
            map.put("l", languageTag);
            return map;
        }
    });

    //
    // From enum
    //
    addConverter(Enum.class, String.class, new TypeConverter.Converter<Enum, String>() {
        public String convert(Enum source) {
            return source.toString();
        }
    });

    // From Period
    addConverter(Period.class, String.class, new TypeConverter.Converter<Period, String>() {
        public String convert(Period source) {
            return source.toString();
        }
    });

    // From Class
    addConverter(Class.class, String.class, new TypeConverter.Converter<Class, String>() {
        public String convert(Class source) {
            return source.getName();
        }
    });

    //
    // Number to Subtypes and Date
    //
    addConverter(Number.class, Boolean.class, new TypeConverter.Converter<Number, Boolean>() {
        public Boolean convert(Number source) {
            return new Boolean(source.longValue() > 0);
        }
    });
    addConverter(Number.class, Byte.class, new TypeConverter.Converter<Number, Byte>() {
        public Byte convert(Number source) {
            return Byte.valueOf(source.byteValue());
        }
    });
    addConverter(Number.class, Short.class, new TypeConverter.Converter<Number, Short>() {
        public Short convert(Number source) {
            return Short.valueOf(source.shortValue());
        }
    });
    addConverter(Number.class, Integer.class, new TypeConverter.Converter<Number, Integer>() {
        public Integer convert(Number source) {
            return Integer.valueOf(source.intValue());
        }
    });
    addConverter(Number.class, Long.class, new TypeConverter.Converter<Number, Long>() {
        public Long convert(Number source) {
            return Long.valueOf(source.longValue());
        }
    });
    addConverter(Number.class, Float.class, new TypeConverter.Converter<Number, Float>() {
        public Float convert(Number source) {
            return Float.valueOf(source.floatValue());
        }
    });
    addConverter(Number.class, Double.class, new TypeConverter.Converter<Number, Double>() {
        public Double convert(Number source) {
            return Double.valueOf(source.doubleValue());
        }
    });
    addConverter(Number.class, Date.class, new TypeConverter.Converter<Number, Date>() {
        public Date convert(Number source) {
            return new Date(source.longValue());
        }
    });
    addConverter(Number.class, String.class, new TypeConverter.Converter<Number, String>() {
        public String convert(Number source) {
            return source.toString();
        }
    });
    addConverter(Number.class, BigInteger.class, new TypeConverter.Converter<Number, BigInteger>() {
        public BigInteger convert(Number source) {
            if (source instanceof BigDecimal) {
                return ((BigDecimal) source).toBigInteger();
            } else {
                return BigInteger.valueOf(source.longValue());
            }
        }
    });
    addConverter(Number.class, BigDecimal.class, new TypeConverter.Converter<Number, BigDecimal>() {
        public BigDecimal convert(Number source) {
            if (source instanceof BigInteger) {
                return new BigDecimal((BigInteger) source);
            } else if (source instanceof Double) {
                return BigDecimal.valueOf((Double) source);
            } else if (source instanceof Float) {
                Float val = (Float) source;
                if (val.isInfinite()) {
                    // What else can we do here?  this is 3.4 E 38 so is fairly big
                    return new BigDecimal(Float.MAX_VALUE);
                }
                return BigDecimal.valueOf((Float) source);
            } else {
                return BigDecimal.valueOf(source.longValue());
            }
        }
    });
    addDynamicTwoStageConverter(Number.class, String.class, InputStream.class);

    //
    // Date, Timestamp ->
    //
    addConverter(Timestamp.class, Date.class, new TypeConverter.Converter<Timestamp, Date>() {
        public Date convert(Timestamp source) {
            return new Date(source.getTime());
        }
    });
    addConverter(Date.class, Number.class, new TypeConverter.Converter<Date, Number>() {
        public Number convert(Date source) {
            return Long.valueOf(source.getTime());
        }
    });
    addConverter(Date.class, String.class, new TypeConverter.Converter<Date, String>() {
        public String convert(Date source) {
            try {
                return ISO8601DateFormat.format(source);
            } catch (PlatformRuntimeException e) {
                throw new TypeConversionException("Failed to convert date " + source + " to string", e);
            }
        }
    });
    addConverter(Date.class, Calendar.class, new TypeConverter.Converter<Date, Calendar>() {
        public Calendar convert(Date source) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(source);
            return calendar;
        }
    });

    addConverter(Date.class, GregorianCalendar.class, new TypeConverter.Converter<Date, GregorianCalendar>() {
        public GregorianCalendar convert(Date source) {
            GregorianCalendar calendar = new GregorianCalendar();
            calendar.setTime(source);
            return calendar;
        }
    });
    addDynamicTwoStageConverter(Date.class, String.class, InputStream.class);

    //
    // Boolean ->
    //
    final Long LONG_FALSE = new Long(0L);
    final Long LONG_TRUE = new Long(1L);
    addConverter(Boolean.class, Long.class, new TypeConverter.Converter<Boolean, Long>() {
        public Long convert(Boolean source) {
            return source.booleanValue() ? LONG_TRUE : LONG_FALSE;
        }
    });
    addConverter(Boolean.class, String.class, new TypeConverter.Converter<Boolean, String>() {
        public String convert(Boolean source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(Boolean.class, String.class, InputStream.class);

    //
    // Character ->
    //
    addConverter(Character.class, String.class, new TypeConverter.Converter<Character, String>() {
        public String convert(Character source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(Character.class, String.class, InputStream.class);

    //
    // Duration ->
    //
    addConverter(Duration.class, String.class, new TypeConverter.Converter<Duration, String>() {
        public String convert(Duration source) {
            return source.toString();
        }

    });
    addDynamicTwoStageConverter(Duration.class, String.class, InputStream.class);

    //
    // Byte
    //
    addConverter(Byte.class, String.class, new TypeConverter.Converter<Byte, String>() {
        public String convert(Byte source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(Byte.class, String.class, InputStream.class);

    //
    // Short
    //
    addConverter(Short.class, String.class, new TypeConverter.Converter<Short, String>() {
        public String convert(Short source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(Short.class, String.class, InputStream.class);

    //
    // Integer
    //
    addConverter(Integer.class, String.class, new TypeConverter.Converter<Integer, String>() {
        public String convert(Integer source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(Integer.class, String.class, InputStream.class);

    //
    // Long
    //
    addConverter(Long.class, String.class, new TypeConverter.Converter<Long, String>() {
        public String convert(Long source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(Long.class, String.class, InputStream.class);

    //
    // Float
    //
    addConverter(Float.class, String.class, new TypeConverter.Converter<Float, String>() {
        public String convert(Float source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(Float.class, String.class, InputStream.class);

    //
    // Double
    //
    addConverter(Double.class, String.class, new TypeConverter.Converter<Double, String>() {
        public String convert(Double source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(Double.class, String.class, InputStream.class);

    //
    // BigInteger
    //
    addConverter(BigInteger.class, String.class, new TypeConverter.Converter<BigInteger, String>() {
        public String convert(BigInteger source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(BigInteger.class, String.class, InputStream.class);

    //
    // Calendar
    //
    addConverter(Calendar.class, Date.class, new TypeConverter.Converter<Calendar, Date>() {
        public Date convert(Calendar source) {
            return source.getTime();
        }
    });
    addConverter(Calendar.class, String.class, new TypeConverter.Converter<Calendar, String>() {
        public String convert(Calendar source) {
            try {
                return ISO8601DateFormat.format(source.getTime());
            } catch (PlatformRuntimeException e) {
                throw new TypeConversionException("Failed to convert date " + source + " to string", e);
            }
        }
    });

    //
    // BigDecimal
    //
    addConverter(BigDecimal.class, JSON.class, new TypeConverter.Converter<BigDecimal, JSON>() {
        public JSON convert(BigDecimal source) {
            String number = source.toPlainString();
            int precision = source.precision();
            JSON map = new JSON();
            map.put("t", "FIXED_POINT");
            map.put("n", number);
            map.put("p", precision);
            return map;
        }
    });
    addConverter(BigDecimal.class, String.class, new TypeConverter.Converter<BigDecimal, String>() {
        public String convert(BigDecimal source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(BigDecimal.class, String.class, InputStream.class);

    //
    // QName
    //
    addConverter(QName.class, String.class, new TypeConverter.Converter<QName, String>() {
        public String convert(QName source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(QName.class, String.class, InputStream.class);

    //
    // EntityRef (NodeRef, ChildAssociationRef, NodeAssociationRef)
    //
    addConverter(EntityRef.class, String.class, new TypeConverter.Converter<EntityRef, String>() {
        public String convert(EntityRef source) {
            return source.toString();
        }
    });
    addConverter(EntityRef.class, JSON.class, new TypeConverter.Converter<EntityRef, JSON>() {
        public JSON convert(EntityRef source) {
            JSON ret = null;

            if (source instanceof NodeRef) {
                NodeRef nodeRef = (NodeRef) source;

                JSON map = new JSON();
                map.put("t", "NODEREF");
                map.put("p", nodeRef.getStoreRef().getProtocol());
                map.put("s", nodeRef.getStoreRef().getIdentifier());
                map.put("id", nodeRef.getId());
                ret = map;
            } else if (source instanceof StoreRef) {
                StoreRef storeRef = (StoreRef) source;

                JSON map = new JSON();
                map.put("t", "STOREREF");
                map.put("p", storeRef.getProtocol());
                map.put("s", storeRef.getIdentifier());
                ret = map;
            } else {
                throw new IllegalArgumentException();
            }

            return ret;
        }
    });
    addDynamicTwoStageConverter(EntityRef.class, String.class, InputStream.class);

    //
    // ContentData
    //
    addConverter(ContentData.class, String.class, new TypeConverter.Converter<ContentData, String>() {
        public String convert(ContentData source) {
            return source.getInfoUrl();
        }
    });
    addDynamicTwoStageConverter(ContentData.class, String.class, InputStream.class);

    //
    // Path
    //
    addConverter(Path.class, String.class, new TypeConverter.Converter<Path, String>() {
        public String convert(Path source) {
            return source.toString();
        }
    });
    addDynamicTwoStageConverter(Path.class, String.class, InputStream.class);

    //
    // Content Reader
    //
    addConverter(ContentReader.class, InputStream.class,
            new TypeConverter.Converter<ContentReader, InputStream>() {
                public InputStream convert(ContentReader source) {
                    return source.getContentInputStream();
                }
            });
    addConverter(ContentReader.class, String.class, new TypeConverter.Converter<ContentReader, String>() {
        public String convert(ContentReader source) {
            // Getting the string from the ContentReader binary is meaningless
            return source.toString();
        }
    });

    //
    // Content Writer
    //
    addConverter(ContentWriter.class, String.class, new TypeConverter.Converter<ContentWriter, String>() {
        public String convert(ContentWriter source) {
            return source.toString();
        }
    });

    //        addConverter(Collection.class, BasicDBList.class, new TypeConverter.Converter<Collection, BasicDBList>()
    //        {
    //            public BasicDBList convert(Collection source)
    //            {
    //                BasicDBList ret = new BasicDBList();
    //                for(Object o : source)
    //                {
    //                    ret.add(o);
    //                }
    //                return ret;
    //            }
    //        });

    //        addConverter(BasicDBList.class, Collection.class, new TypeConverter.Converter<BasicDBList, Collection>()
    //        {
    //            @SuppressWarnings("unchecked")
    //            public Collection convert(BasicDBList source)
    //            {
    //                Collection ret = new LinkedList();
    //                for(Object o : source)
    //                {
    //                    ret.add(o);
    //                }
    //                return ret;
    //            }
    //        });

    //
    // Input Stream
    //
    addConverter(InputStream.class, String.class, new TypeConverter.Converter<InputStream, String>() {
        public String convert(InputStream source) {
            try {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                byte[] buffer = new byte[8192];
                int read;
                while ((read = source.read(buffer)) > 0) {
                    out.write(buffer, 0, read);
                }
                byte[] data = out.toByteArray();
                return new String(data, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw new TypeConversionException("Cannot convert input stream to String.", e);
            } catch (IOException e) {
                throw new TypeConversionException("Conversion from stream to string failed", e);
            } finally {
                if (source != null) {
                    try {
                        source.close();
                    } catch (IOException e) {
                        //NOOP
                    }
                }
            }
        }
    });
    addDynamicTwoStageConverter(InputStream.class, String.class, Date.class);

    addDynamicTwoStageConverter(InputStream.class, String.class, Double.class);

    addDynamicTwoStageConverter(InputStream.class, String.class, Long.class);

    addDynamicTwoStageConverter(InputStream.class, String.class, Boolean.class);

    addDynamicTwoStageConverter(InputStream.class, String.class, QName.class);

    addDynamicTwoStageConverter(InputStream.class, String.class, Path.class);

    addDynamicTwoStageConverter(InputStream.class, String.class, NodeRef.class);

}

From source file:org.apache.camel.dataformat.csv.CsvDataFormatTest.java

@Test
public void shouldOverrideEscape() {
    CsvDataFormat dataFormat = new CsvDataFormat().setEscape('e');

    // Properly saved
    assertSame(CSVFormat.DEFAULT, dataFormat.getFormat());
    assertEquals(Character.valueOf('e'), dataFormat.getEscape());

    // Properly used
    assertEquals(Character.valueOf('e'), dataFormat.getActiveFormat().getEscapeCharacter());
}