?? primitiveorwrapperconverter.java
字號(hào):
/*
* Copyright 2005-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.sf.dozer.util.mapping.converters;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import net.sf.dozer.util.mapping.util.DateFormatContainer;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.apache.commons.beanutils.converters.BigIntegerConverter;
import org.apache.commons.beanutils.converters.BooleanConverter;
import org.apache.commons.beanutils.converters.ByteConverter;
import org.apache.commons.beanutils.converters.CharacterConverter;
import org.apache.commons.beanutils.converters.DoubleConverter;
import org.apache.commons.beanutils.converters.FloatConverter;
import org.apache.commons.beanutils.converters.IntegerConverter;
import org.apache.commons.beanutils.converters.LongConverter;
import org.apache.commons.beanutils.converters.ShortConverter;
/**
* @author tierney.matt
* @author garsombke.franz
* @author benson.matt
*/
public class PrimitiveOrWrapperConverter {
private static final Map PRIMITIVE_TYPE_MAP = new HashMap();
private static final Map CONVERTER_MAP = new HashMap();
private static final Converter DEFAULT_CONVERTER = new StringConstructorConverter();
static {
// Set up PRIMITIVE_TYPE_MAP:
Class[] primitives = { Boolean.TYPE, Byte.TYPE, Character.TYPE, Short.TYPE, Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE };
Class[] wrappers = { Boolean.class, Byte.class, Character.class, Short.class, Integer.class, Long.class, Float.class, Double.class };
for (int i = 0; i < primitives.length; i++) {
PRIMITIVE_TYPE_MAP.put(primitives[i], wrappers[i]);
}
// Set up CONVERTER_MAP:
CONVERTER_MAP.put(Integer.class, new IntegerConverter());
CONVERTER_MAP.put(Double.class, new DoubleConverter());
CONVERTER_MAP.put(Short.class, new ShortConverter());
CONVERTER_MAP.put(Character.class, new CharacterConverter());
CONVERTER_MAP.put(Long.class, new LongConverter());
CONVERTER_MAP.put(Boolean.class, new BooleanConverter());
CONVERTER_MAP.put(Byte.class, new ByteConverter());
CONVERTER_MAP.put(Float.class, new FloatConverter());
CONVERTER_MAP.put(BigDecimal.class, new BigDecimalConverter());
CONVERTER_MAP.put(BigInteger.class, new BigIntegerConverter());
}
public Object convert(Object sourceFieldValue, Class destFieldClass, DateFormatContainer dateFormatContainer) {
if (sourceFieldValue == null || destFieldClass == null
|| (sourceFieldValue.equals("") && !destFieldClass.equals(String.class))) {
return null;
}
Converter converter = getPrimitiveOrWrapperConverter(destFieldClass, dateFormatContainer);
try {
return converter.convert(destFieldClass, sourceFieldValue);
} catch (org.apache.commons.beanutils.ConversionException e) {
throw new net.sf.dozer.util.mapping.converters.ConversionException(e);
}
}
public Object convertUsingIndex(Object sourceFieldValue, int index, Class destFieldClass,
DateFormatContainer dateFormatContainer) {
Object r = null;
if (sourceFieldValue instanceof Object[]) {
Object[] x = (Object[]) sourceFieldValue;
if (index < x.length) {
return x[index];
}
} else if (sourceFieldValue instanceof Collection) {
Collection x = (Collection) sourceFieldValue;
if (index < x.size()) {
Iterator iter = x.iterator();
for (int i = 0; i < index; i++) {
iter.next();
}
r = iter.next();
}
} else {
return convert(sourceFieldValue, destFieldClass, dateFormatContainer);
}
return r;
}
private Converter getPrimitiveOrWrapperConverter(Class destClass, DateFormatContainer dateFormatContainer) {
if (java.util.Date.class.isAssignableFrom(destClass)) {
return new DateConverter(dateFormatContainer.getDateFormat());
}
if (Calendar.class.isAssignableFrom(destClass)) {
return new CalendarConverter(dateFormatContainer.getDateFormat());
}
if (String.class.equals(destClass)) {
return new StringConverter(dateFormatContainer);
}
Converter result = (Converter) CONVERTER_MAP.get(destClass.isPrimitive() ? wrapPrimitive(destClass) : destClass);
return result == null ? DEFAULT_CONVERTER : result;
}
private Class wrapPrimitive(Class c) {
return (Class) PRIMITIVE_TYPE_MAP.get(c);
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -