79e19248515416c441c3520fa5b17b7d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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 :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
  }
Avatar

Vime

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

1 rating. Login to rate!

sorry, Character.getType(c)

Avatar

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' ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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