11 December 2010

Convert a List<String> to a List<DateMidnight>

Valid since: op4j 1.1

Description
Convert a List<String> where each string represents a date in the format MM/dd/yyyy to a List<DateMidnight>

Scenario
Our List<String> list variable contains strings in the format MM/dd/yyyy:

List<String> list = new ArrayList<String>();
list.add("12/24/2000");
list.add("02/02/2010");
list.add("04/04/2002");
list.add("11/22/2005");
list.add("02/07/2005");
list.add("03/05/2005");
list.add("09/13/2006");
list.add("12/29/2007");

We want to create a List<DateMidNight> with the strings in our list converted
to DateMidnight:

List<DateMidnight> result = new ArrayList<DateMidnight>();
result.add(new DateMidnight(2000, 12, 24));
result.add(new DateMidnight(2010, 2, 2));
result.add(new DateMidnight(2002, 4, 4));
result.add(new DateMidnight(2005, 11, 22));
result.add(new DateMidnight(2005, 2, 7));
result.add(new DateMidnight(2005, 3, 5));
result.add(new DateMidnight(2006, 9, 13));
result.add(new DateMidnight(2007, 12, 29));

Recipe
Steps:
1. Iterate the list and convert each string to DateMidnight.

op4j-jodatime functions involved:
  • FnDateMidnight.strToDateMidnight(pattern): it converts the input string to DateMidnight using the given pattern

Let's see it coded with op4j:

String pattern = "MM/dd/yyyy";
List<DateMidnight> datemidnights = Op.on(list).forEach()
.exec(FnDateMidnight.strToDateMidnight(pattern)).get();

05 December 2010

Create a map from a List with the valid and invalid percentages

Valid since: op4j 1.1

Description
Given a List, create a map with two keys: "VALID" with the strings valid as a percentage between 0 and 100 and "INVALID" with the not valid ones

Scenario
Our List<String> list variable contains some strings that, in some cases, represent valid percentages:

List<String> list = new ArrayList<String>();
list.add("5");
list.add("3.4");
list.add("-13");
list.add("0");
list.add("5k7");
list.add("ten");
list.add("32");
list.add("1,2");

Some of the strings in the list, do not represent valid percentages and we want to create a map with two keys: INVALID whose value will be a list with the invalid strings and VALID which will have a list with the valid ones as its value:

Map<String, List<String>> result = new LinkedHashMap<String, List<String>>();
result.put("VALID", Arrays.asList(new String[] {"5", "3.4", "0", "32", "1,2"}));
result.put("INVALID", Arrays.asList(new String[] {"-13", "5k7", "ten"}));

Recipe
Steps:
1. Create a map from the input list with two keys: VALID and INVALID. Depending on whether a string in the input list represents a valid percentage or not, put it into the list of VALID or INVALID values.

op4j funcions involved:
  • FnString.isInteger(decimalPoint): returns whether the input is valid as an integer or not based on the given decimal point. It does not check the string represents a non decimal number that fits in an Integer number but that FnString.toInteger can be executed correctly.

  • FnString.toInteger(decimalPoint): converts the input value into an integer based on the given decimal point.

  • FnNumber.between(min, max): returns wether the input number is between min and max or not

Let's see it coded with op4j:

Map<String, List<String>> validInvalidPercentages = Op.on(list).zipAndGroupKeysBy(
new IFunction<String, String>() {
public String execute(String input, ExecCtx ctx) throws Exception {
return Op.on(input).exec(FnString.isInteger(DecimalPoint.IS_POINT))
.get().booleanValue()
&& Op.on(input).exec(FnString.toInteger(DecimalPoint.IS_POINT))
.exec(FnNumber.between(0, 100)).get().booleanValue()
? "VALID" : "INVALID";
}
}).get();

Comments
We are supposing the valid decimal point is IS_POINT though, if it were COMMA, it would be the same but using DecimalPoint.COMMA instead

Create a map from a List and generate a List with the strings convertible to Integer

Valid since: op4j 1.1

Description
For a given List<String>, create a Map with two keys: "VALID" with the strings valid as integer and "INVALID" with the not valid ones. Convert the valid strings to integer.

Scenario
Our List<String> list variable contains some strings that, in some cases, represent integer numbers:

List<String> list = new ArrayList<String>();
list.add("5");
list.add("3.4");
list.add("89.7");
list.add("-13.999");
list.add("5f7");
list.add("537");
list.add("323a");
list.add("3,23");

Some of the strings in the list, do not represent valid integers and we want to create a map with two keys: INVALID whose value will be a list with the invalid strings and VALID which will have a list with the valid ones as its value:

Map<String, List<String>> result = new LinkedHashMap<String, List<String>>();
result.put("VALID", Arrays.asList(new String[] {"5", "3.4", "89.7", "-13.999", "537", "3,23"}));
result.put("INVALID", Arrays.asList(new String[] {"5f7", "323a"}));

The items in the VALID list, will be converted into integer...and that List<Integer> will be the output we want.

Recipe
Steps:
1. Create a map from the input list with two keys: VALID and INVALID. Depending on whether a string in the input list represents a valid integer or not, put it into the list of VALID or INVALID values.
2. Iterate the list of valid strings and convert them into integer numbers.

op4j funcions involved:
  • FnString.isInteger(decimalPoint): returns whether the input is valid as an integer or not based on the given decimal point. It does not check the string represents a non decimal number that fits in an Integer number but that FnString.toInteger can be executed correctly.

  • FnString.toInteger(decimalPoint): converts the input value into an integer based on the given decimal point.

Let's see it coded with op4j:

Map<String, List<String>> validInvalidIntegers = Op.on(list).zipAndGroupKeysBy(
new IFunction<String, String>() {
public String execute(String input, ExecCtx ctx) throws Exception {
return Op.on(input).exec(FnString.isInteger(DecimalPoint.IS_POINT))
.get().booleanValue()? "VALID" : "INVALID";
}
}).get();

List<Integer> asIntegerIfPoint = Op.on(result.get("VALID")).forEach()
.exec(FnString.toInteger(DecimalPoint.IS_POINT)).get();

Comments
We are supposing the valid decimal point is POINT though, if it were COMMA, it would be the same but using DecimalPoint.IS_COMMA instead