| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.number; |
| 9 |
|
| 10 |
import net.sf.saxon.lib.Numberer; |
| 11 |
import net.sf.saxon.regex.EmptyString; |
| 12 |
import net.sf.saxon.regex.UnicodeString; |
| 13 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 14 |
|
| 15 |
import java.util.Locale; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public abstract class AbstractNumberer implements Numberer { |
| 27 |
|
| 28 |
private String country; |
| 29 |
private String language; |
| 30 |
|
| 31 |
public static final int UPPER_CASE = 0; |
| 32 |
public static final int LOWER_CASE = 1; |
| 33 |
public static final int TITLE_CASE = 2; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public Locale defaultedLocale() { |
| 43 |
return null; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public void setCountry(String country) { |
| 51 |
this.country = country; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public void setLanguage(String language) { |
| 62 |
this.language = language; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public String getLanguage() { |
| 70 |
return language; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public String getCountry() { |
| 80 |
return country; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public final String format(long number, |
| 104 |
UnicodeString picture, |
| 105 |
int groupSize, |
| 106 |
String groupSeparator, |
| 107 |
String letterValue, |
| 108 |
String ordinal) { |
| 109 |
|
| 110 |
return format(number, picture, |
| 111 |
new RegularGroupFormatter(groupSize, groupSeparator, EmptyString.THE_INSTANCE), |
| 112 |
letterValue, ordinal); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public String format(long number, |
| 131 |
UnicodeString picture, |
| 132 |
NumericGroupFormatter numGroupFormatter, |
| 133 |
String letterValue, |
| 134 |
String ordinal) { |
| 135 |
|
| 136 |
|
| 137 |
if (number < 0) { |
| 138 |
return "" + number; |
| 139 |
} |
| 140 |
if (picture == null || picture.uLength() == 0) { |
| 141 |
return "" + number; |
| 142 |
} |
| 143 |
|
| 144 |
int pictureLength = picture.uLength(); |
| 145 |
FastStringBuffer sb = new FastStringBuffer(FastStringBuffer.C16); |
| 146 |
int formchar = picture.uCharAt(0); |
| 147 |
|
| 148 |
FastStringBuffer fsb = new FastStringBuffer(2); |
| 149 |
|
| 150 |
switch (formchar) { |
| 151 |
|
| 152 |
case '0': |
| 153 |
case '1': |
| 154 |
sb.append(toRadical(number, westernDigits, pictureLength, numGroupFormatter)); |
| 155 |
if (ordinal != null && !ordinal.isEmpty()) { |
| 156 |
sb.append(ordinalSuffix(ordinal, number)); |
| 157 |
} |
| 158 |
break; |
| 159 |
|
| 160 |
case 'A': |
| 161 |
if (number == 0) { |
| 162 |
return "0"; |
| 163 |
} |
| 164 |
return toAlphaSequence(number, latinUpper); |
| 165 |
|
| 166 |
case 'a': |
| 167 |
if (number == 0) { |
| 168 |
return "0"; |
| 169 |
} |
| 170 |
return toAlphaSequence(number, latinLower); |
| 171 |
|
| 172 |
case 'w': |
| 173 |
case 'W': |
| 174 |
int wordCase; |
| 175 |
if (picture.uLength() == 1) { |
| 176 |
if (formchar == 'W') { |
| 177 |
wordCase = UPPER_CASE; |
| 178 |
} else { |
| 179 |
wordCase = LOWER_CASE; |
| 180 |
} |
| 181 |
} else { |
| 182 |
|
| 183 |
wordCase = TITLE_CASE; |
| 184 |
} |
| 185 |
if (ordinal != null && !ordinal.isEmpty()) { |
| 186 |
return toOrdinalWords(ordinal, number, wordCase); |
| 187 |
} else { |
| 188 |
return toWords(number, wordCase); |
| 189 |
} |
| 190 |
|
| 191 |
case 'i': |
| 192 |
if (number == 0) { |
| 193 |
return "0"; |
| 194 |
} |
| 195 |
if (letterValue == null || letterValue.isEmpty() || |
| 196 |
letterValue.equals("traditional")) { |
| 197 |
return toRoman(number); |
| 198 |
} else { |
| 199 |
alphaDefault(number, 'i', sb); |
| 200 |
} |
| 201 |
break; |
| 202 |
|
| 203 |
case 'I': |
| 204 |
if (number == 0) { |
| 205 |
return "0"; |
| 206 |
} |
| 207 |
if (letterValue == null || letterValue.isEmpty() || |
| 208 |
letterValue.equals("traditional")) { |
| 209 |
return toRoman(number).toUpperCase(); |
| 210 |
} else { |
| 211 |
alphaDefault(number, 'I', sb); |
| 212 |
} |
| 213 |
break; |
| 214 |
|
| 215 |
case '\u2460': |
| 216 |
|
| 217 |
if (number == 0) { |
| 218 |
return "" + (char) 0x24EA; |
| 219 |
} |
| 220 |
if (number > 20 && number <= 35) { |
| 221 |
return "" + (char) (0x3251 + number - 21); |
| 222 |
} |
| 223 |
if (number > 35 && number <= 50) { |
| 224 |
return "" + (char) (0x32B1 + number - 36); |
| 225 |
} |
| 226 |
if (number > 50) { |
| 227 |
return "" + number; |
| 228 |
} |
| 229 |
return "" + (char) (0x2460 + number - 1); |
| 230 |
|
| 231 |
case '\u2474': |
| 232 |
|
| 233 |
if (number == 0 || number > 20) { |
| 234 |
return "" + number; |
| 235 |
} |
| 236 |
return "" + (char) (0x2474 + number - 1); |
| 237 |
|
| 238 |
case '\u2488': |
| 239 |
|
| 240 |
if (number == 0) { |
| 241 |
return "" + (char) 0xD83C + (char) 0xDD00; |
| 242 |
} |
| 243 |
if (number > 20) { |
| 244 |
return "" + number; |
| 245 |
} |
| 246 |
return "" + (char) (0x2488 + number - 1); |
| 247 |
|
| 248 |
case '\u2776': |
| 249 |
|
| 250 |
if (number == 0) { |
| 251 |
return "" + (char) 0x24FF; |
| 252 |
} |
| 253 |
if (number > 10 && number <= 20) { |
| 254 |
return "" + (char) (0x24EB + number - 11); |
| 255 |
} |
| 256 |
if (number > 20) { |
| 257 |
return "" + number; |
| 258 |
} |
| 259 |
return "" + (char) (0x2776 + number - 1); |
| 260 |
|
| 261 |
case '\u2780': |
| 262 |
|
| 263 |
if (number == 0) { |
| 264 |
return "" + (char) 0xD83C + (char) 0xDD0B; |
| 265 |
} |
| 266 |
if (number > 10) { |
| 267 |
return "" + number; |
| 268 |
} |
| 269 |
return "" + (char) (0x2780 + number - 1); |
| 270 |
|
| 271 |
case '\u24F5': |
| 272 |
|
| 273 |
if (number == 0 || number > 10) { |
| 274 |
return "" + number; |
| 275 |
} |
| 276 |
return "" + (char) (0x24F5 + number - 1); |
| 277 |
|
| 278 |
case '\u278A': |
| 279 |
|
| 280 |
if (number == 0) { |
| 281 |
return "" + (char) 0xD83C + (char) 0xDD0C; |
| 282 |
} |
| 283 |
if (number > 10) { |
| 284 |
return "" + number; |
| 285 |
} |
| 286 |
return "" + (char) (0x278A + number - 1); |
| 287 |
|
| 288 |
case '\u3220': |
| 289 |
|
| 290 |
if (number == 0 || number > 10) { |
| 291 |
return "" + number; |
| 292 |
} |
| 293 |
return "" + (char) (0x3220 + number - 1); |
| 294 |
|
| 295 |
case '\u3280': |
| 296 |
|
| 297 |
if (number == 0 || number > 10) { |
| 298 |
return "" + number; |
| 299 |
} |
| 300 |
return "" + (char) (0x3280 + number - 1); |
| 301 |
|
| 302 |
case 65799: |
| 303 |
|
| 304 |
if (number == 0 || number > 10) { |
| 305 |
return "" + number; |
| 306 |
} |
| 307 |
fsb.appendWideChar(65799 + (int) number - 1); |
| 308 |
return fsb.toString(); |
| 309 |
|
| 310 |
case 69216: |
| 311 |
|
| 312 |
if (number == 0 || number > 10) { |
| 313 |
return "" + number; |
| 314 |
} |
| 315 |
fsb.appendWideChar(69216 + (int) number - 1); |
| 316 |
return fsb.toString(); |
| 317 |
|
| 318 |
case 69714: |
| 319 |
|
| 320 |
if (number == 0 || number > 10) { |
| 321 |
return "" + number; |
| 322 |
} |
| 323 |
fsb.appendWideChar(69714 + (int) number - 1); |
| 324 |
return fsb.toString(); |
| 325 |
|
| 326 |
case 119648: |
| 327 |
|
| 328 |
if (number == 0 || number >= 10) { |
| 329 |
return "" + number; |
| 330 |
} |
| 331 |
fsb.appendWideChar(119648 + (int) number - 1); |
| 332 |
return fsb.toString(); |
| 333 |
|
| 334 |
case 127234: |
| 335 |
|
| 336 |
if (number == 0) { |
| 337 |
fsb.appendWideChar(127233); |
| 338 |
return fsb.toString(); |
| 339 |
} |
| 340 |
if (number >= 10) { |
| 341 |
return "" + number; |
| 342 |
} |
| 343 |
fsb.appendWideChar(127234 + (int) number - 1); |
| 344 |
return fsb.toString(); |
| 345 |
|
| 346 |
case '\u0391': |
| 347 |
if (number == 0) { |
| 348 |
return "0"; |
| 349 |
} |
| 350 |
return toAlphaSequence(number, greekUpper); |
| 351 |
|
| 352 |
case '\u03b1': |
| 353 |
if (number == 0) { |
| 354 |
return "0"; |
| 355 |
} |
| 356 |
return toAlphaSequence(number, greekLower); |
| 357 |
|
| 358 |
case '\u0410': |
| 359 |
if (number == 0) { |
| 360 |
return "0"; |
| 361 |
} |
| 362 |
return toAlphaSequence(number, cyrillicUpper); |
| 363 |
|
| 364 |
case '\u0430': |
| 365 |
if (number == 0) { |
| 366 |
return "0"; |
| 367 |
} |
| 368 |
return toAlphaSequence(number, cyrillicLower); |
| 369 |
|
| 370 |
case '\u05d0': |
| 371 |
if (number == 0) { |
| 372 |
return "0"; |
| 373 |
} |
| 374 |
return toAlphaSequence(number, hebrew); |
| 375 |
|
| 376 |
case '\u3042': |
| 377 |
if (number == 0) { |
| 378 |
return "0"; |
| 379 |
} |
| 380 |
return toAlphaSequence(number, hiraganaA); |
| 381 |
|
| 382 |
case '\u30a2': |
| 383 |
if (number == 0) { |
| 384 |
return "0"; |
| 385 |
} |
| 386 |
return toAlphaSequence(number, katakanaA); |
| 387 |
|
| 388 |
case '\u3044': |
| 389 |
if (number == 0) { |
| 390 |
return "0"; |
| 391 |
} |
| 392 |
return toAlphaSequence(number, hiraganaI); |
| 393 |
|
| 394 |
case '\u30a4': |
| 395 |
if (number == 0) { |
| 396 |
return "0"; |
| 397 |
} |
| 398 |
return toAlphaSequence(number, katakanaI); |
| 399 |
|
| 400 |
case '\u4e00': |
| 401 |
return toJapanese(number); |
| 402 |
|
| 403 |
default: |
| 404 |
|
| 405 |
int digitValue = Alphanumeric.getDigitValue(formchar); |
| 406 |
if (digitValue >= 0) { |
| 407 |
|
| 408 |
int zero = formchar - digitValue; |
| 409 |
int[] digits = new int[10]; |
| 410 |
for (int z = 0; z <= 9; z++) { |
| 411 |
digits[z] = zero + z; |
| 412 |
} |
| 413 |
|
| 414 |
return toRadical(number, digits, pictureLength, numGroupFormatter); |
| 415 |
|
| 416 |
} else { |
| 417 |
if (formchar < '\u1100' && Character.isLetter((char) formchar) && number > 0) { |
| 418 |
alphaDefault(number, (char) formchar, sb); |
| 419 |
} else { |
| 420 |
|
| 421 |
sb.append(toRadical(number, westernDigits, pictureLength, numGroupFormatter)); |
| 422 |
if (ordinal != null && !ordinal.isEmpty()) { |
| 423 |
sb.append(ordinalSuffix(ordinal, number)); |
| 424 |
} |
| 425 |
|
| 426 |
} |
| 427 |
break; |
| 428 |
|
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
return sb.toString(); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
protected String ordinalSuffix(String ordinalParam, long number) { |
| 446 |
return ""; |
| 447 |
} |
| 448 |
|
| 449 |
protected static final int[] westernDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; |
| 450 |
|
| 451 |
protected static final String latinUpper = |
| 452 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 453 |
|
| 454 |
protected static final String latinLower = |
| 455 |
"abcdefghijklmnopqrstuvwxyz"; |
| 456 |
|
| 457 |
protected static final String greekUpper = |
| 458 |
"\u0391\u0392\u0393\u0394\u0395\u0396\u0397\u0398\u0399\u039a" + |
| 459 |
"\u039b\u039c\u039d\u039e\u039f\u03a0\u03a1\u03a2\u03a3\u03a4" + |
| 460 |
"\u03a5\u03a6\u03a7\u03a8\u03a9"; |
| 461 |
|
| 462 |
protected static final String greekLower = |
| 463 |
"\u03b1\u03b2\u03b3\u03b4\u03b5\u03b6\u03b7\u03b8\u03b9\u03ba" + |
| 464 |
"\u03bb\u03bc\u03bd\u03be\u03bf\u03c0\u03c1\u03c2\u03c3\u03c4" + |
| 465 |
"\u03c5\u03c6\u03c7\u03c8\u03c9"; |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
protected static final String cyrillicUpper = |
| 471 |
"\u0410\u0411\u0412\u0413\u0414\u0415\u0416\u0417\u0418" + |
| 472 |
"\u041a\u041b\u041c\u041d\u041e\u041f\u0420\u0421\u0421\u0423" + |
| 473 |
"\u0424\u0425\u0426\u0427\u0428\u0429\u042b\u042d\u042e\u042f"; |
| 474 |
|
| 475 |
protected static final String cyrillicLower = |
| 476 |
"\u0430\u0431\u0432\u0433\u0434\u0435\u0436\u0437\u0438" + |
| 477 |
"\u043a\u043b\u043c\u043d\u043e\u043f\u0440\u0441\u0441\u0443" + |
| 478 |
"\u0444\u0445\u0446\u0447\u0448\u0449\u044b\u044d\u044e\u044f"; |
| 479 |
|
| 480 |
protected static final String hebrew = |
| 481 |
"\u05d0\u05d1\u05d2\u05d3\u05d4\u05d5\u05d6\u05d7\u05d8\u05d9\u05db\u05dc" + |
| 482 |
"\u05de\u05e0\u05e1\u05e2\u05e4\u05e6\u05e7\u05e8\u05e9\u05ea"; |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
protected static final String hiraganaA = |
| 489 |
"\u3042\u3044\u3046\u3048\u304a\u304b\u304d\u304f\u3051\u3053" + |
| 490 |
"\u3055\u3057\u3059\u305b\u305d\u305f\u3061\u3064\u3066\u3068" + |
| 491 |
"\u306a\u306b\u306c\u306d\u306e\u306f\u3072\u3075\u3078\u307b" + |
| 492 |
"\u307e\u307f\u3080\u3081\u3082\u3084\u3086\u3088\u3089\u308a" + |
| 493 |
"\u308b\u308c\u308d\u308f\u3092\u3093"; |
| 494 |
|
| 495 |
protected static final String katakanaA = |
| 496 |
|
| 497 |
"\u30a2\u30a4\u30a6\u30a8\u30aa\u30ab\u30ad\u30af\u30b1\u30b3" + |
| 498 |
"\u30b5\u30b7\u30b9\u30bb\u30bd\u30bf\u30c1\u30c4\u30c6\u30c8" + |
| 499 |
"\u30ca\u30cb\u30cc\u30cd\u30ce\u30cf\u30d2\u30d5\u30d8\u30db" + |
| 500 |
"\u30de\u30df\u30e0\u30e1\u30e2\u30e4\u30e6\u30e8\u30e9\u30ea" + |
| 501 |
"\u30eb\u30ec\u30ed\u30ef\u30f2\u30f3"; |
| 502 |
|
| 503 |
protected static final String hiraganaI = |
| 504 |
|
| 505 |
"\u3044\u308d\u306f\u306b\u307b\u3078\u3068\u3061\u308a\u306c" + |
| 506 |
"\u308b\u3092\u308f\u304b\u3088\u305f\u308c\u305d\u3064\u306d" + |
| 507 |
"\u306a\u3089\u3080\u3046\u3090\u306e\u304a\u304f\u3084\u307e" + |
| 508 |
"\u3051\u3075\u3053\u3048\u3066\u3042\u3055\u304d\u3086\u3081" + |
| 509 |
"\u307f\u3057\u3091\u3072\u3082\u305b\u3059"; |
| 510 |
|
| 511 |
protected static final String katakanaI = |
| 512 |
|
| 513 |
"\u30a4\u30ed\u30cf\u30cb\u30db\u30d8\u30c8\u30c1\u30ea\u30cc" + |
| 514 |
"\u30eb\u30f2\u30ef\u30ab\u30e8\u30bf\u30ec\u30bd\u30c4\u30cd" + |
| 515 |
"\u30ca\u30e9\u30e0\u30a6\u30f0\u30ce\u30aa\u30af\u30e4\u30de" + |
| 516 |
"\u30b1\u30d5\u30b3\u30a8\u30c6\u30a2\u30b5\u30ad\u30e6\u30e1" + |
| 517 |
"\u30df\u30b7\u30f1\u30d2\u30e2\u30bb\u30b9"; |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
protected void alphaDefault(long number, char formchar, FastStringBuffer sb) { |
| 530 |
int min = (int) formchar; |
| 531 |
int max = (int) formchar; |
| 532 |
|
| 533 |
while (Character.isLetterOrDigit((char) (max + 1))) { |
| 534 |
max++; |
| 535 |
} |
| 536 |
sb.append(toAlpha(number, min, max)); |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
protected String toAlpha(long number, int min, int max) { |
| 550 |
if (number <= 0) { |
| 551 |
return "" + number; |
| 552 |
} |
| 553 |
int range = max - min + 1; |
| 554 |
char last = (char) (((number - 1) % range) + min); |
| 555 |
if (number > range) { |
| 556 |
return toAlpha((number - 1) / range, min, max) + last; |
| 557 |
} else { |
| 558 |
return "" + last; |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
protected String toAlphaSequence(long number, String alphabet) { |
| 572 |
if (number <= 0) { |
| 573 |
return "" + number; |
| 574 |
} |
| 575 |
int range = alphabet.length(); |
| 576 |
char last = alphabet.charAt((int) ((number - 1) % range)); |
| 577 |
if (number > range) { |
| 578 |
return toAlphaSequence((number - 1) / range, alphabet) + last; |
| 579 |
} else { |
| 580 |
return "" + last; |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
private String toRadical(long number, int[] digits, int pictureLength, |
| 599 |
NumericGroupFormatter numGroupFormatter) { |
| 600 |
|
| 601 |
FastStringBuffer temp = convertDigitSystem(number, digits, pictureLength); |
| 602 |
|
| 603 |
if (numGroupFormatter == null) { |
| 604 |
return temp.toString(); |
| 605 |
} |
| 606 |
|
| 607 |
return numGroupFormatter.format(temp); |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
public static FastStringBuffer convertDigitSystem(long number, int[] digits, int requiredLength) { |
| 623 |
FastStringBuffer temp = new FastStringBuffer(FastStringBuffer.C16); |
| 624 |
int base = digits.length; |
| 625 |
FastStringBuffer s = new FastStringBuffer(FastStringBuffer.C16); |
| 626 |
long n = number; |
| 627 |
int count = 0; |
| 628 |
while (n > 0) { |
| 629 |
int digit = digits[(int) (n % base)]; |
| 630 |
s.prependWideChar(digit); |
| 631 |
count++; |
| 632 |
n = n / base; |
| 633 |
} |
| 634 |
|
| 635 |
for (int i = 0; i < (requiredLength - count); i++) { |
| 636 |
temp.appendWideChar(digits[0]); |
| 637 |
} |
| 638 |
temp.append(s); |
| 639 |
return temp; |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
public static String toRoman(long n) { |
| 650 |
if (n <= 0 || n > 9999) { |
| 651 |
return "" + n; |
| 652 |
} |
| 653 |
return romanThousands[(int) n / 1000] + |
| 654 |
romanHundreds[((int) n / 100) % 10] + |
| 655 |
romanTens[((int) n / 10) % 10] + |
| 656 |
romanUnits[(int) n % 10]; |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
private static String[] romanThousands = |
| 663 |
{"", "m", "mm", "mmm", "mmmm", "mmmmm", "mmmmmm", "mmmmmmm", "mmmmmmmm", "mmmmmmmmm"}; |
| 664 |
private static String[] romanHundreds = |
| 665 |
{"", "c", "cc", "ccc", "cd", "d", "dc", "dcc", "dccc", "cm"}; |
| 666 |
private static String[] romanTens = |
| 667 |
{"", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc"}; |
| 668 |
private static String[] romanUnits = |
| 669 |
{"", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix"}; |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
public String toJapanese(long number) { |
| 680 |
FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C16); |
| 681 |
if (number == 0) { |
| 682 |
fsb.appendWideChar(0x3007); |
| 683 |
} else if (number <= 9999) { |
| 684 |
toJapanese((int) number, fsb, false); |
| 685 |
} else { |
| 686 |
fsb.append("" + number); |
| 687 |
} |
| 688 |
return fsb.toString(); |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
private static void toJapanese(int nr, FastStringBuffer fsb, boolean isInitial) { |
| 700 |
if (nr == 0) { |
| 701 |
|
| 702 |
} else if (nr <= 9) { |
| 703 |
if (!(nr == 1 && isInitial)) { |
| 704 |
fsb.appendWideChar(kanjiDigits[nr]); |
| 705 |
} |
| 706 |
} else if (nr == 10) { |
| 707 |
fsb.appendWideChar(0x5341); |
| 708 |
} else if (nr <= 99) { |
| 709 |
toJapanese(nr / 10, fsb, true); |
| 710 |
fsb.appendWideChar(0x5341); |
| 711 |
toJapanese(nr % 10, fsb, false); |
| 712 |
} else if (nr <= 999) { |
| 713 |
toJapanese(nr / 100, fsb, true); |
| 714 |
fsb.appendWideChar(0x767e); |
| 715 |
toJapanese(nr % 100, fsb, false); |
| 716 |
} else if (nr <= 9999) { |
| 717 |
toJapanese(nr / 1000, fsb, true); |
| 718 |
fsb.appendWideChar(0x5343); |
| 719 |
toJapanese(nr % 1000, fsb, false); |
| 720 |
} |
| 721 |
|
| 722 |
} |
| 723 |
|
| 724 |
private static final int[] kanjiDigits = |
| 725 |
{0x3007, 0x4e00, 0x4e8c, 0x4e09, 0x56db, 0x4e94, 0x516d, 0x4e03, 0x516b, 0x4e5d}; |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
public abstract String toWords(long number); |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
public String toWords(long number, int wordCase) { |
| 747 |
String s; |
| 748 |
if (number == 0) { |
| 749 |
s = "Zero"; |
| 750 |
} else { |
| 751 |
s = toWords(number); |
| 752 |
} |
| 753 |
switch (wordCase) { |
| 754 |
case UPPER_CASE: |
| 755 |
return s.toUpperCase(); |
| 756 |
case LOWER_CASE: |
| 757 |
return s.toLowerCase(); |
| 758 |
default: |
| 759 |
return s; |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
public abstract String toOrdinalWords(String ordinalParam, long number, int wordCase); |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
public abstract String monthName(int month, int minWidth, int maxWidth); |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
public abstract String dayName(int day, int minWidth, int maxWidth); |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
public String halfDayName(int minutes, int minWidth, int maxWidth) { |
| 807 |
String s; |
| 808 |
if (minutes == 0 && maxWidth >= 8 && "gb".equals(country)) { |
| 809 |
s = "Midnight"; |
| 810 |
} else if (minutes < 12 * 60) { |
| 811 |
switch (maxWidth) { |
| 812 |
case 1: |
| 813 |
s = "A"; |
| 814 |
break; |
| 815 |
case 2: |
| 816 |
case 3: |
| 817 |
s = "Am"; |
| 818 |
break; |
| 819 |
default: |
| 820 |
s = "A.M."; |
| 821 |
} |
| 822 |
} else if (minutes == 12 * 60 && maxWidth >= 8 && "gb".equals(country)) { |
| 823 |
s = "Noon"; |
| 824 |
} else { |
| 825 |
switch (maxWidth) { |
| 826 |
case 1: |
| 827 |
s = "P"; |
| 828 |
break; |
| 829 |
case 2: |
| 830 |
case 3: |
| 831 |
s = "Pm"; |
| 832 |
break; |
| 833 |
default: |
| 834 |
s = "P.M."; |
| 835 |
} |
| 836 |
} |
| 837 |
return s; |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
public String getOrdinalSuffixForDateTime(String component) { |
| 851 |
return "yes"; |
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
public String getEraName(int year) { |
| 861 |
return year > 0 ? "AD" : "BC"; |
| 862 |
} |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
public String getCalendarName(String code) { |
| 871 |
if (code.equals("AD")) { |
| 872 |
return "Gregorian"; |
| 873 |
} else { |
| 874 |
return code; |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
} |
| 880 |
|