View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.util.prefs;
21  
22  import org.xnap.XNap;
23  
24  /***
25   * An integer validator. A range can be defined.
26   */
27  public class IntValidator implements Validator
28  {
29  
30      //--- Data field(s) ---
31  
32      private int min;
33      private int max;
34  
35      //--- Constant(s) ---
36  
37      public IntValidator(int min, int max)
38      {
39  		if (min > max) {
40  			throw new IllegalArgumentException("min must not be greater than max (" + min + " > " + max + ")");
41  		}
42  
43  		this.min = min;
44  		this.max = max;
45      }
46  
47      public IntValidator(int min)
48      {
49  		this(min, Integer.MAX_VALUE);
50      }
51  
52      public IntValidator()
53      {
54  		this(Integer.MIN_VALUE, Integer.MAX_VALUE);
55      }
56  
57      //--- Method(s) ---
58  
59      /***
60       * Validates <code>String</code>.
61       * @exception IllegalArgumentException if newValue is invalid.
62       */
63      public void validate(String value)
64      {
65  		if (value == null) {
66  			throw new IllegalArgumentException
67  				(XNap.tr("Value must not be null"));
68  		}
69  
70  		int i = Integer.parseInt(value);
71  		if (i < min || i > max) {
72  			throw(new IllegalArgumentException(XNap.tr("Value out of range.")));
73  		}
74      }
75  
76  }