CompositeFileListFilter now uses a LinkedHashSet to maintain order.

This commit is contained in:
Mark Fisher
2009-06-30 15:32:36 +00:00
parent 66745112cf
commit 6866661436
2 changed files with 26 additions and 17 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* 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.
@@ -20,7 +20,7 @@ import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
@@ -32,29 +32,32 @@ import org.springframework.util.Assert;
* {@link #filterFiles(File)} method in order to be accepted by the composite.
*
* @author Iwein Fuld
* @author Mark Fisher
*/
public class CompositeFileListFilter implements FileListFilter {
private final Set<FileListFilter> fileFilters;
public CompositeFileListFilter(FileListFilter... fileFilters) {
this.fileFilters = new HashSet<FileListFilter>(Arrays.asList(fileFilters));
this.fileFilters = new LinkedHashSet<FileListFilter>(Arrays.asList(fileFilters));
}
public CompositeFileListFilter(Collection<FileListFilter> fileFilters) {
this.fileFilters = new HashSet<FileListFilter>(fileFilters);
this.fileFilters = new LinkedHashSet<FileListFilter>(fileFilters);
}
/**
* {@inheritDoc}
*
* This implementation delegates to a collection of filters and returns only files that pass all
* the filters.
* This implementation delegates to a collection of filters and returns
* only files that pass all the filters.
*/
public List<File> filterFiles(File[] files) {
Assert.notNull(files, "'files' should not be null");
List<File> leftOver = Arrays.asList(files);
for (FileListFilter fileFilter : fileFilters) {
for (FileListFilter fileFilter : this.fileFilters) {
leftOver = fileFilter.filterFiles(leftOver.toArray(new File[] {}));
}
return leftOver;
@@ -62,24 +65,23 @@ public class CompositeFileListFilter implements FileListFilter {
/**
* @see #addFilters(Collection)
* @param filters one or more new filters to be used
* @return a new CompositeFileFilter with the additional filters
* @param filters one or more new filters to add
* @return this CompositeFileFilter instance with the added filters
*/
public CompositeFileListFilter addFilter(FileListFilter... filters) {
return addFilters(Arrays.asList(filters));
}
/**
* Creates a new CompositeFileFilter that delegates to both the filters of
* this Composite and the new filters passed in.
* Add the new filters to this CompositeFileFilter while maintaining the
* existing filters.
*
* @param filtersToAdd
* @return a new CompositeFileFilter with the added filters
* @param filtersToAdd a list of filters to add
* @return this CompositeFileFilter instance with the added filters
*/
public CompositeFileListFilter addFilters(Collection<FileListFilter> filtersToAdd) {
HashSet<FileListFilter> newFilterSet = new HashSet<FileListFilter>(filtersToAdd);
newFilterSet.addAll(fileFilters);
return new CompositeFileListFilter(newFilterSet);
this.fileFilters.addAll(filtersToAdd);
return this;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.file;
import static org.mockito.Mockito.*;
@@ -34,6 +35,7 @@ import org.springframework.integration.core.Message;
/**
* @author Iwein Fuld
* @author Mark Fisher
*/
@SuppressWarnings("unchecked")
@RunWith(MockitoJUnit44Runner.class)
@@ -103,6 +105,11 @@ public class FileReadingMessageSourceTests {
verify(inputDirectoryMock,times(2)).listFiles();
}
@Test(expected = IllegalArgumentException.class)
public void nullFilter() throws Exception {
source.setFilter(null);
}
@Test
public void orderedReception() throws Exception {
File file1 = mock(File.class);