Java Language Change

I’ve been to JavaEdge 2008 last week, and while many of the sessions were interesting, I found the one about the Java Language Change particularly intriguing.

During the session 10 proposed changes were introduced, all of them minor by definition (as stated by Sun in the JavaOne 2008 TS-5881 presentation), and everyone got to (anonymously) vote.

Here is the list of proposed changes, and even if you don’t get to vote, I think it’s interesting and thought provoking:

1. Map-for-each:


for (String key, Integer value : map) {
...
}

2. For-each iteration control:

for (String str : list : it) {
if (str.length() > 100) {
it.remove();
}
it.set(...);
it.index();
it.isFirst();
it.isLast();

}

3. List/map access:


List<String> list = ...
Map<String, Job> map = ...
Map<String, Map<String, Task>> combined = ...

String str = list[0];
list[0] = "Hi";
Integer value = map[str];
map["Hi"] = 56;
Task task = combined["Test"]["Monitor"];

4. Infer generics in declarations:

Instead of
List<String> list = new ArrayList<String>();
use
List<String> list = new ArrayList<>();

5. Multi-catch of Exceptions:


try {
doSomething();
doSomethingElse();
}
catch (IOException, SQLException ex) {
// handle..
}

6. String switch:



String str = ..
switch (str) {
case "name":
processName();
break;
case "surname":
processSurname;
...
}

7. String interpolation:


String name = ...
String Value = ..
String out = $"The value of ${name} is ${value}";

8. Multi-line Strings


String sql =
"""SELECT firstname, lastname, birth_date,
telephone, account
FROM
preson, phone, email
WHERE
..
""";

9. Resource management:



try (FileReader in = new FileReader(file)) {
processFile(in);
} // and resource validation and release is done automatically

10. Null-handling:



Session sess = ..

String code = sess?.preson()?.address()?.postcode(); // if anything preceding ? is null, the result is null

What’s your vote? 😉

Adding AJAX delay/loading indicator using Java Stripes

While exploring the Stripes framework, I spent a while looking at how to add the ‘spinner’ indicator when your server side is processing a long request. Here’s a short summary of what I did to achieve that, hope it could help save someone a little time:

1. Select a spinner image 🙂 I found this cool place where you can generate an image of your choosing at ajaxload.info. Generate whatever you like and save it somewhere, let’s say under ‘images/smallspinner.gif’.

2. In your .jsp file add the following javascript method just before the invoke method you use (again, assuming you’re working with Stripes):


function showSpinner(container) {
var obj = document.getElementById(container);
obj.innerHTML = "<div ID='spinner'>
<img src='images/smallspinner.gif'/></div>";
}


then update your invoke method to call the showSpinner method at the right time:


function invoke(form, event, container) {
if (!form.onsubmit) {
form.onsubmit = function() { return false }
};
showSpinner(container); //This is the new call..
var params = Form.serialize(form, {submit:event});
new Ajax.Updater(container, form.action,
{method:'post', parameters:params});
}


3. Ah, that’s about it.. A short explanation: The target container’s contents is being replaced just before the call invocation with the ‘spinner’ image, so it shows as ‘loading’ until the call returns, and then the server side replaces it with the actual HTML result once it’s ready.