79e19248515416c441c3520fa5b17b7d
package net.alessandropetrozzelli;

class InvalidISINCharacterException
    extends Exception {
  InvalidISINCharacterException() {
    super();
  }

  InvalidISINCharacterException(String s) {
    super(s);
  }
}

public class Isin {
  static final int numberTableOffset = '0';
  static final int numberTableEndOffset = '9';
  static final int charTableOffset = 'A';
  static final int charTableEndOffset = 'Z';

  private static int getIntValueForISINChar(final char c) throws
      InvalidISINCharacterException {
    int value = 0;

    if ( (c >= numberTableOffset) && (c <= numberTableEndOffset)) {
      value = c - numberTableOffset;
    }
    else if ( (c >= charTableOffset) && (c <= charTableEndOffset)) {
      value = 10 + (c - charTableOffset);
    }
    else {
      throw new InvalidISINCharacterException("Unsupported character: " + c);
    }

    return value;
  }

  private static int getSumOfDigits(final int number) {
    int sumOfDigits=0;
    if (number<=9) {
      sumOfDigits = number;
    } else {
      String s = String.valueOf(number);
      try {
        for (int i = 0; i < s.length(); i++) {
          sumOfDigits += getIntValueForISINChar(s.charAt(i));
        }
      }
      catch (InvalidISINCharacterException ex) {
        System.out.println(ex);
        return -1;
      }
    }
    return sumOfDigits;
  }

  public static int getCheckDigit(final String isin) {
    int checkDigit = 0;

    if ( (isin == null) || (isin.length() != 11)) {
      return -1;
    }
    String incompleteISINcode = isin.toUpperCase();
    StringBuffer buffer = new StringBuffer();

    // convert every single character into number as specified
    try {
      for (int i = 0; i < incompleteISINcode.length(); i++) {
        buffer.append(getIntValueForISINChar(isin.charAt(i)));
      }
    }
    catch (InvalidISINCharacterException ex) {
      System.out.println(ex);
      return -1;
    }

    // sum all digits (multiplying odd digits by 2)
    try {
      for (int i = buffer.length() - 1; i >= 0; i--) {
        if ( (buffer.length() - i) % 2 == 1) {
          checkDigit += getSumOfDigits(getIntValueForISINChar(buffer.charAt(i)) * 2);
        }
        else {
          checkDigit += getIntValueForISINChar(buffer.charAt(i));
        }
      }
    }
    catch (InvalidISINCharacterException ex) {
      // this should never happen
      System.out.println(ex);
      return -1;
    }

    // end result
    checkDigit = (10 - (checkDigit % 10)) % 10;
    return checkDigit;
  }

Refactorings

No refactoring yet !

90ec841f866ac35c4ce90ee3cfffbdc3

Vime

November 8, 2007, November 08, 2007 14:30, permalink

2 ratings. Login to rate!

Just a refactoring for getIntValueForISINChar for now :)

private static int getIntValueForISINChar(final char c) throws
      InvalidISINCharacterException {

    switch( Character.getType() )
    {
      case DECIMAL_DIGIT_NUMBER : 
        return c - '0';

      case UPPERCASE_LETTER :
        return 10 + (c - 'A');

      default :
        throw new InvalidISINCharacterException("Unsupported character: " + c);
    }

    return 0;
  }
D41d8cd98f00b204e9800998ecf8427e

Vime

November 8, 2007, November 08, 2007 14:54, permalink

1 rating. Login to rate!

sorry, Character.getType(c)

55502f40dc8b7c769880b10874abc9d0

olivier.fayau.myopenid.com

November 12, 2007, November 12, 2007 23:42, permalink

1 rating. Login to rate!

Found on german wikipedia :-p
http://de.wikipedia.org/wiki/International_Securities_Identification_Number

Should check if char > 'Z' ...

package net.alessandropetrozzelli;

public class Isin {

	public static int getCheckDigit(String src) {
		int s = 0;
		int a = (src.length() == 12) ? 1 : 2;
		for (int i = src.length() - 1; i >= 0; i--) {
			int c = src.charAt(i);
			if (c > '9') {
				// Character
				c -= ('A' - 10);
				s += (3 - a) * (c / 10) + a * c + (a - 1) * (c % 10) / 5;
			} else {
				// Number
				c -= '0';
				s += a * c + (a - 1) * (c / 5);
				a = 3 - a;
			}
		}
		s %= 10;
		return (10 - s % 10) % 10;
	}
	
}

Your refactoring





Format Copy from initial code

or Cancel