As my last post comparing the three closure proposals - FCM, CICE and BGGA - seemed to be useful, I thought I'd post another. Again, I'll try not to be biased!
Example 3: Sorting
The Java Collections Framework has a callback to support sorting using the Comparator interface. This is used via the two argument sort method on Collections, taking the List and the Comparator. This example will sort a list of strings by length placing the "selected" string first.
The following example is from Java 6. This uses an inner class, which can access the parameter 'selected' so long as it is final:
public void sort(List<String> list, final String selected) {
Collections.sort(list, new Comparator<String>() {
public int compare(String str1, String str2) {
if (str1.equals(selected)) {
return -1;
} else if (str2.equals(selected)) {
return 1;
} else {
return str1.length - str2.length();
}
}
});
}
The following example is from the CICE proposal. This is shorthand for an inner class, so the parameter 'selected' must be final:
public void sort(List<String> list, final String selected) {
Collections.sort(list, Comparator<String>(String str1, String str2) {
if (str1.equals(selected)) {
return -1;
} else if (str2.equals(selected)) {
return 1;
} else {
return str1.length - str2.length();
}
});
}
The following example is from the BGGA proposal, and uses the standard-invocation syntax and closure conversion. The closure can access the parameter 'selected' without it needing to be final. The closure can only return a value from its last line, and does so by not specifying the final semicolon, hence the need for the result local variable:
public void sort(List<String> list, String selected) {
Collections.sort(list, {String str1, String str2 =>
int result = 0
if (str1.equals(selected)) {
result = -1;
} else if (str2.equals(selected)) {
result = 1;
} else {
result = str1.length - str2.length();
}
result
});
}
The BGGA proposal control-invocation syntax may not be used for this example. This is because the control-invocation syntax is not permitted to yield a result as required here.
The following example is from the FCM proposal, and uses an inner method. The inner method has access to the parameter 'selected' without it needing to be final:
public void sort(List<String> list, String selected) {
Collections.sort(list, #(String str1, String str2) {
if (str1.equals(selected)) {
return -1;
} else if (str2.equals(selected)) {
return 1;
} else {
return str1.length - str2.length();
}
});
}
The example cannot be directly translated to an FCM invocable method reference. This is because the functionality being performed relies on the parameter 'selected', which is not being passed into the compareTo method on the Comparator. If the 'selected' variable was an instance variable then an invocable method reference could be used.
Example 4: Looping over a map
This example aims to print each item in a map.
The following example is from Java 6, and uses an enhanced foreach loop:
Map<String, Integer> map = ...
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key + "=" + value);
}
The CICE proposal contains no new syntax to address this issue.
The following example is from the BGGA proposal, and uses the control-invocation syntax.
Map<String, Integer> map = ...
for eachEntry(String key, Integer value : map) {
System.out.println(key + "=" + value);
}
The FCM proposal contains no new syntax to address this issue. However the authors hope to publish a separate proposal to cover this use case at some point.
Summary
This blog contains two more comparisons of CICE, BGGA and FCM with examples. For the previous two, see the previous post.