01 Oct 2013 JavaOne Day 3
With the previous two days of JavaOne 2013 now past us, it was time to get a little more practical and look more closely at some concepts introduced earlier in the week. Today wasn’t going to run as late into the evening as previous days since Oracle had their appreciation event beginning at 7pm. Personally, I was looking forward to the shorter day, getting a break from ‘conference brain’ and getting some time to consolidate what I had already picked up this week.
Lambda Streams API & Lambda Hands On Lab
I began the day with a Navigating the Streams API by Maurice Natfalin who took a look more closely at the new Java 8 Lambda language features with respect to how to manipulate streams for some common cases.
The API designers have gone to great lengths to discuss what methods are required in the streams API, getting feedback from much of the Java community via the Lambda-dev mailing list. Also some JUGs around the world ran hack sessions.
What is a stream good for? Take the basic use case of a Person class with a String name and location, plus an integer age. We then have a
Collection<Person> that we need to do something to. For example we may want to filter the people by a age or location, create a map of one of locations to a list of ages in that location, group people by a certain value, sort or reduce
We can ask the collection for a stream() that has the methods we use to achieve these tasks. Streams operations are classified as either lazy/intermediate or eager/terminal, that is to say, you can chain a whole bunch of lazy stream operations together (sorted, limit, distinct) that won’t actually do anything until they hit a terminal operation (collect, reduce, sum).
To aid in the ‘collecting’ there is also a Collectors API (not to be confused with Collections) that provide functions such as counting, averaging, summing, joining for strings, export stream as a list, map or set, reduce, grouping by and mapping one value to another. The collectors advise the terminal method
Maurice ran through a few examples at the end of the talk. His website lambdafaq.org has more detail of using the Stream API and the ‘why is it not done this way?’ questions that will happen as developers get their heads around the API.
Later in the day there was a 2 hour Lambda Hands on Lab session where we actually got the chance to use Lambdas in a more detailed manner. Although the lab was organised well, we did lose time at the start as the instructors wanted to use the bleeding edge Java 8 build with a bleeding edge Netbeans that everyone had to configure to point to the correct version of the JDK. However once we got started it was good there were experts on hand to help work through the trickier parts of the exercises. Lambdas will introduce multiple ways of skinning a cat and it was good to see that any questions were explained with the VM in mind.
I have to admit, coming from other JVM languages such as Groovy, I did find that the collect and Collectors concept a little different to get my head around. In Groovy, all the GDK closure methods don’t return an interim stream operation, they just return another collection. This is easier to understand but obviously less efficient, you have to process the entire previous operation and create a new collection before feeding this down. Java 8 Streams allow you to avoid the unnecessary processing. It’s going to be a case of watch this space to see how the feature will be picked up by Java developers but it is the most exciting thing to happen to Java in years so I’m glad Oracle have devoted so much of JavaOne to it.
Shine will definitely have more blogs on lambdas as the release date for Java 8 draws near.
Multi project builds with Gradle
Approaching Pure Rest in Java
A lot of people are stuck with whats known as level 2 of the Richardson REST Maturity model.
That is that they support endpoints that have both a verb in the URI as well as the regular HTTP verb.
POST http://mypage.com/person/create/1 (Create should not be there)
GET http://mypage.com/person/view/1 (Get should not be there)
They don’t use PATCH which is for update, but use POST for updates instead, which should actually be used to replace an entity with another.
The talk focused on what level 3 of the Richardson maturity level was and how JAX-RS already supports HATEAOS (Hypermedia as the engine of application state) – the ‘real’ way of doing REST.
The presenter was great and despite English not being his first language he was very funny and kept the audience entertained with how the ‘sins’ he committed in his first attempts of building a REST interface.
He went into further detail about how to setup HttpCaching, concurrency and versioning and made strong arguments that the semantics in HTTP verbs GET/POST/PUT/DELETE/PATCH are enough and if you need more, you should be covering them with endpoints. Eg if you were a bank and wanted to transfer funds, make a resource called transfer and have a transaction on the server side.
One thing I didn’t know about JAX-RS 2.0 is that it’s JSR (339) was in the Adopt A JSR project and sponsoring by SouJava, one of the largest JUGs in the world.
Oracle Appreciation Event
As mentioned the end of the day Oracle shipped everybody to Treasure Island. No not some fictional island with pirates or treasure, Treasure Island is east of downtown San Francisco across a bridge. Oracle put on a music concert featuring Maroon 5 and the Black Keys, the scale of which I could never imagine for a corporate gig. Picture a large Australian music festival with a main stage, stands, rides, games, booze and food with the twist of it all being free and the sponsors being Oracle, CSC, Intel rather than your usual young adult targeted advertising and you’d have a rough idea.