X-Git-Url: http://www2.svjatoslav.eu/gitweb/?p=svjatoslav_commons.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fcommons%2Fcommandline%2Fparameterparser%2Fparameter%2FFileParameter.java;h=b827fee4783997525d00d08b5be8275dad74dbd9;hp=98a9b42ddb353eac9988129bea5afa97462ddede;hb=b8bd1e820265fc15c39c1ee8c06289ea8b8e2c1c;hpb=ab4cc64cf105d4f03b8a0b94ab58d9b973820c8a 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 98a9b42..b827fee 100755 --- 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. - * 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 { + + 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); + } }