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.io.ObjectStreamException;
24 import java.io.Serializable;
25 import java.text.MessageFormat;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.Locale;
31 import java.util.Map;
32 import java.util.Set;
33 import org.jdtaus.core.container.ContainerFactory;
34 import org.jdtaus.core.container.PropertyException;
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class Textschluessel implements Cloneable, Comparable, Serializable
49 {
50
51
52 public static final String PROP_KEY = "org.jdtaus.banking.Textschluessel.PROP_KEY";
53
54
55 public static final String PROP_EXTENSION = "org.jdtaus.banking.Textschluessel.PROP_EXTENSION";
56
57
58 public static final String PROP_VALID_TO = "org.jdtaus.banking.Textschluessel.PROP_VALID_TO";
59
60
61 public static final String PROP_VALID_FROM = "org.jdtaus.banking.Textschluessel.PROP_VALID_FROM";
62
63
64 public static final String PROP_DEBIT = "org.jdtaus.banking.Textschluessel.PROP_DEBIT";
65
66
67 public static final String PROP_REMITTANCE = "org.jdtaus.banking.Textschluessel.PROP_REMITTANCE";
68
69
70 public static final String PROP_VARIABLE = "org.jdtaus.banking.Textschluessel.PROP_VARIABLE";
71
72
73 public static final String PROP_SHORTDESCRIPTION = "org.jdtaus.banking.Textschluessel.PROP_SHORTDESCRIPTION";
74
75
76 private static final long serialVersionUID = -8556424800883022756L;
77
78
79
80
81
82 private int key;
83
84
85
86
87
88 private int extension;
89
90
91
92
93
94 private Date validFrom;
95 private transient long validFromMillis;
96
97
98
99
100
101 private Date validTo;
102 private transient long validToMillis;
103
104
105
106
107
108 private boolean debit;
109
110
111
112
113
114 private boolean remittance;
115
116
117
118
119
120 private boolean variable;
121
122
123
124
125
126 private Map shortDescriptions = new HashMap( 10 );
127
128
129 private transient int hashCode = NO_HASHCODE;
130 private static final int NO_HASHCODE = Integer.MIN_VALUE;
131
132
133 public Textschluessel()
134 {
135 super();
136 this.assertValidProperties();
137 }
138
139
140
141
142
143
144 public int getKey()
145 {
146 return this.key;
147 }
148
149
150
151
152
153
154 public void setKey( final int value )
155 {
156 this.key = value;
157 this.hashCode = NO_HASHCODE;
158 }
159
160
161
162
163
164
165 public int getExtension()
166 {
167 return this.extension;
168 }
169
170
171
172
173
174
175 public void setExtension( final int value )
176 {
177 this.extension = value;
178 this.hashCode = NO_HASHCODE;
179 }
180
181
182
183
184
185
186
187 public Date getValidFrom()
188 {
189 return this.validFrom != null ? (Date) this.validFrom.clone() : null;
190 }
191
192
193
194
195
196
197
198 public void setValidFrom( final Date value )
199 {
200 if ( value == null )
201 {
202 this.validFrom = null;
203 this.validFromMillis = 0L;
204 }
205 else
206 {
207 this.validFrom = (Date) value.clone();
208 this.validFromMillis = value.getTime();
209 }
210 }
211
212
213
214
215
216
217
218 public Date getValidTo()
219 {
220 return this.validTo != null ? (Date) this.validTo.clone() : null;
221 }
222
223
224
225
226
227
228
229 public void setValidTo( final Date value )
230 {
231 if ( value == null )
232 {
233 this.validTo = null;
234 this.validToMillis = 0L;
235 }
236 else
237 {
238 this.validTo = (Date) value.clone();
239 this.validToMillis = 0L;
240 }
241 }
242
243
244
245
246
247
248
249
250
251
252 public boolean isValidAt( final Date date )
253 {
254 if ( date == null )
255 {
256 throw new NullPointerException( "date" );
257 }
258
259
260 return !( ( this.validFrom != null && this.validFromMillis > date.getTime() ) ||
261 ( this.validTo != null && this.validToMillis < date.getTime() ) );
262
263 }
264
265
266
267
268
269
270 public boolean isDebit()
271 {
272 return this.debit;
273 }
274
275
276
277
278
279
280 public void setDebit( final boolean value )
281 {
282 this.debit = value;
283 }
284
285
286
287
288
289
290 public boolean isRemittance()
291 {
292 return this.remittance;
293 }
294
295
296
297
298
299
300 public void setRemittance( final boolean value )
301 {
302 this.remittance = value;
303 }
304
305
306
307
308
309
310
311 public boolean isVariable()
312 {
313 return this.variable;
314 }
315
316
317
318
319
320
321
322 public void setVariable( final boolean value )
323 {
324 this.variable = value;
325 this.hashCode = NO_HASHCODE;
326 }
327
328
329
330
331
332
333
334
335 public String getShortDescription( final Locale locale )
336 {
337 final Locale l = locale == null ? Locale.getDefault() : locale;
338
339 this.assertValidProperties();
340
341
342 String description = (String) this.shortDescriptions.get( l.getLanguage().toLowerCase() );
343
344 if ( description == null )
345 {
346
347 description = (String) this.shortDescriptions.get( this.getDefaultLanguage().toLowerCase() );
348 }
349
350 if ( description == null )
351 {
352
353 description = (String) this.shortDescriptions.get( Locale.getDefault().getLanguage().toLowerCase() );
354 }
355
356 if ( description == null )
357 {
358
359 description = this.getTextschluesselDescriptionMessage(
360 this.getLocale(), new Integer( this.getKey() ), new Integer( this.getExtension() ) );
361
362 }
363
364 return new MessageFormat( description, l ).format( new Object[]
365 {
366 new Integer( this.getKey() ), new Integer( this.getExtension() )
367 } );
368
369 }
370
371
372
373
374
375
376
377
378
379
380
381
382 public String setShortDescription( final Locale locale, final String shortDescription )
383 {
384 if ( shortDescription == null )
385 {
386 throw new NullPointerException( "shortDescription" );
387 }
388
389 final Locale l = locale == null ? Locale.getDefault() : locale;
390 return (String) this.shortDescriptions.put( l.getLanguage().toLowerCase(), shortDescription );
391 }
392
393
394
395
396
397
398 public Locale[] getLocales()
399 {
400 final Set locales = new HashSet( this.shortDescriptions.size() );
401 for ( final Iterator it = this.shortDescriptions.keySet().iterator(); it.hasNext(); )
402 {
403 locales.add( new Locale( (String) it.next() ) );
404 }
405
406 return (Locale[]) locales.toArray( new Locale[ locales.size() ] );
407 }
408
409
410
411
412
413
414
415
416 protected void assertValidProperties()
417 {
418 if ( this.getDefaultLanguage() == null || this.getDefaultLanguage().length() <= 0 )
419 {
420 throw new PropertyException( "defaultLanguage", this.getDefaultLanguage() );
421 }
422 }
423
424
425
426
427
428
429
430
431
432
433
434
435
436 public int compareTo( final Object o )
437 {
438 if ( o == null )
439 {
440 throw new NullPointerException( "o" );
441 }
442 if ( !( o instanceof Textschluessel ) )
443 {
444 throw new ClassCastException( o.getClass().getName() );
445 }
446
447 int result = 0;
448 final Textschluessel that = (Textschluessel) o;
449
450 if ( !this.equals( that ) )
451 {
452 result = this.key == that.key ? 0 : this.key > that.key ? 1 : -1;
453 if ( result == 0 && this.extension != that.extension )
454 {
455 result = this.extension > that.extension ? 1 : -1;
456 }
457 }
458
459 return result;
460 }
461
462
463
464
465
466
467
468 private Object readResolve() throws ObjectStreamException
469 {
470 if ( this.validFrom != null )
471 {
472 this.validFromMillis = this.validFrom.getTime();
473 }
474 if ( this.validTo != null )
475 {
476 this.validToMillis = this.validTo.getTime();
477 }
478
479 return this;
480 }
481
482
483
484
485
486
487 public Object clone()
488 {
489 try
490 {
491 return super.clone();
492 }
493 catch ( final CloneNotSupportedException e )
494 {
495 throw new AssertionError( e );
496 }
497 }
498
499
500
501
502
503
504
505
506
507
508 public boolean equals( final Object o )
509 {
510 boolean equal = o == this;
511
512 if ( !equal && o instanceof Textschluessel )
513 {
514 final Textschluessel that = (Textschluessel) o;
515
516 if ( this.isVariable() )
517 {
518 equal = that.isVariable() && this.key == that.getKey();
519 }
520 else
521 {
522 equal = !that.isVariable() && this.key == that.getKey() && this.extension == that.getExtension();
523 }
524 }
525
526 return equal;
527 }
528
529
530
531
532
533
534 public int hashCode()
535 {
536 if ( this.hashCode == NO_HASHCODE )
537 {
538 int hc = 23;
539
540 hc = 37 * hc + ( this.variable ? 0 : 1 );
541 hc = 37 * hc + this.key;
542
543 if ( !this.variable )
544 {
545 hc = 37 * hc + this.extension;
546 }
547
548 this.hashCode = hc;
549 }
550
551 return this.hashCode;
552 }
553
554
555
556
557
558
559 public String toString()
560 {
561 return super.toString() + this.internalString();
562 }
563
564
565
566
567
568
569 private String internalString()
570 {
571 return new StringBuffer( 200 ).append( '{' ).
572 append( "key=" ).append( this.key ).
573 append( ", extension=" ).append( this.extension ).
574 append( ", validFrom=" ).append( this.validFrom ).
575 append( ", validTo=" ).append( this.validTo ).
576 append( ", debit=" ).append( this.debit ).
577 append( ", remittance=" ).append( this.remittance ).
578 append( ", variable=" ).append( this.variable ).
579 append( ", shortDescription=" ).
580 append( this.getShortDescription( null ) ).
581 append( '}' ).toString();
582
583 }
584
585
586
587
588
589
590
591
592
593
594
595 private Locale getLocale()
596 {
597 return (Locale) ContainerFactory.getContainer().
598 getDependency( this, "Locale" );
599
600 }
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621 private String getTextschluesselDescriptionMessage( final Locale locale,
622 final java.lang.Number k,
623 final java.lang.Number e )
624 {
625 return ContainerFactory.getContainer().
626 getMessage( this, "textschluesselDescription", locale,
627 new Object[]
628 {
629 k,
630 e
631 });
632
633 }
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648 private java.lang.String getDefaultLanguage()
649 {
650 return (java.lang.String) ContainerFactory.getContainer().
651 getProperty( this, "defaultLanguage" );
652
653 }
654
655
656
657
658 }