View Javadoc

1   /*
2    * Copyright 2006 Christian Kalkhoff <me@ninan.info>
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.sf.plausj.bank.german;
18  
19  /**
20   * This is a bank code
21   * 
22   * @author ninan
23   * 
24   */
25  public class BankCode {
26  
27  	/** The minimal allowed length for a bank code */
28  	private final static int BANK_CODE_MIN_LENGTH = 3;
29  
30  	/** The maximum allowd length for a bank code */
31  	private final static int BANK_CODE_MAX_LENGTH = 8;
32  
33  	/** Stores the padded bank code */
34  	private final String bankCode;
35  
36  	/** Stores the digits of the bank code as integers */
37  	private final int[] bankCodeDigits;
38  
39  	/**
40  	 * Default-CTOR for bank code objects
41  	 * 
42  	 * @param bankCode
43  	 *            The bank code this object represents
44  	 */
45  	public BankCode(final String bankCode) {
46  
47  		if (null == bankCode)
48  			throw new IllegalArgumentException(
49  					"Null pointer supplied as bank code.");
50  
51  		if (bankCode.length() < BANK_CODE_MIN_LENGTH)
52  			throw new IllegalArgumentException(
53  					"Supplied bank code is too short. It must have at least "
54  							+ BANK_CODE_MIN_LENGTH + " digits. [bankCode='"
55  							+ bankCode + "']");
56  
57  		if (bankCode.length() > BANK_CODE_MAX_LENGTH)
58  			throw new IllegalArgumentException(
59  					"Supplied bank code is too long. It may have at most "
60  							+ BANK_CODE_MAX_LENGTH + " digits. [accountCode='"
61  							+ bankCode + "']");
62  
63  		for (int i = 0; i < bankCode.length(); ++i) {
64  			char d = bankCode.charAt(i);
65  			// Checks if the character is one of 0 to 9 (48 is ascii for 0,
66  			// 57 is ascii for 9)
67  			if (!(d >= 48 && d <= 57)) {
68  				throw new IllegalArgumentException(
69  						"The supplied bank code must only consist of decimal numbers from 0 to 9. [bankCode='"
70  								+ bankCode + "']");
71  			}
72  		}
73  
74  		final int len = bankCode.length();
75  		final int insertat = BANK_CODE_MAX_LENGTH - len;
76  
77  		if (BANK_CODE_MAX_LENGTH == len) {
78  			this.bankCode = bankCode;
79  		} else {
80  
81  			final StringBuffer sb = new StringBuffer(10);
82  			for (int i = 0; i < insertat; ++i) {
83  				sb.insert(i, '0');
84  
85  			}
86  			sb.insert(insertat, bankCode);
87  			this.bankCode = sb.toString();
88  		}
89  
90  		bankCodeDigits = new int[8];
91  		for (int i = 0; i < BANK_CODE_MAX_LENGTH; ++i) {
92  			bankCodeDigits[i] = Integer.parseInt(this.bankCode.substring(i,
93  					i + 1));
94  		}
95  
96  	}
97  
98  	/**
99  	 * @return Returns the bankCode.
100 	 */
101 	public String getBankCode() {
102 		return bankCode;
103 	}
104 
105 	/**
106 	 * Returns the digit at the specified position of the bank code
107 	 * 
108 	 * @param pos
109 	 *            The position to return
110 	 * @return The digit at that position
111 	 */
112 	public int getDigitAtPos(int pos) {
113 		return bankCodeDigits[pos];
114 	}
115 
116 	/**
117 	 * @see java.lang.Object#toString()
118 	 */
119 	public String toString() {
120 		return this.bankCode;
121 	}
122 
123 }