Add Javadoc comments to enhance code clarity
[cli-helper.git] / src / main / java / eu / svjatoslav / commons / cli_helper / parameter_parser / parameter / FileOptions.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
6
7 import eu.svjatoslav.commons.cli_helper.parameter_parser.Option;
8 import eu.svjatoslav.commons.cli_helper.parameter_parser.ParameterCount;
9
10 import java.io.File;
11 import java.util.List;
12 import java.util.stream.Collectors;
13
14 import static eu.svjatoslav.commons.cli_helper.parameter_parser.parameter.FileOption.validateFile;
15
16 /**
17  * This class represents commandline option which accepts one or more parameters
18  * which are files.
19  */
20 public class FileOptions extends Option<List<File>, FileOptions> {
21
22     private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
23
24     public FileOptions(final String description) {
25         super(description, ParameterCount.ONE_OR_MORE);
26     }
27
28     @Override
29     public String describeFormat() {
30         switch (existenceType) {
31             case MUST_EXIST:
32                 return "One to many existing files.";
33             case MUST_NOT_EXIST:
34                 return "One to many non-existing files.";
35             default:
36                 return "One to many files.";
37         }
38     }
39
40     @Override
41     public List<File> getValue() {
42         return parameters.stream().map(File::new).collect(Collectors.toList());
43     }
44
45     /**
46      * This method is used to define that file shall exist in filesystem.
47      *
48      * @return This object.
49      */
50     public FileOptions mustExist() {
51         existenceType = ExistenceType.MUST_EXIST;
52         return this;
53     }
54
55     /**
56      * This method is used to define that file shall not exist in filesystem.
57      *
58      * @return This object.
59      */
60     public FileOptions mustNotExist() {
61         existenceType = ExistenceType.MUST_NOT_EXIST;
62         return this;
63     }
64
65     @Override
66     public boolean isValid(final String value) {
67         return validateFile(existenceType, value);
68     }
69
70 }