Logo Search packages:      
Sourcecode: cdk version File versions

static boolean org::openscience::cdk::index::CASNumber::isValid ( String  casNumber  )  [inline, static]

Checks wether the registry number is valid.

.keyword CAS number .keyword validation

Definition at line 62 of file CASNumber.java.

                                                    {
        boolean overall = true;
        /*
         * check format
         */
        String format = "^(\\d+)-(\\d\\d)-(\\d)$";
        Pattern pattern = Pattern.compile(format);
        Matcher matcher = pattern.matcher(casNumber);
        overall = overall && matcher.matches();
        
        if (matcher.matches()) {
                  /*
                   * check number
                   */
                  String part1 = matcher.group(1);
                  String part2 = matcher.group(2);
                  String part3 = matcher.group(3);
                      int part1value = Integer.parseInt(part1);
                  if (part1value < 50) {
                          overall = false; 
                      // CAS numbers start at 50-00-0
                  } else {
                int digit = CASNumber.calculateCheckDigit(part1, part2);
                overall = overall && (digit == Integer.parseInt(part3));
            }
        }
        
        return overall;
    }


Generated by  Doxygen 1.6.0   Back to index