Introduce Iterable<Resource> to base Resource
This commit is contained in:
parent
8953873e67
commit
2f26c86ccc
|
@ -32,7 +32,9 @@ import java.nio.file.attribute.FileTime;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import org.eclipse.jetty.util.IO;
|
||||
import org.eclipse.jetty.util.URIUtil;
|
||||
|
@ -47,7 +49,7 @@ import org.slf4j.LoggerFactory;
|
|||
* Supports real filesystems, and also <a href="https://docs.oracle.com/en/java/javase/17/docs/api/jdk.zipfs/module-summary.html">ZipFS</a>.
|
||||
* </p>
|
||||
*/
|
||||
public abstract class Resource
|
||||
public abstract class Resource implements Iterable<Resource>
|
||||
{
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Resource.class);
|
||||
private static final LinkOption[] NO_FOLLOW_LINKS = new LinkOption[]{LinkOption.NOFOLLOW_LINKS};
|
||||
|
@ -152,6 +154,44 @@ public abstract class Resource
|
|||
return equals(resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an Iterator of all Resource's referenced in this Resource.
|
||||
*
|
||||
* <p>
|
||||
* This is meaningful if you have a Composite Resource, otherwise it will be a single entry Iterator.
|
||||
* </p>
|
||||
*
|
||||
* @return the iterator of Resources.
|
||||
*/
|
||||
@Override
|
||||
public Iterator<Resource> iterator()
|
||||
{
|
||||
return new Iterator<>()
|
||||
{
|
||||
private boolean next = true;
|
||||
|
||||
@Override
|
||||
public boolean hasNext()
|
||||
{
|
||||
return next;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resource next()
|
||||
{
|
||||
if (next)
|
||||
{
|
||||
next = false;
|
||||
return Resource.this;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NoSuchElementException("No more Resource entries to iterate");
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Equivalent to {@link Files#exists(Path, LinkOption...)} with the following parameters:
|
||||
* {@link #getPath()} and {@link LinkOption#NOFOLLOW_LINKS}.
|
||||
|
|
|
@ -23,6 +23,7 @@ import java.util.ArrayList;
|
|||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
@ -257,6 +258,12 @@ public class ResourceCollection extends Resource
|
|||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Resource> iterator()
|
||||
{
|
||||
return _resources.iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The list of resource names(merged) contained in the collection of resources.
|
||||
*/
|
||||
|
|
|
@ -18,6 +18,7 @@ import java.io.InputStream;
|
|||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
|
||||
import org.hamcrest.Matchers;
|
||||
|
@ -31,6 +32,7 @@ import static org.hamcrest.Matchers.is;
|
|||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class PathResourceTest
|
||||
{
|
||||
|
@ -140,4 +142,19 @@ public class PathResourceTest
|
|||
assertThat(ePathResource.equals(ePathResource2), Matchers.is(false));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIterable()
|
||||
{
|
||||
Path rpath = MavenTestingUtils.getTestResourcePathFile("resource.txt");
|
||||
PathResource resource = (PathResource)ResourceFactory.root().newResource(rpath);
|
||||
Iterator<Resource> iter = resource.iterator();
|
||||
int count = 0;
|
||||
while (iter.hasNext())
|
||||
{
|
||||
iter.next();
|
||||
count++;
|
||||
}
|
||||
assertEquals(1, count);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -173,6 +173,49 @@ public class ResourceCollectionTest
|
|||
assertThat(actual, contains(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIterable()
|
||||
{
|
||||
Path one = MavenTestingUtils.getTestResourcePathDir("org/eclipse/jetty/util/resource/one");
|
||||
Path two = MavenTestingUtils.getTestResourcePathDir("org/eclipse/jetty/util/resource/two");
|
||||
Path three = MavenTestingUtils.getTestResourcePathDir("org/eclipse/jetty/util/resource/three");
|
||||
Path dirFoo = MavenTestingUtils.getTestResourcePathDir("org/eclipse/jetty/util/resource/two/dir");
|
||||
|
||||
Resource compositeA = Resource.combine(
|
||||
List.of(
|
||||
resourceFactory.newResource(one),
|
||||
resourceFactory.newResource(two),
|
||||
resourceFactory.newResource(three)
|
||||
)
|
||||
);
|
||||
|
||||
Resource compositeB = Resource.combine(
|
||||
List.of(
|
||||
// the original composite Resource
|
||||
compositeA,
|
||||
// a duplicate entry
|
||||
resourceFactory.newResource(two),
|
||||
// a new entry
|
||||
resourceFactory.newResource(dirFoo)
|
||||
)
|
||||
);
|
||||
|
||||
List<URI> actual = new ArrayList<>();
|
||||
for (Resource resource: compositeB)
|
||||
{
|
||||
actual.add(resource.getURI());
|
||||
}
|
||||
|
||||
URI[] expected = new URI[] {
|
||||
one.toUri(),
|
||||
two.toUri(),
|
||||
three.toUri(),
|
||||
dirFoo.toUri()
|
||||
};
|
||||
|
||||
assertThat(actual, contains(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUserSpaceConfigurationNoGlob() throws Exception
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue