-1

Currently I have the code like below:

    public boolean accept(File dir, String filename) {
        ...
        return filename.contains(".png") || filename.contains(".jpg") || file.isDirectory();

(the full code can be found there - https://stackoverflow.com/a/18523047/604388)

So, .png and .jpg are hardcoded now. I would like to make it as function parameter(s) and allow to pass several extensions, not just one. How could I do it?

I think I can pass list of extensions as array, but how could I perform all these checks (filename.contains()) for all array elements?

Community
  • 1
  • 1
LA_
  • 19,823
  • 58
  • 172
  • 308
  • 3
    Given to pass the list of extensions as an array, as you suggest, is there a reason why the simple approach, to iterate over the array, wouldnt work? – vikingsteve Oct 12 '13 at 09:05
  • 1
    @LA_ Please put the real code. You cannot embedd one method in another only if you do it by declaring it into an anonymus class. Is this the case? – Ioan Oct 12 '13 at 09:20
  • @loan, I've added the link to the full code and edited the initial code to avoid the confusion. Sorry for having wrong code before. – LA_ Oct 12 '13 at 09:25

5 Answers5

2

The easiest approach I can come up with is to iterate through array of extensions:

boolean res = false;
for (String ext: extensions) {
    if (filename.endsWith(ext)) {
        res = true;
    }
}
aga
  • 27,954
  • 13
  • 86
  • 121
1

No built-in answer here I fear, so iterate will be the good old solution:

boolean result = false;
for (String extension : extensions) {
    if (filename.contains(extension)) {
        result = true;
        break;
    }
}
return result || file.isDirectory();

extensions may be a String[] or a Collection<String>

Note : Better, yet, to use endsWith() rather than contains() for extensions. But I used the same condition as OP...

Steph
  • 1,989
  • 14
  • 18
  • .contains is not proper method to check for extension. – nullptr Oct 12 '13 at 09:29
  • I agree but it is not the point. He wanted to know how to handle his parameters, and he used contains(). With endsWith() it is more correct, but functionnally different. – Steph Oct 12 '13 at 09:33
1

You should consider using an enum to avoid hardcoding.-

public enum Extension {
    PNG     (".png"),
    JPG     (".jpg");

    private String suffix;

    Extension(String suffix) {
        this.suffix = suffix;
    }

    public String getSuffix() {
        return suffix;
    }
}

Your code would look like this.-

public boolean accept(File dir, String filename, Extension... extensions) {
    boolean res = file.isDirectory();

    if (!res) {
        for (int i = 0; i < extensions.length; i ++) {
            res = res || filename.contains(extensions[i].getSuffix());
        }
    }

    return res;
}
ssantos
  • 16,001
  • 7
  • 50
  • 70
0

You can use the below snippet which should be enough for your needs :

private boolean loadFileList(String directory, String...extensions){
    for (String extension : extensions){
        if (filename.contains(extension))
            return true;
    }
    return false;
}

P.S. I'm not sure what is the filename type, but if returns boolean then the above example should fit

Ioan
  • 5,152
  • 3
  • 31
  • 50
  • Pretty sure `true` does not match the type `file[]` – Eric Oct 12 '13 at 09:10
  • Really ? Well I copied his method signature from the question. As long the question doesn't have any downvote, I cannot understand why you downvoted me. – Ioan Oct 12 '13 at 09:12
  • @Eric: I agree with loan, it is the OP that made a mistake in his return type. If it is the reason of the doynvote on his answer and/or mine, I think you are mistaken – Steph Oct 12 '13 at 09:16
  • @Eric feel better you downvoted one question and two answers ? You should have a look at other answers too. – Ioan Oct 12 '13 at 09:18
0
import java.io.File;
import java.io.FileFilter;

public class NewClass {
    public static File[] loadFileList(String dir, FileFilter filter){
        return new File(dir).listFiles(filter);
    }

    public static void loadFiles(){
        File files[] = loadFileList("c:\\", new FileFilter() {
            public boolean accept(File pathname) {
                if(pathname.getAbsolutePath().toLowerCase().endsWith(".png")){
                    return true;
                }
                if(pathname.getAbsolutePath().toLowerCase().endsWith(".jpg")){
                    return true;
                }
                return false;
            }
        });
        // here do some DIRTY things with files....
    }
}
nullptr
  • 3,320
  • 7
  • 35
  • 68
  • you don't answer the question. Your extensions are not parameters, they are still hardcoded. – Steph Oct 12 '13 at 09:37
  • Thats is what I am doing, passing parameters as FileFilter... then no need to hardcode anything in loadFileList method... – nullptr Oct 12 '13 at 09:52