From f0898f8703dce60fc07c22b68c63c55bf9c36416 Mon Sep 17 00:00:00 2001 From: ribeaudc <ribeaudc> Date: Mon, 26 May 2008 07:16:09 +0000 Subject: [PATCH] remove: - 'RegexFileFilter' - use one of the Apache commons implementations. change: - Instantiate the file filter in the 'LocalProcessor' constructor. minor: - Comments reformatted. SVN: 6302 --- .../common/utilities/RegexFileFilter.java | 176 ---------------- .../common/utilities/RegexFileFilterTest.java | 195 ------------------ 2 files changed, 371 deletions(-) delete mode 100644 common/source/java/ch/systemsx/cisd/common/utilities/RegexFileFilter.java delete mode 100644 common/sourceTest/java/ch/systemsx/cisd/common/utilities/RegexFileFilterTest.java diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/RegexFileFilter.java b/common/source/java/ch/systemsx/cisd/common/utilities/RegexFileFilter.java deleted file mode 100644 index 8bb6e94e78e..00000000000 --- a/common/source/java/ch/systemsx/cisd/common/utilities/RegexFileFilter.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright 2007 ETH Zuerich, CISD - * - * Licensed 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 ch.systemsx.cisd.common.utilities; - -import java.io.File; -import java.io.FileFilter; -import java.util.HashSet; -import java.util.Set; -import java.util.regex.Pattern; - -/** - * A {@link FileFilter} based on a set of path patterns. A path pattern consists of a - * {@link PathType} and a regular expression matching (the entire region of) the name of the path - * entry as returned by {@link File#getName()}. If any path pattern of the set matches a path - * entry, the filter will accept the path entry. - * - * @author Bernd Rinn - */ -public class RegexFileFilter implements FileFilter -{ - - /** A type that complements the regular expression when matching paths. */ - public enum PathType - { - /** Only files will match. */ - FILE, - /** Only directories will match. */ - DIRECTORY, - /** Both files and directories will match. */ - ALL - } - - /** - * A class specifying a pattern for files, combining a regular expression for the file name and - * a file type. - */ - private static class PathPattern - { - private final PathType type; - - private final Pattern pattern; - - PathPattern(PathType type, Pattern pattern) - { - assert type != null; - assert pattern != null; - - this.type = type; - this.pattern = pattern; - } - - /** - * @return <code>true</code> if the <var>file</var> matches the this path pattern. - */ - boolean matches(File file) - { - return matchesType(file) && pattern.matcher(file.getName()).matches(); - } - - private boolean matchesType(File file) - { - if (PathType.ALL.equals(type)) - { - return true; - } - if (PathType.FILE.equals(type) && file.isFile()) - { - return true; - } - if (PathType.DIRECTORY.equals(type) && file.isDirectory()) - { - return true; - } - return false; - } - - @Override - public boolean equals(Object obj) - { - if (obj == null) - { - return false; - } - if (obj instanceof PathPattern == false) - { - return false; - } - final PathPattern that = (PathPattern) obj; - return this.type.equals(that.type) - && this.pattern.pattern().equals(that.pattern.pattern()); - } - - @Override - public int hashCode() - { - return (17 * 59 + type.hashCode()) * 59 + pattern.pattern().hashCode(); - } - - } - - private final Set<PathPattern> pathPatternSet = new HashSet<PathPattern>(); - - public RegexFileFilter() - { - } - - /** - * A convenience constructor that adds one path pattern to the set of patterns. - * - * @see #add(PathType, String) - */ - public RegexFileFilter(PathType type, String regex) - { - add(type, regex); - } - - /** - * Adds a path pattern to this filter. Multiple path pattern can be added by calling this method - * several times. If any path pattern matches a file, the file will be accepted by this filter. - * - * @param type The type of path that a path entry has to be of in order to be accepted by this - * filter. - * @param regexPattern The regular expression pattern that the name of the file (as returned by - * {@link File#getName()} has to match in order to be accepted by this filter. - */ - public void add(PathType type, String regexPattern) - { - assert regexPattern != null; - - add(type, Pattern.compile(regexPattern)); - } - - /** - * Adds a path pattern to this filter. Multiple path pattern can be added by calling this method - * several times. If any path pattern matches a file, the file will be accepted by this filter. - * - * @param type The type of path that a path entry has to be of in order to be accepted by this - * filter. - * @param regex The regular expression that the name of the file (as returned by - * {@link File#getName()} has to match in order to be accepted by this filter. - */ - public void add(PathType type, Pattern regex) - { - assert type != null; - assert regex != null; - - pathPatternSet.add(new PathPattern(type, regex)); - } - - public boolean accept(File pathname) - { - for (PathPattern pattern : pathPatternSet) - { - if (pattern.matches(pathname)) - { - return true; - } - } - return false; - } - -} diff --git a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/RegexFileFilterTest.java b/common/sourceTest/java/ch/systemsx/cisd/common/utilities/RegexFileFilterTest.java deleted file mode 100644 index 1639ec63f7f..00000000000 --- a/common/sourceTest/java/ch/systemsx/cisd/common/utilities/RegexFileFilterTest.java +++ /dev/null @@ -1,195 +0,0 @@ -/* - * Copyright 2007 ETH Zuerich, CISD - * - * Licensed 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 ch.systemsx.cisd.common.utilities; - -import static org.testng.AssertJUnit.assertEquals; - -import java.io.File; -import java.io.IOException; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - -import org.testng.annotations.BeforeClass; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import ch.systemsx.cisd.common.logging.LogInitializer; -import ch.systemsx.cisd.common.utilities.FileUtilities; -import ch.systemsx.cisd.common.utilities.RegexFileFilter; -import ch.systemsx.cisd.common.utilities.RegexFileFilter.PathType; - -/** - * Test cases for the {@link RegexFileFilter}. - * - * @author Bernd Rinn - */ -public class RegexFileFilterTest -{ - - private static final File unitTestRootDirectory = - new File("targets" + File.separator + "unit-test-wd"); - - private static final File workingDirectory = - new File(unitTestRootDirectory, "RegexFileFilterTestTest"); - - @BeforeClass - public void init() - { - LogInitializer.init(); - unitTestRootDirectory.mkdirs(); - assert unitTestRootDirectory.isDirectory(); - } - - @BeforeMethod - public void setUp() - { - FileUtilities.deleteRecursively(workingDirectory); - workingDirectory.mkdirs(); - workingDirectory.deleteOnExit(); - } - - @Test - public void testEmpty() throws IOException - { - createFile("aaa"); - createFile("aba"); - createFile("baa"); - createFile("bba"); - createDirectory("bab"); - Set<File> actual = - new HashSet<File>(Arrays.asList(workingDirectory.listFiles(new RegexFileFilter()))); - assert actual.isEmpty(); - } - - @Test - public void testFileMatching() throws IOException - { - final File f1 = createFile("aaa"); - final File f2 = createFile("aba"); - final File f3 = createFile("baa"); - final File f4 = createFile("bba"); - createDirectory("bab"); - Set<File> actual = - new HashSet<File>(Arrays.asList(workingDirectory.listFiles(new RegexFileFilter( - PathType.FILE, "a.+")))); - Set<File> expected = new HashSet<File>(Arrays.asList(f1, f2)); - assertEquals(expected, actual); - - actual = - new HashSet<File>(Arrays.asList(workingDirectory.listFiles(new RegexFileFilter( - PathType.FILE, ".a.")))); - expected = new HashSet<File>(Arrays.asList(f1, f3)); - assertEquals(expected, actual); - - actual = - new HashSet<File>(Arrays.asList(workingDirectory.listFiles(new RegexFileFilter( - PathType.FILE, ".+a")))); - expected = new HashSet<File>(Arrays.asList(f1, f2, f3, f4)); - assertEquals(expected, actual); - } - - @Test - public void testDirectoryMatching() throws IOException - { - final File d1 = createDirectory("aaa"); - final File d2 = createDirectory("aba"); - final File d3 = createDirectory("baa"); - final File d4 = createDirectory("bba"); - createFile("bab"); - Set<File> actual = - new HashSet<File>(Arrays.asList(workingDirectory.listFiles(new RegexFileFilter( - PathType.DIRECTORY, "a.+")))); - Set<File> expected = new HashSet<File>(Arrays.asList(d1, d2)); - assertEquals(expected, actual); - - actual = - new HashSet<File>(Arrays.asList(workingDirectory.listFiles(new RegexFileFilter( - PathType.DIRECTORY, ".a.")))); - expected = new HashSet<File>(Arrays.asList(d1, d3)); - assertEquals(expected, actual); - - actual = - new HashSet<File>(Arrays.asList(workingDirectory.listFiles(new RegexFileFilter( - PathType.DIRECTORY, ".+a")))); - expected = new HashSet<File>(Arrays.asList(d1, d2, d3, d4)); - assertEquals(expected, actual); - } - - @Test - public void testAllMatching() throws IOException - { - final File f1 = createFile("aaa"); - final File d2 = createDirectory("aba"); - final File f3 = createFile("baa"); - final File d4 = createDirectory("bba"); - final File f5 = createFile("bab"); - Set<File> actual = - new HashSet<File>(Arrays.asList(workingDirectory.listFiles(new RegexFileFilter( - PathType.ALL, "a.+")))); - Set<File> expected = new HashSet<File>(Arrays.asList(f1, d2)); - assertEquals(expected, actual); - - actual = - new HashSet<File>(Arrays.asList(workingDirectory.listFiles(new RegexFileFilter( - PathType.ALL, ".a.")))); - expected = new HashSet<File>(Arrays.asList(f1, f3, f5)); - assertEquals(expected, actual); - - actual = - new HashSet<File>(Arrays.asList(workingDirectory.listFiles(new RegexFileFilter( - PathType.ALL, ".+a")))); - expected = new HashSet<File>(Arrays.asList(f1, d2, f3, d4)); - assertEquals(expected, actual); - } - - @Test - public void testTwoPatterns() throws IOException - { - final File f1 = createFile("aaa"); - final File f2 = createFile("aba"); - final File f3 = createFile("111"); - final File f4 = createFile("222"); - createFile("2a2"); - createFile("c0d"); - final RegexFileFilter filter = new RegexFileFilter(); - filter.add(PathType.ALL, "[a-z]+"); - filter.add(PathType.ALL, "[0-9]+"); - Set<File> actual = new HashSet<File>(Arrays.asList(workingDirectory.listFiles(filter))); - Set<File> expected = new HashSet<File>(Arrays.asList(f1, f2, f3, f4)); - assertEquals(expected, actual); - } - - private File createFile(String name) throws IOException - { - final File file = new File(workingDirectory, name); - file.createNewFile(); - assert file.isFile(); - file.deleteOnExit(); - return file; - } - - private File createDirectory(String name) throws IOException - { - final File file = new File(workingDirectory, name); - file.mkdir(); - assert file.isDirectory(); - file.deleteOnExit(); - return file; - } - -} -- GitLab