View Javadoc

1   // Copyright 2006-2007, by the California Institute of Technology.
2   // ALL RIGHTS RESERVED. United States Government Sponsorship acknowledged.
3   // Any commercial use must be negotiated with the Office of Technology Transfer
4   // at the California Institute of Technology.
5   //
6   // This software is subject to U. S. export control laws and regulations
7   // (22 C.F.R. 120-130 and 15 C.F.R. 730-774). To the extent that the software
8   // is subject to U.S. export control laws and regulations, the recipient has
9   // the responsibility to obtain export licenses or other export authority as
10  // may be required before exporting such information to foreign countries or
11  // providing access to foreign nationals.
12  //
13  // $Id: IntegerChecker.java 2601 2007-04-12 18:34:25Z pramirez $ 
14  //
15  
16  package gov.nasa.pds.tools.dict.type;
17  
18  /***
19   * @author pramirez
20   * @version $Revision: 2601 $
21   * 
22   */
23  public class IntegerChecker extends LengthChecker implements NumericTypeChecker {
24  
25      /* (non-Javadoc)
26       * @see gov.nasa.jpl.pds.tools.label.validate.TypeChecker#cast(java.lang.String)
27       */
28      public Object cast(String value) throws InvalidTypeException {
29          Integer intValue = null;
30          try {
31              String testValue = value.trim();
32              //Java's casting to an integer does not like positive signs in front.
33              if (testValue.startsWith("+"))
34                  testValue = testValue.substring(1);
35              intValue = Integer.valueOf(testValue);
36          } catch(NumberFormatException nfe) {
37              throw new InvalidTypeException(nfe.getMessage());
38          }
39          return intValue;
40      }
41  
42      /* (non-Javadoc)
43       * @see gov.nasa.pds.tools.dict.type.NumericTypeChecker#checkMinValue(java.lang.Number, java.lang.Number)
44       */
45      public void checkMinValue(Number value, Number min) throws OutOfRangeException {
46          if (value.intValue() < min.intValue())
47              throw new OutOfRangeException(value.toString() + " is less than the acceptable minimum of " + min.toString());
48      }
49  
50      /* (non-Javadoc)
51       * @see gov.nasa.pds.tools.dict.type.NumericTypeChecker#checkMaxValue(java.lang.Number, java.lang.Number)
52       */
53      public void checkMaxValue(Number value, Number max) throws OutOfRangeException {
54          if (value.intValue() > max.intValue())
55              throw new OutOfRangeException(value.toString() + " exceeds acceptable maximum of " + max.toString());
56      }
57  
58  }