Changed license to LGPLv3 or later.
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / commandline / parameterparser / parameter / IntegerParameter.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 eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
13 import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
14
15 public class IntegerParameter extends Parameter {
16
17         public IntegerParameter(final String description) {
18                 super(description, ArgumentCount.SINGLE);
19         }
20
21         @Override
22         public IntegerParameter addAliases(final String... aliasArray) {
23                 super.addAliases(aliasArray);
24                 return this;
25         }
26
27         @Override
28         public java.lang.String describeFormat() {
29                 return "integer";
30         }
31
32         public int getValue() {
33                 if (arguments.size() != 1)
34                         throw new RuntimeException("Parameter " + description
35                                         + " shall have exactly 1 argument.");
36                 return Integer.parseInt(arguments.get(0));
37         }
38
39         @Override
40         public IntegerParameter setMandatory() {
41                 mandatory = true;
42                 return this;
43         }
44
45         @Override
46         public boolean validate(final java.lang.String value) {
47                 try {
48                         java.lang.Integer.valueOf(value);
49                         return true;
50                 } catch (final NumberFormatException e) {
51                         return false;
52                 }
53         }
54
55 }