5fa357cfa1cc6f4d86ba5c9acd14785a1b273463
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / commandline / parameterparser / parameter / FileParameter.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 FileParameter extends Parameter {
18
19         private ExistanceType existanceType = ExistanceType.DOES_NOT_MATTER;
20
21         public FileParameter(final String description) {
22                 super(description, ArgumentCount.SINGLE);
23         }
24
25         @Override
26         public FileParameter 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 + "file";
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 FileParameter mustExist() {
46                 existanceType = ExistanceType.MUST_EXIST;
47                 return this;
48         }
49
50         public FileParameter mustNotExist() {
51                 existanceType = ExistanceType.MUST_NOT_EXIST;
52                 return this;
53         }
54
55         @Override
56         public FileParameter 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.isFile())
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.isDirectory())
80                                         return false;
81
82                         return true;
83                 }
84
85                 return false;
86         }
87
88 }