All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vaadin.data.util.converter.DefaultConverterFactory Maven / Gradle / Ivy

There is a newer version: 8.27.3
Show newest version
/*
 * Vaadin Framework 7
 *
 * Copyright (C) 2000-2024 Vaadin Ltd
 *
 * This program is available under Vaadin Commercial License and Service Terms.
 *
 * See  for the full
 * license.
 */

package com.vaadin.data.util.converter;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.logging.Logger;

import com.vaadin.server.VaadinSession;

/**
 * Default implementation of {@link ConverterFactory}. Provides converters for
 * standard types like {@link String}, {@link Double} and {@link Date}.
 * 

*

* Custom converters can be provided by extending this class and using * {@link VaadinSession#setConverterFactory(ConverterFactory)}. *

* * @author Vaadin Ltd * @since 7.0 */ public class DefaultConverterFactory implements ConverterFactory { private final static Logger log = Logger .getLogger(DefaultConverterFactory.class.getName()); @Override public Converter createConverter( Class presentationType, Class modelType) { Converter converter = findConverter( presentationType, modelType); if (converter != null) { log.finest(getClass().getName() + " created a " + converter.getClass()); return converter; } // Try to find a reverse converter Converter reverseConverter = findConverter( modelType, presentationType); if (reverseConverter != null) { log.finest(getClass().getName() + " created a reverse " + reverseConverter.getClass()); return new ReverseConverter(reverseConverter); } log.finest(getClass().getName() + " could not find a converter for " + presentationType.getName() + " to " + modelType.getName() + " conversion"); return null; } protected Converter findConverter( Class presentationType, Class modelType) { if (presentationType == String.class) { // TextField converters and more Converter converter = (Converter) createStringConverter( modelType); if (converter != null) { return converter; } } else if (presentationType == Date.class) { // DateField converters and more Converter converter = (Converter) createDateConverter( modelType); if (converter != null) { return converter; } } return null; } protected Converter createDateConverter(Class sourceType) { if (Long.class.isAssignableFrom(sourceType)) { return new DateToLongConverter(); } else if (java.sql.Date.class.isAssignableFrom(sourceType)) { return new DateToSqlDateConverter(); } else { return null; } } protected Converter createStringConverter(Class sourceType) { if (Double.class.isAssignableFrom(sourceType)) { return new StringToDoubleConverter(); } else if (Float.class.isAssignableFrom(sourceType)) { return new StringToFloatConverter(); } else if (Integer.class.isAssignableFrom(sourceType)) { return new StringToIntegerConverter(); } else if (Long.class.isAssignableFrom(sourceType)) { return new StringToLongConverter(); } else if (BigDecimal.class.isAssignableFrom(sourceType)) { return new StringToBigDecimalConverter(); } else if (Boolean.class.isAssignableFrom(sourceType)) { return new StringToBooleanConverter(); } else if (Date.class.isAssignableFrom(sourceType)) { return new StringToDateConverter(); } else if (Enum.class.isAssignableFrom(sourceType)) { return new StringToEnumConverter(); } else if (BigInteger.class.isAssignableFrom(sourceType)) { return new StringToBigIntegerConverter(); } else if (Short.class.isAssignableFrom(sourceType)) { return new StringToShortConverter(); } else if (Byte.class.isAssignableFrom(sourceType)) { return new StringToByteConverter(); } else { return null; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy