Code formatting.
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / commandline / parameterparser / parameter / FileParameter.java
index 98a9b42..b827fee 100755 (executable)
@@ -1,6 +1,6 @@
 /*
  * 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
@@ -9,81 +9,69 @@
 
 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 FileParameter extends Parameter {
-
-       private ExistanceType existanceType = ExistanceType.DOES_NOT_MATTER;
-
-       public FileParameter(final String description) {
-               super(description, ArgumentCount.SINGLE);
-       }
-
-       @Override
-       public FileParameter addAliases(final String... aliasArray) {
-               super.addAliasesProtected(aliasArray);
-               return this;
-       }
-
-       @Override
-       public java.lang.String describeFormat() {
-               return existanceType.description + " file";
-       }
-
-       @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 FileParameter mustExist() {
-               existanceType = ExistanceType.MUST_EXIST;
-               return this;
-       }
-
-       public FileParameter mustNotExist() {
-               existanceType = ExistanceType.MUST_NOT_EXIST;
-               return this;
-       }
-
-       @Override
-       public FileParameter 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.isFile())
-                               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.isDirectory())
-                                       return false;
-
-                       return true;
-               }
-
-               return false;
-       }
+import java.io.File;
+
+public class FileParameter extends Parameter<File, FileParameter> {
+
+    private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
+
+    public FileParameter(final String description) {
+        super(description, ArgumentCount.SINGLE);
+    }
+
+    protected static boolean validateFile(ExistenceType existenceType, String value) {
+        final File file = new File(value);
+
+        if (existenceType == ExistenceType.MUST_EXIST) {
+            return file.exists() && file.isFile();
+        }
+
+        if (existenceType == ExistenceType.MUST_NOT_EXIST) {
+            return !file.exists();
+        }
+
+        if (existenceType == ExistenceType.DOES_NOT_MATTER) {
+            if (file.exists())
+                if (file.isDirectory())
+                    return false;
+
+            return true;
+        }
+
+        return false;
+    }
+
+    @Override
+    public java.lang.String describeFormat() {
+        return existenceType.description + " file";
+    }
+
+    @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 FileParameter mustExist() {
+        existenceType = ExistenceType.MUST_EXIST;
+        return this;
+    }
+
+    public FileParameter mustNotExist() {
+        existenceType = ExistenceType.MUST_NOT_EXIST;
+        return this;
+    }
+
+    @Override
+    public boolean validate(final java.lang.String value) {
+        return validateFile(existenceType, value);
+    }
 
 }