public class FilteredStrings extends AbstractList<String>
WildcardFilter
.
This class is especially useful for collecting files from directories like below. It also takes care to handle null files
list that File.list()
can return on exceptional conditions.
File directory = new File("."); FilteredStrings sources = new FilteredStrings("*.java"); sources.addAll(directory.list()); for (String source : sources) { // do something with java source }
Modifier and Type | Field and Description |
---|---|
private WildcardFilter |
filter
Wildcard filter used to match pattern and given string.
|
private List<String> |
list
List used to collect strings.
|
modCount
Constructor and Description |
---|
FilteredStrings(String pattern)
Construct a new filtered strings instance.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(String string)
Add a string if it matches this filter pattern.
|
void |
addAll(String[] strings)
Add strings accepted by this filter predicate.
|
void |
addAll(String prefix,
String[] strings)
Add strings accepted by this filter pattern but prefixed with given string.
|
String |
get(int index)
Random, indexed access to a string from this strings list.
|
Iterator<String> |
iterator()
Returns an iterator instance usable to traverse this strings list.
|
int |
size()
Get the size of this strings list.
|
add, addAll, clear, equals, hashCode, indexOf, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subList
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
addAll, contains, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, sort, spliterator, toArray, toArray
parallelStream, removeIf, stream
private WildcardFilter filter
public FilteredStrings(String pattern)
WildcardFilter
.pattern
- string pattern.public boolean add(String string)
add
in interface Collection<String>
add
in interface List<String>
add
in class AbstractList<String>
string
- string to add to this strings list.string
argument matches the pattern and was added to this strings list.public void addAll(String prefix, String[] strings)
File.list()
and can be null in which case this method does nothing. This method accept a prefix argument; it is
inserted at every string start before actually adding to strings list.
File directory = new File("."); FilteredStrings files = new FilteredStrings("index.*"); files.addAll("/var/www/", directory.list());If
directory
contains files like index.htm, index.css, etc. will be added to
files
but prefixed like /var/www/index.htm, respective /var/www/index.css.prefix
- prefix to insert on every string,strings
- strings to scan for pattern, possible null.public void addAll(String[] strings)
File.list()
and can be null in
which case this method does nothing.strings
- strings to scan for pattern, possible null.public Iterator<String> iterator()
public int size()
size
in interface Collection<String>
size
in interface List<String>
size
in class AbstractCollection<String>
public String get(int index) throws IndexOutOfBoundsException
get
in interface List<String>
get
in class AbstractList<String>
index
.IndexOutOfBoundsException
- if the index
is out of range.Copyright © 2019. All rights reserved.