|
1 | 1 | /* |
2 | | - * Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2018, 2026, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * |
4 | 4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
5 | 5 | * |
@@ -91,14 +91,9 @@ final public class UnitLookup { |
91 | 91 | private static final String UNIT_ID_SEPARATOR = ":"; |
92 | 92 | public static final LinearKindOfQuantity MEMORY = createMemory(); |
93 | 93 | public static final LinearKindOfQuantity TIMESPAN = createTimespan(); |
94 | | - /* |
95 | | - * NOTE: These 3 (count, index, and identifier) cannot be persisted/restored due to Long(1) and |
96 | | - * Integer(1) not being equal or comparable. We either need to split into concrete wrappers, |
97 | | - * support a custom Comparator, or wrap into a (simple) IQuantity. |
98 | | - */ |
99 | | - public static final ContentType<Number> COUNT = createCount(); |
100 | | - public static final ContentType<Number> INDEX = createIndex(); |
101 | | - public static final ContentType<Number> IDENTIFIER = createIdentifier(); |
| 94 | + public static final ContentType<Long> COUNT = createCount(); |
| 95 | + public static final ContentType<Long> INDEX = createIndex(); |
| 96 | + public static final ContentType<Long> IDENTIFIER = createIdentifier(); |
102 | 97 | public static final KindOfQuantity<TimestampUnit> TIMESTAMP = createTimestamp(TIMESPAN); |
103 | 98 | public static final LinearKindOfQuantity PERCENTAGE = createPercentage(); |
104 | 99 | public static final LinearKindOfQuantity NUMBER = createNumber(); |
@@ -426,15 +421,99 @@ private static String formatHexNumber(IQuantity quantity) { |
426 | 421 | return String.format("0x%08X", quantity.longValue()); |
427 | 422 | } |
428 | 423 |
|
| 424 | + private static Number parseNumber(String numberStr) { |
| 425 | + try { |
| 426 | + return Long.parseLong(numberStr); |
| 427 | + } catch (NumberFormatException eLong) { |
| 428 | + return Double.parseDouble(numberStr); |
| 429 | + } |
| 430 | + } |
| 431 | + |
429 | 432 | // FIXME: Rename to createPrimitiveNumber? Remove? |
430 | 433 | private static ContentType<Number> createRawNumber() { |
431 | | - ContentType<Number> contentType = new ContentType<>("raw number"); |
| 434 | + ContentType<Number> contentType = new LeafContentType<Number>("raw number") { |
| 435 | + @Override |
| 436 | + public boolean validate(Number value) { |
| 437 | + checkNull(value); |
| 438 | + return false; |
| 439 | + } |
| 440 | + |
| 441 | + @Override |
| 442 | + public String persistableString(Number value) { |
| 443 | + validate(value); |
| 444 | + return value.toString(); |
| 445 | + } |
| 446 | + |
| 447 | + @Override |
| 448 | + public Number parsePersisted(String persistedValue) throws QuantityConversionException { |
| 449 | + checkNull(persistedValue); |
| 450 | + try { |
| 451 | + return parseNumber(persistedValue); |
| 452 | + } catch (NumberFormatException e) { |
| 453 | + throw QuantityConversionException.unparsable(persistedValue, 0, this); |
| 454 | + } |
| 455 | + } |
| 456 | + |
| 457 | + @Override |
| 458 | + public String interactiveFormat(Number value) { |
| 459 | + validate(value); |
| 460 | + return value.toString(); |
| 461 | + } |
| 462 | + |
| 463 | + @Override |
| 464 | + public Number parseInteractive(String interactiveValue) throws QuantityConversionException { |
| 465 | + checkNull(interactiveValue); |
| 466 | + try { |
| 467 | + return parseNumber(interactiveValue); |
| 468 | + } catch (NumberFormatException e) { |
| 469 | + throw QuantityConversionException.unparsable(interactiveValue, 0, this); |
| 470 | + } |
| 471 | + } |
| 472 | + }; |
432 | 473 | contentType.addFormatter(new DisplayFormatter<>(contentType, IDisplayable.AUTO, "Value")); |
433 | 474 | return contentType; |
434 | 475 | } |
435 | 476 |
|
436 | 477 | private static ContentType<Long> createRawLong() { |
437 | | - ContentType<Long> contentType = new ContentType<>("raw long"); |
| 478 | + ContentType<Long> contentType = new LeafContentType<Long>("raw long") { |
| 479 | + @Override |
| 480 | + public boolean validate(Long value) { |
| 481 | + checkNull(value); |
| 482 | + return false; |
| 483 | + } |
| 484 | + |
| 485 | + @Override |
| 486 | + public String persistableString(Long value) { |
| 487 | + validate(value); |
| 488 | + return value.toString(); |
| 489 | + } |
| 490 | + |
| 491 | + @Override |
| 492 | + public Long parsePersisted(String persistedValue) throws QuantityConversionException { |
| 493 | + checkNull(persistedValue); |
| 494 | + try { |
| 495 | + return Long.parseLong(persistedValue); |
| 496 | + } catch (NumberFormatException e) { |
| 497 | + throw QuantityConversionException.unparsable(persistedValue, 0L, this); |
| 498 | + } |
| 499 | + } |
| 500 | + |
| 501 | + @Override |
| 502 | + public String interactiveFormat(Long value) { |
| 503 | + validate(value); |
| 504 | + return value.toString(); |
| 505 | + } |
| 506 | + |
| 507 | + @Override |
| 508 | + public Long parseInteractive(String interactiveValue) throws QuantityConversionException { |
| 509 | + checkNull(interactiveValue); |
| 510 | + try { |
| 511 | + return Long.parseLong(interactiveValue); |
| 512 | + } catch (NumberFormatException e) { |
| 513 | + throw QuantityConversionException.unparsable(interactiveValue, 0L, this); |
| 514 | + } |
| 515 | + } |
| 516 | + }; |
438 | 517 | contentType.addFormatter(new DisplayFormatter<>(contentType, IDisplayable.AUTO, "Value")); |
439 | 518 | return contentType; |
440 | 519 | } |
@@ -568,25 +647,134 @@ private static LinearKindOfQuantity createPercentage() { |
568 | 647 | return percentage; |
569 | 648 | } |
570 | 649 |
|
571 | | - private static ContentType<Number> createCount() { |
572 | | - ContentType<Number> contentType = new ContentType<>("count"); |
573 | | -// contentType.addDisplayUnit( |
574 | | -// new DisplayUnit(contentType, DisplayUnit.ENGINEERING_NOTATION_IDENTIFIER, "Engineering Notation")); |
575 | | - contentType.addFormatter(new DisplayFormatter<>(contentType, IDisplayable.AUTO, "Value")); |
| 650 | + private static ContentType<Long> createCount() { |
| 651 | + ContentType<Long> contentType = new LeafContentType<Long>("count") { |
| 652 | + @Override |
| 653 | + public boolean validate(Long value) { |
| 654 | + checkNull(value); |
| 655 | + return false; |
| 656 | + } |
| 657 | + |
| 658 | + @Override |
| 659 | + public String persistableString(Long value) { |
| 660 | + validate(value); |
| 661 | + return value.toString(); |
| 662 | + } |
| 663 | + |
| 664 | + @Override |
| 665 | + public Long parsePersisted(String persistedValue) throws QuantityConversionException { |
| 666 | + checkNull(persistedValue); |
| 667 | + try { |
| 668 | + return Long.parseLong(persistedValue); |
| 669 | + } catch (NumberFormatException e) { |
| 670 | + throw QuantityConversionException.unparsable(persistedValue, 0L, this); |
| 671 | + } |
| 672 | + } |
| 673 | + |
| 674 | + @Override |
| 675 | + public String interactiveFormat(Long value) { |
| 676 | + validate(value); |
| 677 | + return value.toString(); |
| 678 | + } |
576 | 679 |
|
577 | | -// contentType.addDisplayUnit( |
578 | | -// new DisplayUnit(contentType, DisplayUnit.SCIENTIFIC_NOTATION_IDENTIFIER, "Scientific Notation")); |
| 680 | + @Override |
| 681 | + public Long parseInteractive(String interactiveValue) throws QuantityConversionException { |
| 682 | + checkNull(interactiveValue); |
| 683 | + try { |
| 684 | + return Long.parseLong(interactiveValue); |
| 685 | + } catch (NumberFormatException e) { |
| 686 | + throw QuantityConversionException.unparsable(interactiveValue, 0L, this); |
| 687 | + } |
| 688 | + } |
| 689 | + }; |
| 690 | + contentType.addFormatter(new DisplayFormatter<>(contentType, IDisplayable.AUTO, "Value")); |
579 | 691 | return contentType; |
580 | 692 | } |
581 | 693 |
|
582 | | - private static ContentType<Number> createIdentifier() { |
583 | | - ContentType<Number> contentType = new ContentType<>("identifier"); |
| 694 | + private static ContentType<Long> createIdentifier() { |
| 695 | + ContentType<Long> contentType = new LeafContentType<Long>("identifier") { |
| 696 | + @Override |
| 697 | + public boolean validate(Long value) { |
| 698 | + checkNull(value); |
| 699 | + return false; |
| 700 | + } |
| 701 | + |
| 702 | + @Override |
| 703 | + public String persistableString(Long value) { |
| 704 | + validate(value); |
| 705 | + return value.toString(); |
| 706 | + } |
| 707 | + |
| 708 | + @Override |
| 709 | + public Long parsePersisted(String persistedValue) throws QuantityConversionException { |
| 710 | + checkNull(persistedValue); |
| 711 | + try { |
| 712 | + return Long.parseLong(persistedValue); |
| 713 | + } catch (NumberFormatException e) { |
| 714 | + throw QuantityConversionException.unparsable(persistedValue, 0L, this); |
| 715 | + } |
| 716 | + } |
| 717 | + |
| 718 | + @Override |
| 719 | + public String interactiveFormat(Long value) { |
| 720 | + validate(value); |
| 721 | + return value.toString(); |
| 722 | + } |
| 723 | + |
| 724 | + @Override |
| 725 | + public Long parseInteractive(String interactiveValue) throws QuantityConversionException { |
| 726 | + checkNull(interactiveValue); |
| 727 | + try { |
| 728 | + return Long.parseLong(interactiveValue); |
| 729 | + } catch (NumberFormatException e) { |
| 730 | + throw QuantityConversionException.unparsable(interactiveValue, 0L, this); |
| 731 | + } |
| 732 | + } |
| 733 | + }; |
584 | 734 | contentType.addFormatter(new DisplayFormatter<>(contentType, IDisplayable.AUTO, "Value")); |
585 | 735 | return contentType; |
586 | 736 | } |
587 | 737 |
|
588 | | - private static ContentType<Number> createIndex() { |
589 | | - ContentType<Number> contentType = new ContentType<>("index"); |
| 738 | + private static ContentType<Long> createIndex() { |
| 739 | + ContentType<Long> contentType = new LeafContentType<Long>("index") { |
| 740 | + @Override |
| 741 | + public boolean validate(Long value) { |
| 742 | + checkNull(value); |
| 743 | + return false; |
| 744 | + } |
| 745 | + |
| 746 | + @Override |
| 747 | + public String persistableString(Long value) { |
| 748 | + validate(value); |
| 749 | + return value.toString(); |
| 750 | + } |
| 751 | + |
| 752 | + @Override |
| 753 | + public Long parsePersisted(String persistedValue) throws QuantityConversionException { |
| 754 | + checkNull(persistedValue); |
| 755 | + try { |
| 756 | + return Long.parseLong(persistedValue); |
| 757 | + } catch (NumberFormatException e) { |
| 758 | + throw QuantityConversionException.unparsable(persistedValue, 0L, this); |
| 759 | + } |
| 760 | + } |
| 761 | + |
| 762 | + @Override |
| 763 | + public String interactiveFormat(Long value) { |
| 764 | + validate(value); |
| 765 | + return value.toString(); |
| 766 | + } |
| 767 | + |
| 768 | + @Override |
| 769 | + public Long parseInteractive(String interactiveValue) throws QuantityConversionException { |
| 770 | + checkNull(interactiveValue); |
| 771 | + try { |
| 772 | + return Long.parseLong(interactiveValue); |
| 773 | + } catch (NumberFormatException e) { |
| 774 | + throw QuantityConversionException.unparsable(interactiveValue, 0L, this); |
| 775 | + } |
| 776 | + } |
| 777 | + }; |
590 | 778 | contentType.addFormatter(new DisplayFormatter<>(contentType, IDisplayable.AUTO, "Value")); |
591 | 779 | return contentType; |
592 | 780 | } |
|
0 commit comments