<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Random Thoughts On Coding</title>
	<atom:link href="http://codingjunkie.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://codingjunkie.net</link>
	<description>Practical HowTo&#039;s On Software Development</description>
	<lastBuildDate>Thu, 29 Mar 2012 01:39:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
	<atom:link rel='hub' href='http://codingjunkie.net/?pushpress=hub'/>
		<item>
		<title>Google Guava BloomFIlter</title>
		<link>http://codingjunkie.net/guava-bloomfilter/</link>
		<comments>http://codingjunkie.net/guava-bloomfilter/#comments</comments>
		<pubDate>Thu, 22 Mar 2012 05:01:04 +0000</pubDate>
		<dc:creator>Bill B</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Guava]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://codingjunkie.net/?p=2144</guid>
		<description><![CDATA[When the Guava project released version 11.0, one of the new additions was the BloomFilter class. A BloomFilter is a unique data-structure used to indicate if an element is contained in a set. What makes a BloomFilter interesting is it will indicate if an element is absolutely not contained, or may be contained in a [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://codingjunkie.net/wp-content/uploads/2011/11/google-guava.gif" alt="" title="google-guava" width="48" height="48" class="alignleft size-full wp-image-1133" />When the Guava project released version 11.0, one of the new additions was the BloomFilter class.  A BloomFilter is a unique data-structure used to indicate if an element is contained in a set.  What makes a BloomFilter interesting is it will indicate if an element is <i>absolutely not</i> contained, or <i>may be</i> contained in a set. This property of never having a false negative makes the BloomFilter a great candidate for use as a guard condition to help prevent performing unnecessary and expensive operations.  While BloomFilters have received good exposure lately, using one meant rolling your own, or doing a Google search for code. The trouble with rolling your own BloomFilter is getting the correct hash function to make the filter effective.  Considering Guava uses the Murmur Hash for its&#8217; implementation, we now have the usefulness of an effective BloomFilter just a library away.</p>
<h4>BloomFilter Crash Course</h4>
<p>BloomFilters are essentially bit vectors.  At a high level BloomFilters work in the following manner: </p>
<ol>
<li>Add the element to the filter.</li>
<li>Hash it a few times, than set the bits to 1 where the index matches the results of the hash.</li>
</ol>
<p>When testing if an element is in the set, you follow the same hashing procedure and check if the bits are set to 1 or 0. This process is how a BloomFilter can guarantee an element does not exist. If the bits aren&#8217;t set, it’s simply impossible for the element to be in the set.  However, a positive answer means the element is in the set or a hashing collision occurred.  A more detaild description of a BloomFilter can be found <a href="http://spyced.blogspot.com/2009/01/all-you-ever-wanted-to-know-about.html" target="_blank" onclick="pageTracker._trackPageview('/outgoing/spyced.blogspot.com/2009/01/all-you-ever-wanted-to-know-about.html?referer=');">here</a> and a good tutorial on BloomFilters <a href="http://llimllib.github.com/bloomfilter-tutorial/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/llimllib.github.com/bloomfilter-tutorial/?referer=');">here</a>. According to <a href="http://en.wikipedia.org/wiki/Bloom_filter" target="_blank" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Bloom_filter?referer=');">wikipedia</a>, Google uses BloomFilters in BigTable to avoid disk lookups for non-existent items.  Another interesting usage is <a href="http://asemanfar.com/Using-a-bloom-filter-to-optimize-a-SQL-query" target="_blank" onclick="pageTracker._trackPageview('/outgoing/asemanfar.com/Using-a-bloom-filter-to-optimize-a-SQL-query?referer=');">using a BloomFilter to optimize a sql querry</a>.</p>
<h4>Using the Guava BloomFilter</h4>
<p>A Guava BloomFilter is created by calling the static method create on the BloomFilter class,<br />
passing in a <a href="http://docs.guava-libraries.googlecode.com/git-history/v11.0.2/javadoc/com/google/common/hash/Funnel.html" target="_blank" onclick="pageTracker._trackPageview('/outgoing/docs.guava-libraries.googlecode.com/git-history/v11.0.2/javadoc/com/google/common/hash/Funnel.html?referer=');">Funnel</a> object and an int representing the expected number of insertions. A Funnel, also new in Guava 11, is an object that can send data into a <a href="http://docs.guava-libraries.googlecode.com/git-history/v11.0.2/javadoc/com/google/common/hash/Sink.html"target="_blank">Sink</a>.  The following example is the default implementation and has a percentage of false positives of 3%. Guava provides a <a href="http://docs.guava-libraries.googlecode.com/git-history/v11.0.2/javadoc/com/google/common/hash/Funnels.html" onclick="pageTracker._trackPageview('/outgoing/docs.guava-libraries.googlecode.com/git-history/v11.0.2/javadoc/com/google/common/hash/Funnels.html?referer=');">Funnels</a> class containing two static methods providing implementations of the Funnel interface for inserting a CharSequence or byte Array into a filter.</p>
<pre class="brush:java">
//Creating the BloomFilter
BloomFilter bloomFilter = BloomFilter.create(Funnels.byteArrayFunnel(), 1000);

//Putting elements into the filter
//A BigInteger representing a key of some sort
bloomFilter.put(bigInteger.toByteArray());

//Testing for element in set
boolean mayBeContained = bloomFilter.mayContain(bitIntegerII.toByteArray());
</pre>
<p>UPDATE: based on the comment from Louis Wasserman, here&#8217;s how to create a BloomFilter for BigIntegers with a custom Funnel implementation:</p>
<pre class="brush:java">
//Create the custom filter
class BigIntegerFunnel implements Funnel&lt;BigInteger&gt; {
        @Override
        public void funnel(BigInteger from, Sink into) {
            into.putBytes(from.toByteArray());
        }
    }

//Creating the BloomFilter
BloomFilter bloomFilter = BloomFilter.create(new BigIntegerFunnel(), 1000);

//Putting elements into the filter
//A BigInteger representing a key of some sort
bloomFilter.put(bigInteger);

//Testing for element in set
boolean mayBeContained = bloomFilter.mayContain(bitIntegerII);
</pre>
<h4>Considerations</h4>
<p>It&#8217;s critical to estimate the number of expected insertions correctly. As insertions into the filter approach or exceeds the expected number, the BloomFilter begins to fill up and as a result will generate more false positives to the point of being useless.  There is another version of the BloomFilter.create method taking an additional parameter, a double representing the desired level of false hit probability (must be greater than 0 and less than one). The level of false hit probability affects the number of hashes for storing or searching for elements.  The lower the desired percentage, the higher number of hashes performed.</p>
<h4>Conclusion</h4>
<p>A BloomFilter is a useful item for a developer to have in his/her toolbox.  The Guava project now makes it very simple to begin using a BloomFilter when the need arises.  I hope you enjoyed this post.  Helpful comments and suggestions are welcomed.</p>
<h4>References</h4>
<ul>
<li><a href="https://github.com/bbejeck/guava-blog/blob/master/src/test/java/bbejeck/guava/hash/BloomFilterTest.java" target="_blank" onclick="pageTracker._trackPageview('/outgoing/github.com/bbejeck/guava-blog/blob/master/src/test/java/bbejeck/guava/hash/BloomFilterTest.java?referer=');">Unit Test Demo of Guava BloomFilter</a>.</li>
<li><a href="http://docs.guava-libraries.googlecode.com/git-history/v11.0.2/javadoc/com/google/common/hash/BloomFilter.html" onclick="pageTracker._trackPageview('/outgoing/docs.guava-libraries.googlecode.com/git-history/v11.0.2/javadoc/com/google/common/hash/BloomFilter.html?referer=');">BloomFilter class</a></li>
<li><a href="http://spyced.blogspot.com/2009/01/all-you-ever-wanted-to-know-about.html" target="_blank" onclick="pageTracker._trackPageview('/outgoing/spyced.blogspot.com/2009/01/all-you-ever-wanted-to-know-about.html?referer=');">All You Want to Know about BloomFilters</a>.</li>
<li><a href="http://llimllib.github.com/bloomfilter-tutorial/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/llimllib.github.com/bloomfilter-tutorial/?referer=');">BloomFilter Tutorial</a>.</li>
<li><a href="http://en.wikipedia.org/wiki/Bloom_filter" target="_blank" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Bloom_filter?referer=');">BloomFilter on Wikipedia</a>.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://codingjunkie.net/guava-bloomfilter/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Event Programming Example: Google Guava EventBus and Java 7 WatchService</title>
		<link>http://codingjunkie.net/eventbus-watchservice/</link>
		<comments>http://codingjunkie.net/eventbus-watchservice/#comments</comments>
		<pubDate>Fri, 24 Feb 2012 06:30:05 +0000</pubDate>
		<dc:creator>Bill B</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[Guava]]></category>
		<category><![CDATA[Java 7]]></category>

		<guid isPermaLink="false">http://codingjunkie.net/?p=2004</guid>
		<description><![CDATA[This post is going to cover using the Guava EventBus to publish changes to a directory or sub-directories detected by the Java 7 WatchService. The Guava EventBus is a great way to add publish/subscribe communication to an application. The WatchService, new in the Java 7 java.nio.file package, is used to monitor a directory for changes. [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://codingjunkie.net/wp-content/uploads/2012/01/Java_Logo1-150x150.png" alt="" title="Java_Logo" width="150" height="150" class="alignleft size-thumbnail wp-image-1676" />This post is going to cover using the Guava EventBus to publish changes to a directory or sub-directories detected by the Java 7 WatchService.  The Guava EventBus is a great way to add publish/subscribe communication to an application.  The WatchService, new in the Java 7 java.nio.file package, is used to monitor a directory for changes. Since the EventBus and WatchService have been covered in previous posts, we will not be covering these topics in any depth here.  For more information, the reader is encouraged to view the <a href="http://codingjunkie.net/guava-eventbus/" title="Event Programming with Google Guava EventBus" target="_blank">EventBus</a> and <a href="http://codingjunkie.net/java-7-watchservice/" title="What’s New in Java 7: WatchService" target="_blank">WatchService</a> posts.  [NOTE: post updated on 02/28/2012 for clarity.]</p>
<h3>Why Use the EventBus</h3>
<p>There are two main reasons for using the EventBus with a WatchService.</p>
<ol>
<li>We don&#8217;t want poll for events, but would rather receive asynchronous notification.</li>
<li>Once events are processed, the WatchKey.reset method needs to be called to enable any new changes to be queued.  While the WatchKey object is thread safe, it&#8217;s important that the reset method is called only after all threads have finished processing events, leading to somewhat of a coordination hassle.  Using a single thread to process the events, invoke the reset method, then publish the changes via the EventBus, eliminates this problem.</li>
</ol>
<p>Our plan to accomplish this is simple and will involve taking the following steps:</p>
<ol>
<li>Instantiate an instance of the WatchService.</li>
<li>Register every directory recursively, starting with a given Path object.</li>
<li>Take events off the WatchService queue, then process and publish those events.</li>
<li>Start up a separate thread for taking events off the queue and publishing.</li>
</ol>
<p>The code examples that follow are the more relevant highlights from the <a href="https://github.com/bbejeck/Java-7/blob/master/src/main/java/bbejeck/nio/files/directory/event/DirectoryEventWatcherImpl.java" target="_blank" onclick="pageTracker._trackPageview('/outgoing/github.com/bbejeck/Java-7/blob/master/src/main/java/bbejeck/nio/files/directory/event/DirectoryEventWatcherImpl.java?referer=');">DirectoryEventWatcherImpl</a> class that is going to do all of this work.</p>
<h3>Registering Directories with the WatchService</h3>
<p>While adding or deleting a sub-directory will generate an event, any changes <i>inside</i> a sub-directory of a watched directory will not. We are going to compensate for this by recursively going through all sub-directories (via the Files.walkFileTree method) and register each one with the WatchService object (previously defined in the example here):</p>
<pre class="brush:java">

private void registerDirectories() throws IOException {
        Files.walkFileTree(startPath, new WatchServiceRegisteringVisitor());
}

private class WatchServiceRegisteringVisitor extends SimpleFileVisitor&lt;Path&gt;{
    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
         dir.register(watchService,ENTRY_CREATE,ENTRY_DELETE,ENTRY_MODIFY);
         return FileVisitResult.CONTINUE;
    }
}
</pre>
<p>On line 2 the Files.walkFileTree method uses the WatchServiceRegisteringVisitor class defined on line 5 to register every directory with the WatchService.  The registered events are creation of files/directories, deletion of files/directories or updates to a file.  </p>
<h3>Publishing Events</h3>
<p>The next step is to create a FutureTask that will do the work of checking the queue and publishing the events.</p>
<pre class="brush:java">
 private void createWatchTask() {
    watchTask = new FutureTask&lt;&gt;(new Callable&lt;Integer&gt;() {
       private int totalEventCount;
       @Override
       public Integer call() throws Exception {
           while (keepWatching) {
               WatchKey watchKey = watchService.poll(10, TimeUnit.SECONDS);
               if (watchKey != null) {
                  List&lt;WatchEvent&lt;?&gt;&gt; events = watchKey.pollEvents();
                  Path watched = (Path) watchKey.watchable();
                  PathEvents pathEvents = new PathEvents(watchKey.isValid(), watched);
                  for (WatchEvent event : events) {
                        pathEvents.add(new PathEvent((Path) event.context(), event.kind()));
                        totalEventCount++;
                  }
                  watchKey.reset();
                  eventBus.post(pathEvents);
                }
          }
           return totalEventCount;
        }
      });
    }

private void startWatching() {
  new Thread(watchTask).start();
}
</pre>
<p>On line 7, we are checking the WatchService every 10 seconds for queued events. When a valid WatchKey is returned, the first step is to retrieve the events (line 9) then get the directory where the events occurred (line 10). On line 11 a PathEvents object is created, taking a boolean and the watched directory as constructor arguments. Lines 12-15 are looping over the events retrieved on line 9, using the target Path and event type as arguments to create PathEvent object.  The WatchKey.reset method is called on line 16, setting the WatchKey state back to ready, making it eligible to receive new events and be placed back into the queue. Finally on line 17 the EventBus publishes the PathEvents object to all subscribers.  It&#8217;s important to note here that the PathEvents and PathEvent classes are immutable.  The totalEventCount that is returned from the Callable is never exposed in the API, but is used for testing purposes.  The startWatching method on line 25 starts the thread to run the watching/publishing task defined above.</p>
<h3>Conclusion</h3>
<p>By pairing the WatchService with the Guava EventBus we are able to manage the WatchKey and process events in a single thread and notify any number of subscribers asynchronously of the events. It is hoped the reader found this example useful.  As always comments and suggestions are welcomed.</p>
<h4>Resources</h4>
<ul>
<li><a href="https://github.com/bbejeck/Java-7/tree/master/src/main/java/bbejeck/nio/files/directory/event" target="new" onclick="pageTracker._trackPageview('/outgoing/github.com/bbejeck/Java-7/tree/master/src/main/java/bbejeck/nio/files/directory/event?referer=');">Source code</a> and <a href="https://github.com/bbejeck/Java-7/blob/master/src/test/java/bbejeck/nio/files/directory/event/DirectoryEventWatcherImplTest.java" target="new" onclick="pageTracker._trackPageview('/outgoing/github.com/bbejeck/Java-7/blob/master/src/test/java/bbejeck/nio/files/directory/event/DirectoryEventWatcherImplTest.java?referer=');">unit test</a> for this post</li>
<li><a href="http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/eventbus/package-summary.html" title="Guava EventBus" target="_blank" onclick="pageTracker._trackPageview('/outgoing/guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/eventbus/package-summary.html?referer=');">EventBus API</a></li>
<li><a href="http://docs.oracle.com/javase/7/docs/api/java/nio/file/WatchService.html" title="WatchService API" target="_blank" onclick="pageTracker._trackPageview('/outgoing/docs.oracle.com/javase/7/docs/api/java/nio/file/WatchService.html?referer=');">WatchService API</a></li>
<li>Previous post on the <a href="http://codingjunkie.net/java-7-watchservice/" target="new">WatchService</a>.</li>
<li>Previous post on the <a href="http://codingjunkie.net/guava-eventbus/" target="new">EventBus</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://codingjunkie.net/eventbus-watchservice/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What&#8217;s New in Java 7: WatchService</title>
		<link>http://codingjunkie.net/java-7-watchservice/</link>
		<comments>http://codingjunkie.net/java-7-watchservice/#comments</comments>
		<pubDate>Fri, 24 Feb 2012 06:29:47 +0000</pubDate>
		<dc:creator>Bill B</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[Java 7]]></category>

		<guid isPermaLink="false">http://codingjunkie.net/?p=1950</guid>
		<description><![CDATA[Of all the new features in Java 7, one of the more interesting is the WatchService, adding the capability to watch a directory for changes. The WatchService maps directly to the native file event notification mechanism, if available. If a native event notification mechanism is not available, then the default implementation will use polling. As [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://codingjunkie.net/wp-content/uploads/2012/01/Java_Logo1-150x150.png" alt="" title="Java_Logo" width="150" height="150" class="alignleft size-thumbnail wp-image-1676" />Of all the new features in Java 7, one of the more interesting is the WatchService, adding the capability to watch a directory for changes.  The WatchService maps directly to the native file event notification mechanism, if available.  If a native event notification mechanism is not available, then the default implementation will use polling.  As a result, the responsiveness, ordering of events and details available are implementation specific.</p>
<h3>Watching A Directory</h3>
<p>The Path interface implements the register method that takes a WatchService object and varargs of type WatchEvent.Kind as arguments.  There are 4 events to watch for:</p>
<ol>
<li>ENTRY_CREATE</li>
<li>ENTRY_DELETE</li>
<li>ENTRY_MODIFY</li>
<li>OVERFLOW</li>
</ol>
<p>While the first 3 types are self explanatory, OVERFLOW indicates that events may been lost or discarded. A WatchService is created by calling FileSystem.newWatchService().  Watching a directory is accomplished by registering a Path object with the WatchService:</p>
<pre class="brush:java">
import static java.nio.file.StandardWatchEventKinds.*;
Path path = Paths.get("/home");
WatchService watchService = FileSystems.getDefault().newWatchService();
WatchKey watchKey = path.register(watchService,ENTRY_CREATE,ENTRY_DELETE,ENTRY_MODIFY);
</pre>
<p>As we can see from the example, the register method returns a WatchKey object. The WatchKey is a token that represents the registration of the Path with the WatchService. </p>
<h3>The WatchKey</h3>
<p>As a result of the registration process, the WatchKey is in a &#8216;ready&#8217; state and is considered valid.  A WatchKey remains valid until one of the following occurs:</p>
<ol>
<li>WatchKey.cancel() is called.</li>
<li>The directory being watched is no longer available.</li>
<li>The WatchService object is closed.</li>
</ol>
<h3>Checking For Changes</h3>
<p>When a change is detected, the WatchKey state is set to &#8216;signaled&#8217; and it is placed in a queue for processing. Getting WatchKeys off the queue involves calling WatchService.poll() or WatchService.take(). Here is a basic example:</p>
<pre class="brush:java">
private boolean notDone = true;
while(notDone){
    try{
         WatchKey watchKey = watchService.poll(60,TimeUnit.SECONDS);
         List&lt;WatchEvent.Kind&lt;?&gt;&gt; events = watchKey.pollEvents();
         for(WatchEvent event : events){
            ...process the events
         }
         if(!watchKey.reset()){
            ...handle situation no longer valid
         }
     }catch(InterruptedException e){
            Thread.currentThread().interrupt();
     }
</pre>
<p>On line 5 we are calling the pollEvents method to retrieve all the events for this WatchKey object. On line 9 you&#8217;ll notice a call to the reset method.  The reset method sets the WatchKey state back to &#8216;ready&#8217; and returns a boolean indicating if the WatchKey is still valid.  If there are any pending events, then the WatchKey will be immediately re-queued, otherwise it will remain in the ready state until new events are detected.  Calling reset on a WatchKey that has been cancelled or is in the ready state will have no effect.  If a WatchKey is canceled while it is queued, it will reamin in the queue until retrieved. Cancellation could also happen automatically if the directory was deleted or is no longer available.</p>
<h3>Processing Events</h3>
<p>Now that we have detected an event, how do we determine:</p>
<ol>
<li>On which directory did the event happen? (assuming more than one directory registered)</li>
<li>What was the actual event? (assuming listening for more than one event)</li>
<li>What was the target of the event, i.e what Path object was created,deleted or updated?</li>
</ol>
<p>Jumping in to line 6 in the previous example, we will parse the needed information from a WatchKey and a WatchEvent:</p>
<pre class="brush:java">
 //WatchKey watchable returns the calling Path object of Path.register
 Path watchedPath = (Path) watchKey.watchable();
 //returns the event type
 StandardWatchEventKinds eventKind = event.kind();
 //returns the context of the event
 Path target = (Path)event.context();
</pre>
<p>On line 6 we see the WatchEvent.context method being invoked.  The context method will return a Path object if the event was a creation, delete or update and will be relative to the watched directory. It&#8217;s important to know that when a event is received there is no guarantee that the program(s) performing the operation have completed, so some level of coordination may be required. </p>
<h3>Conclusion</h3>
<p>The WatchService is a very interesting feature of the new java.nio.file package in Java 7.  That being said, there are two things that about the WatchService to keep in mind:</p>
<ol>
<li>The WatchService does not pick up events for sub-directories of a watched directory.</li>
<li>We still need to poll the WatchService for events, rather than receive asynchronous notification.</li>
</ol>
<p>To address the above issues my next post will use the Guava EventBus to process the WatchService events. Thanks for your time</p>
<h4>Resources</h4>
<ol>
<li><a href="http://docs.oracle.com/javase/7/docs/api/java/nio/file/package-summary.html" target="new" onclick="pageTracker._trackPageview('/outgoing/docs.oracle.com/javase/7/docs/api/java/nio/file/package-summary.html?referer=');">java.nio.file</a> package that  contains the WatchService, WatchKey and WatchEvent objects discussed here.</li>
<li>A <a href="https://github.com/bbejeck/Java-7/blob/master/src/test/java/bbejeck/nio/files/watch/WatchDirectoryTest.java" target="new" onclick="pageTracker._trackPageview('/outgoing/github.com/bbejeck/Java-7/blob/master/src/test/java/bbejeck/nio/files/watch/WatchDirectoryTest.java?referer=');">unit test</a> demonstrating the WatchService</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://codingjunkie.net/java-7-watchservice/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Creating An Asynchronous, Recursive DirectoryStream in Java 7</title>
		<link>http://codingjunkie.net/globbing-directories-in-java/</link>
		<comments>http://codingjunkie.net/globbing-directories-in-java/#comments</comments>
		<pubDate>Fri, 10 Feb 2012 07:13:04 +0000</pubDate>
		<dc:creator>Bill B</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[Concurrency]]></category>
		<category><![CDATA[Java 7]]></category>

		<guid isPermaLink="false">http://codingjunkie.net/?p=1865</guid>
		<description><![CDATA[Continuing with my series on the Java 7 java.nio.file package, this time covering the DirectoryStream interface. In this post we are going implement our own DirectoryStream that will iterate over the files in an entire directory tree, not just a single directory. Our goal in the end is to have something that works similar to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://codingjunkie.net/wp-content/uploads/2012/01/Java_Logo1.png"><img src="http://codingjunkie.net/wp-content/uploads/2012/01/Java_Logo1-150x150.png" alt="" title="Java_Logo" width="150" height="150" class="alignleft size-thumbnail wp-image-1676" /></a>Continuing with my series on the Java 7 java.nio.file package, this time covering the DirectoryStream interface.  In this post we are going implement our own DirectoryStream that will iterate over the files in an entire directory tree, not just a single directory.  Our goal in the end is to have something that works similar to Ruby&#8217;s <a href="http://ruby-doc.org/core-1.9.3/Dir.html#method-c-glob" target="new" onclick="pageTracker._trackPageview('/outgoing/ruby-doc.org/core-1.9.3/Dir.html_method-c-glob?referer=');">Dir.glob(&#8220;**&#8221;)</a> method. </p>
<h3>Requirements</h3>
<p>Here are the requirements:</p>
<ol>
<li>Starting from a given Path object, it should recursively go through each directory looking for files that match a given pattern.</li>
<li>It needs to be a single method call, something like DirUtils.glob(Path path, String pattern).</li>
<li>We want the search to be asynchronous, so we can iterate over files as they are found.</li>
</ol>
<p>The plan to meet our objective is straight forward:</p>
<ol>
<li>Create a DirectoryStream.Filter object from the given pattern.</li>
<li>Use the Files.walkFileTree method to recursively search for files that match our pattern.</li>
<li>Run the search in a separate thread, and when a matching file is found, place it in a queue.</li>
<li>The DirectoryStream iterator will pull the files out of the queue as they become available</li>
</ol>
<p><b>Disclosure</b>: This post is merely an attempt to see if making an asynchronous, recursive DirectorySteam will work, I make no guarantees about the code presented here, YMMV.</p>
<h3>The Filter</h3>
<p>DirectoryStream has a static interface, Filter, that is used to determine if the path object should be accepted.  The filter object is constructed by first creating a <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/file/FileSystem.html#getPathMatcher(java.lang.String)" target="new" onclick="pageTracker._trackPageview('/outgoing/docs.oracle.com/javase/7/docs/api/java/nio/file/FileSystem.html_getPathMatcher_java.lang.String?referer=');">PathMatcher</a> object then using the PathMatcher in the Filter&#8217;s accept method:</p>
<pre class="brush:java">
public static DirectoryStream.Filter&lt;Path&gt; buildGlobFilter(String pattern) {
        final PathMatcher pathMatcher = getPathMatcher("glob:"+pattern);
        return new DirectoryStream.Filter&lt;Path&gt;() {
            @Override
            public boolean accept(Path entry) throws IOException {
                return pathMatcher.matches(entry);
            }
        };
    }
</pre>
<h3>The Recursive Search</h3>
<p>To perform the search we are going the use the Files.walkFileTree method with a <a href="https://github.com/bbejeck/Java-7/blob/master/src/main/java/bbejeck/nio/files/visitor/FunctionVisitor.java" target="new" onclick="pageTracker._trackPageview('/outgoing/github.com/bbejeck/Java-7/blob/master/src/main/java/bbejeck/nio/files/visitor/FunctionVisitor.java?referer=');">FunctionVisitor</a> object. FunctionVisitor is a subclass of SimpleFileVisitor that takes a Guava Function as a constructor argument. The provided function is called as each file is visited and is checked by the <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/file/DirectoryStream.Filter.html" target="new" onclick="pageTracker._trackPageview('/outgoing/docs.oracle.com/javase/7/docs/api/java/nio/file/DirectoryStream.Filter.html?referer=');">DirectoryStream.Filter</a> object for a match.  We wrap the creation of the Function object in a method call:</p>
<pre class="brush:java">
 private Function&lt;Path, FileVisitResult&gt; getFunction(final Filter&lt;Path&gt; filter) {
        return new Function&lt;Path, FileVisitResult&gt;() {
            @Override
            public FileVisitResult apply(Path input) {
                try {
                    if (filter.accept(input.getFileName())) {
                        pathsBlockingQueue.offer(input);
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e.getMessage());
                }
                return (pathTask.isCancelled()) ? FileVisitResult.TERMINATE : FileVisitResult.CONTINUE;
            }
        };
    }
</pre>
<p>On line 6 the filter is checking for a match on the filename. If a match is found, the path object is placed in a LinkedBlockingQueue, pathsBlockingQueue, on line 7. On line 12, we see the pathTask instance variable, which is a FutureTask handle to the search thread.  If pathTask has been cancelled we terminate the search, otherwise continue.</p>
<h3>The Search Thread</h3>
<p>To run the search, we wrap the Files.walkFileTree method in a Callable object, which is used as a constructor argument to pathTask, the FutureTask object.  By using a FutureTask, we have a hook into canceling the search as well as being able to check it&#8217;s running status.</p>
<pre class="brush:java">
 private void findFiles(final Path startPath, final Filter filter) {
        pathTask = new FutureTask&lt;Void&gt;(new Callable&lt;Void&gt;() {
            @Override
            public Void call() throws Exception {
                Files.walkFileTree(startPath, new FunctionVisitor(getFunction(filter)));
                return null;
            }
        });
        start(pathTask);
    }
</pre>
<p>On line 5 we see the call to the getFunction method from the previous example.  On line 9 the method call start is used to spin off the search in it&#8217;s own thread:</p>
<pre class="brush:java">
private void start(FutureTask&lt;Void&gt; futureTask) {
        new Thread(futureTask).start();
    }
</pre>
<p>We use a Thread object instead of an ExecutorService because we only need a single thread to execute once. Since we are not submitting any subsequent tasks, an ExecutorService really is not necessary.</p>
<h3>Implementing A DirectoryStream</h3>
<p>To implement a DirectoryStream there are two methods that need to be overridden &#8211; iterator and close. DirectoryStream extends the AutoCloseable interface, so when used with the try-with-resources construct, the close method is automatically invoked, releasing any underlying resources. The most interesting part of this code is overriding the iterator method:</p>
<pre class="brush:java">
public Iterator&lt;Path&gt; iterator() {
        confirmNotClosed();
        findFiles(startPath, filter);
        return new Iterator&lt;Path&gt;() {
            Path path;
            @Override
            public boolean hasNext() {
                try {
                    path = pathsBlockingQueue.poll();
                    while (!pathTask.isDone() &#038;&#038; path == null) {
                        path = pathsBlockingQueue.poll(5, TimeUnit.MILLISECONDS);
                    }
                    return (path != null) ? true : false;
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
                return false;
            }

            @Override
            public Path next() {
                return path;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("Removal not supported");
            }
        };
    }
</pre>
<p>On line 2 we check to see if the iterator was previously closed and throw an UnsupportedOperationException if that is the case.  Line 3 kicks off the search thread as we saw from the previous example.  The hasNext method is where the real &#8220;brains&#8221; of the class resides.  Since the search is asynchronous the DirectoryStream will attempt to iterate over the files immediately, but there needs to be some coordination with the search thread as it finds matching path objects and places them into the queue.  On line 9 we first call poll (a non blocking call) and set the result to the path variable defined on line 5.  If the path object is null we drop into a while loop that calls poll again, but this time it&#8217;s a blocking call with a timeout of 5 milliseconds.  We&#8217;ll stay in that loop until a non-null path is returned or the search thread has completed or cancelled.  On line 13 we check for null to determine if we have a valid result or if there are no more path objects to return.  If hasNext returned true, the next method returns the path object that was retrieved by the previous hasNext call.  This process will continue until all results have been returned. </p>
<h3>Conclusion</h3>
<p>Putting this all together we now have the <a href="https://github.com/bbejeck/Java-7/blob/master/src/main/java/bbejeck/nio/files/directory/AsynchronousRecursiveDirectoryStream.java" target="new" onclick="pageTracker._trackPageview('/outgoing/github.com/bbejeck/Java-7/blob/master/src/main/java/bbejeck/nio/files/directory/AsynchronousRecursiveDirectoryStream.java?referer=');">AsynchronousRecursiveDirectoryStream</a> class.  By combining our new class with <a href="https://github.com/bbejeck/Java-7/blob/master/src/main/java/bbejeck/nio/util/DirUtils.java" target="new" onclick="pageTracker._trackPageview('/outgoing/github.com/bbejeck/Java-7/blob/master/src/main/java/bbejeck/nio/util/DirUtils.java?referer=');">DirUtils</a> we can iterate over an entire directory tree:</p>
<pre class="brush:java">
try(DirectoryStream&lt;Path&gt; directoryStream = DirUtils.glob(path,"*")){
   for(Path path : directoryStream){
      ....
    }
}
</pre>
<p>It is hoped that the reader found this useful.  As always, comments and suggestions are welcomed. Thanks for your time.</p>
<h4>Resources</h4>
<ul>
<li><a href="https://github.com/bbejeck/Java-7/blob/master/src/main/java/bbejeck/nio/files/directory/AsynchronousRecursiveDirectoryStream.java" target="new" onclick="pageTracker._trackPageview('/outgoing/github.com/bbejeck/Java-7/blob/master/src/main/java/bbejeck/nio/files/directory/AsynchronousRecursiveDirectoryStream.java?referer=');">source code</a> and <a href="https://github.com/bbejeck/Java-7/blob/master/src/test/java/bbejeck/nio/files/directory/AsynchronousRecursiveDirectoryStreamTest.java" target="new" onclick="pageTracker._trackPageview('/outgoing/github.com/bbejeck/Java-7/blob/master/src/test/java/bbejeck/nio/files/directory/AsynchronousRecursiveDirectoryStreamTest.java?referer=');">unit test</a> for material covered</li>
<li><a href="http://docs.oracle.com/javase/7/docs/api/java/nio/file/package-summary.html" target="new" onclick="pageTracker._trackPageview('/outgoing/docs.oracle.com/javase/7/docs/api/java/nio/file/package-summary.html?referer=');">Java 7 java.nio.file api</a></li>
]]></content:encoded>
			<wfw:commentRss>http://codingjunkie.net/globbing-directories-in-java/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>What&#8217;s New In Java 7:  Copy and Move Files and Directories</title>
		<link>http://codingjunkie.net/java-7-copy-move/</link>
		<comments>http://codingjunkie.net/java-7-copy-move/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 05:10:34 +0000</pubDate>
		<dc:creator>Bill B</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://codingjunkie.net/?p=1673</guid>
		<description><![CDATA[This post is a continuation of my series on the Java 7 java.nio.file package, this time covering the copying and moving of files and complete directory trees. If you have ever been frustrated by Java&#8217;s lack of copy and move methods, then read on, for relief is at hand. Included in the coverage is the [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://codingjunkie.net/wp-content/uploads/2012/01/Java_Logo-150x150.png" alt="" title="Java_Logo" width="150" height="150" class="alignleft size-thumbnail wp-image-1672" />This post is a continuation of my series on the Java 7 java.nio.file package, this time covering the copying and moving of files and complete directory trees.  If you have ever been frustrated by Java&#8217;s lack of copy and move methods, then read on, for relief is at hand.  Included in the coverage is the very useful Files.walkFileTree method.  Before we dive into the main content however, some background information is needed. </p>
<h3>Path Objects</h3>
<p>Path objects represent a sequence of directories that may or may not include a file. There are three ways to construct a Path object:</p>
<ol>
<li>FileSystems.getDefault().getPath(String first, String&#8230; more)</li>
<li>Paths.get(String path, String&#8230; more), convenience method that calls FileSystems.getDefault().getPath</li>
<li>Calling the toPath method on a java.io.File object</li>
</ol>
<p>From this point forward in all our examples, we will use the Paths.get method.  Here are some examples of creating Path objects:</p>
<pre class="brush:java">
//Path string would be "/foo"
Paths.get("/foo");
//Path string "/foo/bar"
Paths.get("/foo","bar");
</pre>
<p>To manipulate Path objects there are the Path.resolve and Path.relativize methods.  Here is an example of using Path.resolve:</p>
<pre class="brush:java">
//This is our base path "/foo"
Path base = Paths.get("/foo");
//filePath is "/foo/bar/file.txt" while base still "/foo"
Path filePath = base.resolve("bar/file.txt");
</pre>
<p>Using the Path.resolve method will append the given String or Path object to the end of the calling Path, unless the given String or Path represents an absolute path, the the given path is returned, for example:</p>
<pre class="brush:java">
Path path = Paths.get("/foo");
//resolved Path string is "/usr/local"
Path resolved = path.resolve("/usr/local");
</pre>
<p>The Path.relativize works in the opposite fashion, returning a new relative path that if resolved against the calling Path would result in the same Path string.  Here&#8217;s an example:</p>
<pre class="brush:java">
        // base Path string "/usr"
        Path base = Paths.get("/usr");
        // foo Path string "/usr/foo"
        Path foo = base.resolve("foo");
        // bar Path string "/usr/foo/bar"
        Path bar = foo.resolve("bar");
        // relative Path string "foo/bar"
        Path relative = base.relativize(bar);
</pre>
<p>Another method on the Path class that is helpful is the Path.getFileName, that returns the name of the farthest element represented by this Path object, with the name being an actual file or just a directory.  For example:</p>
<pre class="brush:java">
//assume filePath constructed elsewhere as "/home/user/info.txt"
//returns Path with path string "info.txt"
filePath.getFileName()

//now assume dirPath constructed elsewhere as "/home/user/Downloads"
//returns Path with path string "Downloads"
dirPath.getFileName()
</pre>
<p>In the next section we are going to take a look at how we can use Path.resolve and Path.relativize in conjunction with Files class for copying and moving files.</p>
<h3>Files Class</h3>
<p>The Files class consists of static methods that use Path objects to work with files and directories.  While there are over 50 methods in the Files class, at this point we are only going to discuss the copy and move methods. </p>
<h4>Copy A File</h4>
<p>To copy one file to another you would use the (any guesses on the name?) Files.copy method &#8211; copy(Path source, Path target, CopyOption&#8230; options) very concise and no anonymous inner classes, are we sure it&#8217;s Java?.  The options argument are enums that specify how the file should be copied.  (There are actually 2 different Enum classes, LinkOption and StandardCopyOption, but both implement the CopyOption interface) Here is the list of available options for Files.copy:</p>
<ol>
<li>LinkOption.NOFOLLOW_LINKS</li>
<li>StandardCopyOption.COPY_ATTRIBUTES</li>
<li>StandardCopyOption.REPLACE_EXISTING</li>
</ol>
<p>There is also a StandardCopyOption.ATOMIC_MOVE enum, but if this option is specified, an UsupportedOperationException is thrown. If no options are specified, the default is to throw an error if the target file exists or is a symbolic link.  If the path object is a directory then an empty directory is created in the target location. (Wait a minute! didn&#8217;t it say in the introduction that we could copy the entire contents of a directory? The answer is still yes and that is coming!) Here&#8217;s an example of copying a file to another with Path objects using the Path.resolve and Path.relativize methods:</p>
<pre class="brush:java">
        Path sourcePath ...
        Path basePath ...
        Path targetPath ...

        Files.copy(sourcePath, targetPath.resolve(basePath.relativize(sourcePath));
</pre>
<h4>Move A File</h4>
<p>Moving a file is equally as straight forward &#8211; move(Path source, Path target, CopyOption&#8230; options);<br />
The available StandardCopyOptions enums available are:</p>
<ol>
<li>StandardCopyOption.REPLACE_EXISTING</li>
<li>StandardCopyOption.ATOMIC_MOVE</li>
</ol>
<p>If Files.move is called with StandardCopyOption.COPY_ATTRIBUTES an UnsupportedOperationException is thrown.  Files.move can be called on an empty directory or if it does not require moving a directories contents, re-naming for example, the call will succeed, otherwise it will throw an IOException (we&#8217;ll see in the following section how to move non-empty directories).  The default is to throw an Exception if the target file already exists. If the source is a symbolic link, then the link itself is moved, not the target of the link.  Here&#8217;s an example of Files.move, again tying in the Path.relativize and Path.resolve methods:</p>
<pre class="brush:java">
        Path sourcePath ...
        Path basePath ...
        Path targetPath ...

        Files.move(sourcePath, targetPath.resolve(basePath.relativize(sourcePath));
</pre>
<h3>Copying and Moving Directories</h3>
<p>One of the more interesting and useful methods found in the Files class is Files.walkFileTree.  The walkFileTree method performs a depth first traversal of a file tree. There are two signatures:</p>
<ol>
<li>walkFileTree(Path start,Set<FileVisitOption> options,int maxDepth,FileVisitor<? super Path> visitor)</li>
<li>walkFileTree(Path start,FileVisitor<? super Path> visitor)</li>
</ol>
<p>The second option for Files.walkFileTree calls the first option with EnumSet.noneOf(FileVisitOption.class) and Integer.MAX_VALUE.  As of this writing, there is only one file visit option &#8211; FOLLOW_LINKS. The FileVisitor is an interface that has four methods defined:</p>
<ol>
<li>preVisitDirectory(T dir, BasicFileAttributes attrs) called for a directory before all entires are traversed.</li>
<li>visitFile(T file, BasicFileAttributes attrs) called for a file in the directory.</li>
<li>postVisitDirectory(T dir, IOException exc) only called after all files and sub-directories have been traversed.</li>
<li>visitFileFailed(T file, IOException exc) called for files that could not be visited</li>
</ol>
<p>All of the methods return one of the four possible FileVisitResult enums :</p>
<ol>
<li>FileVistitResult.CONTINUE</li>
<li>FileVistitResult.SKIP_SIBLINGS (continue without traversing siblings of the directory or file)</li>
<li>FileVistitResult.SKIP_SUBTREE (continue without traversing contents of the directory)</li>
<li>FileVistitResult.TERMINATE</li>
</ol>
<p>To make life easier there is a default implementation of the FileVisitor, SimpleFileVisitor (validates arguments are not null and returns FileVisitResult.CONTINUE), that can be subclassed co you can override just the methods you need to work with. Let&#8217;s take a look at a basic example for copying an entire directory structure. </p>
<h3>Copying A Directory Tree Example</h3>
<p>Let&#8217;s take a look at a class that extends SimpleFileVisitor used for copying a directory tree (some details left out for clarity):</p>
<pre class="brush:java">
 public class CopyDirVisitor extends SimpleFileVisitor&lt;Path&gt; {
    private Path fromPath;
    private Path toPath;
    private StandardCopyOption copyOption = StandardCopyOption.REPLACE_EXISTING;
    ....
    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
        Path targetPath = toPath.resolve(fromPath.relativize(dir));
        if(!Files.exists(targetPath)){
            Files.createDirectory(targetPath);
        }
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Files.copy(file, toPath.resolve(fromPath.relativize(file)), copyOption);
        return FileVisitResult.CONTINUE;
    }
}
</pre>
<p>On line 9, each directory will be created in the target, &#8216;toPath&#8217;, as each directory from the source, &#8216;fromPath&#8217;,is traversed.  Here we can see the power the Path object with respect to working with directories and files.  As the code moves deeper into the directory structure, the correct Path objects are constructed simply from calling relativize and resolve on the fromPath and toPath objects, respectively.  At no point do we need to be aware of where we are in the directory tree, and as a result no cumbersome StringBuilder manipulations are needed to create the correct paths.  On line 17, we see the Files.copy method used to copy the file from the source directory to the target directory.  Next is a simple example of deleting an entire directory tree.</p>
<h3>Deleting A Directory Tree Example</h3>
<p>In this example SimpleFileVisitor has been subclassed for deleting a directory structure:</p>
<pre class="brush:java">
public class DeleteDirVisitor  extends SimpleFileVisitor&lt;Path&gt; {

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Files.delete(file);
        return FileVisitResult.CONTINUE;
    }

    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
        if(exc == null){
            Files.delete(dir);
            return FileVisitResult.CONTINUE;
        }
        throw exc;
    }
}
</pre>
<p>As you can see, deleting is a very simple operation.  Simply delete each file as you find them then delete the directory on exit.</p>
<h3>Combining Files.walkFileTree with Google Guava</h3>
<p>The previous two examples, although useful, were very &#8216;vanilla&#8217;.  Let&#8217;s take a look at two more examples that are a little more creative by combining the Google Gauva Function and Predicate interfaces. </p>
<pre class="brush:java">
public class FunctionVisitor extends SimpleFileVisitor&lt;Path&gt; {
    Function&lt;Path,FileVisitResult&gt; pathFunction;

    public FunctionVisitor(Function&lt;Path, FileVisitResult&gt; pathFunction) {
        this.pathFunction = pathFunction;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        return pathFunction.apply(file);
    }
}
</pre>
<p>In this very simple example, we subclass SimpleFileVisitor to take a Function object as a constructor parameter and as the directory structure is traversed, apply the function to each file.</p>
<pre class="brush:java">
public class CopyPredicateVisitor extends SimpleFileVisitor&lt;Path&gt; {
    private Path fromPath;
    private Path toPath;
    private Predicate&lt;Path&gt; copyPredicate;

    public CopyPredicateVisitor(Path fromPath, Path toPath, Predicate&lt;Path&gt; copyPredicate) {
        this.fromPath = fromPath;
        this.toPath = toPath;
        this.copyPredicate = copyPredicate;
    }

    @Override
    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
        if (copyPredicate.apply(dir)) {
            Path targetPath = toPath.resolve(fromPath.relativize(dir));
            if (!Files.exists(targetPath)) {
                Files.createDirectory(targetPath);
            }
            return FileVisitResult.CONTINUE;
        }
        return FileVisitResult.SKIP_SUBTREE;
    }

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        Files.copy(file, toPath.resolve(fromPath.relativize(file)));
        return FileVisitResult.CONTINUE;
    }
}
</pre>
<p>In this example the CopyPredicateVisitor takes a Predicate object and based on the returned boolean, parts of the directory structure are not copied.  I would like to point out that the previous two examples, usefulness aside, do work in the unit tests foe the source code provided with this post.</p>
<h3>DirUtils</h3>
<p>Building on everything we&#8217;ve covered so far, I could not resist the opportunity to create a utility class, <a href="https://github.com/bbejeck/Java-7/blob/master/src/main/java/bbejeck/nio/util/DirUtils.java" target="new" onclick="pageTracker._trackPageview('/outgoing/github.com/bbejeck/Java-7/blob/master/src/main/java/bbejeck/nio/util/DirUtils.java?referer=');">DirUtils</a>, as an abstraction for working with directories that provides the following methods:</p>
<pre class="brush:java">
 //deletes all files but leaves the directory tree in place
 DirUtils.clean(Path sourcePath);
 //completely removes a directory tree
 DirUtils.delete(Path sourcePath);
 //replicates a directory tree
 DirUtils.copy(Path sourcePath, Path targetPath);
 //not a true move but performs a copy then a delete of a directory tree
 DirUtils.move(Path sourcePath, Path targetPath);
 //apply the function to all files visited
 DirUtils.apply(Path sourcePath,Path targetPath, Function&lt;Path&gt; function);
</pre>
<p>While I wouldn&#8217;t go as far to say it&#8217;s production ready, it was fun to write.</p>
<h3>Conclusion</h3>
<p>That wraps up the new copy and move functionality provided by the java.nio.file package.  I personally think it&#8217;s very useful and will take much of the pain out of working with files in Java.  There&#8217;s much more to cover, working with symbolic links, stream copy methods, DirectoryStreams etc, so be sure to stick around.  Thanks for your time.  As always comments and suggestions are welcomed.</p>
<h4>Resouces</h4>
<ul>
<li><a href="http://docs.oracle.com/javase/7/docs/api/java/nio/file/package-summary.html" target="new" onclick="pageTracker._trackPageview('/outgoing/docs.oracle.com/javase/7/docs/api/java/nio/file/package-summary.html?referer=');">Java 7 java.nio.file api</a></li>
<li><a href="https://github.com/bbejeck/Java-7" target="new" onclick="pageTracker._trackPageview('/outgoing/github.com/bbejeck/Java-7?referer=');">Source code and tests</a> for the post</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://codingjunkie.net/java-7-copy-move/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>What&#8217;s new in Java 7 &#8211; The (Quiet) NIO File Revolution</title>
		<link>http://codingjunkie.net/java7-file-revolution/</link>
		<comments>http://codingjunkie.net/java7-file-revolution/#comments</comments>
		<pubDate>Fri, 20 Jan 2012 06:32:43 +0000</pubDate>
		<dc:creator>Bill B</dc:creator>
				<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://codingjunkie.net/?p=1700</guid>
		<description><![CDATA[Java 7 (&#8220;Project Coin&#8221;) has been out since July of last year. The additions with this release are useful, for example Try with resources &#8211; having closable resources handled automatically from try blocks, Strings in switch statements, multicatch for Exceptions and the &#8216;&#8216; operator for working with generics. The addition that everyone was anticipating the [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://codingjunkie.net/wp-content/uploads/2012/01/Java_Logo1-150x150.png" alt="" title="Java_Logo" width="150" height="150" class="alignleft size-thumbnail wp-image-1676" />Java 7 (&#8220;Project Coin&#8221;) has been out since July of last year.  The additions with this release are useful, for example Try with resources &#8211; having closable resources handled automatically from try blocks, Strings in switch statements, multicatch for Exceptions and the &#8216;<>&#8216; operator for working with generics.  The addition that everyone was anticipating the most, closures, has been deferred to version 8.  Surprisingly though, there was a small &#8216;revolution&#8217; of sorts with the release of Java 7, that for the most part, in my opinion, went unnoticed and could possibly be the best part of the Java 7 release.  The change I&#8217;m referring to is the addition of the java.nio.file package. The java.nio.file package added classes and interfaces that make working with files and directories in Java much easier. First and formost of these changes is the ability to copy or move files.  I always found it frustrating that if you want to copy or move a file, you have to roll your own version of &#8216;copy&#8217; or &#8216;move&#8217;. The utilities found in the Guava project <a href="http://docs.guava-libraries.googlecode.com/git-history/v11.0.1/javadoc/com/google/common/io/package-summary.html" target="new" onclick="pageTracker._trackPageview('/outgoing/docs.guava-libraries.googlecode.com/git-history/v11.0.1/javadoc/com/google/common/io/package-summary.html?referer=');">com.google.common.io</a> package provides these capabilities, but I feel that copy and move operations should be a core part of the language.  Over the next few posts, I&#8217;ll be going into greater detail (with code examples) on the classes/interfaces discussed here and some others that have not been covered.  This post serves as an introduction and overview of the new functionality in the java.nio.file package. </p>
<h3>Breaking Out Responsibilities</h3>
<p>If you take a look at the java.io package as it stands now, the vast majority of the classes are for input streams, output streams, readers or writers . There is only one class that defines operations for directly working with the file system, the File class. Some of the other classes in java.io will take a File object as an argument in a constructor, but all file and directory interaction is through the File class.  In the java.nio.file package, functionality has been teased out into other classes/interfaces.  The first ones to discuss are the Path interface and the Files class.</p>
<h3>Path and Files</h3>
<p>A Path object is some what analogous to a java.io.File object as it can represent a file or directory on the file system.  A Path object is more abstract though, in that it is a sequence of names that represent a directory hierarchy (that may or may not include a file) on the file system.  There are not methods in the Path interface that allow for working with directories or files. The methods defined are for working with or manipulating Path objects only, resolving one Path to another etc. (There is one method that can be used to obtain a java.io.File object from a Path, toFile.  Likewise the java.io.File class now contains a toPath method.) To work with files and directories, Path objects are used in conjunction with the Files class.  The Files class consists entirely of static methods for manipulating directories and files, including copy, move and functions for working with symbolic links. Another interesting method in the Files class is the newDirectoryStream method that returns a DirectoryStream object that can iterate over all the entries in a directory.  Although the java.io.File class has the list method where you provide a FileFilter instance, the newDirectoryStream can take a String &#8216;glob&#8217; like &#8216;*.txt&#8217; to filter on. </p>
<h3>FileStore</h3>
<p>As previously mentioned, all interaction with the file system in the java.io package is through the File class.  This includes getting information on used or available space in the file system.  In java.nio.file there is a FileStore class that represents the storage type for the files whether it&#8217;s a device, partition or concreate file system.  The FileStore class defines methods for getting information about the file storage such as getTotalSpace, getUsableSpace, getUnallocated space.  A FileStore can be obtained by calling the Files.getFileStore(Path path) method that will return the FileStore for that particular file.</p>
<h3>FileSystem and FileSystems</h3>
<p>A FileSystem, as the name implies, provides access to the file system and is a factory for other objects in the file system.  For example, the FileSystem class defines a getPath method that converts a string (/foo/bar) into a system dependent Path object that can be used for accessing a file or directory.  The FileSystem class also provides a getFileStores method that returns an iterable of all FileStores in the FileSystem.  The FileSystems class provides access to the FileSystem object with the static FactorySystems.getDefault method.  There are also static methods for creating custom FileSystem objects.</p>
<h3>Conclusion</h3>
<p>This has been a fast, high level view of the new functionality for working with files provided by the java.nio.file package.  There is much more information that has not been covered here, so take a look at the <a href="http://docs.oracle.com/javase/7/docs/api/java/nio/file/package-summary.html" target="new" onclick="pageTracker._trackPageview('/outgoing/docs.oracle.com/javase/7/docs/api/java/nio/file/package-summary.html?referer=');">api docs</a> Hopefully this post has been able get the reader interested in the improved file handling in Java 7.  Be sure to stick around as we begin to explore in more detail what the java.nio.file package has to offer. </p>
]]></content:encoded>
			<wfw:commentRss>http://codingjunkie.net/java7-file-revolution/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Event Programming with Google Guava EventBus</title>
		<link>http://codingjunkie.net/guava-eventbus/</link>
		<comments>http://codingjunkie.net/guava-eventbus/#comments</comments>
		<pubDate>Fri, 06 Jan 2012 07:41:19 +0000</pubDate>
		<dc:creator>Bill B</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Guava]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://codingjunkie.net/?p=1597</guid>
		<description><![CDATA[It&#8217;s a given in any software application there are objects that need to share information in order to get work done. In Java applications, one way of achieving information sharing is to have event listeners, whose sole purpose is to take some action when a desired event occurs. For the most part this process works [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://codingjunkie.net/wp-content/uploads/2011/11/toolbox.jpg"><img src="http://codingjunkie.net/wp-content/uploads/2011/11/toolbox-150x150.jpg" alt="" title="toolbox" width="150" height="150" class="alignleft size-thumbnail wp-image-1130" /></a> It&#8217;s a given in any software application there are objects that need to share information in order to get work done.  In Java applications, one way of achieving information sharing is to have event listeners, whose sole purpose is to take some action when a desired event occurs.  For the most part this process works and most experienced Java developers are used to writing the requisite anonymous inner class that implements some event listener interface.  This post is about taking a different approach to handling Java events using Guava&#8217;s EventBus.  The EventBus allows for objects to <i>subscribe</i> for or <i>publish</i> events, without having explicit knowledge of each other. The EventBus is not meant to be a general purpose publish/subscribe system or support interprocess communication. </p>
<h3>EventBus Class</h3>
<p>The EventBus is very flexible and can be used as a singleton, or an application can have several instances to accomodate transferring events in different contexts.  The EventBus will dispatch all events serially, so it is important to keep the event handling methods lightweight.  If you need to do heavier processing in the event handlers, there is another flavor of the EventBus, AsyncEventBus.  The AsyncEventBus is identical in functionality, but takes an ExecutorService as a constructor argument to allow for asynchronous dispatching of events.</p>
<h3>Subscribing for Events</h3>
<p>An object subscribes for events by taking the following steps: </p>
<ol>
<li>Define a public method that takes a single argument of the desired event type and place a @Subscribe annotation on that method.</li>
<li>Register with the EventBus by passing an instance of the object to the EventBus.register method.</li>
</ol>
<p>Here is a brief example with details omitted for clarity:</p>
<pre class="brush:java">
 public class PurchaseSubscriber {
    @Subscribe
    public void handlePurchaseEvent(PurchaseEvent event) {
       .....
    }
   .....

  EventBus eventBus = new EventBus();
  PurchaseSubscriber purchaseSubscriber = new PurchaseSubscriber();
  eventBus.register(purchaseSubscriber);
</pre>
<p>There is another annotation that can be used in conjunction with @Subscribe, and that is @AllowConcurrentEvents. The @AllowConcurrentEvents marks a handler method as thread safe so an EventBus (most likely an AsyncEventBus) could potentially call the event handler from simultaneous threads.  One interesting thing I discovered in unit testing is that if a handler method does not have the @AllowConcurrentEvents annotation, it will invoke handlers for an event serially even when using the AsyncEventBus.  It&#8217;s important to note that @AllowConcurrentEvents will not mark a method as an event handler, the @Subscribe annotation still needs to be present. Finally, the event handling method must have one and only one parameter or an IllegalArgumentException will be thrown when you register the object with the EventBus.</p>
<h3>Publishing Events</h3>
<p>Publishing events with the EventBus is equally straight forward.  In the section of code where you want to send the notification of an event, call EventBus.post and all subscribers registered for that event object will be notified. </p>
<pre class="brush:java">
  public void handleTransaction(){
    purchaseService.purchase(item,amount);
    eventBus.post(new CashPurchaseEvent(item,amount));
    ....
  }
</pre>
<p>While it might be obvious, it&#8217;s important that the subscribing and publishing classes share the same EventBus instance, and it would make sense to use Guice or Spring to help manage the dependencies.</p>
<h3>More About Event Handlers</h3>
<p>A very powerful feature of the EventBus is that you can make your handlers as course or fine grained as needed.  The EventBus will call the registered subscribers for all subtypes and implemented interfaces of a posted event object. For example, to handle any and all events one could create an event handler that takes a parameter of type Object. To handle only singular events, create a handler that is very type specific.  To help illustrate, consider the following simple event hierarchy:</p>
<pre class="brush:java">
public abstract class PurchaseEvent {
        String item;
   public PurchaseEvent(String item){
       this.item = item;
   }
}

public class CashPurchaseEvent extends PurchaseEvent {
       int amount;
       public CashPurchaseEvent(String item, int amount){
          super(item);
          this.amount = amount;
       }
}

public class CreditPurchaseEvent extends PurchaseEvent {
       int amount;
       String cardNumber;
       public CreditPurchaseEvent(String item,int amount, String cardNumber){
          super(item);
          this.amount = amount;
          this.cardNumber = cardNumber;
       }
}
</pre>
<p>Here are the corresponding event handling classes:</p>
<pre class="brush:java">
//Would only be notified of Cash purchase events
 public class CashPurchaseSubscriber {
     @Subscribe
     public void handleCashPurchase(CashPurchaseEvent event){
      ...
     }
 }
 //Would only be notified of credit purchases
 public class CreditPurchaseSubscriber {
    @Subscribe
    public void handleCreditPurchase(CreditPurchaseEvent event) {
     ....
    }
}
//Notified of any purchase event
 public class PurchaseSubscriber {
    @Subscribe
    public void handlePurchaseEvent(PurchaseEvent event) {
       .....
    }
}
</pre>
<p>If there is a need to capture a broad range of event types, an alternative would be to have more than one event handling method in a class.  Having multiple handlers could be a better solution as you would not have to do any &#8220;instanceof&#8221; checks on the event object parameter.  Here&#8217;s a simple example from my unit test:</p>
<pre class="brush:java">
public class MultiHandlerSubscriber {

    List&lt;CashPurchaseEvent&gt; cashEvents = new ArrayList&lt;ashPurchaseEvent&gt;();
    List&lt;CreditPurchaseEvent&gt; creditEvents = new ArrayList&lt;CreditPurchaseEvent&gt;();
    List&lt;SimpleEvent&gt; simpleEvents = new ArrayList&lt;SimpleEvent&gt;();

    public MultiHandlerSubscriber(EventBus eventBus){
        eventBus.register(this);
    }

    @Subscribe
    public void handleCashEvents(CashPurchaseEvent event){
        cashEvents.add(event);
    }

    @Subscribe
    public void handleCreditEvents(CreditPurchaseEvent event){
        creditEvents.add(event);
    }

    @Subscribe
    public void handleSimpleEvents(SimpleEvent event){
        simpleEvents.add(event);
    }
....
</pre>
<h3>Testing</h3>
<p>Since the event handlers are just plain methods, they can easily be tested by instantiating an EventBus in the test case or simulate the EventBus by passing the appropriate event object.  When working with the EventBus I found it was very easy to:</p>
<ul>
<li>Forget to register the subscribing object with the EventBus</li>
<li>Neglect to add the @Subscribe annotation.</li>
</ul>
<p>If it seems that an event handler is not being called, check for those two errors first.<br />
A useful debugging technique is to subscribe to the DeadEvent class.  An EventBus will wrap any published event with no handlers in a DeadEvent instance. DeadEvent provides the getEvent method that returns the original event object. </p>
<h3>Conclusion</h3>
<p>The Guava EventBus class provides an attractive and useful alternative to the standard Java event handling mechanism.  It is hoped the reader will find the EventBus as useful as I do.  As always comments and suggestions are welcomed.</p>
<h4>Resources</h4>
<ul>
<li><a href="http://docs.guava-libraries.googlecode.com/git-history/v10.0.1/javadoc/com/google/common/eventbus/package-summary.html" target="new" onclick="pageTracker._trackPageview('/outgoing/docs.guava-libraries.googlecode.com/git-history/v10.0.1/javadoc/com/google/common/eventbus/package-summary.html?referer=');">EventBus API</a></li>
<li><a href="http://code.google.com/p/guava-libraries/" target="new" onclick="pageTracker._trackPageview('/outgoing/code.google.com/p/guava-libraries/?referer=');">Guava API</a></li>
<li><a href="https://github.com/bbejeck/guava-blog/blob/master/src/test/java/bbejeck/guava/eventbus/EventBusTest.java" target="new" onclick="pageTracker._trackPageview('/outgoing/github.com/bbejeck/guava-blog/blob/master/src/test/java/bbejeck/guava/eventbus/EventBusTest.java?referer=');">Sample Code</a></li>
<li><a href="http://www.martinfowler.com/eaaDev/EventCollaboration.html" target="new" onclick="pageTracker._trackPageview('/outgoing/www.martinfowler.com/eaaDev/EventCollaboration.html?referer=');">Event Collaboration</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://codingjunkie.net/guava-eventbus/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Guava Functions &amp; Java 8 Lambdas</title>
		<link>http://codingjunkie.net/guava-functions-java-8-lambdas/</link>
		<comments>http://codingjunkie.net/guava-functions-java-8-lambdas/#comments</comments>
		<pubDate>Fri, 23 Dec 2011 05:48:58 +0000</pubDate>
		<dc:creator>Bill B</dc:creator>
				<category><![CDATA[Guava]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[Java 8]]></category>
		<category><![CDATA[lamba]]></category>

		<guid isPermaLink="false">http://codingjunkie.net/?p=1512</guid>
		<description><![CDATA[I recently read Brian Goetz&#8217;s The State of the Lambda and after reading that article I wanted to try using Java 8 lambda expressions. In his article, Brian goes on to describe interfaces that have one method as &#8220;functional&#8221; interfaces. Functional interfaces are almost always used as anonymous classes, with the ActionListener being the canonical [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://codingjunkie.net/wp-content/uploads/2011/12/red-lambda.png"><img src="http://codingjunkie.net/wp-content/uploads/2011/12/red-lambda.png" alt="" title="red-lambda" width="68" height="100" class="alignleft size-full wp-image-1514" /></a>I recently read Brian Goetz&#8217;s <a href="http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-4.html" target="new" onclick="pageTracker._trackPageview('/outgoing/cr.openjdk.java.net/_briangoetz/lambda/lambda-state-4.html?referer=');">The State of the Lambda</a> and after reading that article I wanted to try using Java 8 lambda expressions.  In his article, Brian goes on to describe interfaces that have one method as &#8220;functional&#8221; interfaces. Functional interfaces are almost always used as anonymous classes, with the ActionListener being the canonical example.  These &#8220;functional&#8221; interfaces are the initial target for lambda expressions, with the main goal being removing much of the boilerplate or ceremony around their usage.  As I have been writing a series on the Guava library, the Function interface immediately came to mind as a prime candiate for trying lambda expressions.  My goal is simple, take the unit test from my <a href="" target="new">Guava Futures</a>  blog (it makes heavy use of the Function interface) and convert it to use lambda expressions.  While I will describe the structure of the lamba expression as it pertains to the example presented, this post is not a tutorial on Java 8 lambda expressions.  Rather it&#8217;s documenting my first attempts at using lambda expressions in Java.</p>
<h3>Lambda Expressions</h3>
<p>The first example is a test for the Futures.chain method, which takes a Function as one of it&#8217;s arguments:</p>
<pre class="brush:java highlight:[1,2,3,4,5,6,7];">
Function&lt;List&lt;String&gt;, ListenableFuture&lt;List&lt;Person&gt;&gt;&gt; queryFunction =
      new Function&lt;List&lt;String&gt;, ListenableFuture&lt;List&lt;Person&gt;&gt;&gt;() {
            @Override
            public ListenableFuture&lt;List&lt;Person&gt;&gt; apply(final List&lt;String&gt; ids) {
                 return dbService.getPersonsByIdAsync(ids);
            }
        };

ListenableFuture&lt;List&lt;String&gt;&gt; indexSearch =
      luceneSearcher.searchAsync("firstName:martin");
ListenableFuture&lt;List&lt;Person&gt;&gt; results =
      Futures.chain(indexSearch,queryFunction,executorService);
</pre>
<p>Using lamba expressions it would now look like:</p>
<pre class="brush:java highlight:[2];">
Function&lt;List&lt;String&gt;, ListenableFuture&lt;List&lt;Person&gt;&gt;&gt; queryFunction =
      ids -&gt;(dbService.getPersonsByIdAsync(ids));

  ListenableFuture&lt;List&lt;String&gt;&gt; indexSearch =
      luceneSearcher.searchAsync("firstName:martin");
  ListenableFuture&lt;List&lt;Person&gt;&gt;results =
      Futures.chain(indexSearch, queryFunction,executorService);
</pre>
<p>Keep in mind that from the code examples above the two highlighted sections are equivalent.  Let&#8217;s take a look at line 2 and explain how this matches up with lines 1-7 from the first code example</p>
<ol>
<li><code>ids</code> is the input to the apply method and corresponds to the <code> final List&lt;String&gt; ids</code> parameter on line 4 in the first code example.  The types of the lamba expression are being inferred from the context in which it is being used, so we don&#8217;t need to repeat them for the <code>ids</code> parameter.</li>
<li>Then there is the arrow (-&gt;) token which is part of the general syntax of Java 8 lambda expressions in their current form</li>
<li>Then we have the body of the lambda (dbService.getPersonsByIdAsync(ids)) which is a method call that returns a ListenableFuture that in turn yields a List of Person objects.  Note that we did not have to put a return statement, as this is a single expression and is evaluated and returned.</li>
</ol>
<p>The next example is a utility method from the test that returned ListenableFutures by passing anonymous Callable instances into an ExecutorService:</p>
<pre class="brush:java">
private ListenableFuture&lt;List&lt;Person&gt;&gt; getPersonsByFirstNameFuture(final String firstName, final boolean error) {
return executorService.submit(new Callable&lt;List&lt;Person&gt;&gt;() {
            @Override
            public List&lt;Person&gt; call() throws Exception {
                startSignal.await();
                if (error) {
                    throw new RuntimeException("Ooops!");
                }
                List&lt;String&gt; ids = luceneSearcher.search("firstName:" + firstName);
                return dbService.getPersonsById(ids);
            }
        });
}
</pre>
<p>And here is the equivalent using a lambda expression:</p>
<pre class="brush:java">
private ListenableFuture&lt;List&lt;Person&gt;&gt; getPersonsByFirstNameFuture(final String firstName, final boolean error) {
 return executorService.submit(() -&gt; {startSignal.await();
                                       if (error) {
                                        throw new RuntimeException("Ooops!");
                                       }
                                     List&lt;String&gt; ids = luceneSearcher.search("firstName:" + firstName);
                                     return dbService.getPersonsById(ids);
                                  });
}
</pre>
<p>In this example there are no input arguments so the expression starts with empty parentheses () on line 2.  There is the -&gt; token, but in this example, the body contains multiple statements surrounded by { &#8230;}.  Since there are multiple statements an explicit return statement is needed on line 7.</p>
<h3>Environment to Run Java 8</h3>
<p>My current laptop is a MacBook Pro, so I needed to set up an environment to run Java 8 with lambda support.  Here are the steps I took: </p>
<ol>
<li>Installed LinuxMint 12 on VirtualBox.</li>
<li>Created a directory and shared it with the LinuxMint guest</li>
<li>Installed the <a href="http://jdk8.java.net/lambda/" target="new" onclick="pageTracker._trackPageview('/outgoing/jdk8.java.net/lambda/?referer=');">developer preview</a> of java 8.</li>
<li>To get the source and test source from the existing maven project I ran <code>mvn jar:jar jar:test-jar</code> and put the resulting jar files in the shared directory</li>
<li>Placed all the dependencies in the shared directory (guava,lucene,h2 and junit)</li>
<li>Re-wrote the unit test to use lambdas and ran the new test from the command line</li>
</ol>
<h3>Conclusion</h3>
<p>Although it will be some time before Java 8 with lambda support is released, what is available in the developer preview looks promising.Thanks for your time and as always comments and suggestions are welcomed</p>
<h4>Resources</h4>
<ul>
<li><a href="http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-4.html" target="new" onclick="pageTracker._trackPageview('/outgoing/cr.openjdk.java.net/_briangoetz/lambda/lambda-state-4.html?referer=');">State of the Lambda, Part 4</a></li>
<li><a href="http://jdk8.java.net/lambda/" target="new" onclick="pageTracker._trackPageview('/outgoing/jdk8.java.net/lambda/?referer=');">Java 8 </a>developer preview</li>
<li><a href="https://gist.github.com/1513247" target="new" onclick="pageTracker._trackPageview('/outgoing/gist.github.com/1513247?referer=');">Source code</a> for this post</li>
<li><a href="http://code.google.com/p/guava-libraries/" target="new" onclick="pageTracker._trackPageview('/outgoing/code.google.com/p/guava-libraries/?referer=');">Guava Project Home</a></li>
<li><a href="https://github.com/bbejeck/guava-blog" target="new" onclick="pageTracker._trackPageview('/outgoing/github.com/bbejeck/guava-blog?referer=');">Source Code</a> for Guava blog series</li>
]]></content:encoded>
			<wfw:commentRss>http://codingjunkie.net/guava-functions-java-8-lambdas/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Google Guava Cache</title>
		<link>http://codingjunkie.net/google-guava-cache/</link>
		<comments>http://codingjunkie.net/google-guava-cache/#comments</comments>
		<pubDate>Wed, 14 Dec 2011 05:44:37 +0000</pubDate>
		<dc:creator>Bill B</dc:creator>
				<category><![CDATA[Guava]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[cache]]></category>

		<guid isPermaLink="false">http://codingjunkie.net/?p=1438</guid>
		<description><![CDATA[This Post is a continuation of my series on Google Guava, this time covering Guava Cache. Guava Cache offers more flexibility and power than either a HashMap or ConcurrentHashMap, but is not as heavy as using EHCache or Memcached (or robust for that matter, as Guava Cache operates solely in memory). The Cache interface has [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://codingjunkie.net/wp-content/uploads/2011/11/toolbox.jpg"><img src="http://codingjunkie.net/wp-content/uploads/2011/11/toolbox-150x150.jpg" alt="" title="toolbox" width="150" height="150" class="alignleft size-thumbnail wp-image-1130" /></a>This Post is a continuation of my series on Google Guava, this time covering Guava Cache. Guava Cache offers more flexibility and power than either a HashMap or ConcurrentHashMap, but is not as heavy as using EHCache or Memcached (or robust for that matter, as Guava Cache operates solely in memory).  The Cache interface has methods you would expect to see like &#8216;get&#8217;, and &#8216;invalidate&#8217;.  A method you won&#8217;t find is &#8216;put&#8217;, because Guava Cache is &#8216;self-populating&#8217;, values that aren&#8217;t present when requested are fetched or calculated, then stored. This means a &#8216;get&#8217; call will never return null. In all fairness, the previous statement is not %100 accurate.  There is another method &#8216;asMap&#8217; that exposes the entries in the cache as a thread safe map. Using &#8216;asMap&#8217; will result in not having any of the self loading operations performed, so calls to &#8216;get&#8217; will return null if the value is not present (What fun is that?).  Although this is a post about Guava Cache, I am going to spend the bulk of the time talking about CacheLoader and CacheBuilder.  CacheLoader specifies how to load values, and CacheBuilder is used to set the desired features and actually build the cache. </p>
<h3>CacheLoader</h3>
<p><a href="http://docs.guava-libraries.googlecode.com/git-history/v10.0.1/javadoc/com/google/common/cache/CacheLoader.html" target="new" onclick="pageTracker._trackPageview('/outgoing/docs.guava-libraries.googlecode.com/git-history/v10.0.1/javadoc/com/google/common/cache/CacheLoader.html?referer=');">CacheLoader</a> is an abstract class that specifies how to calculate or load values, if not present.  There are two ways to create an instance of a CacheLoader:</p>
<ol>
<li>Extend the CacheLoader&lt;K,V&gt; class</li>
<li>Use the static factory method CacheLoader.from</li>
</ol>
<p>If you extend CacheLoader you need to override the <code> V load(K key)</code> method, instructing how to generate the value for a given key.  Using the static <code>CacheLoader.from</code> method you build a CacheLoader either by supplying a Function or Supplier interface.  When supplying a Function object, the Function is applied to the key to calculate or retrieve the results.  Using a Supplier interface the value is obtained independent of the key. </p>
<h3>CacheBuilder</h3>
<p>The <a href="http://docs.guava-libraries.googlecode.com/git-history/v10.0.1/javadoc/com/google/common/cache/CacheBuilder.html" target="new" onclick="pageTracker._trackPageview('/outgoing/docs.guava-libraries.googlecode.com/git-history/v10.0.1/javadoc/com/google/common/cache/CacheBuilder.html?referer=');">CacheBuilder</a> is used to construct cache instances.  It uses the fluent style of building and gives you the option of setting the following properties on the cache:</p>
<ul>
<li>Cache Size limit (removals use a LRU algorithm)</li>
<li>Wrapping keys in WeakReferences (Strong references used by default for keys)</li>
<li>Wrapping values in either WeakReferences or SoftReferences (Strong references used by default)</li>
<li>Time to expire entires after last access</li>
<li>Time based expiration of entries after being written or updated</li>
<li>Setting a RemovalListener that can recieve events once an entry is removed from the cache</li>
<li>Concurrency Level of the cache (defaults to 4)</li>
</ul>
<p>The concurrency level option is used to partition the table internally such that updates can occur without contention. The ideal setting would be the maximum number of threads that could potentially access the cache at one time.  Here is an example of a possible usage scenario for Guava Cache.</p>
<pre class="brush:java">
public class PersonSearchServiceImpl implements SearchService&lt;List&lt;Person&gt;&gt; {

public PersonSearchServiceImpl(SampleLuceneSearcher luceneSearcher, SampleDBService dbService) {
        this.luceneSearcher = luceneSearcher;
        this.dbService = dbService;
        buildCache();
    }

    @Override
    public List&lt;Person&gt; search(String query) throws Exception {
        return cache.get(query);
    }

    private void buildCache() {
        cache = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES)
                .maximumSize(1000)
                .build(new CacheLoader&lt;String, List&lt;Person&gt;&gt;() {
                    @Override
                    public List&lt;Person&gt; load(String queryKey) throws Exception {
                        List&lt;String&gt; ids = luceneSearcher.search(queryKey);
                        return dbService.getPersonsById(ids);
                    }
                });
    }
}
</pre>
<p>In this example, I am setting the cache entries to expire after 10 minutes of being written or updated in the cache, with a maximum amount of 1,000 entires. Note the usage of CacheLoader on line 15.</p>
<h3>RemovalListener</h3>
<p>The <a href="http://docs.guava-libraries.googlecode.com/git-history/v10.0.1/javadoc/com/google/common/cache/RemovalListener.html" target="new" onclick="pageTracker._trackPageview('/outgoing/docs.guava-libraries.googlecode.com/git-history/v10.0.1/javadoc/com/google/common/cache/RemovalListener.html?referer=');">RemovalListener</a> will receive notification of an item being removed from the cache.  These notifications could be from manual invalidations or from a automatic one due to time expiration or garbage collection.  The RemovalListener&lt;K,V&gt; parameters can be set to listen for specific type.  To receive notifications for any key or value set them to use Object.  It should be noted here that a RemovalListener will receive a RemovalNotification&lt;K,V&gt; object that implements the Map.Entry interface.  The key or value could be null if either has already been garbage collected.  Also the key and value object will be strong references, regardless of the type of references used by the cache.</p>
<h3>CacheStats</h3>
<p>There is also a very useful class <a href="http://docs.guava-libraries.googlecode.com/git-history/v10.0.1/javadoc/com/google/common/cache/CacheStats.html" target="new" onclick="pageTracker._trackPageview('/outgoing/docs.guava-libraries.googlecode.com/git-history/v10.0.1/javadoc/com/google/common/cache/CacheStats.html?referer=');">CacheStats</a> that can be retrieved via a call to Cache.stats().  The CacheStats object can give<br />
insight into the effectiveness and performance of your cache by providing statistics such as:</p>
<ul>
<li>hit count</li>
<li>miss count</li>
<li>total load timme</li>
<li>total requests</li>
</ul>
<p>CacheStats provides many other counts in addition to the ones listed above.</p>
<h3>Conclusion</h3>
<p>The Guava Cache presents some very compelling functionality.  The decision to use a Guava Cache really comes down to the tradeoff between memory availability/usage versus increases in performance.  I have added a unit test <a href="https://github.com/bbejeck/guava-blog/blob/master/src/test/java/bbejeck/guava/cache/CacheTest.java" target="new" onclick="pageTracker._trackPageview('/outgoing/github.com/bbejeck/guava-blog/blob/master/src/test/java/bbejeck/guava/cache/CacheTest.java?referer=');">CacheTest</a> demonstrating the usages discussed here. As alway comments and suggestions are welcomed. Thanks for your time.</p>
<h4>Resources</h4>
<ul>
<li><a href="http://code.google.com/p/guava-libraries/" target="new" onclick="pageTracker._trackPageview('/outgoing/code.google.com/p/guava-libraries/?referer=');">Guava Project Home</a></li>
<li><a href="http://docs.guava-libraries.googlecode.com/git-history/v10.0.1/javadoc/com/google/common/cache/package-summary.html" target="new" onclick="pageTracker._trackPageview('/outgoing/docs.guava-libraries.googlecode.com/git-history/v10.0.1/javadoc/com/google/common/cache/package-summary.html?referer=');">Cache API</a></li>
<li><a href="https://github.com/bbejeck/guava-blog" target="new" onclick="pageTracker._trackPageview('/outgoing/github.com/bbejeck/guava-blog?referer=');">Source Code</a> for blog series</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://codingjunkie.net/google-guava-cache/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Simple WordPress Backups</title>
		<link>http://codingjunkie.net/simple-wordpress-backups/</link>
		<comments>http://codingjunkie.net/simple-wordpress-backups/#comments</comments>
		<pubDate>Wed, 07 Dec 2011 04:36:16 +0000</pubDate>
		<dc:creator>Bill B</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://codingjunkie.net/?p=1404</guid>
		<description><![CDATA[Backing up your data is an important task. As we all know, it&#8217;s not a matter of if you are going to experience a crash or failure, but when. Blogs are no exception. I wanted to take a break from my regular style of posts to share my simple backup script. While I&#8217;m know there [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://codingjunkie.net/wp-content/uploads/2011/12/black-drive-backup-256x256.png"><img src="http://codingjunkie.net/wp-content/uploads/2011/12/black-drive-backup-256x256-150x150.png" alt="" title="black-drive-backup-256x256" width="150" height="150" class="alignleft size-thumbnail wp-image-1405" /></a>Backing up your data is an important task.  As we all know, it&#8217;s not a matter of if you are going to experience a crash or failure, but when.  Blogs are no exception.  I wanted to take a break from my regular style of posts to share my simple backup script.  While I&#8217;m know there ar more sophisticated approaches, I really enjoy automating tasks, like remote backups, with bash scripts. My approach is simple. I create a gzipped tar of my WordPress install directory, and then run a <a href="http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html" target="new" onclick="pageTracker._trackPageview('/outgoing/dev.mysql.com/doc/refman/5.1/en/mysqldump.html?referer=');">mysqldump</a> command.  It&#8217;s all done locally from my MacBook Pro via ssh and the results end up in my Dropbox directory.  </p>
<h3>Setting up Password-less SSH</h3>
<p>While this might be obvious, it is very helpful to set up your account to use password-less logins via public keys.  If you have not generated your private/public ssh keys, you will need to open up a terminal session on your local computer and from the command line type:</p>
<pre class="brush: bash; title: ; notranslate">ssh-keygen -t rsa</pre>
<p> After you run this command you will be prompted to:</p>
<ol>
<li>Enter the file where you want to store the key, hit return to accept the default (~/.ssh/id_rsa)</li>
<li>Enter a passphrase.  It&#8217;s important to just hit return, then return again when asked to confirm the empty passphrase.</li>
</ol>
<p>You now have a private key, id_rsa and a public key, id_rsa.pub in the ~/.ssh directory.  It is very important that you do not share your private key with anyone. The next step is to copy the public key, id_rsa.pub to the ~/.ssh/authorized_keys file on the remote server.  Run the following from the terminal:</p>
<pre class="brush:bash">
ssh USERNAME@HOST cat < ~/.ssh/id_rsa.pub ">>" ~/.ssh/authorized_keys
</pre>
<p>With that step completed, it should possible to run remote commands via ssh without being prompted for a password.</p>
<h3>Running the Backup</h3>
<p>Now it&#8217;s time to run the backup script:</p>
<pre class="brush:bash">
#! /bin/sh

. backup_config.sh

NOW=$(date +"%m%d%Y")
FILENAME="${BLOG}_${NOW}.tar.gz"
DATABASE_BAK="Database_${BLOG}_${NOW}.gz"

echo "Running remote tar backup of blog directory"
ssh $USER@$IP_ADDRESS "cd $BASE_DIR &#038;&#038; tar -zcf ~/$FILENAME ."

echo "Begin remote copy"; scp $USER@$IP_ADDRESS:~/$FILENAME $DEST_DIR \
&#038;&#038; echo "Remote copy successful, removing backup on server"; ssh $USER@$IP_ADDRESS "rm ~/$FILENAME"

echo "Starting Mysql dump"
ssh $USER@$IP_ADDRESS "mysqldump -u$DBUSER -p$PASS $DATABASE | gzip -c" >  $DEST_DIR/$DATABASE_BAK
echo "Mysql dump completed"

echo "Created backup files:"
ls -la  $DEST_DIR | grep $NOW
</pre>
<p>Let&#8217;s go over the interesting parts of the backup script:</p>
<ol>
<li>Line 3 sources the backup_config.sh file that sets all the variables.</li>
<li>Line 10 changes into the specified directory then makes a gzip compressed tar file of it&#8217;s contents, placing the resulting file in the home directory</li>
<li>Line 12 then copies the tar file from the remote server to your local machine.  If the copy is successful, the remote tar file is deleted</li>
<li>Line 16 runs a mysqldump commmand (the contents and structure of the database) compressing the output with gzip then sends the contents over the wire to the locally specified database backup file.
</ol>
<p>As final convenience, I set an alias to the script so I am always just one short command away from running a backup.</p>
<h3>Conclusion</h3>
<p>So that is my basic, but effective backup script.  The script has lots of room for improvement:</p>
<ul>
<li>There is no error handling.</li>
<li>Copying the entire WordPress install is a little heavy.</li>
<li>Automatic removal of old backups should be included.</li>
</ul>
<p>If you want to try this out for yourself just edit the backup_config.sh file with the appropriate values. Hopefully others will be able to benefit from this simple backup script as well.</p>
<h4>Resources</h4>
<p>All sources for this post can be found at this <a href="https://gist.github.com/1441472" target="new" onclick="pageTracker._trackPageview('/outgoing/gist.github.com/1441472?referer=');">gist</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://codingjunkie.net/simple-wordpress-backups/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 0.813 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-05-18 13:53:46 -->
<!-- Compression = gzip -->
