1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.jdtaus.banking;
22
23 import java.lang.ref.Reference;
24 import java.lang.ref.SoftReference;
25 import java.text.DecimalFormat;
26 import java.text.ParseException;
27 import java.text.ParsePosition;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.Map;
31
32
33
34
35
36
37
38
39
40
41
42
43 public final class Bankleitzahl extends Number implements Comparable
44 {
45
46
47
48
49
50 public static final int ELECTRONIC_FORMAT = 3001;
51
52
53
54
55
56
57
58 public static final int LETTER_FORMAT = 3002;
59
60
61 public static final int MAX_DIGITS = 8;
62
63
64 public static final int MAX_CHARACTERS = 10;
65
66
67 private static final double[] EXP10 =
68 {
69 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000
70 };
71
72
73 private static final long serialVersionUID = -3329406998979147668L;
74
75
76 private static volatile Reference cacheReference = new SoftReference( null );
77
78
79
80
81
82 private int blz;
83
84
85
86
87
88 private int clearingArea;
89
90
91
92
93
94 private int localityCode;
95
96
97
98
99
100 private int networkCode;
101
102
103
104
105
106 private int instituteCode;
107
108
109
110
111
112
113
114
115
116
117
118 private Bankleitzahl( final Number bankCode )
119 {
120 if ( !Bankleitzahl.checkBankleitzahl( bankCode ) )
121 {
122 throw new IllegalArgumentException( bankCode.toString() );
123 }
124
125 final int[] digits = Bankleitzahl.toDigits( bankCode.longValue() );
126 final long lCode = bankCode.longValue();
127
128 this.clearingArea = digits[7];
129 this.localityCode = (int) Math.floor( lCode / Bankleitzahl.EXP10[5] );
130 this.networkCode = digits[4];
131 this.instituteCode =
132 (int) Math.floor( lCode - digits[7] * Bankleitzahl.EXP10[7] -
133 digits[6] * Bankleitzahl.EXP10[6] -
134 digits[5] * Bankleitzahl.EXP10[5] -
135 digits[4] * Bankleitzahl.EXP10[4] );
136
137 this.blz = bankCode.intValue();
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154 public static Bankleitzahl parse( final String bankCode, final ParsePosition pos )
155 {
156 if ( bankCode == null )
157 {
158 throw new NullPointerException( "bankCode" );
159 }
160 if ( pos == null )
161 {
162 throw new NullPointerException( "pos" );
163 }
164
165 Bankleitzahl ret = null;
166 boolean sawSpace = false;
167 boolean failed = false;
168 final ParsePosition fmtPos = new ParsePosition( 0 );
169 final int len = bankCode.length();
170 final int startIndex = pos.getIndex();
171 final int maxIndex = startIndex + MAX_CHARACTERS;
172 final StringBuffer digits = new StringBuffer( MAX_DIGITS );
173 int mode = ELECTRONIC_FORMAT;
174 int part = 0;
175 int partStart = 0;
176 int partEnd = 2;
177 int digit = 0;
178 int i = startIndex;
179
180 for ( ; i < len && i < maxIndex && digits.length() < MAX_DIGITS; i++ )
181 {
182 final char c = bankCode.charAt( i );
183
184 if ( Character.isDigit( c ) )
185 {
186 sawSpace = false;
187
188 if ( mode == LETTER_FORMAT )
189 {
190 if ( digit < partStart || digit > partEnd )
191 {
192 failed = true;
193 }
194 else
195 {
196 digits.append( c );
197 }
198 }
199 else
200 {
201 digits.append( c );
202 }
203
204 digit++;
205 }
206 else if ( c == ' ' )
207 {
208 if ( sawSpace || i == startIndex || ( mode == ELECTRONIC_FORMAT && digit != 3 ) )
209 {
210 failed = true;
211 }
212 else
213 {
214 mode = LETTER_FORMAT;
215 switch ( part )
216 {
217 case 0:
218 partStart = 3;
219 partEnd = 5;
220 break;
221 case 1:
222 partStart = 6;
223 partEnd = 7;
224 break;
225 default:
226 failed = true;
227 break;
228 }
229 part++;
230
231 if ( digit < partStart || digit > partEnd )
232 {
233 failed = true;
234 }
235 }
236
237 sawSpace = true;
238 }
239 else
240 {
241 failed = true;
242 }
243
244 if ( failed )
245 {
246 pos.setErrorIndex( i );
247 break;
248 }
249 }
250
251 if ( !failed )
252 {
253 final Number num = new DecimalFormat( "########" ).parse( digits.toString(), fmtPos );
254
255 if ( num != null && fmtPos.getErrorIndex() == -1 )
256 {
257 final String key = num.toString();
258 ret = (Bankleitzahl) getCache().get( key );
259
260 if ( ret == null )
261 {
262 if ( !Bankleitzahl.checkBankleitzahl( num ) )
263 {
264 pos.setErrorIndex( startIndex );
265 ret = null;
266 }
267 else
268 {
269 pos.setIndex( i );
270 ret = new Bankleitzahl( num );
271 getCache().put( key, ret );
272 }
273 }
274 else
275 {
276 pos.setIndex( i );
277 }
278 }
279 else
280 {
281 pos.setErrorIndex( startIndex );
282 }
283 }
284
285 return ret;
286 }
287
288
289
290
291
292
293
294
295
296
297
298
299
300 public static Bankleitzahl parse( final String bankCode ) throws ParseException
301 {
302 if ( bankCode == null )
303 {
304 throw new NullPointerException( "bankCode" );
305 }
306
307 Bankleitzahl blz = (Bankleitzahl) getCache().get( bankCode );
308
309 if ( blz == null )
310 {
311 final ParsePosition pos = new ParsePosition( 0 );
312 blz = Bankleitzahl.parse( bankCode, pos );
313
314 if ( blz == null || pos.getErrorIndex() != -1 || pos.getIndex() < bankCode.length() )
315 {
316 throw new ParseException( bankCode, pos.getErrorIndex() != -1 ? pos.getErrorIndex() : pos.getIndex() );
317 }
318 else
319 {
320 getCache().put( bankCode, blz );
321 }
322 }
323
324 return blz;
325 }
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340 public static Bankleitzahl valueOf( final Number bankCode )
341 {
342 if ( bankCode == null )
343 {
344 throw new NullPointerException( "bankCode" );
345 }
346
347 final String key = bankCode.toString();
348 Bankleitzahl ret = (Bankleitzahl) getCache().get( key );
349
350 if ( ret == null )
351 {
352 ret = new Bankleitzahl( bankCode );
353 getCache().put( key, ret );
354 }
355
356 return ret;
357 }
358
359
360
361
362
363
364
365
366
367
368
369
370
371 public static Bankleitzahl valueOf( final String bankCode )
372 {
373 try
374 {
375 return Bankleitzahl.parse( bankCode );
376 }
377 catch ( final ParseException e )
378 {
379 throw (IllegalArgumentException) new IllegalArgumentException( bankCode ).initCause( e );
380 }
381 }
382
383
384
385
386
387
388
389
390 public static boolean checkBankleitzahl( final Number bankCode )
391 {
392 boolean valid = bankCode != null;
393
394 if ( valid )
395 {
396 final long num = bankCode.longValue();
397 valid = num > 0L && num < 100000000L;
398 if ( valid && num > 9999999 )
399 {
400 final int[] digits = Bankleitzahl.toDigits( num );
401 valid = digits[7] != 0 && digits[7] != 9;
402 }
403 }
404
405 return valid;
406 }
407
408
409
410
411
412
413 public int intValue()
414 {
415 return this.blz;
416 }
417
418
419
420
421
422
423 public long longValue()
424 {
425 return this.blz;
426 }
427
428
429
430
431
432
433 public float floatValue()
434 {
435 return this.blz;
436 }
437
438
439
440
441
442
443 public double doubleValue()
444 {
445 return this.blz;
446 }
447
448
449
450
451
452
453
454
455
456 public boolean isClearingAreaCodeSupported()
457 {
458 return this.blz > 9999999;
459 }
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482 public int getClearingArea()
483 {
484 return this.getClearingAreaCode();
485 }
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506 public int getClearingAreaCode()
507 {
508 if ( !this.isClearingAreaCodeSupported() )
509 {
510 throw new UnsupportedOperationException();
511 }
512
513 return this.clearingArea;
514 }
515
516
517
518
519
520
521
522
523
524 public boolean isLocalityCodeSupported()
525 {
526 return this.blz > 99999;
527 }
528
529
530
531
532
533
534
535
536
537
538 public int getLocalityCode()
539 {
540 if ( !this.isLocalityCodeSupported() )
541 {
542 throw new UnsupportedOperationException();
543 }
544
545 return this.localityCode;
546 }
547
548
549
550
551
552
553
554
555
556 public boolean isNetworkCodeSupported()
557 {
558 return this.blz > 9999;
559 }
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605 public int getNetworkCode()
606 {
607 if ( !this.isNetworkCodeSupported() )
608 {
609 throw new UnsupportedOperationException();
610 }
611
612 return this.networkCode;
613 }
614
615
616
617
618
619
620 public int getInstituteCode()
621 {
622 return this.instituteCode;
623 }
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639 public StringBuffer format( final int style, final StringBuffer toAppendTo )
640 {
641 if ( toAppendTo == null )
642 {
643 throw new NullPointerException( "toAppendTo" );
644 }
645 if ( style != Bankleitzahl.ELECTRONIC_FORMAT && style != Bankleitzahl.LETTER_FORMAT )
646 {
647 throw new IllegalArgumentException( Integer.toString( style ) );
648 }
649
650 final int[] digits = Bankleitzahl.toDigits( this.blz );
651 for ( int i = digits.length - 1, lastDigit = 0; i >= 0; i-- )
652 {
653 if ( digits[i] != 0 || lastDigit > 0 )
654 {
655 toAppendTo.append( digits[i] );
656 lastDigit++;
657 }
658
659 if ( style == Bankleitzahl.LETTER_FORMAT && ( lastDigit == 3 || lastDigit == 6 ) )
660 {
661 toAppendTo.append( ' ' );
662 }
663 }
664
665 return toAppendTo;
666 }
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683 public String format( final int style )
684 {
685 return this.format( style, new StringBuffer() ).toString();
686 }
687
688
689
690
691
692
693
694
695
696
697
698
699
700 public static String toString( final Bankleitzahl bankleitzahl )
701 {
702 if ( bankleitzahl == null )
703 {
704 throw new NullPointerException( "bankleitzahl" );
705 }
706
707 return bankleitzahl.format( ELECTRONIC_FORMAT );
708 }
709
710
711
712
713
714
715
716
717 private static int[] toDigits( final long number )
718 {
719 int i;
720 int j;
721 int subst;
722 final int[] ret = new int[ MAX_DIGITS ];
723
724 for ( i = MAX_DIGITS - 1; i >= 0; i-- )
725 {
726 for ( j = i + 1, subst = 0; j < MAX_DIGITS; j++ )
727 {
728 subst += ret[j] * EXP10[j];
729 }
730 ret[i] = (int) Math.floor( ( number - subst ) / EXP10[i] );
731 }
732
733 return ret;
734 }
735
736
737
738
739
740
741 private String internalString()
742 {
743 return new StringBuffer( 500 ).append( '{' ).
744 append( "blz=" ).append( this.blz ).
745 append( ", clearingAreaCodeSupported=" ).
746 append( this.isClearingAreaCodeSupported() ).
747 append( ", clearingArea=" ).append( this.clearingArea ).
748 append( ", instituteCode=" ).append( this.instituteCode ).
749 append( ", localityCodeSupported=" ).
750 append( this.isLocalityCodeSupported() ).
751 append( ", localityCode=" ).append( this.localityCode ).
752 append( ", networkCodeSupported=" ).
753 append( this.isNetworkCodeSupported() ).
754 append( ", networkCode=" ).append( this.networkCode ).
755 append( '}' ).toString();
756
757 }
758
759
760
761
762
763
764 private static Map getCache()
765 {
766 Map cache = (Map) cacheReference.get();
767 if ( cache == null )
768 {
769 cache = Collections.synchronizedMap( new HashMap( 1024 ) );
770 cacheReference = new SoftReference( cache );
771 }
772
773 return cache;
774 }
775
776
777
778
779
780
781
782
783
784
785
786
787 public int compareTo( final Object o )
788 {
789 if ( o == null )
790 {
791 throw new NullPointerException( "o" );
792 }
793 if ( !( o instanceof Bankleitzahl ) )
794 {
795 throw new ClassCastException( o.getClass().getName() );
796 }
797
798 int result = 0;
799 final Bankleitzahl that = (Bankleitzahl) o;
800
801 if ( !this.equals( that ) )
802 {
803 result = this.blz > that.blz
804 ? 1
805 : -1;
806 }
807
808 return result;
809 }
810
811
812
813
814
815
816
817
818 public boolean equals( final Object o )
819 {
820 boolean equal = o == this;
821
822 if ( !equal && o instanceof Bankleitzahl )
823 {
824 equal = this.blz == ( (Bankleitzahl) o ).blz;
825 }
826
827 return equal;
828 }
829
830
831
832
833
834
835 public int hashCode()
836 {
837 return this.blz;
838 }
839
840
841
842
843
844
845 public String toString()
846 {
847 return super.toString() + this.internalString();
848 }
849
850 }