1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.plausj.bank.german;
17
18 /**
19 * This is a german bank account code
20 *
21 * @author ninan
22 *
23 */
24 public class AccountCode {
25
26 /** The minimal length for an account code */
27 public static final int ACCOUNT_CODE_MIN_LENGTH = 2;
28
29 /** The maximum length for an account code */
30 public static final int ACCOUNT_CODE_MAX_LENGTH = 10;
31
32 /** The padded account code */
33 private final String accountCode;
34
35 /** The padded account code as integer array */
36 private final int[] accountCodeIntArray = new int[ACCOUNT_CODE_MAX_LENGTH];
37
38 /** A numeric representation of the account code */
39 private final long accountCodeLongNumber;
40
41 /**
42 * The real length of the account code, before it is padded with 0 on the
43 * left side
44 */
45 private final int realLength;
46
47 /**
48 * The default-CTOR for the account code object
49 *
50 * @param accountCode
51 * The account code this object represents
52 *
53 * @throws IllegalArgumentException
54 * Thrown if the supplied account code is invalid
55 */
56 public AccountCode(final String accountCode)
57 throws IllegalArgumentException {
58
59 if (null == accountCode)
60 throw new IllegalArgumentException(
61 "Null pointer supplied as account code.");
62
63 if (accountCode.length() < ACCOUNT_CODE_MIN_LENGTH)
64 throw new IllegalArgumentException(
65 "Supplied account code is too short. It must have at least "
66 + ACCOUNT_CODE_MIN_LENGTH
67 + " digits. [accountCode='" + accountCode + "']");
68
69 if (accountCode.length() > ACCOUNT_CODE_MAX_LENGTH)
70 throw new IllegalArgumentException(
71 "Supplied account code is too long. It may have at most "
72 + ACCOUNT_CODE_MAX_LENGTH
73 + " digits. [accountCode='" + accountCode + "']");
74
75 for (int i = 0; i < accountCode.length(); ++i) {
76 char d = accountCode.charAt(i);
77
78
79 if (!(d >= 48 && d <= 57)) {
80 throw new IllegalArgumentException(
81 "The supplied account code must only consist of decimal numbers from 0 to 9. [accountCode='"
82 + accountCode + "']");
83 }
84 }
85
86 realLength = accountCode.length();
87 final int insertat = ACCOUNT_CODE_MAX_LENGTH - realLength;
88
89 if (ACCOUNT_CODE_MAX_LENGTH == realLength) {
90 this.accountCode = accountCode;
91 } else {
92
93 final StringBuffer sb = new StringBuffer(10);
94 for (int i = 0; i < insertat; ++i) {
95 sb.insert(i, '0');
96
97 }
98 sb.insert(insertat, accountCode);
99 this.accountCode = sb.toString();
100 }
101
102 for (int i = 0; i < ACCOUNT_CODE_MAX_LENGTH; ++i) {
103 this.accountCodeIntArray[i] = Integer.parseInt(this.accountCode
104 .substring(i, i + 1));
105 }
106
107 accountCodeLongNumber = Long.parseLong(accountCode);
108 }
109
110 /**
111 * Returns the account code as padded string
112 *
113 * @return The accountCode.
114 */
115 public String getAccountCode() {
116 return accountCode;
117 }
118
119 /**
120 * Returns an integer array containing the digits of the account code
121 *
122 * @return An integer array containing the digits of the account code
123 */
124 public int[] getAsIntArray() {
125 return (int[]) accountCodeIntArray.clone();
126 }
127
128 public long getAsLong() {
129 return accountCodeLongNumber;
130 }
131
132 /**
133 * Returns the digit of the account code on the specified position
134 *
135 * @param pos
136 * the position to return the digit for
137 * @return The digit that is on the supplied position
138 *
139 * @throws IndexOutOfBoundsException
140 * Thrown if the pos exceeds the range of the accountCode
141 */
142 public int getDigitAtPos(final int pos) {
143 try {
144 return accountCodeIntArray[pos];
145 } catch (IndexOutOfBoundsException e) {
146 throw new IndexOutOfBoundsException(
147 "The supplied position was out of the range of the account code length. [pos='"
148 + pos + "'");
149 }
150 }
151
152 public boolean matchesCheckDigit(CheckDigit cd) {
153 return getDigitAtPos(cd.getPos()) == cd.getDigit();
154 }
155
156 public int getRealLength() {
157 return realLength;
158 }
159
160 /**
161 * @see java.lang.Object#toString()
162 */
163 public String toString() {
164 return accountCode;
165 }
166
167 }