Add Javadoc comments to enhance code clarity
[cli-helper.git] / src / main / java / eu / svjatoslav / commons / cli_helper / parameter_parser / parameter / DirectoryOptions.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.ParameterCount;
8 import eu.svjatoslav.commons.cli_helper.parameter_parser.Option;
9
10 import java.io.File;
11 import java.util.List;
12 import java.util.stream.Collectors;
13
14 /**
15  * DirectoryOptions class represents a command-line option for one or more directories.
16  */
17 public class DirectoryOptions extends Option<List<File>, DirectoryOptions> {
18
19     /**
20      * This enum is used to define if resource denoted by particular option parameter shall exist or not.
21      * <p>
22      * This allows to specify for example if directory shall exist or not.
23      */
24     private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
25
26     /**
27      * Creates a new DirectoryOptions object with the given description.
28      *
29      * @param description The description of the directory options.
30      */
31     public DirectoryOptions(final String description) {
32         super(description, ParameterCount.ONE_OR_MORE);
33     }
34
35     /**
36      * Returns a string describing the format of the option.
37      *
38      * @return A string describing the format of the option.
39      */
40     @Override
41     public String describeFormat() {
42         switch (existenceType) {
43             case MUST_EXIST:
44                 return "One to many existing directories.";
45             case MUST_NOT_EXIST:
46                 return "One to many non-existing directories.";
47             default:
48                 return "One to many directories.";
49         }
50     }
51
52     /**
53      * Returns the value of the option as a list of File objects.
54      *
55      * @return The value of the option as a list of File objects.
56      */
57     @Override
58     public List<File> getValue() {
59         return parameters.stream().map(File::new).collect(Collectors.toList());
60     }
61
62     public DirectoryOptions mustExist() {
63         existenceType = ExistenceType.MUST_EXIST;
64         return this;
65     }
66
67     public DirectoryOptions mustNotExist() {
68         existenceType = ExistenceType.MUST_NOT_EXIST;
69         return this;
70     }
71
72
73     @Override
74     public boolean isValid(final String value) {
75         final File file = new File(value);
76
77         if (existenceType == ExistenceType.MUST_EXIST) {
78             return file.exists() && file.isDirectory();
79         }
80
81         if (existenceType == ExistenceType.MUST_NOT_EXIST) {
82             return !file.exists();
83         }
84
85         if (existenceType == ExistenceType.DOES_NOT_MATTER) {
86             if (file.exists())
87                 if (file.isFile())
88                     return false;
89
90             return true;
91         }
92
93         return false;
94     }
95
96 }