Skip to content

Commit

Permalink
Merge pull request apache#71 from derekd/derekd-config-nested-nulls
Browse files Browse the repository at this point in the history
Handle nested config validation nulls distinctly
  • Loading branch information
Bobby Evans committed Aug 29, 2013
2 parents 779cabd + cdc464f commit 192d061
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions storm-core/src/jvm/backtype/storm/ConfigValidation.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public void validateField(String name, Object field) throws IllegalArgumentExcep
/**
* Returns a new NestableFieldValidator for a given class.
* @param cls the Class the field should be a type of
* @param nullAllowed whether or not a value of null is valid
* @return a NestableFieldValidator for that class
*/
public static NestableFieldValidator fv(final Class cls) {
public static NestableFieldValidator fv(final Class cls, final boolean nullAllowed) {
return new NestableFieldValidator() {
@Override
public void validateField(String pd, String name, Object field)
throws IllegalArgumentException {
if (field == null) {
// A null value is acceptable.
if (nullAllowed && field == null) {
return;
}
if (! cls.isInstance(field)) {
Expand All @@ -64,24 +64,26 @@ public void validateField(String pd, String name, Object field)
/**
* Returns a new NestableFieldValidator for a List of the given Class.
* @param cls the Class of elements composing the list
* @param nullAllowed whether or not a value of null is valid
* @return a NestableFieldValidator for a list of the given class
*/
public static NestableFieldValidator listFv(Class cls) {
return listFv(fv(cls));
public static NestableFieldValidator listFv(Class cls, boolean nullAllowed) {
return listFv(fv(cls, false), nullAllowed);
}

/**
* Returns a new NestableFieldValidator for a List where each item is validated by validator.
* @param validator used to validate each item in the list
* @param nullAllowed whether or not a value of null is valid
* @return a NestableFieldValidator for a list with each item validated by a different validator.
*/
public static NestableFieldValidator listFv(final NestableFieldValidator validator) {
public static NestableFieldValidator listFv(final NestableFieldValidator validator,
final boolean nullAllowed) {
return new NestableFieldValidator() {
@Override
public void validateField(String pd, String name, Object field)
throws IllegalArgumentException {
if (field == null) {
// A null value is acceptable.
if (nullAllowed && field == null) {
return;
}
if (field instanceof Iterable) {
Expand All @@ -91,7 +93,8 @@ public void validateField(String pd, String name, Object field)
return;
}
throw new IllegalArgumentException(
"Field " + name + " must be an Iterable " + field.getClass());
"Field " + name + " must be an Iterable but was " +
((field == null) ? "null" : ("a " + field.getClass())));
}
};
}
Expand All @@ -100,26 +103,29 @@ public void validateField(String pd, String name, Object field)
* Returns a new NestableFieldValidator for a Map of key to val.
* @param key the Class of keys in the map
* @param val the Class of values in the map
* @param nullAllowed whether or not a value of null is valid
* @return a NestableFieldValidator for a Map of key to val
*/
public static NestableFieldValidator mapFv(final Class key, final Class val) {
return mapFv(fv(key), fv(val));
public static NestableFieldValidator mapFv(Class key, Class val,
boolean nullAllowed) {
return mapFv(fv(key, false), fv(val, false), nullAllowed);
}

/**
* Returns a new NestableFieldValidator for a Map.
* @param key a validator for the keys in the map
* @param val a validator for the values in the map
* @param nullAllowed whether or not a value of null is valid
* @return a NestableFieldValidator for a Map
*/
public static NestableFieldValidator mapFv(final NestableFieldValidator key, final NestableFieldValidator val) {
public static NestableFieldValidator mapFv(final NestableFieldValidator key,
final NestableFieldValidator val, final boolean nullAllowed) {
return new NestableFieldValidator() {
@SuppressWarnings("unchecked")
@Override
public void validateField(String pd, String name, Object field)
throws IllegalArgumentException {
if (field == null) {
// A null value is acceptable.
if (nullAllowed && field == null) {
return;
}
if (field instanceof Map) {
Expand All @@ -138,17 +144,17 @@ public void validateField(String pd, String name, Object field)
/**
* Validates a list of Numbers.
*/
public static Object NumbersValidator = listFv(Number.class);
public static Object NumbersValidator = listFv(Number.class, true);

/**
* Validates a list of Strings.
*/
public static Object StringsValidator = listFv(String.class);
public static Object StringsValidator = listFv(String.class, true);

/**
* Validates a map of Strings to Numbers.
*/
public static Object MapOfStringToNumberValidator = mapFv(String.class, Number.class);
public static Object MapOfStringToNumberValidator = mapFv(String.class, Number.class, true);

/**
* Validates a power of 2.
Expand Down

0 comments on commit 192d061

Please sign in to comment.