I’m a bit stuck on a project..
Basically, I get a directory to scan for files that passes certain filters.
The command files contains lines with filtering (given filters name are stored in an enum file) instructions. like this for example:
suffix%txt
exec%YES
if that’s all, it will return all the files ending with txt (extension) and are executable..
so far no problem.
the problem starts with line like this:
suffix%txt exec%YES (on the same line) in this case, it should return all the files that ends with txt OR executable..
I’m splitting the line using String.split(“%”) and getting it into a map with a key and value, than iterate over each key and checks what filter in the enum it is, and perform the desire checking.
I’m kinda stuck on how to identify that when i have more than 1 filter per line.
I tried doing a HashMap with the 1st filter as the key, and the value is a list that contains all the other (using split(” “) for breaking up the filters..
I can’t rely on the keys being on the even or odd indexes, because a filter may have another %NOT at the end of it (suffix%txt%NOT) which will return all the files that doesn’t end with txt…
Any help would be appreciated!
Thanks!
How about this: create a
Filterinterface, and write a function that parses a file into a single filter based on the contents of the file. You’d have something like:With this in place, you just need to build up all the basic filters, and then adjacent elements on the same line get
OrFiltered together while filters on separate lines (or-filtered or not) getAndFiltered together. Here’s a sketch:You’d call
readAndFilteron lines read out from your file, and what you get back is always a singleFilterthat you can pass files to and it’ll tell you whether they pass the filter. (Note: if you wanted to, you could special-case myreadAndFilterandreadOrFiltermethods to check if the list they’re going to return is of length 1, and if so just return that one filter rather than anAndorOrof one element. It doesn’t affect correctness, but it might make debugging output a little easier to read.)