com.softicar.platform.common.io.resource.default_.DefaultResourceFilename Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of platform-common Show documentation
Show all versions of platform-common Show documentation
The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.
package com.softicar.platform.common.io.resource.default_;
import com.softicar.platform.common.io.resource.key.IResourceKey;
import java.util.Objects;
import java.util.regex.Pattern;
/**
* Facilitates handling and validation of a file name of a default resource, as
* used in an {@link IResourceKey}.
*
* @author Alexander Schmidt
*/
public class DefaultResourceFilename {
private static final Pattern VALIDATION_REGEX = Pattern.compile("[a-zA-Z0-9]((\\-)?[a-zA-Z0-9]+)*((\\.)?[a-zA-Z0-9]+)?");
private final String filename;
/**
* Constructs a new {@link DefaultResourceFilename} from the given literal
* default file name.
*
* @param filename
* the default file name (never null)
* @throws NullPointerException
* if the given {@link String} is null
*/
public DefaultResourceFilename(String filename) {
this.filename = Objects.requireNonNull(filename);
}
@Override
public String toString() {
return filename;
}
/**
* Returns this {@link DefaultResourceFilename} as a {@link String}.
*
* @return this {@link DefaultResourceFilename} as a {@link String} (never
* null)
*/
public String get() {
return filename;
}
/**
* Checks whether the file name is valid for a default resource.
*
* A file name is valid if all of the following conditions are met:
*
* - the {@link String} is non-empty
* - all characters are either letters ("a-zA-Z"), or digits ("0-9"),
* dashes ("-") or dots (".")
* - the first character is a letter or a digit
* - the last character is a letter or a digit
* - the {@link String} does not contain a dash or dot that is adjacent to
* another dash or dot
* - the {@link String} contains at most one dot
*
*
* @return true if the file name is valid; false otherwise
*/
public boolean isValid() {
return VALIDATION_REGEX.asMatchPredicate().test(filename);
}
}