naming improvements since design review

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1419 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Keith Donald 2009-06-24 22:24:15 +00:00
parent d22697c41b
commit 1535d67b3c
12 changed files with 41 additions and 39 deletions

View File

@ -25,7 +25,7 @@ import org.springframework.ui.format.Formatter;
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
* @param <M> The kind of model object this binder binds to * @param <M> The kind of model object this binder binds to
* @see #add(BindingConfiguration) * @see #configureBinding(BindingConfiguration)
* @see #bind(UserValues) * @see #bind(UserValues)
*/ */
public interface Binder { public interface Binder {
@ -37,7 +37,7 @@ public interface Binder {
Object getModel(); Object getModel();
/** /**
* Configures if this binder is <i>strict</i>; a strict binder requires all bindings to be registered explicitly using {@link #add(BindingConfiguration)}. * Configures if this binder is <i>strict</i>; a strict binder requires all bindings to be registered explicitly using {@link #configureBinding(BindingConfiguration)}.
* An <i>optimistic</i> binder will implicitly create bindings as required to support {@link #bind(UserValues)} operations. * An <i>optimistic</i> binder will implicitly create bindings as required to support {@link #bind(UserValues)} operations.
* Default is optimistic. * Default is optimistic.
* @param strict strict binder status * @param strict strict binder status
@ -46,23 +46,23 @@ public interface Binder {
/** /**
* Adds a new binding. * Adds a new binding.
* @param binding the binding configuration * @param configuration the binding configuration
* @return the new binding created from the configuration provided * @return the new binding created from the configuration provided
*/ */
Binding add(BindingConfiguration binding); Binding configureBinding(BindingConfiguration configuration);
/** /**
* Adds a Formatter that will format property values of type <code>propertyType</coe>. * Adds a Formatter that will format property values of type <code>propertyType</coe>.
* @param formatter the formatter
* @param propertyType the property type * @param propertyType the property type
* @param formatter the formatter
*/ */
void add(Formatter<?> formatter, Class<?> propertyType); void registerFormatter(Class<?> propertyType, Formatter<?> formatter);
/** /**
* Adds a AnnotationFormatterFactory that will format values of properties annotated with a specific annotation. * Adds a AnnotationFormatterFactory that will format values of properties annotated with a specific annotation.
* @param factory the annotation formatter factory * @param factory the annotation formatter factory
*/ */
void add(AnnotationFormatterFactory<?, ?> factory); void registerFormatterFactory(AnnotationFormatterFactory<?, ?> factory);
/** /**
* Returns the binding for the property. * Returns the binding for the property.

View File

@ -22,7 +22,7 @@ import org.springframework.ui.format.Formatter;
* Configuration used to create a new {@link Binding} registered with a {@link GenericBinder}. * Configuration used to create a new {@link Binding} registered with a {@link GenericBinder}.
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
* @see GenericBinder#add(BindingConfiguration) * @see GenericBinder#configureBinding(BindingConfiguration)
*/ */
public class BindingConfiguration { public class BindingConfiguration {

View File

@ -26,10 +26,10 @@ public interface FormatterRegistry {
* <p> * <p>
* Note the Formatter's formatted object type does not have to equal the associated property type. * Note the Formatter's formatted object type does not have to equal the associated property type.
* When the property type differs from the formatted object type, the caller of the Formatter is expected to coerse a property value to the type expected by the Formatter. * When the property type differs from the formatted object type, the caller of the Formatter is expected to coerse a property value to the type expected by the Formatter.
* @param formatter the formatter
* @param propertyType the type * @param propertyType the type
* @param formatter the formatter
*/ */
void add(Formatter<?> formatter, Class<?> propertyType); void add(Class<?> propertyType, Formatter<?> formatter);
/** /**
* Adds a AnnotationFormatterFactory that will format values of properties annotated with a specific annotation. * Adds a AnnotationFormatterFactory that will format values of properties annotated with a specific annotation.

View File

@ -22,6 +22,7 @@ import java.util.List;
/** /**
* A holder for a list of UserValues. * A holder for a list of UserValues.
* TODO just use a map here
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
* @see Binder#bind(UserValues) * @see Binder#bind(UserValues)

View File

@ -67,7 +67,7 @@ import org.springframework.util.Assert;
* TODO - localization of alert messages using MessageResolver/MesageSource * TODO - localization of alert messages using MessageResolver/MesageSource
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
* @see #add(BindingConfiguration) * @see #configureBinding(BindingConfiguration)
* @see #bind(UserValues) * @see #bind(UserValues)
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -120,29 +120,29 @@ public class GenericBinder implements Binder {
this.formatterRegistry = formatterRegistry; this.formatterRegistry = formatterRegistry;
} }
public Binding add(BindingConfiguration binding) { public Binding configureBinding(BindingConfiguration configuration) {
Binding newBinding; Binding binding;
try { try {
newBinding = new BindingImpl(binding); binding = new BindingImpl(configuration);
} catch (org.springframework.expression.ParseException e) { } catch (org.springframework.expression.ParseException e) {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
} }
bindings.put(binding.getProperty(), newBinding); bindings.put(configuration.getProperty(), binding);
return newBinding; return binding;
} }
public void add(Formatter<?> formatter, Class<?> propertyType) { public void registerFormatter(Class<?> propertyType, Formatter<?> formatter) {
formatterRegistry.add(formatter, propertyType); formatterRegistry.add(propertyType, formatter);
} }
public void add(AnnotationFormatterFactory<?, ?> factory) { public void registerFormatterFactory(AnnotationFormatterFactory<?, ?> factory) {
formatterRegistry.add(factory); formatterRegistry.add(factory);
} }
public Binding getBinding(String property) { public Binding getBinding(String property) {
Binding binding = bindings.get(property); Binding binding = bindings.get(property);
if (binding == null && !strict) { if (binding == null && !strict) {
return add(new BindingConfiguration(property, null)); return configureBinding(new BindingConfiguration(property, null));
} else { } else {
return binding; return binding;
} }

View File

@ -32,7 +32,7 @@ import org.springframework.ui.format.Formatter;
* A generic implementation of {@link FormatterRegistry} suitable for use in most binding environments. * A generic implementation of {@link FormatterRegistry} suitable for use in most binding environments.
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
* @see #add(Formatter, Class) * @see #add(Class, Formatter)
* @see #add(AnnotationFormatterFactory) * @see #add(AnnotationFormatterFactory)
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -58,7 +58,7 @@ public class GenericFormatterRegistry implements FormatterRegistry {
} }
} }
public void add(Formatter<?> formatter, Class<?> propertyType) { public void add(Class<?> propertyType, Formatter<?> formatter) {
if (propertyType.isAnnotation()) { if (propertyType.isAnnotation()) {
annotationFormatters.put(propertyType, new SimpleAnnotationFormatterFactory(formatter)); annotationFormatters.put(propertyType, new SimpleAnnotationFormatterFactory(formatter));
} else { } else {

View File

@ -109,7 +109,7 @@ public class WebBindAndValidateLifecycle {
Bound b = AnnotationUtils.getAnnotation(getter, Bound.class); Bound b = AnnotationUtils.getAnnotation(getter, Bound.class);
if (b != null) { if (b != null) {
// TODO should we wire formatter here if using a format annotation - an optimization? // TODO should we wire formatter here if using a format annotation - an optimization?
binder.add(new BindingConfiguration(prop.getName(), null)); binder.configureBinding(new BindingConfiguration(prop.getName(), null));
} }
} }
// TODO @Bound fields // TODO @Bound fields

View File

@ -43,6 +43,7 @@ import org.springframework.expression.spel.standard.SpelExpressionParser;
* invalidFormat=The #{label} must be in format #{format}. * invalidFormat=The #{label} must be in format #{format}.
* mathForm.decimalField=Decimal Field * mathForm.decimalField=Decimal Field
* </pre> * </pre>
* TODO favor MessageBuilder accepting message source
* @author Keith Donald * @author Keith Donald
* @since 3.0 * @since 3.0
* @see #code(String) * @see #code(String)

View File

@ -91,34 +91,34 @@ public class GenericBinderTests {
@Test @Test
public void bindSingleValuePropertyFormatter() throws ParseException { public void bindSingleValuePropertyFormatter() throws ParseException {
binder.add(new BindingConfiguration("date", new DateFormatter())); binder.configureBinding(new BindingConfiguration("date", new DateFormatter()));
binder.bind(UserValues.single("date", "2009-06-01")); binder.bind(UserValues.single("date", "2009-06-01"));
assertEquals(new DateFormatter().parse("2009-06-01", Locale.US), bean.getDate()); assertEquals(new DateFormatter().parse("2009-06-01", Locale.US), bean.getDate());
} }
@Test @Test
public void bindSingleValuePropertyFormatterParseException() { public void bindSingleValuePropertyFormatterParseException() {
binder.add(new BindingConfiguration("date", new DateFormatter())); binder.configureBinding(new BindingConfiguration("date", new DateFormatter()));
binder.bind(UserValues.single("date", "bogus")); binder.bind(UserValues.single("date", "bogus"));
} }
@Test @Test
public void bindSingleValueWithFormatterRegistedByType() throws ParseException { public void bindSingleValueWithFormatterRegistedByType() throws ParseException {
binder.add(new DateFormatter(), Date.class); binder.registerFormatter(Date.class, new DateFormatter());
binder.bind(UserValues.single("date", "2009-06-01")); binder.bind(UserValues.single("date", "2009-06-01"));
assertEquals(new DateFormatter().parse("2009-06-01", Locale.US), bean.getDate()); assertEquals(new DateFormatter().parse("2009-06-01", Locale.US), bean.getDate());
} }
@Test @Test
public void bindSingleValueWithFormatterRegisteredByAnnotation() throws ParseException { public void bindSingleValueWithFormatterRegisteredByAnnotation() throws ParseException {
binder.add(new CurrencyFormatter(), CurrencyFormat.class); binder.registerFormatter(CurrencyFormat.class, new CurrencyFormatter());
binder.bind(UserValues.single("currency", "$23.56")); binder.bind(UserValues.single("currency", "$23.56"));
assertEquals(new BigDecimal("23.56"), bean.getCurrency()); assertEquals(new BigDecimal("23.56"), bean.getCurrency());
} }
@Test @Test
public void bindSingleValueWithnAnnotationFormatterFactoryRegistered() throws ParseException { public void bindSingleValueWithnAnnotationFormatterFactoryRegistered() throws ParseException {
binder.add(new CurrencyAnnotationFormatterFactory()); binder.registerFormatterFactory(new CurrencyAnnotationFormatterFactory());
binder.bind(UserValues.single("currency", "$23.56")); binder.bind(UserValues.single("currency", "$23.56"));
assertEquals(new BigDecimal("23.56"), bean.getCurrency()); assertEquals(new BigDecimal("23.56"), bean.getCurrency());
} }
@ -160,7 +160,7 @@ public class GenericBinderTests {
binder.setStrict(true); binder.setStrict(true);
Binding b = binder.getBinding("integer"); Binding b = binder.getBinding("integer");
assertNull(b); assertNull(b);
binder.add(new BindingConfiguration("integer", null)); binder.configureBinding(new BindingConfiguration("integer", null));
b = binder.getBinding("integer"); b = binder.getBinding("integer");
assertFalse(b.isCollection()); assertFalse(b.isCollection());
assertEquals("0", b.getValue()); assertEquals("0", b.getValue());
@ -172,7 +172,7 @@ public class GenericBinderTests {
@Test @Test
public void bindStrictNoMappingBindings() { public void bindStrictNoMappingBindings() {
binder.setStrict(true); binder.setStrict(true);
binder.add(new BindingConfiguration("integer", null)); binder.configureBinding(new BindingConfiguration("integer", null));
UserValues values = new UserValues(); UserValues values = new UserValues();
values.add("integer", "3"); values.add("integer", "3");
values.add("foo", "BAR"); values.add("foo", "BAR");
@ -190,7 +190,7 @@ public class GenericBinderTests {
@Test @Test
public void getBindingCustomFormatter() { public void getBindingCustomFormatter() {
binder.add(new BindingConfiguration("currency", new CurrencyFormatter())); binder.configureBinding(new BindingConfiguration("currency", new CurrencyFormatter()));
Binding b = binder.getBinding("currency"); Binding b = binder.getBinding("currency");
assertFalse(b.isCollection()); assertFalse(b.isCollection());
assertEquals("", b.getValue()); assertEquals("", b.getValue());
@ -201,7 +201,7 @@ public class GenericBinderTests {
@Test @Test
public void getBindingCustomFormatterRequiringTypeCoersion() { public void getBindingCustomFormatterRequiringTypeCoersion() {
// IntegerFormatter formats Longs, so conversion from Integer -> Long is performed // IntegerFormatter formats Longs, so conversion from Integer -> Long is performed
binder.add(new BindingConfiguration("integer", new IntegerFormatter())); binder.configureBinding(new BindingConfiguration("integer", new IntegerFormatter()));
Binding b = binder.getBinding("integer"); Binding b = binder.getBinding("integer");
b.setValue("2,300"); b.setValue("2,300");
assertEquals("2,300", b.getValue()); assertEquals("2,300", b.getValue());
@ -270,7 +270,7 @@ public class GenericBinderTests {
@Test @Test
public void formatPossibleValue() { public void formatPossibleValue() {
binder.add(new BindingConfiguration("currency", new CurrencyFormatter())); binder.configureBinding(new BindingConfiguration("currency", new CurrencyFormatter()));
Binding b = binder.getBinding("currency"); Binding b = binder.getBinding("currency");
assertEquals("$5.00", b.format(new BigDecimal("5"))); assertEquals("$5.00", b.format(new BigDecimal("5")));
} }

View File

@ -40,8 +40,8 @@ public class WebBinderTests {
@Test @Test
public void bindUserValuesCreatedFromUserMap() throws ParseException { public void bindUserValuesCreatedFromUserMap() throws ParseException {
binder.add(new CurrencyFormatter(), CurrencyFormat.class); binder.registerFormatter(CurrencyFormat.class, new CurrencyFormatter());
binder.add(new BindingConfiguration("date", new DateFormatter())); binder.configureBinding(new BindingConfiguration("date", new DateFormatter()));
Map<String, String> userMap = new LinkedHashMap<String, String>(); Map<String, String> userMap = new LinkedHashMap<String, String>();
userMap.put("string", "test"); userMap.put("string", "test");
userMap.put("_integer", "doesn't matter"); userMap.put("_integer", "doesn't matter");

View File

@ -59,7 +59,7 @@ public class WebBindAndValidateLifecycleTests {
public void testExecuteLifecycleInvalidFormatBindingErrors() { public void testExecuteLifecycleInvalidFormatBindingErrors() {
Map<String, Object> userMap = new HashMap<String, Object>(); Map<String, Object> userMap = new HashMap<String, Object>();
GenericFormatterRegistry registry = new GenericFormatterRegistry(); GenericFormatterRegistry registry = new GenericFormatterRegistry();
registry.add(new IntegerFormatter(), Integer.class); registry.add(Integer.class, new IntegerFormatter());
lifecycle.setFormatterRegistry(registry); lifecycle.setFormatterRegistry(registry);
userMap.put("string", "test"); userMap.put("string", "test");
userMap.put("integer", "bogus"); userMap.put("integer", "bogus");
@ -76,7 +76,7 @@ public class WebBindAndValidateLifecycleTests {
lifecycle = new WebBindAndValidateLifecycle(model, alertContext); lifecycle = new WebBindAndValidateLifecycle(model, alertContext);
Map<String, Object> userMap = new HashMap<String, Object>(); Map<String, Object> userMap = new HashMap<String, Object>();
GenericFormatterRegistry registry = new GenericFormatterRegistry(); GenericFormatterRegistry registry = new GenericFormatterRegistry();
registry.add(new IntegerFormatter(), Integer.class); registry.add(Integer.class, new IntegerFormatter());
lifecycle.setFormatterRegistry(registry); lifecycle.setFormatterRegistry(registry);
userMap.put("editable", "foo"); userMap.put("editable", "foo");
lifecycle.execute(userMap); lifecycle.execute(userMap);
@ -90,7 +90,7 @@ public class WebBindAndValidateLifecycleTests {
lifecycle = new WebBindAndValidateLifecycle(model, alertContext); lifecycle = new WebBindAndValidateLifecycle(model, alertContext);
Map<String, Object> userMap = new HashMap<String, Object>(); Map<String, Object> userMap = new HashMap<String, Object>();
GenericFormatterRegistry registry = new GenericFormatterRegistry(); GenericFormatterRegistry registry = new GenericFormatterRegistry();
registry.add(new IntegerFormatter(), Integer.class); registry.add(Integer.class, new IntegerFormatter());
lifecycle.setFormatterRegistry(registry); lifecycle.setFormatterRegistry(registry);
userMap.put("editable", "foo"); userMap.put("editable", "foo");
userMap.put("nonEditable", "whatev"); userMap.put("nonEditable", "whatev");

View File

@ -66,7 +66,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
FlightType flight = new FlightType(); FlightType flight = new FlightType();
flight.setNumber(42L); flight.setNumber(42L);
flights = new Flights(); flights = new Flights();
flights.getFlight().add(flight); flights.getFlight().configureBinding(flight);
return flights; return flights;
} }