/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.creadur.tentacles;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @version $Rev$ $Date$
 */
public class Files {

    public static List<File> collect(final File dir, final String regex) {
        return collect(dir, Pattern.compile(regex));
    }

    public static List<File> collect(final File dir, final Pattern pattern) {
        return collect(dir, new FileFilter() {
            @Override
            public boolean accept(File file) {
                return pattern.matcher(file.getAbsolutePath()).matches();
            }
        });
    }


    public static List<File> collect(File dir, FileFilter filter) {
        final List<File> accepted = new ArrayList<File>();
        if (filter.accept(dir)) accepted.add(dir);

        final File[] files = dir.listFiles();
        if (files != null) for (File file : files) {
            accepted.addAll(collect(file, filter));
        }

        return accepted;
    }

    public static void exists(File file, String s) {
        if (!file.exists()) throw new RuntimeException(s + " does not exist: " + file.getAbsolutePath());
    }

    public static void dir(File file) {
        if (!file.isDirectory()) throw new RuntimeException("Not a directory: " + file.getAbsolutePath());
    }

    public static void file(File file) {
        if (!file.isFile()) throw new RuntimeException("Not a file: " + file.getAbsolutePath());
    }

    public static void writable(File file) {
        if (!file.canWrite()) throw new RuntimeException("Not writable: " + file.getAbsolutePath());
    }

    public static void readable(File file) {
        if (!file.canRead()) throw new RuntimeException("Not readable: " + file.getAbsolutePath());
    }

    public static void mkdir(File file) {
        if (file.exists()) return;
        if (!file.mkdirs()) throw new RuntimeException("Cannot mkdir: " + file.getAbsolutePath());
    }

    public static void mkparent(File file) {
        mkdirs(file.getParentFile());
    }

    public static void mkdirs(File file) {

        if (!file.exists()) {

            assert file.mkdirs() : "mkdirs " + file;

            return;
        }

        assert file.isDirectory() : "not a directory" + file;
    }
}

@@ -1158,37 +1158,38 @@
 ));%0A    %7D%0A%0A    p
-ublic
+rivate
  static List%3CFil
@@ -1343,16 +1343,22 @@
  accept(
+final 
 File fil
@@ -1469,17 +1469,16 @@
 %0A    %7D%0A%0A
-%0A
     publ
@@ -1506,16 +1506,22 @@
 collect(
+final 
 File dir
@@ -1521,16 +1521,22 @@
 ile dir,
+ final
  FileFil
@@ -1639,16 +1639,30 @@
 pt(dir))
+ %7B%0A           
  accepte
@@ -1672,16 +1672,26 @@
 dd(dir);
+%0A        %7D
 %0A%0A      
@@ -1761,13 +1761,33 @@
 ll) 
-for (
+%7B%0A            for (final 
 File
@@ -1799,24 +1799,28 @@
  : files) %7B%0A
+    
             
@@ -1855,24 +1855,38 @@
 , filter));%0A
+            %7D%0A
         %7D%0A%0A 
@@ -1943,139 +1943,69 @@
 oid 
-exists(File file, String s) %7B%0A        if (!file.exists()) throw new RuntimeException(s + %22 does not exist: %22 + file.getAbsolutePath
+mkparent(final File file) %7B%0A        mkdirs(file.getParentFile
 ());
@@ -2035,20 +2035,29 @@
 ic void 
+mk
 dir
-(
+s(final 
 File fil
@@ -2053,32 +2053,33 @@
 al File file) %7B%0A
+%0A
         if (!fil
@@ -2084,986 +2084,223 @@
 ile.
-isDirectory()) throw new RuntimeException(%22Not a directory: %22 + file.getAbsolutePath());%0A    %7D%0A%0A    public static void file(File file) %7B%0A        if (!file.isFile()) throw new RuntimeException(%22Not a file: %22 + file.getAbsolutePath());%0A    %7D%0A%0A    public static void writable(File file) %7B%0A        if (!file.canWrite()) throw new RuntimeException(%22Not writable: %22 + file.getAbsolutePath()
+exists()) %7B%0A%0A            final boolean success = file.mkdirs(
 );%0A    
-%7D%0A%0A
     
-public static void readable(File file) %7B%0A        if (!file.canRead()) throw new RuntimeException(%22Not readable: %22 + file.getAbsolutePath());%0A    %7D%0A%0A    public static void mkdir(File file) %7B%0A        if (file.exists()) return;%0A        if (!file.mkdirs()) throw new RuntimeException(%22Cannot mkdir: %22 + file.getAbsolutePath());%0A    %7D%0A%0A    public static void mkparent(File file) %7B%0A        mkdirs(file.getParentFile());%0A    %7D%0A%0A    public static void mkdirs(File file) %7B%0A%0A        if (!file.exists()) %7B%0A%0A            assert file.mkdirs() : %22mkdirs %22 + file;%0A%0A            return;%0A        %7D%0A
+    assert success : %22mkdirs failed to create %22 + file;%0A%0A            return;%0A        %7D%0A%0A        final boolean isDirectory = file.isDirectory();
 %0A   
@@ -2311,21 +2311,16 @@
  assert 
-file.
 isDirect
@@ -2326,15 +2326,13 @@
 tory
-()
  : %22
-n
+N
 ot a
@@ -2341,16 +2341,18 @@
 irectory
+: 
 %22 + file

Tidy up and fix assertions