本文整理匯總了Java中com.taobao.tddl.common.utils.convertor.Convertor.convert方法的典型用法代碼示例。如果您正苦於以下問題:Java Convertor.convert方法的具體用法?Java Convertor.convert怎麽用?Java Convertor.convert使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.taobao.tddl.common.utils.convertor.Convertor
的用法示例。
在下文中一共展示了Convertor.convert方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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));
}
示例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() + "]");
}
}
}
示例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]);
}
示例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]);
}
示例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());
}
示例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));
}
示例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);
}
示例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);
}
示例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;
}
}
}
示例10: 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;
}
}
}