From 7b0d4e85fa892dc8422bfa10ab3b4328bc9f1253 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Tue, 26 Dec 2017 01:01:06 +0200 Subject: [PATCH] Added FileParameters type. --- .../parameterparser/Parameter.java | 5 +- .../parameter/FileParameter.java | 4 ++ .../parameter/FileParameters.java | 54 +++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100755 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameters.java diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java index 95df0c0..8208583 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java @@ -11,13 +11,14 @@ package eu.svjatoslav.commons.commandline.parameterparser; import java.util.ArrayList; import java.util.Collections; +import java.util.List; public abstract class Parameter { public final String description; - public final ArrayList arguments = new ArrayList<>(); + public final List arguments = new ArrayList<>(); final ArgumentCount argumentCount; - private final ArrayList aliases = new ArrayList<>(); + private final List aliases = new ArrayList<>(); /** * Indicates that at least one argument is mandatory for this parameter. */ 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 fc8c450..21a84ff 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 @@ -49,6 +49,10 @@ public class FileParameter extends Parameter { @Override public boolean validate(final java.lang.String value) { + return validateFile(existenceType, value); + } + + protected static boolean validateFile(ExistenceType existenceType, String value) { final File file = new File(value); if (existenceType == ExistenceType.MUST_EXIST) { diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameters.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameters.java new file mode 100755 index 0000000..b56c867 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameters.java @@ -0,0 +1,54 @@ +/* + * 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. + */ + +package eu.svjatoslav.commons.commandline.parameterparser.parameter; + +import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount; +import eu.svjatoslav.commons.commandline.parameterparser.Parameter; + +import java.io.File; +import java.util.List; +import java.util.stream.Collectors; + +import static eu.svjatoslav.commons.commandline.parameterparser.parameter.FileParameter.validateFile; + +public class FileParameters extends Parameter, FileParameters> { + + private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER; + + public FileParameters(final String description) { + super(description, ArgumentCount.MULTI); + } + + @Override + public String describeFormat() { + return existenceType.description + " files"; + } + + @Override + public List getValue() { + return arguments.stream().map(File::new).collect(Collectors.toList()); + } + + public FileParameters mustExist() { + existenceType = ExistenceType.MUST_EXIST; + return this; + } + + public FileParameters mustNotExist() { + existenceType = ExistenceType.MUST_NOT_EXIST; + return this; + } + + @Override + public boolean validate(final String value) { + return validateFile(existenceType, value); + } + +} -- 2.20.1