/
Currency.java
693 lines (622 loc) · 24.1 KB
/
Currency.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
package com.jbidwatcher.util;
/*
* Copyright (c) 2000-2007, CyberFOX Software, Inc. All Rights Reserved.
*
* Developed by mrs (Morgan Schweers)
*/
import com.jbidwatcher.util.config.JConfig;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.Map;
import java.util.HashMap;
import java.beans.PersistenceDelegate;
import java.beans.DefaultPersistenceDelegate;
public class Currency implements Comparable {
public static final String VALUE_REGEX="^[0-9]+([,.0-9]*)$";
public static final String NAME_REGEX = "(USD|GBP|JPY|CHF|FRF|EUR|CAD|AUD|NTD|TWD|HKD|MYR|SGD|INR)";
private static NumberFormat df = NumberFormat.getNumberInstance(Locale.US); // We create a lot of these, so minimizing memory usage is good.
public static final int NONE=0, US_DOLLAR=1, UK_POUND=2, JP_YEN=3, GER_MARK=4, FR_FRANC=5, CAN_DOLLAR=6;
public static final int EURO=7, AU_DOLLAR=8, CH_FRANC=9, NT_DOLLAR=10, TW_DOLLAR=10, HK_DOLLAR=11;
public static final int MY_REAL=12, SG_DOLLAR=13, IND_RUPEE=14;
private static Currency _noValue = null;
/**
* @brief This provides a concept of a currency value that is
* invalid, not just 'zero' in some arbitrary currency.
*
* @return A single, consistent, 'Empty Value', which indicates an
* invalid currency.
*/
public static Currency NoValue() {
if(_noValue == null) _noValue = new Currency(NONE, 0.0);
return _noValue;
}
protected int mCurrencyType;
protected double mValue;
private static final char pound = '\u00A3';
private static final Character objPound = '\u00A3';
private static Map<Integer,Double> sCurrencyMap = new HashMap<Integer,Double>();
/**
* Convert a non-US currency to USD, usually for sorting purposes.
*
* Takes two values (usd, non-usd) which are believed to be
* equivalent, and a currency amount to convert based off the
* ratio between the first two. If the USD amount is null or $0,
* it looks in a table it keeps around for converting. If that fails,
* it just returns the non-USD's value.
*
* @param usd - A sample US dollar amount.
* @param nonusd - A non-US dollar amount that is equivalent to the usd paramter.
* @param cvt - The non-USD amount to convert to USD.
*
* @return - 'cvt' converted by the ratio of usd:nonusd, or by an internal table if
* it couldn't figure out the ratio, or just the non-usd's amount as a USD amount if
* there wasn't even an entry in the table.
*/
public static Currency convertToUSD(Currency usd, Currency nonusd, Currency cvt) {
if(cvt != null && !cvt.isNull() && cvt.getCurrencyType() != US_DOLLAR) {
double multiple;
if(usd == null || usd.isNull() || usd.getValue() == 0.0 ||
nonusd == null || nonusd.isNull() || nonusd.getValue() == 0.0) {
if(sCurrencyMap.containsKey(cvt.getCurrencyType())) {
multiple = sCurrencyMap.get(cvt.getCurrencyType());
} else {
// If we have nothing else to go on, treat it as exactly equal to USD.
multiple = 1.0;
}
} else {
multiple = usd.getValue() / nonusd.getValue();
if(multiple != 0.0) sCurrencyMap.put(nonusd.getCurrencyType(), multiple);
}
return getCurrency(US_DOLLAR, multiple*cvt.getValue());
}
return cvt;
}
/*!@class CurrencyTypeException
*
* @brief A class to yell about currency type comparison exceptions.
*
* This is used when comparing two currencies of disparate monies.
*/
public static class CurrencyTypeException extends Exception {
String _associatedString;
public CurrencyTypeException(String inString) {
_associatedString = inString;
}
public String toString() {
return _associatedString;
}
}
private static final Integer CurDollar = US_DOLLAR; // American Dollar
private static final Integer CurPound = UK_POUND; // British Pound
private static final Integer CurYen = JP_YEN; // Japanese Yen
private static final Integer CurMark = GER_MARK; // German Mark
private static final Integer CurFranc = FR_FRANC; // French Franc
private static final Integer CurSwiss = CH_FRANC; // Swiss Franc
private static final Integer CurCan = CAN_DOLLAR; // Canadian Dollar
private static final Integer CurEuro = EURO; // Euro
private static final Integer CurAu = AU_DOLLAR; // Australian Dollar
private static final Integer CurTaiwan = NT_DOLLAR; // New Taiwanese Dollar
private static final Integer CurHK = HK_DOLLAR; // Hong Kong Dollar
private static final Integer CurMyr = MY_REAL; // Malaysia Real(?)
private static final Integer CurSGD = SG_DOLLAR; // Singapore Dollar
private static final Integer CurRupee = IND_RUPEE; // Indian Rupee
// The fundamental list of the textual representation for different
// currencies, and the Currency type it translates to.
private static final Object xlateTable[][] = {
{ "USD", CurDollar },
{ "US $", CurDollar },
{ "AU $", CurAu },
{ "au$", CurAu },
{ "AU", CurAu },
{ "AUD", CurAu },
{ "US", CurDollar },
{ "USD $", CurDollar },
{ "$", CurDollar },
{ "C", CurCan },
{ "C $", CurCan },
{ "CAD", CurCan },
{ "c$", CurCan },
{ "GBP", CurPound },
{ objPound.toString(), CurPound },
{ "pound", CurPound },
{ "\u00A3", CurPound },
{ "£", CurPound },
{ "Y", CurYen },
{ "JPY", CurYen },
{ "¥", CurYen },
{ "\u00A5", CurYen },
{ "DM", CurMark },
{ "FRF", CurFranc },
{ "fr", CurFranc },
{ "CHF", CurSwiss },
{ "chf", CurSwiss },
{ "dm", CurMark },
{ "\u20AC", CurEuro },
{ "eur", CurEuro },
{ "EUR", CurEuro },
{ "Eur", CurEuro },
{ "NT$", CurTaiwan },
{ "nt$", CurTaiwan },
{ "NTD", CurTaiwan },
{ "HK$", CurHK },
{ "hk$", CurHK },
{ "HKD", CurHK },
{ "MYR", CurMyr },
{ "myr", CurMyr },
{ "SGD", CurSGD },
{ "sgd", CurSGD },
{ "INR", CurRupee },
{ "inr", CurRupee }
};
/**
* @brief Convert from a string containing a recognized symbol into
* a currency type.
*
* @param symbol - The string representation of a currency.
*
* @return - The integer value associated with the provided
* currency, or NONE for unrecognized currencies.
*/
private int xlateSymbolToType(String symbol) {
for (Object[] aXlateTable : xlateTable) {
if (symbol.equals(aXlateTable[0])) {
return (Integer) aXlateTable[1];
}
}
return NONE;
}
private boolean isDigit(char ch) {
return(ch>='0' && ch<='9');
}
public static boolean isCurrency(String test) {
return !getCurrency(test).isNull();
}
public static Currency getCurrency(String wholeValue) {
if(wholeValue == null || wholeValue.length() == 0 || wholeValue.startsWith("UNK")) return NoValue();
return new Currency(wholeValue);
}
public static Currency getCurrency(int whatType, double startValue) {
if(whatType == NONE) return NoValue();
return new Currency(whatType, startValue);
}
public static Currency getCurrency(String symbol, double startValue) {
if(symbol == null || symbol.equalsIgnoreCase("UNK")) return NoValue();
return new Currency(symbol, startValue);
}
public static Currency getCurrency(String symbol, String startValue) {
if(symbol == null || symbol.equalsIgnoreCase("UNK")) return NoValue();
return new Currency(symbol, startValue);
}
public Currency(String wholeValue) {
setValues(wholeValue);
}
public Currency(int whatType, double startValue) {
setValues(whatType, startValue);
}
public Currency(String symbol, double startValue) {
setValues(symbol, startValue);
}
public Currency(String symbol, String startValue) {
setValues(symbol, Double.parseDouble(cleanCommas(startValue)));
}
// Convert [###.###.]###,## to [###,###,]###.##
private static String cleanCommas(String startValue) {
int decimalPos = startValue.length()-3;
if(decimalPos > 0) {
if (startValue.charAt(decimalPos) == '.') {
startValue = startValue.replaceAll(",", "");
} else if(startValue.charAt(decimalPos) == ',') {
startValue = startValue.replaceAll("\\.", "").replaceAll(",", ".");
}
}
return startValue;
}
private int checkLengthMatchStart(String value, String currencyName) {
String lowVal = value.toLowerCase();
String curNam = currencyName.toLowerCase();
if(lowVal.startsWith(curNam + " ")) {
return currencyName.length()+1;
}
if(lowVal.startsWith(curNam)) {
int len = currencyName.length();
while(len < value.length() && !Character.isDigit(value.charAt(len))) len++;
return len;
}
return 0;
}
/**
* @brief Provided an entire string containing a currency prefix and
* an amount, extract the two and set this object's value to equal
* the result.
*
* Is there a reason this doesn't use xlateSymbolToType?
* BUGBUG -- mrs: 03-January-2003 01:28
*
* @param wholeValue - The string containing an entire currency+amount text.
*/
private void setValues(String wholeValue) {
if(wholeValue == null || wholeValue.equals("null")) {
setValues(Currency.NONE, 0.0);
} else {
char firstChar = wholeValue.charAt(0);
int eurLen = checkLengthMatchStart(wholeValue, "EUR");
int gbpLen = checkLengthMatchStart(wholeValue, "GBP");
int frfLen = checkLengthMatchStart(wholeValue, "FRF");
int chfLen = checkLengthMatchStart(wholeValue, "CHF");
int cdnLen = checkLengthMatchStart(wholeValue, "CAD");
int ntdLen = checkLengthMatchStart(wholeValue, "NTD");
int audLen = checkLengthMatchStart(wholeValue, "AUD");
int usdLen = checkLengthMatchStart(wholeValue, "USD");
String parseCurrency;
String valuePortion;
if(wholeValue.startsWith("US $")) {
parseCurrency = "US $";
valuePortion = wholeValue.substring(4);
} else if(wholeValue.startsWith("USD $")) {
// In case eBay ever corrects to the RIGHT currency code for USD.
parseCurrency = "USD $";
valuePortion = wholeValue.substring(5);
} else if(wholeValue.startsWith("AU $")) {
parseCurrency = "AU $";
valuePortion = wholeValue.substring(4);
} else if(usdLen != 0) {
parseCurrency = "USD";
valuePortion = wholeValue.substring(usdLen);
} else if(eurLen != 0) {
parseCurrency = "EUR";
valuePortion = wholeValue.substring(eurLen);
} else if(gbpLen != 0) {
parseCurrency = "GBP";
valuePortion = wholeValue.substring(gbpLen);
} else if(frfLen != 0) {
parseCurrency = "FRF";
valuePortion = wholeValue.substring(frfLen);
} else if(chfLen != 0) {
parseCurrency = "CHF";
valuePortion = wholeValue.substring(chfLen);
} else if(cdnLen != 0) {
parseCurrency = "CAD";
valuePortion = wholeValue.substring(cdnLen);
} else if(ntdLen != 0) {
parseCurrency = "NTD";
valuePortion = wholeValue.substring(ntdLen);
} else if(audLen != 0) {
parseCurrency = "AUD";
valuePortion = wholeValue.substring(audLen);
} else if(wholeValue.startsWith("NT$")) {
parseCurrency = "NTD";
valuePortion = wholeValue.substring(3);
} else if(wholeValue.startsWith("SGD")) {
parseCurrency = "SGD";
valuePortion = wholeValue.substring(3);
} else if(wholeValue.startsWith("sgd")) {
parseCurrency = "SGD";
valuePortion = wholeValue.substring(3);
} else if(wholeValue.startsWith("INR")) {
parseCurrency = "INR";
valuePortion = wholeValue.substring(3);
} else if(wholeValue.startsWith("inr")) {
parseCurrency = "INR";
valuePortion = wholeValue.substring(3);
} else if(wholeValue.startsWith("nt$")) {
parseCurrency = "NTD";
valuePortion = wholeValue.substring(3);
} else if(wholeValue.startsWith("au$")) {
parseCurrency = "AUD";
valuePortion = wholeValue.substring(3);
} else if(wholeValue.startsWith("C $")) {
parseCurrency = "C $";
valuePortion = wholeValue.substring(3);
} else if(wholeValue.charAt(0) == pound) {
parseCurrency = "GBP";
valuePortion = wholeValue.substring(1);
} else {
if(!isDigit(firstChar) && firstChar != '$') {
int semiIndex = wholeValue.indexOf(";");
if(semiIndex == -1) {
semiIndex = wholeValue.indexOf(" ");
}
if(semiIndex != -1) {
parseCurrency = wholeValue.substring(0, semiIndex);
valuePortion = wholeValue.substring(parseCurrency.length()+1);
} else {
parseCurrency = "$";
valuePortion = wholeValue;
}
} else {
parseCurrency = "$";
if(isDigit(firstChar)) {
valuePortion = wholeValue;
} else {
valuePortion = wholeValue.substring(1);
}
}
}
// Kill off non-digit characters.
while(valuePortion.length() != 0 && !Character.isDigit(valuePortion.charAt(0))) valuePortion = valuePortion.substring(1);
// If anything's left, try and parse it.
if(valuePortion.length() != 0) {
double actualValue;
try {
String cvt = cleanCommas(valuePortion);
actualValue = df.parse(cvt).doubleValue();
} catch(java.text.ParseException e) {
JConfig.log().handleException("currency parse!", e);
actualValue = 0.0;
}
setValues(parseCurrency, actualValue);
} else {
setValues(null);
}
}
}
/**
* @brief If it's set as two seperate entries, then we use the MUCH
* cleaner xlateSymbolToType function.
*
* This should be the basic method that setValues works also.
*
* @param symbol - The string form of a currency symbol.
* @param startValue - The amount associated with the currency.
*/
private void setValues(String symbol, double startValue) {
setValues(xlateSymbolToType(symbol), startValue);
}
/**
* @brief The underlying setter that assigns the currency and amounts.
*
* @param whatType - The Currency type to set to.
* @param startValue - The amount represented.
*/
private void setValues(int whatType, double startValue) {
mCurrencyType = whatType;
mValue = startValue;
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(2);
}
/**
* @brief Get the full, storable textual name for the currency type
* of this object.
*
* @return A string containing a full ISO currency name.
*/
public String fullCurrencyName() {
switch(mCurrencyType) {
case US_DOLLAR: return("USD");
case AU_DOLLAR: return("AUD");
case NT_DOLLAR: return("NTD");
case HK_DOLLAR: return("HKD");
case MY_REAL: return("MYR");
case SG_DOLLAR: return("SGD");
case IND_RUPEE: return("INR");
case UK_POUND: return("GBP");
case JP_YEN: return("JPY");
case GER_MARK: return("DM");
case FR_FRANC: return("FRF");
case CH_FRANC: return("CHF");
case CAN_DOLLAR: return("CAD");
case EURO: return("EUR");
default: return("UNK");
}
}
public double getValue() { return mValue; }
public String fullCurrency() {
return fullCurrencyName() + " " + getValueString();
}
/**
* @brief Add two currencies and return a new currency containing
* the result of the two added together.
*
* @param addValue - The currency value/amount to add. It must be
* of the same currency type as 'this'.
*
* @return A new currency object containing the sum of the two
* amounts provided, with the same currency type as them.
*
* @throws CurrencyTypeException if the two objects are of different currencies.
*/
public Currency add(Currency addValue) throws CurrencyTypeException {
if(addValue == null) throw new CurrencyTypeException("Cannot add null Currency.");
if(addValue.getCurrencyType() == mCurrencyType) {
return new Currency(mCurrencyType, mValue + addValue.getValue());
}
// If only one currency is known, return the result as the known currency.
if (mCurrencyType == NONE) return new Currency(addValue.getCurrencyType(), mValue + addValue.getValue());
if (addValue.getCurrencyType() == NONE) return new Currency(mCurrencyType, mValue + addValue.getValue());
throw new CurrencyTypeException("Cannot add " + fullCurrencyName() + " to " + addValue.fullCurrencyName() + ".");
}
/**
* @brief Subtract two currencies and return a new currency containing
* the result of the passed value subtracted from this objects value.
*
* @param subValue - The currency value/amount to subtract. It must be
* of the same currency type as 'this'.
*
* @return A new currency object containing the difference of the two
* amounts provided, with the same currency type as them.
*
* @throws CurrencyTypeException if the two objects are of different currencies.
*/
public Currency subtract(Currency subValue) throws CurrencyTypeException {
if(subValue == null) throw new CurrencyTypeException("Cannot subtract null Currency.");
if(subValue.getCurrencyType() == mCurrencyType) {
return new Currency(mCurrencyType, mValue - subValue.getValue());
}
// If only one currency is known, return the result as the known currency.
if(mCurrencyType == NONE) return new Currency(subValue.getCurrencyType(), mValue - subValue.getValue());
if(subValue.getCurrencyType() == NONE) return new Currency(mCurrencyType, mValue - subValue.getValue());
throw new CurrencyTypeException("Cannot subtract " + fullCurrencyName() + " from " + subValue.fullCurrencyName() + ".");
}
public int getCurrencyType() { return mCurrencyType; }
public String getCurrencySymbol() {
switch(mCurrencyType) {
case US_DOLLAR: return("$");
case NT_DOLLAR: return("nt$");
case HK_DOLLAR: return("hk$");
case MY_REAL: return("myr");
case SG_DOLLAR: return("sgd");
case IND_RUPEE: return("Rs.");
case UK_POUND: return(objPound.toString());
case JP_YEN: return("\u00A5"); // HACKHACK
case FR_FRANC: return("fr");
case CH_FRANC: return("chf");
case GER_MARK: return("dm");
case CAN_DOLLAR: return("c$");
case AU_DOLLAR: return("au$");
case EURO: return("\u20AC");
default: return("unk");
}
}
/**
* @brief Format the currency and amount as appropriate for the
* current locale.
*
* This is kind of interesting, because it will display in one
* fashion, but when it snipes or bids, it's all against the
* US sites, so it's all operating in US forms at that point.
*
* @return A nicely formatted, locale-correct money value, prefixed
* with the best currency symbol for the currency type.
*/
public String toString() {
if(isNull()) {
return("null");
} else {
String cvtToString = getCurrencySymbol();
cvtToString += df.format(mValue);
return(cvtToString);
}
}
/**
* @brief Format the amount as appropriate for the current locale.
*
* This is kind of interesting, because it will display in one
* fashion, but when it snipes or bids, it's all against the
* US sites, so it's all operating in US forms at that point.
*
* @return A nicely formatted, locale-correct money value, prefixed
* with the best currency symbol for the currency type.
*/
public String getValueString() {
if(isNull()) {
return("null");
} else {
return df.format(mValue);
}
}
/**
* @brief Implementing equals means I should implement hashCode().
*
* @return - The hash code of the string consisting of the full
* currency named followed by the value as a string. Null/invalid
* currency entries return 0.
*/
public int hashCode() {
if(isNull()) return 0;
String tmp = fullCurrencyName() + getValueString();
return tmp.hashCode();
}
/**
* @brief Must be able to compare currency values for equality.
*
* @param inValue - The value to compare against.
*
* @return True if the two values are the same, or the currency and
* amount are the same. False otherwise, including false if it is
* an entirely different class. Differing currencies are always
* unequal.
*/
public boolean equals(Object inValue) {
// Be careful not to compare with null.
if(inValue == null) return false;
// Shortcut for this.equals(this)
if(inValue == this) return true;
// Is it this class even?
if(!(inValue instanceof Currency)) return false;
// Okay, now cast it because it's safe.
Currency otherValue = (Currency) inValue;
boolean sameCurrency = (otherValue.getCurrencyType() == mCurrencyType);
boolean sameValue = ((int) (otherValue.getValue() * 1000)) == ((int) (mValue * 1000));
return(sameCurrency && sameValue);
}
/**
* @brief Determine if (this < otherValue).
*
* This only works for items of the same currency type.
*
* @param otherValue - The value to compare against.
*
* @return - True if this amount is less than the otherValue amount
* and both currency types are equal. If the otherValue is null,
* the same object as this (this.less(this)), or this amount is
* actually less, then it returns false.
*
* @throws CurrencyTypeException if you try to compare different currencies.
*/
public boolean less(Currency otherValue) throws CurrencyTypeException {
// Be careful
if(otherValue == null) return false;
// Shortcut
if(otherValue == this) return false;
boolean sameCurrency = (otherValue.getCurrencyType() == mCurrencyType);
if(!sameCurrency) {
throw new CurrencyTypeException("Cannot compare different currencies.");
}
boolean lowerValue = Double.compare((double) ((int) (otherValue.getValue() * 1000)), (double) (int) (mValue * 1000)) == 1;
return(lowerValue);
}
/**
* @brief Utility function to check if this is a purely invalid currency.
*
* It should probably check against the invalid currency object first...
*
* @return True if this is a 'null currency' object.
*/
public boolean isNull() {
return(mValue == 0.0 && mCurrencyType == NONE);
}
/**
* @brief The comparable interface defines this, and so I'm
* comparing using the well defined set of rules for Comparables.
*
* Defined with 'equals' and less', but both should be special cases
* of this, since some checks are duplicated.
*
* @param o - The object to compare against.
*
* @return -1 if o's class is Currency, it's the same currency type,
* and the amount of this is less than o's amount.
* 0 if o's class is Currency, it's the same currency type,
* and the amount of this is the same as o's amount.
* 1 if o's class is Currency, it's the same currency type,
* and the amount of this is greater than o's amount.
*
* @throws ClassCastException if you try to compareTo non-Currency classes.
*/
public int compareTo(Object o) {
// We are always greater than null
if(o == null) return 1;
// We are always equal to ourselves
if(o == this) return 0;
// This is an incorrect usage and should be caught.
if(!(o instanceof Currency)) throw new ClassCastException("Currency cannot compareTo different classes!");
// Okay, now cast it because it's safe.
Currency otherValue = (Currency) o;
if(otherValue.isNull()) return 1;
if(isNull()) return -1;
try {
if(less(otherValue)) return -1;
} catch(ClassCastException e) {
/* This should be impossible */
throw new ClassCastException("Currency cannot compareTo different classes!\n" + e);
} catch (CurrencyTypeException e) {
// Can't re-throw (or not catch!) because Object.compareTo doesn't throw CurrencyTypeException!
throw new ClassCastException("Currency cannot compareTo different currencies!\n" + e);
}
if(equals(otherValue)) return 0;
return 1;
}
public static PersistenceDelegate getDelegate() {
return new DefaultPersistenceDelegate(new String[]{"mCurrencyType", "mValue"});
}
}