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   * A check digit pointer. The value of the the pos property may have special
21   * meanings represented by the MAGICs in this class
22   * 
23   * @author ninan
24   * 
25   * @see CheckDigit#ALWAYS_VALID
26   * @see CheckDigit#ALWAYS_INVALID
27   * 
28   */
29  public class CheckDigit {
30  
31  	/** Symbolic constant meaning there is no check */
32  	public static final CheckDigit NO_CHECK = null;
33  
34  	/** If pos has this value, the check digit is always valid */
35  	public static final int ALWAYS_VALID = -1;
36  
37  	/** If pos has this value, the check digit is always invalid */
38  	public static final int ALWAYS_INVALID = -2;
39  
40  	/** If pos has this value, the bank code was not found */
41  	public static final int BANK_CODE_NOT_FOUND = -3;
42  
43  	/** If pos has this value, the check was not implemented */
44  	public static final int NOT_IMPLEMENTED = -4;
45  
46  	/**
47  	 * The position of the digit in the account code. The counting is based on
48  	 * 0. So the first digit is 0.
49  	 */
50  	private final int pos;
51  
52  	/** The check digit in the account code */
53  	private final int digit;
54  
55  	/**
56  	 * The default CTOR for a check digit
57  	 * 
58  	 * @param pos
59  	 *            The position of the check digit on the account code. See
60  	 *            special meaning.
61  	 * @param digit
62  	 *            The check digit calculated by the method.
63  	 */
64  	public CheckDigit(final int pos, final int digit) {
65  
66  		if (pos < -4 || pos >= AccountCode.ACCOUNT_CODE_MAX_LENGTH) {
67  			throw new IllegalArgumentException(
68  					"Invalid position supplied. [pos='" + pos + "']");
69  		}
70  
71  		if (digit < 0 || digit > 9) {
72  			throw new IllegalArgumentException(
73  					"Invalid digit supplied. [digit='" + digit + "']");
74  		}
75  
76  		this.pos = pos;
77  		this.digit = digit;
78  	}
79  
80  	/**
81  	 * @return Returns the digit.
82  	 */
83  	public int getDigit() {
84  		return digit;
85  	}
86  
87  	/**
88  	 * @return Returns the pos.
89  	 */
90  	public int getPos() {
91  		return pos;
92  	}
93  
94  	/**
95  	 * Tells you if the check digit is always valid
96  	 * 
97  	 * @return True if the check digit is always valid, false if you need to
98  	 *         check yourself
99  	 */
100 	public boolean isAlwaysValid() {
101 		if (pos == ALWAYS_VALID)
102 			return true;
103 		else
104 			return false;
105 	}
106 
107 	/**
108 	 * Tells you if the check digit is always invalid
109 	 * 
110 	 * @return True if the check digit is always invalid, false if you need to
111 	 *         check yourself
112 	 */
113 	public boolean isAlwaysInvalid() {
114 		if (pos == ALWAYS_INVALID)
115 			return true;
116 		else
117 			return false;
118 	}
119 
120 	/**
121 	 * Tells you if the bank code was not found
122 	 * 
123 	 * @return True if the bank code was not found, false if it was
124 	 */
125 	public boolean isBankCodeNotFound() {
126 		if (pos == BANK_CODE_NOT_FOUND)
127 			return true;
128 		else
129 			return false;
130 	}
131 
132 	/**
133 	 * Tells you if the strategy for this bank code was not implemented
134 	 * 
135 	 * @return True if the strategy is not implemented, false if it exists
136 	 */
137 	public boolean isNotImplemented() {
138 		if (pos == NOT_IMPLEMENTED)
139 			return true;
140 		else
141 			return false;
142 	}
143 
144 	/**
145 	 * @see java.lang.Object#toString()
146 	 */
147 	public String toString() {
148 
149 		if (isAlwaysValid())
150 			return "Check digit is always valid";
151 
152 		if (isAlwaysInvalid())
153 			return "Check digit is always invalid";
154 
155 		if (isBankCodeNotFound())
156 			return "Bank code was not found";
157 
158 		if (isNotImplemented())
159 			return "Strategy is not implemented";
160 
161 		return "Check digit is '" + digit + "' at position '" + pos + "'";
162 	}
163 }