Showing posts with label java8. Show all posts
Showing posts with label java8. Show all posts

Saturday, 12 July 2014

Update and New Career

I thought it was about time I dropped an update onto the blog about why I've been quiet recently and what has been going on.

Early this year I decided to change career paths slightly and become a technical trainer. The highlights of my career as a developer have been to help others improve and learn from others in an informal supportive environment. I am now lucky enough to be able to do this full time designing new and fun ways of helping others learn Java, Python, C++ and .NET technologies. I work for my own company James Gough Ltd, with one primary client Mallon Associates. So far I've run ten training courses and had people booking onto my future courses, it's been challenging but one of the best things I've done. I have a fairly packed end to 2014 as I gear up for our 15 week training program.

As well as working on private training I have also teamed up with Richard Warburton and Raoul-Gabriel Urma. I am excited to announce will be offering public Java 8 courses with a focus on Lambdas and preparing Java developers for the changes that are right on our doorstep. Java as we know it is going to change, for years some developers have been dragging their feet with Java 1.4 style syntax and missing some key changes. An appreciation for the Object Oriented/Functional relationship is now required to both be able to design and build code that is succinct and solves the business problem in the best way.

This year is very busy with it being my first year in business and getting married so sadly I've had to put conferences on hold for a year, but I'm hoping that early next year I can attend jfokus.


Monday, 20 January 2014

Java 8 Date Time: Happy Martin Luther King Day

Today is Martin Luther King Day in the US. I had totally forgotten about this holiday until I saw a few tweets this morning about it. The interesting thing about this holiday is that rather than being on a specific day, it is on the third Monday of January.

As I'm currently preparing some JSR-310 material, I thought it would be interesting to share (and discuss) TemporalQuery - a new interface in Java 8 that makes it possible to write custom queries against the new DateTime classes. There is often a use case in business that an event happens that is unique to a particular domain, an example would be Futures market rolls of contracts or time periods of validity.

TemporalQuery allows us to capture the logic to perform an operation against a temporal class and return an object representing what we were querying. The interface is generic so we can choose what we return. This is a really neat design feature in Java 8 allowing us to externalise logic outside of the core API, but encapsulate the functionality as a data operation.

TemporalQuery is also a FunctionalInterface so it can be used as a lambda on a stream of temporals. In the example below we just consider one date. To find the next Martin Luther King day we can take the current date and perform the following:

LocalDate.now().query(new NextMartinLutherKingDayQuery());

The NextMartinLutherKingDayQuery is our implementation of a TemporalQuery:

Here are some things to note about the implementation:

  • In the private method I take a date at the beginning of January of the year passed. Using a TemporalAdjuster I manipulate that date to cycle it forward to where I want to be. TemporalAdjusters add more power than just plusDays or minusMonths to allow us to work on more expressive situations with date and time. 
    • The TemporalAdjusters class has some really nice methods as used here, or you can write your own TemporalAdjuster if it doesn't exist. 
    • NextOrSame is useful for this type of operation where we don't know what day we are on, but if it's the day we are querying we don't want to advance the day. We use it to iterate to the third Monday of the month of January in the year in question.
  • I then use a the Period class to find out from the date passed whether Martin Luther King Day has passed, or it is today. If it has passed I return next year's date, otherwise returning this year's date. Period is a nice abstraction so I don't have to start subtracting millisecond values etc - yes we've all seen it. 
If you're interested in playing around with this I suggest downloading the latest Java 8 beta and having a go. 

Happy Martin Luther King Day!