added user values holder after review with juergen

This commit is contained in:
Keith Donald
2009-06-10 20:36:43 +00:00
parent 7c433712d1
commit 72e89510da
8 changed files with 127 additions and 70 deletions

View File

@@ -27,7 +27,7 @@ import org.springframework.ui.format.Formatter;
*
* @param <M> The kind of model object this binder binds to
* @see #add(BindingConfiguration)
* @see #bind(Map)
* @see #bind(UserValues)
*/
public interface Binder<M> {
@@ -39,7 +39,7 @@ public interface Binder<M> {
/**
* Configures if this binder is <i>strict</i>; a strict binder requires all bindings to be registered explicitly using {@link #add(BindingConfiguration)}.
* An <i>optimistic</i> binder will implicitly create bindings as required to support {@link #bind(Map)} operations.
* 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
*/
@@ -75,9 +75,9 @@ 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
* @param values user-entered values to bind
*/
List<BindingResult> bind(List<UserValue> userValues);
List<BindingResult> bind(UserValues values);
/**
* Creates a {@link UserValue} list from a Map of user-submitted fields.
@@ -85,8 +85,8 @@ public interface Binder<M> {
* 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)}.
* @return the UserValue list that can be passed to {@link #bind(UserValues)}.
*/
List<UserValue> createUserValues(Map<String, ? extends Object> userMap);
UserValues createUserValues(Map<String, ? extends Object> userMap);
}

View File

@@ -18,7 +18,7 @@ package org.springframework.ui.binding;
/**
* The result of a bind operation.
* @author Keith Donald
* @see Binder#bind(java.util.List)
* @see Binder#bind(UserValues)
* @see Binding#setValue(Object)
*/
public interface BindingResult {

View File

@@ -15,9 +15,6 @@
*/
package org.springframework.ui.binding;
import java.util.ArrayList;
import java.util.List;
/**
* Holds a user-entered value to bind to a model property.
* @author Keith Donald
@@ -53,16 +50,4 @@ public class UserValue {
return value;
}
/**
* Creates a new UserValue list with a single element.
* @param property the property
* @param value the actual user-entered value
* @return the singleton user value list
*/
public static List<UserValue> single(String property, Object value) {
List<UserValue> values = new ArrayList<UserValue>(1);
values.add(new UserValue(property, value));
return values;
}
}

View File

@@ -0,0 +1,74 @@
package org.springframework.ui.binding;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* A simpler holder for a list of UserValues.
*
* @author Keith Donald
* @see Binder#bind(UserValues)
*/
public class UserValues implements Iterable<UserValue> {
private List<UserValue> values;
/**
* Creates a new user values list of the default size.
*/
public UserValues() {
values = new ArrayList<UserValue>();
}
/**
* Creates a new user values list of the size provided.
*/
public UserValues(int size) {
values = new ArrayList<UserValue>(size);
}
// implementing Iterable
public Iterator<UserValue> iterator() {
return values.iterator();
}
/**
* The user values list.
* The returned list is not modifiable.
*/
public List<UserValue> asList() {
return Collections.unmodifiableList(values);
}
/**
* Add a new user value.
* @param property the property the value should be bound to
* @param value the actual user-entered value
*/
public void add(String property, Object value) {
values.add(new UserValue(property, value));
}
/**
* The number of user values in the list.
*/
public int size() {
return values.size();
}
/**
* Creates a new UserValues list with a single element.
* @param property the property
* @param value the actual user-entered value
* @return the singleton user value list
*/
public static UserValues single(String property, Object value) {
UserValues values = new UserValues(1);
values.add(property, value);
return values;
}
}

View File

@@ -48,6 +48,7 @@ import org.springframework.ui.binding.Binding;
import org.springframework.ui.binding.BindingConfiguration;
import org.springframework.ui.binding.BindingResult;
import org.springframework.ui.binding.UserValue;
import org.springframework.ui.binding.UserValues;
import org.springframework.ui.format.AnnotationFormatterFactory;
import org.springframework.ui.format.Formatter;
@@ -55,9 +56,9 @@ import org.springframework.ui.format.Formatter;
* Binds user-entered values to properties of a model object.
* @author Keith Donald
*
* @param <M> The type of model object this binder binds to
* @param <M> The type of model object this binder binds to - TODO is this worth it?
* @see #add(BindingConfiguration)
* @see #bind(Map)
* @see #bind(UserValues)
*/
@SuppressWarnings("unchecked")
public class GenericBinder<M> implements Binder<M> {
@@ -149,19 +150,19 @@ public class GenericBinder<M> implements Binder<M> {
}
}
public List<BindingResult> bind(List<UserValue> userValues) {
List<BindingResult> results = new ArrayList<BindingResult>(userValues.size());
for (UserValue value : userValues) {
public List<BindingResult> bind(UserValues values) {
List<BindingResult> results = new ArrayList<BindingResult>(values.size());
for (UserValue value : values) {
BindingImpl binding = (BindingImpl) getBinding(value.getProperty());
results.add(binding.setValue(value.getValue()));
}
return results;
}
public List<UserValue> createUserValues(Map<String, ? extends Object> userMap) {
List<UserValue> values = new ArrayList<UserValue>();
public UserValues createUserValues(Map<String, ? extends Object> userMap) {
UserValues values = new UserValues(userMap.size());
for (Map.Entry<String, ? extends Object> entry : userMap.entrySet()) {
values.add(new UserValue(entry.getKey(), entry.getValue()));
values.add(entry.getKey(), entry.getValue());
}
return values;
}

View File

@@ -15,11 +15,9 @@
*/
package org.springframework.ui.binding.support;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.ui.binding.UserValue;
import org.springframework.ui.binding.UserValues;
/**
* A binder designed for use in HTTP (web) environments.
@@ -60,24 +58,24 @@ public class WebBinder<M> extends GenericBinder<M> {
}
@Override
public List<UserValue> createUserValues(Map<String, ? extends Object> userMap) {
List<UserValue> values = new ArrayList<UserValue>();
public UserValues createUserValues(Map<String, ? extends Object> userMap) {
UserValues values = new UserValues();
for (Map.Entry<String, ? extends Object> entry : userMap.entrySet()) {
String field = entry.getKey();
Object value = entry.getValue();
if (field.startsWith(fieldDefaultPrefix)) {
field = field.substring(fieldDefaultPrefix.length());
if (!userMap.containsKey(field)) {
values.add(new UserValue(field, value));
values.add(field, value);
}
} else if (field.startsWith(fieldMarkerPrefix)) {
field = field.substring(fieldMarkerPrefix.length());
if (!userMap.containsKey(field) && !userMap.containsKey(fieldDefaultPrefix + field)) {
value = getEmptyValue((BindingImpl) getBinding(field));
values.add(new UserValue(field, value));
values.add(field, value);
}
} else {
values.add(new UserValue(entry.getKey(), entry.getValue()));
values.add(entry.getKey(), entry.getValue());
}
}
return values;