-package eu.svjatoslav.commons.commandline.parameterparser.parameter;
+/*
+ * Svjatoslav Commons - shared library of common functionality.
+ * 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
+ * or later as published by the Free Software Foundation.
+ */
-import java.io.File;
+package eu.svjatoslav.commons.commandline.parameterparser.parameter;
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;
+import java.io.File;
- public DirectoryParameter(final String description) {
- super(description, ArgumentCount.SINGLE);
- }
+public class DirectoryParameter extends Parameter<DirectoryParameter> {
- @Override
- public DirectoryParameter addAliases(final String... aliasArray) {
- super.addAliases(aliasArray);
- return this;
- }
+ private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
- @Override
- public java.lang.String describeFormat() {
- return existanceType.description + "directory";
- }
+ public DirectoryParameter(final String description) {
+ super(description, ArgumentCount.SINGLE);
+ }
- public File getValue() {
+ @Override
+ public java.lang.String describeFormat() {
+ return existenceType.description + "directory";
+ }
- if (arguments.size() != 1)
- throw new RuntimeException("Parameter " + description
- + " shall have exactly 1 argument.");
+ @Override
+ public File getValue() {
- return new File(arguments.get(0));
- }
+ if (arguments.size() != 1)
+ throw new RuntimeException("Parameter " + description
+ + " shall have exactly 1 argument.");
- public DirectoryParameter mustExist() {
- existanceType = ExistanceType.MUST_EXIST;
- return this;
- }
+ return new File(arguments.get(0));
+ }
- public DirectoryParameter mustNotExist() {
- existanceType = ExistanceType.MUST_NOT_EXIST;
- return this;
- }
+ public DirectoryParameter mustExist() {
+ existenceType = ExistenceType.MUST_EXIST;
+ return this;
+ }
- @Override
- public DirectoryParameter setMandatory() {
- mandatory = true;
- 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);
+ @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 (existenceType == ExistenceType.MUST_EXIST) {
+ return file.exists() && file.isDirectory();
+ }
- if (existanceType == ExistanceType.MUST_NOT_EXIST) {
- if (file.exists())
- return false;
- return true;
- }
+ if (existenceType == ExistenceType.MUST_NOT_EXIST) {
+ return !file.exists();
+ }
- if (existanceType == ExistanceType.DOES_NOT_MATTER) {
- if (file.exists())
- if (file.isFile())
- return false;
+ if (existenceType == ExistenceType.DOES_NOT_MATTER) {
+ if (file.exists())
+ if (file.isFile())
+ return false;
- return true;
- }
+ return true;
+ }
- return false;
- }
+ return false;
+ }
}