当前位置: 首页>>代码示例>>Java>>正文


Java Convertor类代码示例

本文整理汇总了Java中com.taobao.tddl.common.utils.convertor.Convertor的典型用法代码示例。如果您正苦于以下问题:Java Convertor类的具体用法?Java Convertor怎么用?Java Convertor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


Convertor类属于com.taobao.tddl.common.utils.convertor包,在下文中一共展示了Convertor类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testStringAndDateDefault

import com.taobao.tddl.common.utils.convertor.Convertor; //导入依赖的package包/类
@Test
public void testStringAndDateDefault() {
    Convertor stringDate = helper.getConvertor(String.class, Date.class);
    Convertor dateString = helper.getConvertor(Date.class, String.class);

    Convertor stringCalendar = helper.getConvertor(String.class, Calendar.class);
    Convertor calendarString = helper.getConvertor(Calendar.class, String.class);

    String time = "2010-10-01 23:59:59";
    Calendar c1 = Calendar.getInstance();
    c1.set(2010, 10 - 1, 01, 23, 59, 59);
    c1.set(Calendar.MILLISECOND, 0);
    Date timeDate = c1.getTime();

    // 验证默认的转化器
    Object stringDateValue = stringDate.convert(time, Date.class);
    Assert.assertTrue(timeDate.equals(stringDateValue));
    Object dateStringValue = dateString.convert(timeDate, String.class);
    Assert.assertTrue(time.equals(dateStringValue));

    Object stringCalendarValue = stringCalendar.convert(time, Calendar.class);
    Assert.assertTrue(c1.equals(stringCalendarValue));
    Object calendarStringValue = calendarString.convert(c1, String.class);
    Assert.assertTrue(time.equals(calendarStringValue));
}
 
开发者ID:loye168,项目名称:tddl5,代码行数:26,代码来源:StringConvertorTest.java

示例2: longValue

import com.taobao.tddl.common.utils.convertor.Convertor; //导入依赖的package包/类
public static Long longValue(Object o) {
    if (o == null) {
        return null;
    }

    try {
        return (Long) commonConvertor.convert(o, Long.class);
    } catch (ConvertorException e) {
        Convertor convertor = ConvertorHelper.getInstance().getConvertor(o.getClass(), Long.class);
        if (convertor != null) {
            return (Long) convertor.convert(o, Long.class);
        } else {
            throw new ConvertorException("Unsupported convert: [" + o.getClass().getName() + ","
                                         + Long.class.getName() + "]");
        }
    }
}
 
开发者ID:loye168,项目名称:tddl5,代码行数:18,代码来源:GroovyStaticMethod.java

示例3: testArrayToList

import com.taobao.tddl.common.utils.convertor.Convertor; //导入依赖的package包/类
@Test
public void testArrayToList() {
    Convertor intList = helper.getConvertor(int[].class, List.class);
    Convertor integerList = helper.getConvertor(Integer[].class, List.class);

    int[] intArray = new int[] { 1, 2 };
    Integer[] integerArray = new Integer[] { 1, 2 };
    List intListValue = (List) intList.convert(intArray, List.class);
    List integerListValue = (List) integerList.convert(integerArray, List.class);
    assertEquals(intListValue.size(), intArray.length);
    assertEquals(intListValue.get(0), intArray[0]);
    assertEquals(integerListValue.size(), integerArray.length);
    assertEquals(integerListValue.get(0), integerArray[0]);
    // 测试不同类型转化, common对象
    List<BigInteger> intListValueOther = (List) intList.convertCollection(intArray, List.class, BigInteger.class); // int强制转为BigInteger
    List<BigDecimal> integerListValueOther = (List) intList.convertCollection(intArray,
        List.class,
        BigDecimal.class); // int强制转为BigDecimal
    assertEquals(intListValueOther.size(), intArray.length);
    assertEquals(intListValueOther.get(0).intValue(), intArray[0]);
    assertEquals(integerListValueOther.size(), integerArray.length);
    assertEquals(integerListValueOther.get(0).intValue(), integerArray[0].intValue());

    // BigDecimal & BigInteger
    Convertor bigDecimalList = helper.getConvertor(BigDecimal[].class, ArrayList.class);
    Convertor bigIntegerList = helper.getConvertor(BigInteger[].class, Vector.class);

    BigDecimal[] bigDecimalArray = new BigDecimal[] { BigDecimal.ZERO, BigDecimal.ONE };
    BigInteger[] bigIntegerArray = new BigInteger[] { BigInteger.ZERO, BigInteger.ONE };
    List bigDecimalListValue = (List) bigDecimalList.convert(bigDecimalArray, ArrayList.class);
    List bigIntegerListValue = (List) bigIntegerList.convert(bigIntegerArray, Vector.class);
    assertEquals(bigDecimalListValue.size(), bigDecimalArray.length);
    assertEquals(bigDecimalListValue.get(0), bigDecimalArray[0]);
    assertEquals(bigIntegerListValue.size(), bigIntegerArray.length);
    assertEquals(bigIntegerListValue.get(0), bigIntegerArray[0]);

}
 
开发者ID:loye168,项目名称:tddl5,代码行数:38,代码来源:CollectionAndCollectionTest.java

示例4: testArrayAndSet

import com.taobao.tddl.common.utils.convertor.Convertor; //导入依赖的package包/类
@Test
public void testArrayAndSet() {
    Convertor intSet = helper.getConvertor(int[].class, Set.class);
    Convertor integerSet = helper.getConvertor(Integer[].class, Set.class);

    int[] intArray = new int[] { 1, 2 };
    Integer[] integerArray = new Integer[] { 1, 2 };
    Set intSetValue = (Set) intSet.convert(intArray, Set.class);
    Set integerSetValue = (Set) integerSet.convert(integerArray, Set.class);
    assertEquals(intSetValue.size(), intArray.length);
    assertEquals(intSetValue.iterator().next(), intArray[0]);
    assertEquals(integerSetValue.size(), integerArray.length);
    assertEquals(integerSetValue.iterator().next(), integerArray[0]);
    // 测试不同类型转化, common对象
    Set<BigInteger> intSetValueOther = (Set) intSet.convertCollection(intArray, Set.class, BigInteger.class); // int强制转为BigInteger
    Set<BigDecimal> integerSetValueOther = (Set) integerSet.convertCollection(intArray, Set.class, BigDecimal.class); // int强制转为BigDecimal
    assertEquals(intSetValueOther.size(), intArray.length);
    assertEquals(intSetValueOther.iterator().next().intValue(), intArray[0]);
    assertEquals(integerSetValueOther.size(), integerArray.length);

    // BigDecimal & BigInteger
    Convertor bigDecimalSet = helper.getConvertor(BigDecimal[].class, HashSet.class);
    Convertor bigIntegerSet = helper.getConvertor(BigInteger[].class, LinkedHashSet.class);

    BigDecimal[] bigDecimalArray = new BigDecimal[] { BigDecimal.ZERO, BigDecimal.ONE };
    BigInteger[] bigIntegerArray = new BigInteger[] { BigInteger.ZERO, BigInteger.ONE };
    Set bigDecimalSetValue = (Set) bigDecimalSet.convert(bigDecimalArray, HashSet.class);
    Set bigIntegerSetValue = (Set) bigIntegerSet.convert(bigIntegerArray, LinkedHashSet.class);
    assertEquals(bigDecimalSetValue.size(), bigDecimalArray.length);
    assertEquals(bigDecimalSetValue.iterator().next(), bigDecimalArray[0]);
    assertEquals(bigIntegerSetValue.size(), bigIntegerArray.length);
    assertEquals(bigIntegerSetValue.iterator().next(), bigIntegerArray[0]);

}
 
开发者ID:loye168,项目名称:tddl5,代码行数:35,代码来源:CollectionAndCollectionTest.java

示例5: testCollectionToArray

import com.taobao.tddl.common.utils.convertor.Convertor; //导入依赖的package包/类
@Test
public void testCollectionToArray() {
    // 进行List -> Array处理
    List<Integer> intListValue = Arrays.asList(1);
    // 测试不同类型转化, common对象
    Convertor intList = helper.getConvertor(List.class, int[].class);
    Convertor integerList = helper.getConvertor(List.class, Integer[].class);
    int[] intArray = (int[]) intList.convert(intListValue, int[].class);
    Integer[] integerArray = (Integer[]) integerList.convert(intListValue, Integer[].class);
    assertEquals(intListValue.size(), intArray.length);
    assertEquals(intListValue.get(0).intValue(), intArray[0]);
    assertEquals(intListValue.size(), integerArray.length);
    assertEquals(intListValue.get(0), integerArray[0]);
    // 测试不同类型转化, common对象
    BigInteger[] bigIntegerValueOther = (BigInteger[]) intList.convertCollection(intListValue,
        BigInteger[].class,
        BigInteger.class); // int强制转为BigInteger
    BigDecimal[] bigDecimalValueOther = (BigDecimal[]) intList.convertCollection(intListValue,
        BigDecimal[].class,
        BigDecimal.class); // int强制转为BigDecimal
    assertEquals(bigIntegerValueOther.length, intListValue.size());
    assertEquals(bigIntegerValueOther[0].intValue(), intListValue.get(0).intValue());
    assertEquals(bigDecimalValueOther.length, intListValue.size());
    assertEquals(bigDecimalValueOther[0].intValue(), intListValue.get(0).intValue());

    // BigDecimal & BigInteger
    Convertor bigDecimalSet = helper.getConvertor(List.class, BigDecimal[].class);
    Convertor bigIntegerSet = helper.getConvertor(List.class, BigInteger[].class);

    List<BigDecimal> bigDecimalList = Arrays.asList(BigDecimal.ONE);
    List<BigInteger> bigIntegerList = Arrays.asList(BigInteger.ONE);
    BigDecimal[] bigDecimalArrayValue = (BigDecimal[]) bigDecimalSet.convert(bigDecimalList, BigDecimal[].class);
    BigInteger[] bigIntegerArrayValue = (BigInteger[]) bigIntegerSet.convert(bigIntegerList, BigInteger[].class);
    assertEquals(bigDecimalArrayValue.length, bigDecimalList.size());
    assertEquals(bigDecimalArrayValue[0].intValue(), bigDecimalList.get(0).intValue());
    assertEquals(bigIntegerArrayValue.length, bigIntegerList.size());
    assertEquals(bigIntegerArrayValue[0].intValue(), bigIntegerList.get(0).intValue());

}
 
开发者ID:loye168,项目名称:tddl5,代码行数:40,代码来源:CollectionAndCollectionTest.java

示例6: testCollectionAndCollection

import com.taobao.tddl.common.utils.convertor.Convertor; //导入依赖的package包/类
@Test
public void testCollectionAndCollection() {
    Convertor intSet = helper.getConvertor(List.class, Set.class);

    List intList = Arrays.asList(1);
    Set intSetValue = (Set) intSet.convert(intList, Set.class);
    assertEquals(intSetValue.size(), intList.size());
    assertEquals(intSetValue.iterator().next(), intList.get(0));
    // 测试不同类型转化, common对象
    Set<BigInteger> intSetValueOther = (Set) intSet.convertCollection(intList, Set.class, BigInteger.class); // int强制转为BigInteger
    Set<BigDecimal> decimalSetValueOther = (Set) intSet.convertCollection(intList, Set.class, BigDecimal.class); // int强制转为BigDecimal
    assertEquals(intSetValueOther.size(), intList.size());
    assertEquals(intSetValueOther.iterator().next().intValue(), intList.get(0));
    assertEquals(decimalSetValueOther.size(), intList.size());
    assertEquals(decimalSetValueOther.iterator().next().intValue(), intList.size());

    // BigDecimal & BigInteger
    Convertor bigDecimalSet = helper.getConvertor(List.class, HashSet.class);
    Convertor bigIntegerSet = helper.getConvertor(List.class, LinkedHashSet.class);

    List bigDecimalList = Arrays.asList(BigDecimal.ONE);
    List bigIntegerList = Arrays.asList(BigInteger.ONE);
    Set bigDecimalSetValue = (Set) bigDecimalSet.convert(bigDecimalList, HashSet.class);
    Set bigIntegerSetValue = (Set) bigIntegerSet.convert(bigIntegerList, LinkedHashSet.class);
    assertEquals(bigDecimalSetValue.size(), bigDecimalList.size());
    assertEquals(bigDecimalSetValue.iterator().next(), bigDecimalList.get(0));
    assertEquals(bigIntegerSetValue.size(), bigIntegerList.size());
    assertEquals(bigIntegerSetValue.iterator().next(), bigIntegerList.get(0));
}
 
开发者ID:loye168,项目名称:tddl5,代码行数:30,代码来源:CollectionAndCollectionTest.java

示例7: testDateAndSqlDate

import com.taobao.tddl.common.utils.convertor.Convertor; //导入依赖的package包/类
@Test
public void testDateAndSqlDate() {
    Calendar c1 = Calendar.getInstance();
    c1.set(2010, 10 - 1, 01, 23, 59, 59);
    c1.set(Calendar.MILLISECOND, 0);
    Date timeDate = c1.getTime();

    Convertor dateToSql = helper.getConvertor(Date.class, java.sql.Date.class);
    java.sql.Date sqlDate = (java.sql.Date) dateToSql.convert(timeDate, java.sql.Date.class);
    Assert.assertNotNull(sqlDate);

    java.sql.Time sqlTime = (java.sql.Time) dateToSql.convert(timeDate, java.sql.Time.class);
    Assert.assertNotNull(sqlTime);

    java.sql.Timestamp sqlTimestamp = (java.sql.Timestamp) dateToSql.convert(timeDate, java.sql.Timestamp.class);
    Assert.assertNotNull(sqlTimestamp);

    Convertor sqlToDate = helper.getConvertor(java.sql.Date.class, Date.class);
    Date date = (Date) sqlToDate.convert(sqlDate, Date.class);
    Assert.assertEquals(timeDate, date);

    date = (Date) sqlToDate.convert(sqlTime, Date.class);
    Assert.assertEquals(timeDate, date);

    date = (Date) sqlToDate.convert(sqlTimestamp, Date.class);
    Assert.assertEquals(timeDate, date);
}
 
开发者ID:loye168,项目名称:tddl5,代码行数:28,代码来源:DateAndSqlDateTest.java

示例8: testStringAndEnum

import com.taobao.tddl.common.utils.convertor.Convertor; //导入依赖的package包/类
@Test
public void testStringAndEnum() {
    Convertor enumToString = helper.getConvertor(TestEnum.class, String.class);
    Convertor stringtoEnum = helper.getConvertor(String.class, TestEnum.class);
    String VALUE = "TWO";

    Object str = enumToString.convert(TestEnum.TWO, String.class); // 数字
    Assert.assertEquals(str, VALUE);
    Object enumobj = stringtoEnum.convert(VALUE, TestEnum.class); // BigDecimal
    Assert.assertEquals(enumobj, TestEnum.TWO);
}
 
开发者ID:loye168,项目名称:tddl5,代码行数:12,代码来源:StringAndEnumTest.java

示例9: convertFrom

import com.taobao.tddl.common.utils.convertor.Convertor; //导入依赖的package包/类
@Override
public DATA convertFrom(Object value) {
    if (value == null || value instanceof NullValue) {
        return null;
    } else {
        Convertor convertor = getConvertor(value.getClass());
        if (convertor != null) {
            // 没有convertor代表类型和目标相同,不需要做转化
            return (DATA) convertor.convert(value, getDataClass());
        } else {
            return (DATA) value;
        }
    }
}
 
开发者ID:loye168,项目名称:tddl5,代码行数:15,代码来源:AbstractDataType.java

示例10: getConvertor

import com.taobao.tddl.common.utils.convertor.Convertor; //导入依赖的package包/类
/**
 * 获取convertor接口
 */
protected Convertor getConvertor(Class clazz) {
    if (clazz.equals(getDataClass())) {
        return null;
    }

    Convertor convertor = ConvertorHelper.getInstance().getConvertor(clazz, getDataClass());
    if (convertor == null) {
        throw new ConvertorException("Unsupported convert: [" + clazz.getName() + "," + getDataClass().getName()
                                     + "]");
    } else {
        return convertor;
    }
}
 
开发者ID:loye168,项目名称:tddl5,代码行数:17,代码来源:AbstractDataType.java

示例11: convertFrom

import com.taobao.tddl.common.utils.convertor.Convertor; //导入依赖的package包/类
@Override
public DATA convertFrom(Object value) {
    if (value == null) {
        return null;
    } else {
        Convertor convertor = getConvertor(value.getClass());
        if (convertor != null) {
            // 没有convertor代表类型和目标相同,不需要做转化
            return (DATA) convertor.convert(value, getDataClass());
        } else {
            return (DATA) value;
        }
    }
}
 
开发者ID:beebeandwer,项目名称:TDDL,代码行数:15,代码来源:AbstractDataType.java


注:本文中的com.taobao.tddl.common.utils.convertor.Convertor类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。