Skip to content

Instantly share code, notes, and snippets.

@ge0ffrey
Created January 23, 2017 10:07
Show Gist options
  • Save ge0ffrey/7474576fe615ae7e0f72d30b625ca4aa to your computer and use it in GitHub Desktop.
Save ge0ffrey/7474576fe615ae7e0f72d30b625ca4aa to your computer and use it in GitHub Desktop.
Requirements:
1) Parse n properties from a Map
2) Fail fast with good error message if such a property is not the correct type (for example partCount is not an int)
3) Fail fast if an unknown key is in the Map
4) Do not modify the Map because it is reused
Proposal A:
public void applyCustomProperties(Map<String, String> customPropertyMap) {
String partCountString = customPropertyMap.remove("partCount"); // BUG: MODIFIES MAP
try {
partCount = partCountString == null ? 4 : Integer.parseInt(partCountString);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("The partCount (" + partCountString + ") cannot be parsed.", e);
}
String minimumProcessListSizeString = customPropertyMap.remove("minimumProcessListSize");
try {
minimumProcessListSize = minimumProcessListSizeString == null ? 75 : Integer.parseInt(minimumProcessListSizeString);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("The minimumProcessListSize (" + minimumProcessListSizeString + ") cannot be parsed.", e);
}
if (!customPropertyMap.isEmpty()) {
throw new IllegalStateException("The customProperties (" + customPropertyMap.keySet()
+ ") are not supported.");
}
}
Proposal B:
public void applyCustomProperties(Map<String, String> customPropertyMap) {
partCount = 4;
minimumProcessListSize = 25;
customPropertyMap.forEach((key, value) -> {
switch (key) {
case "partCount":
try {
partCount = Integer.parseInt(value);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("The partCount (" + value + ") cannot be parsed.", e);
}
break;
case "minimumProcessListSize":
try {
minimumProcessListSize = Integer.parseInt(value);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("The minimumProcessListSize (" + value + ") cannot be parsed.", e);
}
break;
default:
throw new IllegalArgumentException("The customProperty (" + key + ") with value (" + value
+ ") is not supported.");
}
});
}
@ge0ffrey
Copy link
Author

8

int partCount = 8;

int partCount = 4; // DEFAULT

8

// => Exception

public void applyCustomProperties(Map<String, String> customPropertyMap) {
partCount = 4;
minimumProcessListSize = 25;
customPropertyMap.forEach((key, value) -> {
switch (key) {
case "partCount":
try {
partCount = Integer.parseInt(value);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("The partCount (" + value + ") cannot be parsed.", e);
}
break;
case "minimumProcessListSize":
try {
minimumProcessListSize = Integer.parseInt(value);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("The minimumProcessListSize (" + value + ") cannot be parsed.", e);
}
break;
default:
throw new IllegalArgumentException("The customProperty (" + key + ") with value (" + value
+ ") is not supported.");
}
});
}

validation monad

private int a;
private double b;

public void doRegisters(foo) {
a = foo.extract(("a", String value) -> Integer.parseInt(value), 4);
b = foo.extract(("b", String value) -> Double.parseDouble(value), 0.25);
foo.validateNoOtherKeys();
}

Map<String, Object>
"a" -> 4;
"b" -> 0.25;

Map<String, Function>
("a", value) -> Integer.parseInt(value);
("b", value) -> Double.parseDouble(value);

Map<String, String> customPropertyMap

private int a;
private double b;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment