Add Javadoc comments to enhance code clarity
[cli-helper.git] / src / main / java / eu / svjatoslav / commons / cli_helper / parameter_parser / parameter / IntegerOption.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.commons.cli_helper.parameter_parser.parameter;
6
7 import eu.svjatoslav.commons.cli_helper.parameter_parser.ParameterCount;
8 import eu.svjatoslav.commons.cli_helper.parameter_parser.Option;
9
10 /**
11  * This class represents commandline option which accepts exactly one parameter
12  * which is an integer.
13  */
14 public class IntegerOption extends Option<Integer, IntegerOption> {
15
16     public IntegerOption(final String description) {
17         super(description, ParameterCount.ONE);
18     }
19
20     @Override
21     public java.lang.String describeFormat() {
22         return "Integer.";
23     }
24
25     @Override
26     public Integer getValue() {
27         if (parameters.size() != 1)
28             throw new RuntimeException("Parameter " + description
29                     + " shall have exactly 1 argument.");
30         return Integer.parseInt(parameters.get(0));
31     }
32
33     @Override
34     public boolean isValid(final java.lang.String value) {
35         try {
36             java.lang.Integer.valueOf(value);
37             return true;
38         } catch (final NumberFormatException e) {
39             return false;
40         }
41     }
42 }