improved javadoc
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / commandline / parameterparser / Parameter.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;
11
12 import java.util.ArrayList;
13
14 public abstract class Parameter<T> {
15
16         /**
17          * Indicates that at least one argument is mandatory for this parameter.
18          */
19         protected boolean mandatory;
20
21         private final ArrayList<String> aliases = new ArrayList<String>();
22
23         public final String description;
24
25         public final ArrayList<String> arguments = new ArrayList<String>();
26
27         final ArgumentCount argumentCount;
28
29         /**
30          * If this parameter was present in the commandline, then this boolean will
31          * be set to <code>true</code>.
32          */
33         private boolean parameterSpecified;
34
35         public Parameter(final boolean mandatory,
36                         final ArgumentCount argumentCount, final String description,
37                         final String... aliases2) {
38
39                 this.mandatory = mandatory;
40                 this.description = description;
41                 this.argumentCount = argumentCount;
42
43                 // save aliases
44                 for (final String alias : aliases2)
45                         aliases.add(alias);
46
47         };
48
49         public Parameter(final String description, final ArgumentCount argumentCount) {
50                 this.description = description;
51                 this.argumentCount = argumentCount;
52         }
53
54         @SuppressWarnings("unchecked")
55         public T addAliases(final String... aliasArray) {
56
57                 // save aliases
58                 for (final String alias : aliasArray)
59                         aliases.add(alias);
60
61                 return (T) this;
62         }
63
64         /**
65          * @param argumentString
66          *            argument to add
67          * @return <code>true</code> if no errors were found. <code>false</code>
68          *         otherwise.
69          */
70         public boolean addArgument(final String argumentString) {
71                 // check if arguments are allowed for this parameter
72                 if (argumentCount.equals(ArgumentCount.NONE)) {
73                         System.out
74                                         .println("Error! No arguments are allowed for parameters: "
75                                                         + getAliases());
76                         return false;
77                 }
78
79                 // check if multiple arguments are allowed
80                 if ((arguments.size() > 0)
81                                 && (argumentCount.equals(ArgumentCount.SINGLE))) {
82                         System.out
83                                         .println("Error! Only single argument is allowed for parameters: "
84                                                         + getAliases());
85                         return false;
86                 }
87
88                 if (!validate(argumentString)) {
89
90                         System.out.println("Error! Invalid argument \"" + argumentString
91                                         + "\". It shall be " + describeFormat() + ".");
92                         return false;
93
94                 }
95
96                 arguments.add(argumentString);
97
98                 return true;
99         }
100
101         /**
102          * @return Single line argument type description.
103          */
104         public abstract String describeFormat();
105
106         public String getAliases() {
107                 final StringBuffer buffer = new StringBuffer();
108
109                 for (final String alias : aliases) {
110
111                         if (buffer.length() > 0)
112                                 buffer.append(", ");
113
114                         buffer.append(alias);
115                 }
116
117                 return buffer.toString();
118         }
119
120         public String getHelp() {
121                 final StringBuffer buffer = new StringBuffer();
122
123                 // first line
124                 buffer.append(getAliases());
125                 if (!argumentCount.equals(ArgumentCount.NONE)) {
126                         buffer.append(" (" + describeFormat() + ")");
127
128                         if (argumentCount.equals(ArgumentCount.MULTI))
129                                 buffer.append("...");
130                 }
131                 buffer.append("\n");
132
133                 // second line
134                 buffer.append("    " + description + "\n");
135
136                 return buffer.toString();
137         }
138
139         public abstract Object getValue();
140
141         public boolean isMandatory() {
142                 return mandatory;
143         }
144
145         /**
146          * @return the parameterSpecified
147          */
148         public boolean isParameterSpecified() {
149                 return parameterSpecified;
150         }
151
152         /**
153          * @param alias
154          *            alias to check against
155          * @return <code>true</code> if given alias is registered for this
156          *         parameter.
157          */
158         public boolean matchesAlias(final String alias) {
159                 if (aliases.contains(alias))
160                         return true;
161
162                 return false;
163         }
164
165         /**
166          * Notifies this parameter that no more arguments will follow. This gives
167          * parameter chance to verify if this is ok.
168          *
169          * @return <code>true</code> if no errors were found. <code>false</code>
170          *         otherwise.
171          */
172         public boolean noMoreArguments() {
173
174                 if ((!argumentCount.equals(ArgumentCount.NONE))
175                                 && (arguments.isEmpty())) {
176
177                         System.out.println("Error! " + getAliases()
178                                         + " require at least one following argument.");
179
180                         return false;
181                 }
182                 return true;
183         }
184
185         @SuppressWarnings("unchecked")
186         public T setMandatory() {
187                 mandatory = true;
188                 return (T) this;
189         }
190
191         /**
192          * @param parameterSpecified
193          *            the parameterSpecified to set
194          */
195         public void setParameterSpecified(final boolean parameterSpecified) {
196                 this.parameterSpecified = parameterSpecified;
197         }
198
199         /**
200          * @param value
201          *            value to validate
202          * @return <code>true</code> if value is correct, <code>false</code>
203          *         otherwise.
204          */
205         public abstract boolean validate(String value);
206
207 }