001/* 002 * jDTAUS Banking API 003 * Copyright (C) 2005 Christian Schulte 004 * <cs@schulte.it> 005 * 006 * This library is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 2.1 of the License, or any later version. 010 * 011 * This library is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with this library; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 019 * 020 */ 021package org.jdtaus.banking; 022 023import java.lang.ref.Reference; 024import java.lang.ref.SoftReference; 025import java.text.DecimalFormat; 026import java.text.ParseException; 027import java.text.ParsePosition; 028import java.util.Collections; 029import java.util.HashMap; 030import java.util.Map; 031 032/** 033 * Unique entity identifier. 034 * <p>A Referenznummer11 is a positive integer with a maximum of eleven 035 * digits.</p> 036 * 037 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 038 * @version $JDTAUS: Referenznummer11.java 8661 2012-09-27 11:29:58Z schulte $ 039 */ 040public final class Referenznummer11 extends Number implements Comparable 041{ 042 043 /** 044 * Constant for the electronic format of a Referenznummer11. 045 * <p>The electronic format of a Referenznummer11 is an eleven digit number with leading zeros omitted 046 * (e.g. 6789).</p> 047 */ 048 public static final int ELECTRONIC_FORMAT = 6001; 049 050 /** 051 * Constant for the letter format of a Referenznummer11. 052 * <p>The letter format of a Referenznummer11 is an eleven digit number with leading zeros omitted separated by 053 * spaces between the first three digits and the second three digits, the second three digits and the third three 054 * digits, and between the third three digits and the last two digits 055 * (e.g. 123 456 789 01).</p> 056 */ 057 public static final int LETTER_FORMAT = 6002; 058 059 /** Maximum number of digits of a Referenznummer11. */ 060 public static final int MAX_DIGITS = 11; 061 062 /** Maximum number of characters of a Referenznummer11. */ 063 public static final int MAX_CHARACTERS = 14; 064 065 /** {@code 10^0..10^10}. */ 066 private static final double[] EXP10 = 067 { 068 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 069 1000000000, 10000000000L 070 }; 071 072 /** Serial version UID for backwards compatibility with 1.0.x classes. */ 073 private static final long serialVersionUID = -6729406850413828467L; 074 075 /** Used to cache instances. */ 076 private static volatile Reference cacheReference = new SoftReference( null ); 077 078 /** 079 * Reference code. 080 * @serial 081 */ 082 private long ref; 083 084 /** 085 * Creates a new {@code Referenznummer11} instance. 086 * 087 * @param referenceCode The long to create an instance from. 088 * 089 * @throws IllegalArgumentException if {@code referenceCode} is negative, zero or greater than 99999999999. 090 * 091 * @see #checkReferenznummer11(Number) 092 */ 093 private Referenznummer11( final Number referenceCode ) 094 { 095 if ( !Referenznummer11.checkReferenznummer11( referenceCode ) ) 096 { 097 throw new IllegalArgumentException( referenceCode.toString() ); 098 } 099 100 this.ref = referenceCode.longValue(); 101 } 102 103 /** 104 * Parses text from a string to produce a {@code Referenznummer11}. 105 * <p>The method attempts to parse text starting at the index given by {@code pos}. If parsing succeeds, then the 106 * index of {@code pos} is updated to the index after the last character used (parsing does not necessarily use all 107 * characters up to the end of the string), and the parsed value is returned. The updated {@code pos} can be used to 108 * indicate the starting point for the next call to this method.</p> 109 * 110 * @param referenceCode A Referenznummer11 in either electronic or letter format. 111 * @param pos A {@code ParsePosition} object with index and error index information as described above. 112 * 113 * @return The parsed value, or {@code null} if the parse fails. 114 * 115 * @throws NullPointerException if either {@code referenceCode} or {@code pos} is {@code null}. 116 */ 117 public static Referenznummer11 parse( final String referenceCode, final ParsePosition pos ) 118 { 119 if ( referenceCode == null ) 120 { 121 throw new NullPointerException( "referenceCode" ); 122 } 123 if ( pos == null ) 124 { 125 throw new NullPointerException( "pos" ); 126 } 127 128 Referenznummer11 ret = null; 129 boolean sawSpace = false; 130 boolean failed = false; 131 final ParsePosition fmtPos = new ParsePosition( 0 ); 132 final int len = referenceCode.length(); 133 final int startIndex = pos.getIndex(); 134 final int maxIndex = startIndex + MAX_CHARACTERS; 135 final StringBuffer digits = new StringBuffer( MAX_DIGITS ); 136 int mode = ELECTRONIC_FORMAT; 137 int part = 0; 138 int partStart = 0; 139 int partEnd = 2; 140 int digit = 0; 141 int i = startIndex; 142 143 for ( ; i < len && i < maxIndex && digits.length() < MAX_DIGITS; i++ ) 144 { 145 final char c = referenceCode.charAt( i ); 146 147 if ( Character.isDigit( c ) ) 148 { 149 sawSpace = false; 150 151 if ( mode == LETTER_FORMAT ) 152 { 153 if ( digit < partStart || digit > partEnd ) 154 { 155 failed = true; 156 } 157 else 158 { 159 digits.append( c ); 160 } 161 } 162 else 163 { 164 digits.append( c ); 165 } 166 167 digit++; 168 } 169 else if ( c == ' ' ) 170 { 171 if ( sawSpace || i == startIndex || ( mode == ELECTRONIC_FORMAT && digit != 3 ) ) 172 { 173 failed = true; 174 } 175 else 176 { 177 mode = LETTER_FORMAT; 178 switch ( part ) 179 { 180 case 0: 181 partStart = 3; 182 partEnd = 5; 183 break; 184 case 1: 185 partStart = 6; 186 partEnd = 8; 187 break; 188 case 2: 189 partStart = 9; 190 partEnd = 10; 191 break; 192 default: 193 failed = true; 194 break; 195 } 196 part++; 197 198 if ( digit < partStart || digit > partEnd ) 199 { 200 failed = true; 201 } 202 } 203 204 sawSpace = true; 205 } 206 else 207 { 208 failed = true; 209 } 210 211 if ( failed ) 212 { 213 pos.setErrorIndex( i ); 214 break; 215 } 216 } 217 218 if ( !failed ) 219 { 220 final Number num = new DecimalFormat( "###########" ).parse( digits.toString(), fmtPos ); 221 if ( num != null && fmtPos.getErrorIndex() == -1 ) 222 { 223 final String key = num.toString(); 224 ret = (Referenznummer11) getCache().get( key ); 225 226 if ( ret == null ) 227 { 228 if ( !Referenznummer11.checkReferenznummer11( num ) ) 229 { 230 pos.setErrorIndex( startIndex ); 231 ret = null; 232 } 233 else 234 { 235 pos.setIndex( i ); 236 ret = new Referenznummer11( num ); 237 getCache().put( key, ret ); 238 } 239 } 240 else 241 { 242 pos.setIndex( i ); 243 } 244 } 245 else 246 { 247 pos.setErrorIndex( startIndex ); 248 } 249 } 250 251 return ret; 252 } 253 254 /** 255 * Parses text from the beginning of the given string to produce a {@code Referenznummer11}. 256 * <p>Unlike the {@link #parse(String, ParsePosition)} method this method throws a {@code ParseException} if 257 * {@code referenceCode} cannot be parsed or is of invalid length.</p> 258 * 259 * @param referenceCode A Referenznummer11 in either electronic or letter format. 260 * 261 * @return The parsed value. 262 * 263 * @throws NullPointerException if {@code referenceCode} is {@code null}. 264 * @throws ParseException if the parse fails or {@code referenceCode} is of invalid length. 265 */ 266 public static Referenznummer11 parse( final String referenceCode ) throws ParseException 267 { 268 if ( referenceCode == null ) 269 { 270 throw new NullPointerException( "referenceCode" ); 271 } 272 273 Referenznummer11 ref = (Referenznummer11) getCache().get( referenceCode ); 274 275 if ( ref == null ) 276 { 277 final ParsePosition pos = new ParsePosition( 0 ); 278 ref = Referenznummer11.parse( referenceCode, pos ); 279 if ( ref == null || pos.getErrorIndex() != -1 || pos.getIndex() < referenceCode.length() ) 280 { 281 throw new ParseException( referenceCode, 282 pos.getErrorIndex() != -1 ? pos.getErrorIndex() : pos.getIndex() ); 283 284 } 285 else 286 { 287 getCache().put( referenceCode, ref ); 288 } 289 } 290 291 return ref; 292 } 293 294 /** 295 * Returns an instance for the Referenznummer11 identified by the given number. 296 * 297 * @param referenceCode A number identifying a Referenznummer11. 298 * 299 * @return An instance for {@code referenceCode}. 300 * 301 * @throws IllegalArgumentException if {@code referenceCode} is negative, zero or greater than 99999999999. 302 * 303 * @see #checkReferenznummer11(Number) 304 */ 305 public static Referenznummer11 valueOf( final Number referenceCode ) 306 { 307 final String key = referenceCode.toString(); 308 Referenznummer11 ret = (Referenznummer11) getCache().get( key ); 309 310 if ( ret == null ) 311 { 312 ret = new Referenznummer11( referenceCode ); 313 getCache().put( key, ret ); 314 } 315 316 return ret; 317 } 318 319 /** 320 * Parses text from the beginning of the given string to produce a {@code Referenznummer11}. 321 * <p>Unlike the {@link #parse(String)} method this method throws an {@code IllegalArgumentException} if 322 * {@code referenceCode} cannot be parsed or is of invalid length.</p> 323 * 324 * @param referenceCode A Referenznummer11 in either electronic or letter format. 325 * 326 * @return The parsed value. 327 * 328 * @throws NullPointerException if {@code referenceCode} is {@code null}. 329 * @throws IllegalArgumentException if the parse fails or {@code referenceCode} is of invalid length. 330 */ 331 public static Referenznummer11 valueOf( final String referenceCode ) 332 { 333 try 334 { 335 return Referenznummer11.parse( referenceCode ); 336 } 337 catch ( final ParseException e ) 338 { 339 throw (IllegalArgumentException) new IllegalArgumentException( referenceCode ).initCause( e ); 340 } 341 } 342 343 /** 344 * Checks a given number to conform to a Referenznummer11. 345 * 346 * @param referenceCode The number to check. 347 * 348 * @return {@code true} if {@code referenceCode} is a valid Referenznummer11; {@code false} if not. 349 */ 350 public static boolean checkReferenznummer11( final Number referenceCode ) 351 { 352 boolean valid = referenceCode != null; 353 354 if ( valid ) 355 { 356 final long num = referenceCode.longValue(); 357 valid = num >= 0L && num < 100000000000L; 358 } 359 360 return valid; 361 } 362 363 /** 364 * Returns this Referenznummer11 as an int value. 365 * 366 * @return This Referenznummer11 as an int value. 367 */ 368 public int intValue() 369 { 370 return (int) this.ref; 371 } 372 373 /** 374 * Returns this Referenznummer11 as a long value. 375 * 376 * @return This Referenznummer11 as a long value. 377 */ 378 public long longValue() 379 { 380 return this.ref; 381 } 382 383 /** 384 * Returns this Referenznummer11 as a float value. 385 * 386 * @return This Referenznummer11 as a float value. 387 */ 388 public float floatValue() 389 { 390 return this.ref; 391 } 392 393 /** 394 * Returns this Referenznummer11 as a double value. 395 * 396 * @return This Referenznummer11 as a double value. 397 */ 398 public double doubleValue() 399 { 400 return this.ref; 401 } 402 403 /** 404 * Formats a Referenznummer11 and appends the resulting text to the given string buffer. 405 * 406 * @param style The style to use ({@code ELECTRONIC_FORMAT} or {@code LETTER_FORMAT}). 407 * @param toAppendTo The buffer to which the formatted text is to be appended. 408 * 409 * @return The value passed in as {@code toAppendTo}. 410 * 411 * @throws NullPointerException if {@code toAppendTo} is {@code null}. 412 * @throws IllegalArgumentException if {@code style} is neither {@code ELECTRONIC_FORMAT} nor {@code LETTER_FORMAT}. 413 * 414 * @see #ELECTRONIC_FORMAT 415 * @see #LETTER_FORMAT 416 */ 417 public StringBuffer format( final int style, final StringBuffer toAppendTo ) 418 { 419 if ( toAppendTo == null ) 420 { 421 throw new NullPointerException( "toAppendTo" ); 422 } 423 if ( style != Referenznummer11.ELECTRONIC_FORMAT && style != Referenznummer11.LETTER_FORMAT ) 424 { 425 throw new IllegalArgumentException( Integer.toString( style ) ); 426 } 427 428 if ( this.ref == 0L ) 429 { 430 toAppendTo.append( '0' ); 431 } 432 else 433 { 434 final int[] digits = Referenznummer11.toDigits( this.ref ); 435 for ( int i = digits.length - 1, lastDigit = 0; i >= 0; i-- ) 436 { 437 if ( digits[i] != 0 || lastDigit > 0 ) 438 { 439 toAppendTo.append( digits[i] ); 440 lastDigit++; 441 } 442 443 if ( style == Referenznummer11.LETTER_FORMAT && ( lastDigit == 3 || lastDigit == 6 || lastDigit == 9 ) ) 444 { 445 toAppendTo.append( ' ' ); 446 } 447 } 448 } 449 450 return toAppendTo; 451 } 452 453 /** 454 * Formats a Referenznummer11 to produce a string. Same as 455 * <blockquote> 456 * {@link #format(int, StringBuffer) format<code>(style, new StringBuffer()).toString()</code>} 457 * </blockquote> 458 * 459 * @param style The style to use ({@code ELECTRONIC_FORMAT} or {@code LETTER_FORMAT}). 460 * 461 * @return The formatted string. 462 * 463 * @throws IllegalArgumentException if {@code style} is neither {@code ELECTRONIC_FORMAT} nor {@code LETTER_FORMAT}. 464 * 465 * @see #ELECTRONIC_FORMAT 466 * @see #LETTER_FORMAT 467 */ 468 public String format( final int style ) 469 { 470 return this.format( style, new StringBuffer() ).toString(); 471 } 472 473 /** 474 * Formats a Referenznummer11 to produce a string. Same as 475 * <blockquote> 476 * {@link #format(int) referenznummer11.format(ELECTRONIC_FORMAT)} 477 * </blockquote> 478 * 479 * @param referenznummer11 The {@code Referenznummer11} instance to format. 480 * 481 * @return The formatted string. 482 * 483 * @throws NullPointerException if {@code referenznummer11} is {@code null}. 484 */ 485 public static String toString( final Referenznummer11 referenznummer11 ) 486 { 487 if ( referenznummer11 == null ) 488 { 489 throw new NullPointerException( "referenznummer11" ); 490 } 491 492 return referenznummer11.format( ELECTRONIC_FORMAT ); 493 } 494 495 /** 496 * Creates an array holding the digits of {@code number}. 497 * 498 * @param number The number to return the digits for. 499 * 500 * @return An array holding the digits of {@code number}. 501 */ 502 private static int[] toDigits( final long number ) 503 { 504 int i; 505 int j; 506 long subst; 507 final int[] ret = new int[ MAX_DIGITS ]; 508 509 for ( i = MAX_DIGITS - 1; i >= 0; i-- ) 510 { 511 for ( j = i + 1, subst = 0L; j < MAX_DIGITS; j++ ) 512 { 513 subst += ret[j] * EXP10[j]; 514 } 515 ret[i] = (int) Math.floor( ( number - subst ) / EXP10[i] ); 516 } 517 518 return ret; 519 } 520 521 /** 522 * Creates a string representing the properties of the instance. 523 * 524 * @return A string representing the properties of the instance. 525 */ 526 private String internalString() 527 { 528 return new StringBuffer( 500 ).append( "{referenceNumber=" ). 529 append( this.ref ).append( '}' ).toString(); 530 531 } 532 533 /** 534 * Gets the current cache instance. 535 * 536 * @return Current cache instance. 537 */ 538 private static Map getCache() 539 { 540 Map cache = (Map) cacheReference.get(); 541 if ( cache == null ) 542 { 543 cache = Collections.synchronizedMap( new HashMap( 1024 ) ); 544 cacheReference = new SoftReference( cache ); 545 } 546 547 return cache; 548 } 549 550 /** 551 * Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer 552 * as this object is less than, equal to, or greater than the specified object.<p> 553 * 554 * @param o The Object to be compared. 555 * @return A negative integer, zero, or a positive integer as this object is less than, equal to, or greater than 556 * the specified object. 557 * 558 * @throws NullPointerException if {@code o} is {@code null}. 559 * @throws ClassCastException if the specified object's type prevents it from being compared to this Object. 560 */ 561 public int compareTo( final Object o ) 562 { 563 if ( o == null ) 564 { 565 throw new NullPointerException( "o" ); 566 } 567 if ( !( o instanceof Referenznummer11 ) ) 568 { 569 throw new ClassCastException( o.getClass().getName() ); 570 } 571 572 int result = 0; 573 final Referenznummer11 that = (Referenznummer11) o; 574 575 if ( !this.equals( that ) ) 576 { 577 result = this.ref > that.ref ? 1 : -1; 578 } 579 580 return result; 581 } 582 583 /** 584 * Indicates whether some other object is equal to this one. 585 * 586 * @param o The reference object with which to compare. 587 * 588 * @return {@code true} if this object is the same as {@code o}; {@code false} otherwise. 589 */ 590 public boolean equals( final Object o ) 591 { 592 boolean equal = o == this; 593 594 if ( !equal && o instanceof Referenznummer11 ) 595 { 596 equal = this.ref == ( (Referenznummer11) o ).ref; 597 } 598 599 return equal; 600 } 601 602 /** 603 * Returns a hash code value for this object. 604 * 605 * @return A hash code value for this object. 606 */ 607 public int hashCode() 608 { 609 return (int) ( this.ref ^ ( this.ref >>> 32 ) ); 610 } 611 612 /** 613 * Returns a string representation of the object. 614 * 615 * @return A string representation of the object. 616 */ 617 public String toString() 618 { 619 return super.toString() + this.internalString(); 620 } 621 622}