createUserValues factory method to Binder

This commit is contained in:
Keith Donald
2009-06-10 13:38:24 +00:00
parent 332d2287da
commit 991f618a7d
3 changed files with 40 additions and 5 deletions

View File

@@ -74,8 +74,19 @@ public interface Binder<M> {
/**
* Bind values in the map to the properties of the model object.
* TODO consider returning BindingResults object that makes it easier to query/introspect results
* @param userValues user-entered values to bind
*/
List<BindingResult> bind(List<UserValue> userValues);
/**
* Creates a {@link UserValue} list from a Map of user-submitted fields.
* The Binder may apply transformations as part of the creation process.
* For example, a Binder might insert empty or default values for fields that are not present.
* As another example, a Binder might collapse multiple fields into a single {@link UserValue} object.
* @param userMap the map of user-submitted fields
* @return the UserValue list that can be passed to {@link #bind(List)}.
*/
List<UserValue> createUserValues(Map<String, ? extends Object> userMap);
}

View File

@@ -109,6 +109,10 @@ public class GenericBinder<M> implements Binder<M> {
typeConverter = new DefaultTypeConverter();
}
public M getModel() {
return model;
}
public void setStrict(boolean strict) {
this.strict = strict;
}
@@ -136,10 +140,6 @@ public class GenericBinder<M> implements Binder<M> {
annotationFormatters.put(getAnnotationType(factory), factory);
}
public M getModel() {
return model;
}
public Binding getBinding(String property) {
Binding binding = bindings.get(property);
if (binding == null && !strict) {
@@ -158,6 +158,14 @@ public class GenericBinder<M> implements Binder<M> {
return results;
}
public List<UserValue> createUserValues(Map<String, ? extends Object> userMap) {
List<UserValue> values = new ArrayList<UserValue>();
for (Map.Entry<String, ? extends Object> entry : userMap.entrySet()) {
values.add(new UserValue(entry.getKey(), entry.getValue()));
}
return values;
}
// internal helpers
class BindingImpl implements Binding {