nanomader
nanomader

Reputation: 410

File names validation

I am validating many files in directory in one thread every minute. Currently I'm using function below, which works fine. My questions is whether I can rewrite this method to run faster (java8?) and, hopefully, to be shorter.

public boolean validateFile(String fileName) {
    fileName = fileName.substring(0, fileName.lastIndexOf('.'));
    String[] values = fileName.split("_");
    if (values.length == 4) {
        if(!values[1].matches("0*")) {
            if(!fileName.contains(" ")) {
                if(values[3].equals("PASS") || values[3].equals("FAIL")) {
                    return true;
                }
            }
        }
    }
    return false;
}

Rules for validation:

  1. Filename cannot contain any whitespace character.
  2. Filename splitter is always _ character.
  3. Values[1] cannot contains only zeros (0). It must have 12 digits.
  4. Values[3] have to contains either "PASS" or "FAIL".
  5. Array values[] length have to be always 4.

You can run and fork this code with filename examples on http://ideone.com/dh8j9M

Upvotes: 1

Views: 953

Answers (2)

holi-java
holi-java

Reputation: 30686

Even if @Eugene's answer is more expressive, but I think you also can break the whole regex into pieces to make it clearly. for example:

private static final String PREFIX = "\\w+";
private static final String DIGIT12 = "(?:[1-9]\\d{11}|0\\d{11})";
private static final String TIMESTAMP = "[1-9]\\d{11}";
private static final String STATUS = "(?:PASS|FAIL)";
private static final String EXTENSION = "\\p{Alpha}+";

private static final String FILENAME_REGEX =
        format("%s_%s_%s_%s\\.%s", PREFIX, DIGIT12, TIMESTAMP, STATUS, EXTENSION);

private boolean validateFile(String fileName) {
    return fileName.matches(FILENAME_REGEX);
}

Upvotes: 2

Eugene
Eugene

Reputation: 120858

Something like this - just a plain transformation of the branches to java-8. May be more readable, but not faster.

 return Optional.ofNullable("test") // your fileName
            .filter(x -> !x.contains(" "))
            .map(x -> x.substring(0, x.lastIndexOf(".")))
            .map(x -> x.split("_"))
            .filter(arr -> arr.length == 4)
            .filter(arr -> !arr[1].matches("0*"))
            .filter(arr -> arr[3].equals("PASS") || arr[3].equals("FAIL"))
            .isPresent();

Upvotes: 5

Related Questions