projects
/
svjatoslav_commons.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Code formatting.
[svjatoslav_commons.git]
/
src
/
main
/
java
/
eu
/
svjatoslav
/
commons
/
commandline
/
parameterparser
/
parameter
/
FileParameter.java
diff --git
a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java
b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java
index
79b0a10
..
b827fee
100755
(executable)
--- a/
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java
+++ b/
src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java
@@
-1,6
+1,6
@@
/*
* Svjatoslav Commons - shared library of common functionality.
/*
* Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-201
4
, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ * Copyright ©2012-201
7
, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 3 of the GNU Lesser General Public License
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 3 of the GNU Lesser General Public License
@@
-9,69
+9,69
@@
package eu.svjatoslav.commons.commandline.parameterparser.parameter;
package eu.svjatoslav.commons.commandline.parameterparser.parameter;
-import java.io.File;
-
import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
-public class FileParameter extends Parameter<FileParameter> {
+import java.io.File;
+
+public class FileParameter extends Parameter<File, FileParameter> {
+
+ private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
- private ExistanceType existanceType = ExistanceType.DOES_NOT_MATTER;
+ public FileParameter(final String description) {
+ super(description, ArgumentCount.SINGLE);
+ }
- public FileParameter(final String description) {
- super(description, ArgumentCount.SINGLE);
- }
+ protected static boolean validateFile(ExistenceType existenceType, String value) {
+ final File file = new File(value);
- @Override
- public java.lang.String describeFormat() {
- return existanceType.description + " file";
- }
+ if (existenceType == ExistenceType.MUST_EXIST) {
+ return file.exists() && file.isFile();
+ }
- @Override
- public File getValue() {
+ if (existenceType == ExistenceType.MUST_NOT_EXIST) {
+ return !file.exists();
+ }
- if (arguments.size() != 1)
- throw new RuntimeException("Parameter " + description
- + " shall have exactly 1 argument.");
+ if (existenceType == ExistenceType.DOES_NOT_MATTER) {
+ if (file.exists())
+ if (file.isDirectory())
+ return false;
-
return new File(arguments.get(0))
;
- }
+
return true
;
+
}
- public FileParameter mustExist() {
- existanceType = ExistanceType.MUST_EXIST;
- return this;
- }
+ return false;
+ }
- public FileParameter mustNotExist() {
- existanceType = ExistanceType.MUST_NOT_EXIST;
-
return this
;
-
}
+ @Override
+ public java.lang.String describeFormat() {
+
return existenceType.description + " file"
;
+ }
- @Override
- public boolean validate(final java.lang.String value) {
- final File file = new File(value);
+ @Override
+ public File getValue() {
- if (existanceType == ExistanceType.MUST_EXIST) {
- if (file.exists() && file.isFile())
- return true;
- return false;
- }
+ if (arguments.size() != 1)
+ throw new RuntimeException("Parameter " + description
+ + " shall have exactly 1 argument.");
- if (existanceType == ExistanceType.MUST_NOT_EXIST) {
- if (file.exists())
- return false;
- return true;
- }
+ return new File(arguments.get(0));
+ }
-
if (existanceType == ExistanceType.DOES_NOT_MATTER
) {
- if (file.exists())
- if (file.isDirectory())
- return false;
+
public FileParameter mustExist(
) {
+ existenceType = ExistenceType.MUST_EXIST;
+ return this;
+ }
- return true;
- }
+ public FileParameter mustNotExist() {
+ existenceType = ExistenceType.MUST_NOT_EXIST;
+ return this;
+ }
- return false;
- }
+ @Override
+ public boolean validate(final java.lang.String value) {
+ return validateFile(existenceType, value);
+ }
}
}