View Javadoc
1   /* ***************************************************************************************
2    * Class: com.cardatechnologies.utils.validators.abaroutevalidator.AbaRouteValidator.java
3    * Date:  2015/02/11
4    * ***************************************************************************************
5    *
6    * Copyright 2015 - CardaTechnologies, LLC
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   *
20   * ***************************************************************************************
21   */
22  
23  package com.cardatechnologies.utils.validators.abaroutevalidator;
24  
25  //~--- non-JDK imports --------------------------------------------------------
26  
27  import com.cardatechnologies.utils.validators.abaroutevalidator.exceptions.AbaRouteValidationException;
28  
29  //~--- classes ----------------------------------------------------------------
30  
31  /**
32   * <dl>
33   * <dt><font face="Arial"><span class="simpleTagLabel">All Rights Reserved</span></font></dt>
34   * <dd>Copyright &copy; 2015 - CardaTechnologies, LLC</dd>
35   * <dt><font face="Arial"><span class="simpleTagLabel">Class Description:</span></font></dt>
36   * <dd>This class is used to validate a ABA Routing Transmit Number.</dd>
37   * </dl>
38   *
39   * @version        1.0.0
40   * @author         Daniel Carda
41   */
42  public class AbaRouteValidator {
43  
44      /**
45       * Method description
46       *
47       * @param paramAbaRouteNumber The incoming ABA Number.
48       *
49       * @throws AbaRouteValidationException
50       */
51       private static void breakdownAbaNumber( final String paramAbaRouteNumber )
52              throws AbaRouteValidationException {
53  
54          // Check first two digits.
55          validateFedNumber( paramAbaRouteNumber );
56  
57          // Alright, now see if the number holds up to scrunity!
58          validateAbaNumberChecksum( paramAbaRouteNumber );
59      }
60  
61      //~--- get methods --------------------------------------------------------
62  
63      /**
64       * Method description
65       *
66       *
67       * @param paramStr The string which needs to be made up of all numbers.
68       *
69       * @return True if all the characters were digits.  False if there was a problem.
70       */
71      static private boolean isNumeric( final String paramStr ) {
72  
73          // Cycle through the character array
74          for( char _char : paramStr.toCharArray() ) {
75              if( !Character.isDigit( _char ) ) {
76  
77                  // This is bad!
78                  return ( false );
79              }
80          }
81  
82          // All is well!
83          return ( true );
84      }
85  
86      //~--- methods ------------------------------------------------------------
87  
88      /**
89       * This method is the starting point to validate whether a incoming string is
90       * an ABA Routing Transmit Number.
91       *
92       * @param paramAbaRouteNumber The ABA number to be tested.
93       *
94       * @return  True if it passes validation.  False if it fails validation.
95       *
96       * @throws AbaRouteValidationException A error occurred when parsing the suspect
97       *                                     ABA Routing Transmit Number.
98       */
99      static public boolean validate( final String paramAbaRouteNumber )
100             throws AbaRouteValidationException {
101 
102         // http://en.wikipedia.org/wiki/Routing_transit_number
103         //
104         // See FRB Regulation CC, Appendix A, which available online here:
105         // <http://www.bankersonline.com/regs/229/a229a.html>
106         // Quick Check
107         // Is the parameter null
108         if( paramAbaRouteNumber == null ) {
109             throw new AbaRouteValidationException( "The ABA Route Number was null." );
110         }
111 
112         // Quick Check
113         // Is the parameter empty
114         if( paramAbaRouteNumber.trim().equals( "" ) ) {
115             throw new AbaRouteValidationException( "The ABA Route Number was empty." );
116         }
117 
118         // Quick Check
119         // Make sure the string length is right
120         int _strLen;
121 
122         _strLen = paramAbaRouteNumber.length();
123 
124         // See if it's the right length
125         if( _strLen != 9 ) {
126 
127             // Is it to short?
128             if( _strLen < 9 ) {
129                 throw new AbaRouteValidationException( "The ABA Route Number was not long enough.  (Size was "
130                         + _strLen + ")." );
131             } else {
132 
133                 // Must be to long.
134                 throw new AbaRouteValidationException( "The ABA Route Number was tot long.  (Size was " + _strLen
135                         + ")." );
136             }
137         }
138 
139         // Quick Check
140         // Finally, let's just do a scan and make sure it's a number
141         if( !isNumeric( paramAbaRouteNumber ) ) {
142             throw new AbaRouteValidationException(
143                 "The ABA Route Number appears to have alphanumeric or formatting characters in it." );
144         }
145 
146         // -----------------------------------------------------------------------------
147         // -----------------------------------------------------------------------------
148         // So we got this far, lets start breaking it down.
149         breakdownAbaNumber( paramAbaRouteNumber );
150 
151         // Must be good!
152         return ( true );
153     }
154 
155     /**
156      * Method description
157      *
158      *
159      * @param paramAbaRouteNumber The target ABA number to test.
160      *
161      * @throws AbaRouteValidationException
162      */
163     static private void validateAbaNumberChecksum( final String paramAbaRouteNumber )
164             throws AbaRouteValidationException {
165 
166         // Set up all the int's
167         int i1, i2, i3, i4, i5, i6, i7, i8, i9;
168 
169         // Break up the string so we can look at the numbers.
170         i1 = Character.getNumericValue( paramAbaRouteNumber.charAt( 0 ) );
171         i2 = Character.getNumericValue( paramAbaRouteNumber.charAt( 1 ) );
172         i3 = Character.getNumericValue( paramAbaRouteNumber.charAt( 2 ) );
173         i4 = Character.getNumericValue( paramAbaRouteNumber.charAt( 3 ) );
174         i5 = Character.getNumericValue( paramAbaRouteNumber.charAt( 4 ) );
175         i6 = Character.getNumericValue( paramAbaRouteNumber.charAt( 5 ) );
176         i7 = Character.getNumericValue( paramAbaRouteNumber.charAt( 6 ) );
177         i8 = Character.getNumericValue( paramAbaRouteNumber.charAt( 7 ) );
178         i9 = Character.getNumericValue( paramAbaRouteNumber.charAt( 8 ) );
179 
180         // Okay, lets crank it through the formula
181         int checksumTotal;
182 
183         checksumTotal = ( ( i3 + i6 + i9 ) + ( 3 * ( i1 + i4 + i7 ) ) + ( 7 * ( i2 + i5 + i8 ) ) );
184 
185         // Check the modulus and we're done!
186         if( ( checksumTotal % 10 ) != 0 ) {
187             throw new AbaRouteValidationException( "The ABA Route Number has failed its checksum test." );
188         }
189     }
190 
191     /**
192      * This method will test to see if the first two characters, when combined to create a
193      * number, are within an acceptable range.
194      *
195      * @param paramAbaRouteNumber The target string to test.
196      *
197      * @throws AbaRouteValidationException
198      */
199     static private void validateFedNumber( final String paramAbaRouteNumber )
200             throws AbaRouteValidationException {
201 
202         // String off the first 2 numbers and see if they validate.
203         String _tempStr;
204 
205         _tempStr = paramAbaRouteNumber.substring( 0, 2 );
206 
207         // Now, convert the substring to an int
208         int _fedNumb;
209 
210         _fedNumb = Integer.parseInt( _tempStr );
211 
212         // Okay, let's see if it works!
213         if( !( ( ( _fedNumb >= 0 ) && ( _fedNumb <= 12 ) ) || ( ( _fedNumb >= 21 ) && ( _fedNumb <= 32 ) )
214                 || ( ( _fedNumb >= 61 ) && ( _fedNumb <= 72 ) ) || ( ( _fedNumb == 80 ) ) ) ) {
215             throw new AbaRouteValidationException(
216                 "The first two characters of the ABA Routing Number do no correspond to a valid Federal ABA prefix." );
217         }
218     }
219 }
220 
221 /* ***************************************************************************************
222  * Copyright 2015 - CardaTechnologies, LLC
223  *
224  * Class: com.cardatechnologies.utils.validators.abaroutevalidator.AbaRouteValidator.java  
225  * Date:  2015/02/11
226  *************************************************************************************** */