[prev in list] [next in list] [prev in thread] [next in thread] 

List:       jakarta-commons-dev
Subject:    svn commit: r721744 - in /commons/proper/io/trunk/src:
From:       niallp () apache ! org
Date:       2008-11-30 0:01:37
Message-ID: 20081130000137.98EBD23888A2 () eris ! apache ! org
[Download RAW message or body]

Author: niallp
Date: Sat Nov 29 16:01:36 2008
New Revision: 721744

URL: http://svn.apache.org/viewvc?rev=721744&view=rev
Log:
IO-186 new Composite and DIrectory File Comparator implementations

Added:
    commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/CompositeFileComparator.java \
(with props)  commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/DirectoryFileComparator.java \
(with props)  commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/CompositeFileComparatorTest.java \
(with props)  commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/DirectoryFileComparatorTest.java \
(with props)

Added: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/CompositeFileComparator.java
                
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/CompositeFileComparator.java?rev=721744&view=auto
 ==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/CompositeFileComparator.java \
                (added)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/CompositeFileComparator.java \
Sat Nov 29 16:01:36 2008 @@ -0,0 +1,114 @@
+/*
+ * 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.commons.io.comparator;
+
+import java.io.File;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+
+/**
+ * Compare two files using a set of delegate file {@link Comparator}.
+ * <p>
+ * This comparator can be used to sort lists or arrays of files
+ * by combining a number other comparators.
+ * <p>
+ * Example of sorting a list of files by type (i.e. directory or file)
+ * and then by name:
+ * <pre>
+ *       CompositeFileComparator comparator =
+ *                       new CompositeFileComparator(
+ *                                   DirectoryFileComparator.DIRECTORY_COMPARATOR,
+ *                                   NameFileComparator.NAME_COMPARATOR);
+ *       List&lt;File&gt; list = ...
+ *       comparator.sort(list);
+ * </pre>
+ *
+ * @version $Revision$ $Date$
+ * @since Commons IO 2.0
+ */
+public class CompositeFileComparator extends AbstractFileComparator implements \
Serializable { +
+    private final Comparator<File>[] delegates;
+
+    /**
+     * Create a composite comparator for the set of delegate comparators.
+     *
+     * @param delegates The delegate file comparators
+     */
+    public CompositeFileComparator(Comparator<File>... delegates) {
+        int size = (delegates == null ? 0 : delegates.length);
+        this.delegates = new Comparator[size];
+        for (int i = 0; i < size; i++) {
+            this.delegates[i] = delegates[i];
+        }
+    }
+
+    /**
+     * Create a composite comparator for the set of delegate comparators.
+     *
+     * @param delegates The delegate file comparators
+     */
+    public CompositeFileComparator(Iterable<Comparator<File>> delegates) {
+        List<Comparator<File>> list = new ArrayList<Comparator<File>>();
+        if (delegates != null) {
+            for (Comparator<File> comparator : delegates) {
+                list.add(comparator);
+            }
+        }
+        this.delegates = list.toArray(new Comparator[list.size()]);
+    }
+
+    /**
+     * Compare the two files using delegate comparators.
+     * 
+     * @param file1 The first file to compare
+     * @param file2 The second file to compare
+     * @return the first non-zero result returned from
+     * the delegate comparators or zero.
+     */
+    public int compare(File file1, File file2) {
+        int result = 0;
+        for (int i = 0; i < delegates.length; i++) {
+            result = delegates[i].compare(file1, file2);
+            if (result != 0) {
+                break;
+            }
+        }
+        return result;
+    }
+
+    /**
+     * String representation of this file comparator.
+     *
+     * @return String representation of this file comparator
+     */
+    public String toString() {
+        StringBuilder builder = new StringBuilder();
+        builder.append(super.toString());
+        builder.append('{');
+        for (int i = 0; i < delegates.length; i++) {
+            if (i > 0) {
+                builder.append(',');
+            }
+            builder.append(delegates[i]);
+        }
+        builder.append('}');
+        return builder.toString();
+    }
+}

Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/CompositeFileComparator.java
                
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/CompositeFileComparator.java
                
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision

Added: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/DirectoryFileComparator.java
                
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/DirectoryFileComparator.java?rev=721744&view=auto
 ==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/DirectoryFileComparator.java \
                (added)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/DirectoryFileComparator.java \
Sat Nov 29 16:01:36 2008 @@ -0,0 +1,79 @@
+/*
+ * 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.commons.io.comparator;
+
+import java.io.File;
+import java.io.Serializable;
+import java.util.Comparator;
+
+/**
+ * Compare two files using the <b>default</b> {@link File#isDirectory()} method.
+ * <p>
+ * This comparator can be used to sort lists or arrays by directories and files.
+ * <p>
+ * Example of sorting a list of files/directories using the
+ * {@link #DIRECTORY_COMPARATOR} singleton instance:
+ * <pre>
+ *       List&lt;File&gt; list = ...
+ *       DirectoryFileComparator.DIRECTORY_COMPARATOR.sort(list);
+ * </pre>
+ * <p>
+ * Example of doing a <i>reverse</i> sort of an array of files/directories using the
+ * {@link #DEFAULT_REVERSE} singleton instance:
+ * <pre>
+ *       File[] array = ...
+ *       DirectoryFileComparator.DIRECTORY_REVERSE.sort(array);
+ * </pre>
+ * <p>
+ *
+ * @version $Revision$ $Date$
+ * @since Commons IO 2.0
+ */
+public class DirectoryFileComparator extends AbstractFileComparator implements \
Serializable { +
+    /** Singleton default comparator instance */
+    public static final Comparator<File> DIRECTORY_COMPARATOR = new \
DirectoryFileComparator(); +
+    /** Singleton reverse default comparator instance */
+    public static final Comparator<File> DIRECTORY_REVERSE = new \
ReverseComparator(DIRECTORY_COMPARATOR); +
+    /**
+     * Compare the two files using the {@link File#isDirectory()} method.
+     * 
+     * @param file1 The first file to compare
+     * @param file2 The second file to compare
+     * @return the result of calling file1's
+     * {@link File#compareTo(File)} with file2 as the parameter.
+     */
+    public int compare(File file1, File file2) {
+        return (getType(file1) - getType(file2));
+    }
+
+    /**
+     * Convert type to numeric value.
+     *
+     * @param file The file
+     * @return 1 for directories and 2 for files
+     */
+    private int getType(File file) {
+        if (file.isDirectory()) {
+            return 1;
+        } else {
+            return 2;
+        }
+    }
+}

Propchange: commons/proper/io/trunk/src/java/org/apache/commons/io/comparator/DirectoryFileComparator.java
                
------------------------------------------------------------------------------
    svn:mergeinfo = 

Added: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/CompositeFileComparatorTest.java
                
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/CompositeFileComparatorTest.java?rev=721744&view=auto
 ==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/CompositeFileComparatorTest.java \
                (added)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/CompositeFileComparatorTest.java \
Sat Nov 29 16:01:36 2008 @@ -0,0 +1,110 @@
+/*
+ * 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.commons.io.comparator;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+/**
+ * Test case for {@link CompositeFileComparator}.
+ */
+public class CompositeFileComparatorTest extends ComparatorAbstractTestCase {
+
+    /**
+     * Run the test.
+     *
+     * @param args arguments
+     */
+    public static void main(String[] args) {
+        TestRunner.run(suite());
+    }
+
+    /**
+     * Create a test suite.
+     *
+     * @return The test suite
+     */
+    public static Test suite() {
+        return new TestSuite(CompositeFileComparatorTest.class);
+    }
+
+    /**
+     * Construct a new test case with the specified name.
+     *
+     * @param name Name of the test
+     */
+    public CompositeFileComparatorTest(String name) {
+        super(name);
+    }
+
+    /** @see junit.framework.TestCase#setUp() */
+    protected void setUp() throws Exception {
+        super.setUp();
+        comparator = new CompositeFileComparator(SizeFileComparator.SIZE_COMPARATOR,
+                                                 \
ExtensionFileComparator.EXTENSION_COMPARATOR); +        reverse = new \
ReverseComparator(comparator); +        File dir = getTestDirectory();
+        lessFile   = new File(dir, "xyz.txt");
+        equalFile1 = new File(dir, "foo.txt");
+        equalFile2 = new File(dir, "bar.txt");
+        moreFile   = new File(dir, "foo.xyz");
+        createFile(lessFile,   32);
+        createFile(equalFile1, 48);
+        createFile(equalFile2, 48);
+        createFile(moreFile,   48);
+    }
+
+    /**
+     * Test Constructor with null Iterable
+     */
+    public void testConstructorIterable() {
+        List<Comparator<File>> list = new ArrayList<Comparator<File>>();
+        list.add(SizeFileComparator.SIZE_COMPARATOR);
+        list.add(ExtensionFileComparator.EXTENSION_COMPARATOR);
+        Comparator<File> c = new CompositeFileComparator(list);
+
+        assertTrue("equal", c.compare(equalFile1, equalFile2) == 0);
+        assertTrue("less",  c.compare(lessFile, moreFile) < 0);
+        assertTrue("more",  c.compare(moreFile, lessFile) > 0);
+    }
+
+    /**
+     * Test Constructor with null Iterable
+     */
+    public void testConstructorIterableNull() {
+        Comparator<File> c = new CompositeFileComparator((Iterable)null);
+        assertEquals("less,more", 0, c.compare(lessFile, moreFile));
+        assertEquals("more,less", 0, c.compare(moreFile, lessFile));
+        assertEquals("toString", "CompositeFileComparator{}", c.toString());
+    }
+
+    /**
+     * Test Constructor with null array
+     */
+    public void testConstructorArrayNull() {
+        Comparator<File> c = new CompositeFileComparator((Comparator[])null);
+        assertEquals("less,more", 0, c.compare(lessFile, moreFile));
+        assertEquals("more,less", 0, c.compare(moreFile, lessFile));
+        assertEquals("toString", "CompositeFileComparator{}", c.toString());
+    }
+}

Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/CompositeFileComparatorTest.java
                
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/CompositeFileComparatorTest.java
                
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision

Added: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/DirectoryFileComparatorTest.java
                
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/DirectoryFileComparatorTest.java?rev=721744&view=auto
 ==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/DirectoryFileComparatorTest.java \
                (added)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/DirectoryFileComparatorTest.java \
Sat Nov 29 16:01:36 2008 @@ -0,0 +1,85 @@
+/*
+ * 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.commons.io.comparator;
+
+import java.io.File;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+/**
+ * Test case for {@link DirectoryFileComparator}.
+ */
+public class DirectoryFileComparatorTest extends ComparatorAbstractTestCase {
+
+    /**
+     * Run the test.
+     *
+     * @param args arguments
+     */
+    public static void main(String[] args) {
+        TestRunner.run(suite());
+    }
+
+    /**
+     * Create a test suite.
+     *
+     * @return The test suite
+     */
+    public static Test suite() {
+        return new TestSuite(DirectoryFileComparatorTest.class);
+    }
+
+    /**
+     * Construct a new test case with the specified name.
+     *
+     * @param name Name of the test
+     */
+    public DirectoryFileComparatorTest(String name) {
+        super(name);
+    }
+
+    /** @see junit.framework.TestCase#setUp() */
+    protected void setUp() throws Exception {
+        super.setUp();
+        comparator = \
(AbstractFileComparator)DirectoryFileComparator.DIRECTORY_COMPARATOR; +        \
reverse = DirectoryFileComparator.DIRECTORY_REVERSE; +        File currentDir = new \
File("."); +        equalFile1 = new File(currentDir, "src");
+        equalFile2 = new File(currentDir, "xdocs");
+        lessFile   = new File(currentDir, "src");
+        moreFile   = new File(currentDir, "pom.xml");
+    }
+
+    /**
+     * Test the comparator array sort.
+     */
+    @Override
+    public void testSortArray() {
+        // skip sort test
+    }
+
+    /**
+     * Test the comparator array sort.
+     */
+    @Override
+    public void testSortList() {
+        // skip sort test
+    }
+}
+

Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/DirectoryFileComparatorTest.java
                
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/io/trunk/src/test/org/apache/commons/io/comparator/DirectoryFileComparatorTest.java
                
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic