View Javadoc
1   package org.kuali.ole.ingest.util;
2   
3   import org.apache.log4j.Logger;
4   
5   /**
6    * Created with IntelliJ IDEA.
7    * User: ?
8    * Date: 11/6/12
9    * Time: 3:38 PM
10   * To change this template use File | Settings | File Templates.
11   */
12  public class ISSNUtil
13  {
14      private String issn;
15      private static Logger logger = Logger.getLogger( ISSNUtil.class );
16  
17  
18      public ISSNUtil(String issn) throws Exception
19      {
20          this.issn = issn.replace( "-", "" ); // remove hyphens
21          if( this.issn == null || this.issn.length() != 8 )
22          {
23              logger.debug( "ISSN length=" + this.issn.length() );
24              throw new Exception( "Bad ISSN length:" + this.issn );
25          }
26      }
27  
28  
29      public ISSNUtil(int issn)
30      {}
31  
32  
33      public int getCheckSum()
34      {
35          int sum = 0;
36          int weight = 0;
37  
38          for( int i = 0; i < 7; i++ )
39          {
40              switch( i ){
41                  case 0:
42                      weight = 8;
43                      break;
44                  case 1:
45                      weight = 7;
46                      break;
47                  case 2:
48                      weight = 6;
49                      break;
50                  case 3:
51                      weight = 5;
52                      break;
53                  case 4:
54                      weight = 4;
55                      break;
56                  case 5:
57                      weight = 3;
58                      break;
59                  case 6:
60                      weight = 2;
61                      break;
62              }
63  
64              char c = issn.charAt( i );
65              int n = Character.digit( c, 10 ); // convert character to digit
66              sum += ( n * weight );
67          }
68  
69          int cSum = sum % 11;
70  
71          return cSum == 0 ? cSum : 11 - cSum;
72      }
73  
74  
75      /**
76       * @return whether this ISBN has a correct checksum
77       */
78      public boolean hasCorrectChecksum()
79      {
80          char checksumChar;
81          int checksum = getCheckSum(); // get the calculated check sum digit
82  
83          if( checksum == 10 )
84          {
85              checksumChar = 'X';
86          }
87          else
88          {
89              checksumChar = Character.forDigit( checksum, 10 ); // convert digit to character
90          }
91  
92          char fileDigit = issn.charAt( 7 ); // check sum char
93  
94          return checksumChar == fileDigit;
95      }
96  
97  }