Convert URL to URI using lambdaj

import ch.lambdaj.function.convert.Converter;
 
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
 
public class ConvertUrlToUri implements Converter<URL, URI>
{
   public static ConvertUrlToUri convertUrlToUri()
   {
      return new ConvertUrlToUri();
   }
 
   @Override
   public URI convert(final URL from)
   {
      try
      {
         return from.toURI();
      }
      catch (final URISyntaxException e)
      {
         throw new RuntimeException("unable to convert " + from + " to URI", e);
      }
   }
}

Unzip with Apache VFS 2.0 (Java)

Apache VFS 2.0 is strange but super powerful. Uncompress a file at a given URI to a given output location:

<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-vfs2</artifactId>
   <version>2.0</version>
</dependency>
import java.io.File;
import java.io.IOException;
import java.net.URI;
 
import org.apache.commons.vfs2.AllFileSelector;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.VFS;
 
public class ZipfileUnpacker
{
   private final FileSystemManager fileSystemManager;
   private final URI packLocation;
 
   public ZipfileUnpacker(final URI packLocation) throws FileSystemException
   {
      this.fileSystemManager = VFS.getManager();
      this.packLocation = packLocation;
   }
 
   @Override
   public void unpack(final File outputDir) throws IOException
   {
      outputDir.mkdirs();
 
      final FileObject packFileObject = fileSystemManager.resolveFile(packLocation.toString());
      try
      {
         final FileObject zipFileSystem = fileSystemManager.createFileSystem(packFileObject);
         try
         {
            fileSystemManager.toFileObject(outputDir).copyFrom(zipFileSystem, new AllFileSelector());
         }
         finally
         {
            zipFileSystem.close();
         }
      }
      finally
      {
         packFileObject.close();
      }
   }
}

Hamcrest regexp matcher

Matches a string against a regular expression, inspired by Piotr Gabryanczyk with a couple of issues fixed.

import java.util.regex.Pattern;
 
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
 
/**
 * Matchers that use regular expressions
 *
 * @author t.wood
 */
public class RegexMatcher
{
   private static abstract class AbstractRegexpMatcher extends TypeSafeMatcher<String>
   {
      protected final String regex;
      protected final Pattern compiledRegex;
 
      private AbstractRegexpMatcher(final String regex)
      {
         this.regex = regex;
         compiledRegex = Pattern.compile(regex);
      }
   }
 
   private static class MatchesRegexpMatcher extends AbstractRegexpMatcher
   {
      private MatchesRegexpMatcher(final String regex)
      {
         super(regex);
      }
 
      @Override
      public boolean matchesSafely(final String item)
      {
         return compiledRegex.matcher(item).matches();
      }
 
      @Override
      public void describeTo(final Description description)
      {
         description.appendText("matches regex ").appendValue(regex);
      }
   }
 
   private static class ContainsMatchRegexpMatcher extends AbstractRegexpMatcher
   {
      private ContainsMatchRegexpMatcher(final String regex)
      {
         super(regex);
      }
 
      @Override
      public boolean matchesSafely(final String item)
      {
         return compiledRegex.matcher(item).find();
      }
 
      @Override
      public void describeTo(final Description description)
      {
         description.appendText("contains match for regex ").appendValue(regex);
      }
   }
 
   /**
    * Match the regexp against the whole input string
    *
    * @param regex the regular expression to match
    *
    * @return a matcher which matches the whole input string
    */
   public static Matcher<String> matches(final String regex)
   {
      return new MatchesRegexpMatcher(regex);
   }
 
   /**
    * Match the regexp against any substring of the input string
    *
    * @param regex the regular expression to match
    *
    * @return a matcher which matches anywhere in the input string
    */
   public static Matcher<String> containsMatch(final String regex)
   {
      return new ContainsMatchRegexpMatcher(regex);
   }
}