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: NonDecimalChecker.java 2655 2007-04-30 19:12:21Z pramirez $ 
14  //
15  
16  package gov.nasa.pds.tools.dict.type;
17  
18  /***
19   * @author pramirez
20   * @version $Revision: 2655 $
21   * 
22   */
23  public class NonDecimalChecker extends LengthChecker implements NumericTypeChecker {
24  
25      /* (non-Javadoc)
26       * @see gov.nasa.jpl.pds.tools.dict.type.TypeChecker#cast(java.lang.String)
27       */
28      public Object cast(String value) throws InvalidTypeException {
29          Long longValue = null;
30          
31          try {
32              //Check to see if there is a base associated with the value
33              if (value.indexOf("#") != -1) {
34                  int radix = Integer.parseInt(value.substring(0, value.indexOf("#")));
35                  String numericValue = value.substring(value.indexOf("#")+1, value.length()-1);
36                  String sign = null;
37                  if (numericValue.startsWith("+") || numericValue.startsWith("-")) {
38                      sign = numericValue.substring(0,1);
39                      numericValue = numericValue.substring(1);
40                  }
41                  longValue = Long.valueOf(numericValue, radix);
42                  if ("-".equals(sign))
43                      longValue = new Long((long)-1.0 * longValue.longValue());
44              } else
45                  longValue = Long.valueOf(value);
46          } catch (NumberFormatException nfe) {
47              throw new InvalidTypeException("Could not create a non decimal value from " + value);
48          }
49          return longValue;
50      }
51  
52      /* (non-Javadoc)
53       * @see gov.nasa.pds.tools.dict.type.NumericTypeChecker#checkMinValue(java.lang.Number, java.lang.Number)
54       */
55      public void checkMinValue(Number value, Number min) throws OutOfRangeException {
56          if (value.longValue() < min.longValue())
57              throw new OutOfRangeException(value.toString() + " is less than the acceptable minimum of " + min.toString());
58      }
59  
60      /* (non-Javadoc)
61       * @see gov.nasa.pds.tools.dict.type.NumericTypeChecker#checkMaxValue(java.lang.Number, java.lang.Number)
62       */
63      public void checkMaxValue(Number value, Number max) throws OutOfRangeException {
64          if (value.longValue() > max.longValue())
65              throw new OutOfRangeException(value.toString() + " exceeds acceptable maximum of " + max.toString());
66      }
67  
68  }