Added special commandline argument type to support multiple strings.
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / commandline / parameterparser / parameter / DirectoryParameter.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality.
3  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.commons.commandline.parameterparser.parameter;
11
12 import java.io.File;
13
14 import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
15 import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
16
17 public class DirectoryParameter extends Parameter {
18
19         private ExistanceType existanceType = ExistanceType.DOES_NOT_MATTER;
20
21         public DirectoryParameter(final String description) {
22                 super(description, ArgumentCount.SINGLE);
23         }
24
25         @Override
26         public DirectoryParameter addAliases(final String... aliasArray) {
27                 super.addAliasesProtected(aliasArray);
28                 return this;
29         }
30
31         @Override
32         public java.lang.String describeFormat() {
33                 return existanceType.description + "directory";
34         }
35
36         @Override
37         public File getValue() {
38
39                 if (arguments.size() != 1)
40                         throw new RuntimeException("Parameter " + description
41                                         + " shall have exactly 1 argument.");
42
43                 return new File(arguments.get(0));
44         }
45
46         public DirectoryParameter mustExist() {
47                 existanceType = ExistanceType.MUST_EXIST;
48                 return this;
49         }
50
51         public DirectoryParameter mustNotExist() {
52                 existanceType = ExistanceType.MUST_NOT_EXIST;
53                 return this;
54         }
55
56         @Override
57         public DirectoryParameter setMandatory() {
58                 setMandatoryProtected();
59                 return this;
60         }
61
62         @Override
63         public boolean validate(final java.lang.String value) {
64                 final File file = new File(value);
65
66                 if (existanceType == ExistanceType.MUST_EXIST) {
67                         if (file.exists() && file.isDirectory())
68                                 return true;
69                         return false;
70                 }
71
72                 if (existanceType == ExistanceType.MUST_NOT_EXIST) {
73                         if (file.exists())
74                                 return false;
75                         return true;
76                 }
77
78                 if (existanceType == ExistanceType.DOES_NOT_MATTER) {
79                         if (file.exists())
80                                 if (file.isFile())
81                                         return false;
82
83                         return true;
84                 }
85
86                 return false;
87         }
88
89 }