Skip to content

Commit

Permalink
Added an always true filter for atlas loading (#198)
Browse files Browse the repository at this point in the history
* Added an always true filter for atlas loading

* Added a unit test.

* Checkstyle

* Checkstyle 2

* Refactored a predicate class into a simple predicate

* Improved syntax

* Added some javadoc to help explain the changes

* Updated unit test to clean up after itself.

* Umm why is this random test failing.
  • Loading branch information
lucaspcram authored and MikeGost committed Aug 17, 2018
1 parent 1686af6 commit 98c63d3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@

/**
* Load an {@link Atlas} from a {@link Resource} or an {@link Iterable} of {@link Resource}s.
* Supports also loading based on a resource name filter.
* Supports also loading based on a resource name filter. Note that by default, this class will
* filter the provided {@link Iterable} and remove any {@link Resource} that does not have a valid
* atlas file extension (defined in the {@link FileSuffix} enum). This funtionality can be disabled
* by calling {@link AtlasResourceLoader#withAtlasFileExtensionFilterSetTo(boolean)} with
* {@code false}. Disabling this functionality is useful if combining this class with atlases
* fetched from a cache that does not respect the .atlas file extension convention.
*
* @author cstaylor
* @author mgostintsev
Expand Down Expand Up @@ -59,9 +64,12 @@ public List<Resource> select(final File file)

private static final Logger logger = LoggerFactory.getLogger(AtlasResourceLoader.class);

private final Predicate<Resource> alwaysTrueAtlasFilter = resource -> true;

private Predicate<Resource> resourceFilter;
private Predicate<AtlasEntity> atlasEntityFilter;
private String multiAtlasName;
private boolean filterForAtlasFileExtension = true;

public AtlasResourceLoader()
{
Expand All @@ -71,8 +79,11 @@ public AtlasResourceLoader()

public Atlas load(final Iterable<? extends Resource> input)
{
final Predicate<Resource> toggleableAtlasFileFilter = this.filterForAtlasFileExtension
? IS_ATLAS : this.alwaysTrueAtlasFilter;

final List<Resource> resources = Iterables.stream(input).flatMap(this::resourcesIn)
.filter(IS_ATLAS).filter(this.resourceFilter).collectToList();
.filter(toggleableAtlasFileFilter).filter(this.resourceFilter).collectToList();
final long size = resources.size();
if (size == 1)
{
Expand Down Expand Up @@ -141,6 +152,19 @@ public AtlasResourceLoader withAtlasEntityFilter(final Predicate<AtlasEntity> fi
return this;
}

/**
* Enable or disable atlas file extension filtering on this loader.
*
* @param value
* whether to enable or disable
* @return the modified {@link AtlasResourceLoader}
*/
public AtlasResourceLoader withAtlasFileExtensionFilterSetTo(final boolean value)
{
this.filterForAtlasFileExtension = value;
return this;
}

public AtlasResourceLoader withMultiAtlasName(final String multiAtlasName)
{
this.multiAtlasName = multiAtlasName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ public void oneFile()
}
}

@Test
public void testAtlasFileExtensionFilter()
{
final File parent = File.temporaryFolder();
try
{
final File atlas1 = parent.child("iAmNotAnAtlas2.txt");
atlas1.writeAndClose("1");
Assert.assertNull(new AtlasResourceLoader().load(atlas1));
Assert.assertNotNull(new AtlasResourceLoader().withAtlasFileExtensionFilterSetTo(false)
.load(atlas1));
}
finally
{
parent.deleteRecursively();
}
}

@Test
public void testLoadingAtlasResourceWithFiltering()
{
Expand Down

0 comments on commit 98c63d3

Please sign in to comment.