formatter improvements

This commit is contained in:
Keith Donald
2008-03-18 20:50:26 +00:00
parent 1809e7341a
commit 02dc93f66f
2 changed files with 15 additions and 70 deletions

View File

@@ -16,17 +16,18 @@
package org.springframework.binding.convert.support;
import org.springframework.binding.convert.ConversionContext;
import org.springframework.binding.format.support.LabeledEnumFormatter;
import org.springframework.core.enums.LabeledEnum;
import org.springframework.core.enums.LabeledEnumResolver;
import org.springframework.core.enums.StaticLabeledEnumResolver;
/**
* Converter that converts textual representations of enum instances to a specific instance of <code>LabeledEnum</code>.
* Converts from a textual representation to a {@link LabeledEnum}. The text should be the enum's label.
*
* @author Keith Donald
*/
public class TextToLabeledEnum extends AbstractConverter {
private LabeledEnumFormatter labeledEnumFormatter = new LabeledEnumFormatter();
private LabeledEnumResolver labeledEnumResolver = StaticLabeledEnumResolver.instance();
public Class[] getSourceClasses() {
return new Class[] { String.class };
@@ -37,6 +38,7 @@ public class TextToLabeledEnum extends AbstractConverter {
}
protected Object doConvert(Object source, Class targetClass, ConversionContext context) throws Exception {
return labeledEnumFormatter.parseValue((String) source, targetClass);
String label = (String) source;
return labeledEnumResolver.getLabeledEnumByLabel(targetClass, label);
}
}

View File

@@ -16,81 +16,24 @@
package org.springframework.binding.format;
/**
* Source for shared and commonly used <code>Formatters</code>.
* <p>
* Formatters are typically not thread safe as <code>Format</code> objects aren't thread safe: so implementations of
* this service should take care to synchronize them as neccessary.
*
* @see java.text.Format
* A factory for creating stateful formatter instances.
*
* @author Keith Donald
*/
public interface FormatterFactory {
/**
* Returns a date formatter for the encoded date format.
* @param encodedFormat the format
* @return the formatter
* The type of object the returned formatters are capable of formatting. This is the most general type supported,
* some formatters may apply specific formatting rules to subclasses of this general type.
* @return the formatted class
*/
public Formatter getDateFormatter(String encodedFormat);
public Class getFormattedClass();
/**
* Returns the default date format for the current locale.
* @return the date formatter
* Create a new formatter.
* @param context context for formatter creation
* @return the new formatter
*/
public Formatter getDateFormatter();
/**
* Returns the date format with the specified style for the current locale.
* @param style the style
* @return the formatter
*/
public Formatter getDateFormatter(Style style);
/**
* Returns the default date/time format for the current locale.
* @return the date/time formatter
*/
public Formatter getDateTimeFormatter();
/**
* Returns the date format with the specified styles for the current locale.
* @param dateStyle the date style
* @param timeStyle the time style
* @return the formatter
*/
public Formatter getDateTimeFormatter(Style dateStyle, Style timeStyle);
/**
* Returns the default time format for the current locale.
* @return the time formatter
*/
public Formatter getTimeFormatter();
/**
* Returns the time format with the specified style for the current locale.
* @param style the style
* @return the formatter
*/
public Formatter getTimeFormatter(Style style);
/**
* Returns a number formatter for the specified class.
* @param numberClass the number class
* @return the number formatter
*/
public Formatter getNumberFormatter(Class numberClass);
/**
* Returns a percent number formatter.
* @return the percent formatter
*/
public Formatter getPercentFormatter();
/**
* Returns a currency number formatter.
* @return the currency formatter
*/
public Formatter getCurrencyFormatter();
public Formatter createFormatter(FormatterFactoryContext context);
}