From 3f41987556cc43fe413ef320831e60204a941838 Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Sun, 22 Jun 2008 16:23:01 +0000 Subject: [PATCH] formatter polishing --- .../converters/FormatterConverter.java | 59 +++++++++++++++++++ .../format/formatters/IntegerFormatter.java | 36 +++++++++++ 2 files changed, 95 insertions(+) create mode 100644 spring-binding/src/main/java/org/springframework/binding/convert/converters/FormatterConverter.java create mode 100644 spring-binding/src/main/java/org/springframework/binding/format/formatters/IntegerFormatter.java diff --git a/spring-binding/src/main/java/org/springframework/binding/convert/converters/FormatterConverter.java b/spring-binding/src/main/java/org/springframework/binding/convert/converters/FormatterConverter.java new file mode 100644 index 00000000..bd03212b --- /dev/null +++ b/spring-binding/src/main/java/org/springframework/binding/convert/converters/FormatterConverter.java @@ -0,0 +1,59 @@ +/* + * Copyright 2004-2008 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 org.springframework.binding.convert.converters; + +import org.springframework.binding.convert.Converter; +import org.springframework.binding.format.Formatter; + +/** + * Adapts a Formatter to the Converter interface. Allows a Formatter to be used as a Converter. Allows for both the + * to-string and from-string logic to be used. + * + * @see Formatter + * @see Formatter#format(Object) + * @see Formatter#parse(String) + * + * @author Keith Donald + */ +public class FormatterConverter implements Converter { + + private Formatter formatter; + + /** + * Creates a new formatter converter. + * @param formatter the formatter instance to adapt to the Converter API + */ + public FormatterConverter(Formatter formatter) { + this.formatter = formatter; + } + + public Class[] getSourceClasses() { + return new Class[] { formatter.getObjectType(), String.class }; + } + + public Class[] getTargetClasses() { + return new Class[] { String.class, formatter.getObjectType() }; + } + + public Object convert(Object source, Class targetClass, Object context) throws Exception { + if (targetClass.equals(String.class)) { + return formatter.format(source); + } else { + return formatter.parse((String) source); + } + } + +} \ No newline at end of file diff --git a/spring-binding/src/main/java/org/springframework/binding/format/formatters/IntegerFormatter.java b/spring-binding/src/main/java/org/springframework/binding/format/formatters/IntegerFormatter.java new file mode 100644 index 00000000..c50ff952 --- /dev/null +++ b/spring-binding/src/main/java/org/springframework/binding/format/formatters/IntegerFormatter.java @@ -0,0 +1,36 @@ +/* + * Copyright 2004-2008 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 org.springframework.binding.format.formatters; + +import java.text.NumberFormat; +import java.util.Locale; + +/** + * The default formatter for integer instances that are whole numbers and do not have fractions, such as Integers, + * Longs, and Shorts. Simply delegates to {@link NumberFormat#getIntegerInstance(Locale)}. + * + * @author Keith Donald + */ +public class IntegerFormatter extends NumberFormatter { + + public IntegerFormatter(Class numberClass) { + super(numberClass); + } + + protected NumberFormat createNumberFormat(Locale locale) { + return NumberFormat.getIntegerInstance(locale); + } +} \ No newline at end of file