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