Restored filter implementations for backwards compatibility reasons.

- Restore FileListFilter implementations
- Have FileListFilter implementations extend their EntryListFilter counterpart.
- Deprecate AbstractFileListFilter in favor of EntryListFilter hierarchy
This commit is contained in:
Iwein Fuld
2010-10-15 10:35:13 +02:00
parent 15a5eaf265
commit 2bc8b31255
5 changed files with 233 additions and 3 deletions

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* 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 org.springframework.integration.file.filters;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* A convenience base class for any {@link FileListFilter} whose criteria can be
* evaluated against each File in isolation. If the entire List of files is
* required for evaluation, implement the FileListFilter interface directly.
*
* @author Mark Fisher
* @author Iwein Fuld
*
* @deprecated Replaced by AbstractEntryListFilter in 2.0.0
*/
@Deprecated
public abstract class AbstractFileListFilter implements FileListFilter {
/**
* {@inheritDoc}
*/
public final List<File> filterFiles(File[] files) {
List<File> accepted = new ArrayList<File>();
if (files != null) {
for (File file : files) {
if (this.accept(file)) {
accepted.add(file);
}
}
}
return accepted;
}
/**
* Subclasses must implement this method.
*/
protected abstract boolean accept(File file);
}

View File

@@ -0,0 +1,61 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* 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 org.springframework.integration.file.filters;
import org.springframework.integration.file.entries.AcceptOnceEntryFileListFilter;
import java.io.File;
import java.util.List;
/**
* {@link FileListFilter} that passes files only one time. This can
* conveniently be used to prevent duplication of files, as is done in
* {@link org.springframework.integration.file.FileReadingMessageSource}.
* <p/>
* This implementation is thread safe.
*
* @author Iwein Fuld
* @since 1.0.0
*/
public class AcceptOnceFileListFilter extends AcceptOnceEntryFileListFilter<File> implements FileListFilter{
/**
* Creates an AcceptOnceFileFilter that is based on a bounded queue. If the
* queue overflows, files that fall out will be passed through this filter
* again if passed to the {@link #filterFiles(File[])} method.
*
* @param maxCapacity the maximum number of Files to maintain in the 'seen'
* queue.
*/
public AcceptOnceFileListFilter(int maxCapacity) {
super(maxCapacity);
}
/**
* Creates an AcceptOnceFileFilter based on an unbounded queue.
*/
public AcceptOnceFileListFilter() {
super();
}
/**
* Filter out all the files that this instance has seen before.
*/
public List<File> filterFiles(File[] files) {
return this.filterEntries(files);
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2002-2009 the original author or authors.
*
* 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 org.springframework.integration.file.filters;
import org.springframework.integration.file.entries.CompositeEntryListFilter;
import org.springframework.integration.file.entries.EntryListFilter;
import org.springframework.util.Assert;
import java.io.File;
import java.io.FileFilter;
import java.util.*;
/**
* Composition that delegates to multiple {@link FileFilter}s. The composition is AND based, meaning that a file must
* pass through each filter's {@link #filterFiles(java.io.File[])} method in order to be accepted by the composite.
*
* @author Iwein Fuld
* @author Mark Fisher
*/
public class CompositeFileListFilter extends CompositeEntryListFilter<File> implements FileListFilter{
public CompositeFileListFilter(EntryListFilter<File>... fileFilters) {
this(Arrays.asList(fileFilters));
}
public CompositeFileListFilter(Collection<? extends EntryListFilter<File>> fileFilters) {
super(fileFilters);
}
/**
* {@inheritDoc}
* <p/>
* This implementation delegates to a collection of filters and returns only files that pass all the filters.
* @deprecated use {@link #filterEntries} instead
*/
@Deprecated
public List<File> filterFiles(File[] files) {
Assert.notNull(files, "'files' should not be null");
return this.filterEntries(files);
}
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* 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 org.springframework.integration.file.filters;
import org.springframework.integration.file.entries.FileEntryNamer;
import org.springframework.integration.file.entries.PatternMatchingEntryListFilter;
import org.springframework.util.Assert;
import java.io.File;
import java.util.List;
import java.util.regex.Pattern;
/**
* An {@link org.springframework.integration.file.entries.EntryListFilter} implementation that matches a File against a {@link Pattern}.
*
* @author Iwein Fuld
* @author Mark Fisher
*
* @since 1.0.0
*/
public class PatternMatchingFileListFilter extends PatternMatchingEntryListFilter<File> implements FileListFilter{
/**
* Create a file filter for the given pattern.
*/
public PatternMatchingFileListFilter(Pattern pattern) {
super(new FileEntryNamer(), pattern);
}
public PatternMatchingFileListFilter(String pattern) {
super(new FileEntryNamer(), pattern);
}
/**
* Filter out the files of which the name doesn't match the pattern of this filter
*
* @deprecated use {@link #filterEntries} instead
*/
public List<File> filterFiles(File[] files) {
Assert.notNull(files, "'files' must not be null");
return this.filterEntries(files);
}
}

View File

@@ -31,6 +31,7 @@ import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.matchers.JUnitMatchers.hasItems;
import static org.springframework.integration.test.matcher.PayloadMatcher.hasPayload;
@@ -53,21 +54,20 @@ public class FileInboundChannelAdapterWithRecursiveDirectoryTests {
//when
File folder = directory.newFolder("foo");
File file = new File(folder, "bar");
file.createNewFile();
assertTrue(file.createNewFile());
//verify
assertThat(files.receive(), hasPayload(file));
}
@Test(timeout = 2000)
@SuppressWarnings("unchecked")
public void shouldReturnFilesMultipleLevels() throws IOException {
//when
File folder = directory.newFolder("foo");
File siblingFile = directory.newFile("bar");
File childFile = new File(folder, "baz");
childFile.createNewFile();
assertTrue(childFile.createNewFile());
List<Message<?>> received = Arrays.asList(files.receive(), files.receive());
//verify