Showing posts with label Manning. Show all posts
Showing posts with label Manning. Show all posts

Thursday, 15 September 2011

My Interview with MyFear

I was humbled to be interviwed as part of MyFear's Java Hero's series - If you want to know some of my motivations then go here

Monday, 9 August 2010

A Little Directory love

Hi all,

So my next little snippet also comes from Chapter 2 of the book where we deal with the new NIO.2 (Non-Blocking I/O 2) APIs being introduced to Java 7.  In particular I'm showing a small code snippet to showcase some of the new support for directories that Java 7 has.  The code sample certainly needs a little tidy-up by using the new Automatic Resource Management (ARM) features in Java 7, but for now the example will suffice.


Dealing with Directories

The java.nio.file.DirectoryStream interface and its implementing classes allow you to:

  • Iterate over entries in a directory.
  • Deal with large directory structures.
  • Filter entries whilst processing using regular expressions and MIME based content detection.
  • Walk the tree structure in order to perform recursive move, copy, delete operations.
A common use case here is to list entries in a directory, e.g. Java source files as shown by the listing below.

DirectoryStream stream = null;
try
{
    // Define the directory
    Path dir = Paths.get("c:\\workspace");

    // Declare a filtering stream
    stream = dir.newDirectoryStream("*.java");
    // List each .java file in that directory
    for (Path entry: stream)
    {
        System.out.println(entry.getName());
    }
}
catch (IOException e)
{
    // Poor man's exception handling
    e.printStackTrace();
}

As you can see, with the new Java 7 APIs this particular task is much simpler than it used to be and this theme is prevalent in the rest of the NIO.2 APIs.

There are several other use cases which we will show case in the "NIO.2 in action" section.as well as on our upcoming website (to be revealed later).


So are we there yet?  In terms of File and Directory interaction in Java 7 there's certainly a massive improvement, so I'll say "Yes" once Java 7 is released.

Cheers,
Martijn

Thursday, 5 August 2010

The Well Grounded Java 7 Developer

Hi all,

I've obviously not posted for quite some time and here is the reason why.....

It's been a long time coming, but I'm very happy to announce that Ben Evans and I have been contracted to write "The Well  Grounded Java 7 Developer" for Manning publications.  We've been spending the last month frantically getting the first few chapters out and a whole host of other book related activities, but now that I'm in full chapter writing mode I'll be adding regular posts to this blog.

A majority of my future posts will focus on interesting areas in this book and firstly I'd like to show a reworked extract from the new Date and Time API section.

Modeling Date and Time

The new API models time as a sequence of consecutive instants separated by fixed durations[1].  Java 7 maps these concepts directly on to classes, here's a more detailed explanation:
  • The javax.time.Instant class represents a specific point on a discrete time-line e.g. January 23rd, 1996 at 09:00:00,0 UTC, the day that Java 1.0 was released.
  • The javax.time.Duration class represents a section of elapsed time in nanoseconds, e.g. The 400 nanoseconds it takes for a PIC12C672-04 Microchip to execute an instruction set.

So as you can imagine, any point of time can be modeled by simply applying a Duration (positive or negative) to a starting Instant.
...
...

Calendering

With regards to calendaring, the main Calendar is ISOChronology, but other calendars such as CopticChronology and ThaiBuddhistChronology are supported.  The Chronology interface can be extended from to provide other implementations.
...
...

Timezone support

Timezone support is also provided via three core classes:
  • LocalDateTime – Represents date/time without an offset or a time zone.
  • OffsetDateTime - Represents date/time with an offset but not time zone.
  • ZonedDateTime - Represents date/time with an offset and a time zone.
...
...


[1]               This phrase is almost 100% uplifted from an excellent early primer article by Jesse Farnham, see  http://today.java.net/pub/a/today/2008/09/18/jsr-310-new-java-date-time-api.html for details


The book will be loaded with more in depth explanations and of course plenty of code samples.  If you're interested in becoming a reviewer then please let me know!