/*
* Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ * Copyright ©2012-2017, 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
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;
-public class DirectoryParameter extends Parameter {
-
- private ExistanceType existanceType = ExistanceType.DOES_NOT_MATTER;
-
- public DirectoryParameter(final String description) {
- super(description, ArgumentCount.SINGLE);
- }
-
- @Override
- public DirectoryParameter addAliases(final String... aliasArray) {
- super.addAliasesProtected(aliasArray);
- return this;
- }
-
- @Override
- public java.lang.String describeFormat() {
- return existanceType.description + "directory";
- }
-
- @Override
- public File getValue() {
-
- if (arguments.size() != 1)
- throw new RuntimeException("Parameter " + description
- + " shall have exactly 1 argument.");
-
- return new File(arguments.get(0));
- }
-
- public DirectoryParameter mustExist() {
- existanceType = ExistanceType.MUST_EXIST;
- return this;
- }
-
- public DirectoryParameter mustNotExist() {
- existanceType = ExistanceType.MUST_NOT_EXIST;
- return this;
- }
-
- @Override
- public DirectoryParameter setMandatory() {
- setMandatoryProtected();
- return this;
- }
-
- @Override
- public boolean validate(final java.lang.String value) {
- final File file = new File(value);
-
- if (existanceType == ExistanceType.MUST_EXIST) {
- if (file.exists() && file.isDirectory())
- return true;
- return false;
- }
-
- if (existanceType == ExistanceType.MUST_NOT_EXIST) {
- if (file.exists())
- return false;
- return true;
- }
-
- if (existanceType == ExistanceType.DOES_NOT_MATTER) {
- if (file.exists())
- if (file.isFile())
- return false;
-
- return true;
- }
-
- return false;
- }
+import java.io.File;
+
+public class DirectoryParameter extends Parameter<File, DirectoryParameter> {
+
+ private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
+
+ public DirectoryParameter(final String description) {
+ super(description, ArgumentCount.SINGLE);
+ }
+
+ @Override
+ public java.lang.String describeFormat() {
+ return existenceType.description + "directory";
+ }
+
+ @Override
+ public File getValue() {
+
+ if (arguments.size() != 1)
+ throw new RuntimeException("Parameter " + description
+ + " shall have exactly 1 argument.");
+
+ return new File(arguments.get(0));
+ }
+
+ public DirectoryParameter mustExist() {
+ existenceType = ExistenceType.MUST_EXIST;
+ return this;
+ }
+
+ public DirectoryParameter mustNotExist() {
+ existenceType = ExistenceType.MUST_NOT_EXIST;
+ return this;
+ }
+
+ @Override
+ public boolean validate(final java.lang.String value) {
+ final File file = new File(value);
+
+ if (existenceType == ExistenceType.MUST_EXIST) {
+ return file.exists() && file.isDirectory();
+ }
+
+ if (existenceType == ExistenceType.MUST_NOT_EXIST) {
+ return !file.exists();
+ }
+
+ if (existenceType == ExistenceType.DOES_NOT_MATTER) {
+ if (file.exists())
+ if (file.isFile())
+ return false;
+
+ return true;
+ }
+
+ return false;
+ }
}