Add Javadoc comments to enhance code clarity
[cli-helper.git] / src / main / java / eu / svjatoslav / commons / cli_helper / parameter_parser / parameter / DirectoryOption.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
12
13 /**
14  * This class is used to define commandline option which accepts directory as parameter.
15  */
16 public class DirectoryOption extends Option<File, DirectoryOption> {
17
18     /**
19      * This enum is used to define if directory shall exist in filesystem or not.
20      */
21     private ExistenceType existenceType = ExistenceType.DOES_NOT_MATTER;
22
23     /**
24      * Constructor.
25      *
26      * @param description Description of the option.
27      */
28     public DirectoryOption(final String description) {
29         super(description, ParameterCount.ONE);
30     }
31
32     @Override
33     public java.lang.String describeFormat() {
34         switch (existenceType) {
35             case MUST_EXIST:
36                 return "Existing directory.";
37             case MUST_NOT_EXIST:
38                 return "Non-existing directory.";
39             default:
40                 return "Directory.";
41         }
42     }
43
44     /**
45      * Retrieves the value of the option as a {@link File} object.
46      *
47      * @return The value of the option as a {@link File} object.
48      * @throws RuntimeException if the option does not have exactly 1 argument.
49      */
50     @Override
51     public File getValue() {
52
53         if (parameters.size() != 1)
54             throw new RuntimeException("Parameter " + description
55                     + " shall have exactly 1 argument.");
56
57         return new File(parameters.get(0));
58     }
59
60     /**
61      * This method sets that directory shall exist.
62      *
63      * @return This object.
64      */
65     public DirectoryOption mustExist() {
66         existenceType = ExistenceType.MUST_EXIST;
67         return this;
68     }
69
70     /**
71      * This method sets that directory shall not exist.
72      *
73      * @return This object.
74      */
75     public DirectoryOption mustNotExist() {
76         existenceType = ExistenceType.MUST_NOT_EXIST;
77         return this;
78     }
79
80     /**
81      * This method checks if a provided directory path is valid based on the specified existence type.
82      *
83      * @param value The directory path to validate.
84      * @return True if the directory path is valid according to the existence type, otherwise false.
85      */
86     @Override
87     public boolean isValid(final java.lang.String value) {
88         final File file = new File(value);
89
90         if (existenceType == ExistenceType.MUST_EXIST) {
91             return file.exists() && file.isDirectory();
92         }
93
94         if (existenceType == ExistenceType.MUST_NOT_EXIST) {
95             return !file.exists();
96         }
97
98         if (existenceType == ExistenceType.DOES_NOT_MATTER) {
99             if (file.exists())
100                 if (file.isFile())
101                     return false;
102
103             return true;
104         }
105
106         return false;
107     }
108
109 }