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
* @since 3.0
* @param <M> The kind of model object this binder binds to
* @see #add(BindingConfiguration)
* @see #configureBinding(BindingConfiguration)
* @see #bind(UserValues)
*/
public interface Binder {
@ -37,7 +37,7 @@ public interface Binder {
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.
* Default is optimistic.
* @param strict strict binder status
@ -46,23 +46,23 @@ public interface Binder {
/**
* Adds a new binding.
* @param binding the binding configuration
* @param configuration the binding configuration
* @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>.
* @param formatter the formatter
* @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.
* @param factory the annotation formatter factory
*/
void add(AnnotationFormatterFactory<?, ?> factory);
void registerFormatterFactory(AnnotationFormatterFactory<?, ?> factory);
/**
* 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}.
* @author Keith Donald
* @since 3.0
* @see GenericBinder#add(BindingConfiguration)
* @see GenericBinder#configureBinding(BindingConfiguration)
*/
public class BindingConfiguration {

View File

@ -26,10 +26,10 @@ public interface FormatterRegistry {
* <p>
* 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.
* @param formatter the formatter
* @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.

View File

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

View File

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

View File

@ -109,7 +109,7 @@ public class WebBindAndValidateLifecycle {
Bound b = AnnotationUtils.getAnnotation(getter, Bound.class);
if (b != null) {
// 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

View File

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

View File

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

View File

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

View File

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

View File

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