當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。