35ab4397268efa7980e885c2fcfdae3f22ddd4cf
[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.addAliases(aliasArray);
28                 return this;
29         }
30
31         @Override
32         public java.lang.String describeFormat() {
33                 return existanceType.description + "directory";
34         }
35
36         public File getValue() {
37
38                 if (arguments.size() != 1)
39                         throw new RuntimeException("Parameter " + description
40                                         + " shall have exactly 1 argument.");
41
42                 return new File(arguments.get(0));
43         }
44
45         public DirectoryParameter mustExist() {
46                 existanceType = ExistanceType.MUST_EXIST;
47                 return this;
48         }
49
50         public DirectoryParameter mustNotExist() {
51                 existanceType = ExistanceType.MUST_NOT_EXIST;
52                 return this;
53         }
54
55         @Override
56         public DirectoryParameter setMandatory() {
57                 mandatory = true;
58                 return this;
59         }
60
61         @Override
62         public boolean validate(final java.lang.String value) {
63                 final File file = new File(value);
64
65                 if (existanceType == ExistanceType.MUST_EXIST) {
66                         if (file.exists() && file.isDirectory())
67                                 return true;
68                         return false;
69                 }
70
71                 if (existanceType == ExistanceType.MUST_NOT_EXIST) {
72                         if (file.exists())
73                                 return false;
74                         return true;
75                 }
76
77                 if (existanceType == ExistanceType.DOES_NOT_MATTER) {
78                         if (file.exists())
79                                 if (file.isFile())
80                                         return false;
81
82                         return true;
83                 }
84
85                 return false;
86         }
87
88 }