UnitSpan.java 38.3 KB
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 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.xfa.ut;

import com.adobe.xfa.ut.ExFull;
import com.adobe.xfa.ut.MsgFormat;
import com.adobe.xfa.ut.ResId;
import com.adobe.xfa.ut.StringUtils;

public final class UnitSpan
implements Comparable<UnitSpan> {
    private static final UnitMapEntry[] gUnitMap = new UnitMapEntry[]{new UnitMapEntry("pt", "PT", 19, 1000), new UnitMapEntry("mm", "MM", 17, 25000), new UnitMapEntry("in", "IN", 3, 72000), new UnitMapEntry("cm", "CM", 1, 250000), new UnitMapEntry("mp", "MP", 35, 1), new UnitMapEntry("pc", "PC", 51, 12000)};
    private static final String gsCm = "cm, centimeters";
    private static final String gsInch = "in, inches";
    private static final String gsMm = "mm, millimeters";
    private static final String gsMp = "mp, millipoints";
    private static final String gsPt = "pt, points";
    private static final String gsPica = "pc, picas";
    private static final String gsNumeric = "-0123456789.";
    public static final int INCHES_1M = 0;
    public static final int CM_250K = 1;
    public static final int INCHES_72K = 3;
    public static final int MM_25K = 17;
    public static final int POINTS_1K = 19;
    public static final int PICA_PT_1K = 19;
    public static final int MILLIPOINT = 35;
    public static final int PICA_PT_10K = 2;
    public static final int PICAS_12K = 51;
    public static final int UNIT_MASK = 15;
    public static final int UNIT_UNKNOWN = 255;
    public static final int UNITS_CM_250K = 635000;
    public static final int UNITS_INCHES_1M = 1000000;
    public static final int UNITS_INCHES_72K = 72000;
    public static final int UNITS_MILLIPOINT = 72000;
    public static final int UNITS_MM_25K = 635000;
    public static final int UNITS_POINTS_1K = 72000;
    public static final int UNITS_PICAS_12K = 72000;
    public static final UnitSpan ZERO = new UnitSpan();
    private final int meUnit;
    private final int mnValue;

    static int applyFactor(int nValue, int nNumerator, int nDenominator) {
        int nRemainder = nValue % nDenominator;
        if (nValue > 0) {
            return (nValue - nRemainder) / nDenominator * nNumerator + (10 * nRemainder * nNumerator / nDenominator + 5) / 10;
        }
        return (nValue - nRemainder) / nDenominator * nNumerator + (10 * nRemainder * nNumerator / nDenominator - 5) / 10;
    }

    public UnitSpan changeUnits(int eUnits) {
        return new UnitSpan(eUnits, this.valueAsUnit(eUnits));
    }

    static int convertReally(int nTo, int nFrom, int nValue) {
        int nUnitsTo = nTo & 15;
        int nUnitsFrom = nFrom & 15;
        if (nUnitsTo == nUnitsFrom) {
            return nValue;
        }
        int nNumerator = 1;
        int nDenominator = 1;
        block0 : switch (nUnitsTo) {
            case 0: {
                switch (nUnitsFrom) {
                    case 1: {
                        nNumerator = 200;
                        nDenominator = 127;
                        break block0;
                    }
                    case 2: {
                        nNumerator = 25;
                        nDenominator = 18;
                        break block0;
                    }
                    case 3: {
                        nNumerator = 125;
                        nDenominator = 9;
                    }
                }
                break;
            }
            case 1: {
                switch (nUnitsFrom) {
                    case 0: {
                        nNumerator = 127;
                        nDenominator = 200;
                        break block0;
                    }
                    case 2: {
                        nNumerator = 127;
                        nDenominator = 144;
                        break block0;
                    }
                    case 3: {
                        nNumerator = 635;
                        nDenominator = 72;
                    }
                }
                break;
            }
            case 2: {
                switch (nUnitsFrom) {
                    case 0: {
                        nNumerator = 18;
                        nDenominator = 25;
                        break block0;
                    }
                    case 1: {
                        nNumerator = 144;
                        nDenominator = 127;
                        break block0;
                    }
                    case 3: {
                        nNumerator = 10;
                        nDenominator = 1;
                    }
                }
                break;
            }
            case 3: {
                switch (nUnitsFrom) {
                    case 0: {
                        nNumerator = 9;
                        nDenominator = 125;
                        break block0;
                    }
                    case 1: {
                        nNumerator = 72;
                        nDenominator = 635;
                        break block0;
                    }
                    case 2: {
                        nNumerator = 1;
                        nDenominator = 10;
                    }
                }
                break;
            }
            default: {
                throw new ExFull(new MsgFormat(ResId.ERR_UNITSPAN_UNITS, Integer.toString(nTo)));
            }
        }
        return UnitSpan.applyFactor(nValue, nNumerator, nDenominator);
    }

    public static int convertUnit(int eNewUnits, int eOldUnits, int nValue) {
        if ((eNewUnits & 15) == (eOldUnits & 15)) {
            return nValue;
        }
        return UnitSpan.convertReally(eNewUnits, eOldUnits, nValue);
    }

    public static int defaultUnits() {
        return 3;
    }

    public static String measurementValidate(String sText, boolean negValid) {
        int nUnitStart;
        int nNumStart = StringUtils.skipUntil(sText, "-0123456789.", 0);
        int nLength = StringUtils.skipOver(sText, "-0123456789.", nNumStart);
        int n = nUnitStart = nLength == 0 ? 0 : nNumStart + nLength;
        if (nNumStart > 0) {
            return new UnitSpan(sText).text(8, false, false);
        }
        int MAXUNITCHARS = 6;
        char[] cUnits = new char[MAXUNITCHARS + 1];
        int nUnitPos = 0;
        boolean bUnitsStarted = false;
        int strLen = sText.length();
        for (int n2 = nUnitStart; n2 < strLen; ++n2) {
            char c = sText.charAt(n2);
            if (c == ' ' || c == '.' || c == '\u0000') {
                if (!bUnitsStarted && c != '\u0000') continue;
                break;
            }
            cUnits[nUnitPos++] = c;
            bUnitsStarted = true;
            if (nUnitPos != MAXUNITCHARS) continue;
            cUnits[nUnitPos] = '\u0000';
            break;
        }
        bUnitsStarted = false;
        if (nUnitPos > 1) {
            if (cUnits[0] == 'i' || cUnits[0] == 'I') {
                bUnitsStarted = true;
            } else if (cUnits[0] == 'm' || cUnits[0] == 'M') {
                bUnitsStarted = true;
                if (cUnits[1] == 'p' || cUnits[1] == 'P') {
                    bUnitsStarted = true;
                } else if (nUnitPos > 5 && (cUnits[5] == 'p' || cUnits[5] == 'P')) {
                    bUnitsStarted = true;
                }
            } else if (cUnits[0] == 'c' || cUnits[0] == 'C') {
                bUnitsStarted = true;
            } else if (cUnits[0] == 'p' || cUnits[0] == 'P') {
                bUnitsStarted = true;
            }
        }
        if (!bUnitsStarted) {
            return new UnitSpan(sText).text(8, false, false);
        }
        int nDecimals = 0;
        for (int i = nNumStart; i != nNumStart + nLength; ++i) {
            char c = sText.charAt(i);
            if (c == '-') {
                if (!negValid) {
                    return ZERO.text(0, false, false);
                }
                if (i == nNumStart) continue;
                return new UnitSpan(sText).text(0, false, false);
            }
            if (c != '.') continue;
            if (nDecimals > 1) {
                return new UnitSpan(sText).text(0, false, false);
            }
            ++nDecimals;
        }
        return sText;
    }

    public static int stringToUnit(String sUnit, int eDefaultUnits) {
        if (!StringUtils.isEmpty(sUnit)) {
            String sUnitText = UnitSpan.unitToString(3);
            if (StringUtils.findNoCase(sUnitText, sUnit, 0) != -1) {
                return 3;
            }
            sUnitText = UnitSpan.unitToString(1);
            if (StringUtils.findNoCase(sUnitText, sUnit, 0) != -1) {
                return 1;
            }
            sUnitText = UnitSpan.unitToString(17);
            if (StringUtils.findNoCase(sUnitText, sUnit, 0) != -1) {
                return 17;
            }
            sUnitText = UnitSpan.unitToString(19);
            if (StringUtils.findNoCase(sUnitText, sUnit, 0) != -1) {
                return 19;
            }
            sUnitText = UnitSpan.unitToString(35);
            if (StringUtils.findNoCase(sUnitText, sUnit, 0) != -1) {
                return 35;
            }
            sUnitText = UnitSpan.unitToString(51);
            if (StringUtils.findNoCase(sUnitText, sUnit, 0) != -1) {
                return 51;
            }
        }
        return eDefaultUnits != 255 ? eDefaultUnits : UnitSpan.defaultUnits();
    }

    public static int unitsPerInch(int eUnit) {
        switch (eUnit) {
            case 0: {
                return 1000000;
            }
            case 1: 
            case 17: {
                return 635000;
            }
            case 3: {
                return 72000;
            }
            case 19: {
                return 72000;
            }
            case 35: {
                return 72000;
            }
            case 51: {
                return 72000;
            }
        }
        throw new ExFull(new MsgFormat(ResId.ERR_UNITSPAN_UNITS, Integer.toString(eUnit)));
    }

    public static String unitToString(int eUnit) {
        switch (eUnit) {
            case 0: 
            case 3: {
                return "in, inches";
            }
            case 1: {
                return "cm, centimeters";
            }
            case 17: {
                return "mm, millimeters";
            }
            case 19: {
                return "pt, points";
            }
            case 35: {
                return "mp, millipoints";
            }
            case 51: {
                return "pc, picas";
            }
        }
        throw new ExFull(new MsgFormat(ResId.ERR_UNITSPAN_UNITS, Integer.toString(eUnit)));
    }

    public static int unitToValue(double dValue, int eUnit) {
        switch (eUnit) {
            case 0: {
                return (int)Math.round(dValue * 1000000.0);
            }
            case 1: {
                return (int)Math.round(dValue * 250000.0);
            }
            case 17: {
                return (int)Math.round(dValue * 25000.0);
            }
            case 3: {
                return (int)Math.round(dValue * 72000.0);
            }
            case 19: {
                return (int)Math.round(dValue * 1000.0);
            }
            case 35: {
                return (int)Math.round(dValue);
            }
            case 51: {
                return (int)Math.round(dValue * 12000.0);
            }
        }
        throw new ExFull(new MsgFormat(ResId.ERR_UNITSPAN_UNITS, Integer.toString(eUnit)));
    }

    public static double valueToUnit(int nValue, int eUnit) {
        switch (eUnit) {
            case 0: {
                return (double)nValue / 1000000.0;
            }
            case 1: {
                return (double)nValue / 250000.0;
            }
            case 17: {
                return (double)nValue / 25000.0;
            }
            case 3: {
                return (double)nValue / 72000.0;
            }
            case 19: {
                return (double)nValue / 1000.0;
            }
            case 35: {
                return nValue;
            }
            case 51: {
                return (double)nValue / 12000.0;
            }
        }
        throw new ExFull(new MsgFormat(ResId.ERR_UNITSPAN_UNITS, Integer.toString(eUnit)));
    }

    public static UnitSpan zero() {
        return ZERO;
    }

    public UnitSpan() {
        this.meUnit = UnitSpan.defaultUnits();
        this.mnValue = 0;
    }

    public UnitSpan(double dValue, int eUnits) {
        this.meUnit = eUnits;
        this.mnValue = UnitSpan.unitToValue(dValue, eUnits);
    }

    public UnitSpan(int nValue) {
        this.meUnit = 255;
        this.mnValue = nValue;
    }

    public UnitSpan(int eUnits, int nValue) {
        this.meUnit = eUnits;
        this.mnValue = nValue;
    }

    public UnitSpan(int eNewUnits, int eOldUnits, int nOldValue) {
        this.meUnit = eNewUnits;
        this.mnValue = UnitSpan.convertUnit(eNewUnits, eOldUnits, nOldValue);
    }

    public UnitSpan(String sText) {
        this(sText, 255, false);
    }

    public UnitSpan(String sText, int eDefaultUnits, boolean bDefaultValuePerUnit) {
        ParseData oParseData = UnitSpan.validatingParse(sText, eDefaultUnits, true, bDefaultValuePerUnit, false);
        if (oParseData != null && oParseData.meUnits != 255) {
            this.meUnit = oParseData.meUnits;
            this.mnValue = oParseData.mnValue;
            return;
        }
        int nNumStart = StringUtils.skipUntil(sText, "-0123456789.", 0);
        int nLength = StringUtils.skipOver(sText, "-0123456789.", nNumStart);
        int nUnitStart = nLength == 0 ? 0 : nNumStart + nLength;
        int nUnitPos = 0;
        boolean bUnitsStarted = false;
        boolean bSlashFound = false;
        int eUnits = eDefaultUnits != 255 ? eDefaultUnits : UnitSpan.defaultUnits();
        boolean bDefaultUnitsUsed = true;
        boolean bValuePerUnit = bDefaultValuePerUnit;
        int c1 = 0;
        int c2 = 0;
        char c5 = '\u0000';
        for (int n = nUnitStart; n < sText.length(); ++n) {
            char c = sText.charAt(n);
            if (c == ' ' || c == '.' || c == '\u0000') {
                if (!bUnitsStarted && c != '\u0000') continue;
                break;
            }
            if (c == '/') {
                bSlashFound = true;
                continue;
            }
            if (nUnitPos == 0) {
                c1 = c;
            } else if (nUnitPos == 1) {
                c2 = c;
            } else if (nUnitPos == 5) {
                c5 = c;
            }
            ++nUnitPos;
            bUnitsStarted = true;
        }
        if (nUnitPos > 1) {
            bDefaultUnitsUsed = false;
            if (c1 == 105 || c1 == 73) {
                eUnits = 3;
                bValuePerUnit = bSlashFound;
                if (bValuePerUnit) {
                    eUnits = 0;
                }
            } else if (c1 == 109 || c1 == 77) {
                eUnits = 17;
                if (c2 == 112 || c2 == 80) {
                    eUnits = 35;
                } else if (nUnitPos > 5 && (c5 == 'p' || c5 == 'P')) {
                    eUnits = 35;
                }
                bValuePerUnit = bSlashFound;
            } else if (c1 == 99 || c1 == 67) {
                eUnits = 1;
                bValuePerUnit = bSlashFound;
            } else if (c1 == 112 || c1 == 80) {
                eUnits = 19;
                bValuePerUnit = bSlashFound;
                if (c2 == 99 || c2 == 67 || c2 == 105 || c2 == 73) {
                    eUnits = 51;
                }
            } else {
                bDefaultUnitsUsed = true;
            }
        }
        this.meUnit = eUnits;
        int nFactor = 0;
        switch (this.units()) {
            case 0: {
                nFactor = 1000000;
                break;
            }
            case 1: {
                nFactor = 250000;
                break;
            }
            case 17: {
                nFactor = 25000;
                break;
            }
            case 3: {
                nFactor = 72000;
                break;
            }
            case 19: {
                nFactor = 1000;
                break;
            }
            case 35: {
                nFactor = 1;
                break;
            }
            case 51: {
                nFactor = 12000;
            }
        }
        int nValue = 0;
        int nFraction = 0;
        int nDecimals = -1;
        boolean bNegative = false;
        for (int i = nNumStart; i != nNumStart + nLength; ++i) {
            char c = sText.charAt(i);
            if (c == '-') {
                bNegative = true;
                continue;
            }
            if (c == '.') {
                nDecimals = 0;
                continue;
            }
            if (nDecimals == -1) {
                nValue *= 10;
                if ((nValue += (c - 48) * nFactor) >= 0) continue;
                nValue = Integer.MAX_VALUE;
                break;
            }
            if (c != '0') {
                int nDecimal = (c - 48) * nFactor;
                for (int k = 0; k < nDecimals; ++k) {
                    nDecimal /= 10;
                }
                nFraction += nDecimal;
            }
            ++nDecimals;
        }
        if ((nValue += (nFraction + 5) / 10) < 0) {
            this.mnValue = Integer.MAX_VALUE;
            return;
        }
        if (bNegative) {
            nValue *= -1;
        } else if (bValuePerUnit) {
            if (bDefaultUnitsUsed && eUnits == 17) {
                nValue /= 10;
            } else if (eUnits == 19) {
                nValue /= 12;
            }
            double dFactor = nFactor;
            nValue = (int)(dFactor * dFactor / (double)nValue);
        }
        this.mnValue = nValue;
    }

    public UnitSpan(UnitSpan source) {
        this.meUnit = source.meUnit;
        this.mnValue = source.mnValue;
    }

    public UnitSpan abs() {
        if (this.value() > 0) {
            return this;
        }
        return new UnitSpan(this.units(), - this.value());
    }

    public UnitSpan add(UnitSpan add) {
        int nAdd = UnitSpan.convertUnit(this.units(), add.units(), add.value());
        return new UnitSpan(this.units(), this.value() + nAdd);
    }

    UnitSpan applyFactor(int nNumerator, int nDenominator) {
        if (nNumerator % nDenominator == 0) {
            return new UnitSpan(this.units(), this.value() * (nNumerator / nDenominator));
        }
        if (this.value() % nDenominator == 0) {
            return new UnitSpan(this.units(), nNumerator * (this.value() / nDenominator));
        }
        int nTop = nNumerator;
        int nBottom = nDenominator;
        while (nTop % 10 == 0 && nBottom % 10 == 0) {
            nTop = (int)((long)nTop / 10);
            nBottom = (int)((long)nBottom / 10);
            if (this.value() % nBottom != 0) continue;
            return new UnitSpan(this.units(), nTop * (this.value() / nBottom));
        }
        double dScale = (double)nNumerator / (double)nDenominator;
        return new UnitSpan(this.units(), (int)Math.round((double)this.value() * dScale));
    }

    public UnitSpan divide(int nDivisor) {
        return new UnitSpan(this.units(), this.mnValue / nDivisor);
    }

    public double divide(UnitSpan divisor) {
        UnitSpan oConverted = new UnitSpan(this.units(), divisor.units(), divisor.value());
        return (double)this.value() / (double)oConverted.value();
    }

    public boolean equals(Object object) {
        if (this == object) {
            return true;
        }
        if (object == null) {
            return false;
        }
        if (object.getClass() != this.getClass()) {
            return false;
        }
        return this.compareTo((UnitSpan)object) == 0;
    }

    public int hashCode() {
        return 31 * (this.meUnit & 15) ^ this.mnValue;
    }

    public UnitSpan grid(UnitSpan grid) {
        int value = UnitSpan.convertUnit(grid.units(), this.units(), this.value());
        int gridValue = grid.value();
        if (gridValue != 0 && value % gridValue != 0) {
            value = (int)(Math.floor((double)value / (double)gridValue) * (double)gridValue);
            return new UnitSpan(this.units(), UnitSpan.convertUnit(this.units(), grid.units(), value));
        }
        return this;
    }

    public boolean gt(UnitSpan compare) {
        return this.compareTo(compare) > 0;
    }

    public boolean gte(UnitSpan compare) {
        return this.compareTo(compare) >= 0;
    }

    public boolean lt(UnitSpan compare) {
        return this.compareTo(compare) < 0;
    }

    public boolean lte(UnitSpan compare) {
        return this.compareTo(compare) <= 0;
    }

    public UnitSpan multiply(double dScale) {
        return new UnitSpan(this.units(), (int)Math.round((double)this.value() * dScale));
    }

    public UnitSpan multiply(int nScale) {
        return new UnitSpan(this.units(), this.mnValue * nScale);
    }

    public UnitSpan round(UnitSpan round) {
        int rounded = UnitSpan.convertUnit(round.units(), this.units(), this.value());
        int nAbsRound = Math.abs(round.value());
        if (round.value() != 0 && rounded % round.value() != 0) {
            int nQuotient = rounded / nAbsRound;
            if (Math.abs((double)rounded / (double)nAbsRound - (double)nQuotient) >= 0.5) {
                nQuotient = nQuotient > 0 ? ++nQuotient : --nQuotient;
            }
            rounded = nQuotient * nAbsRound;
            return new UnitSpan(this.units(), UnitSpan.convertUnit(this.units(), round.units(), rounded));
        }
        return this;
    }

    public UnitSpan subtract(UnitSpan subtract) {
        int lSubtract = UnitSpan.convertUnit(this.units(), subtract.units(), subtract.value());
        return new UnitSpan(this.units(), this.value() - lSubtract);
    }

    public String text(int nPrecision, boolean bTruncate, boolean bValuePerUnits) {
        if (this.units() == 255) {
            return Integer.toString(this.value());
        }
        int nValue = this.value();
        int nFactor = 0;
        int STRSIZE = 32;
        char[] cValue = new char[32];
        cValue[31] = '\u0000';
        switch (this.units()) {
            case 0: {
                nFactor = 6;
                cValue[29] = 105;
                cValue[30] = 110;
                break;
            }
            case 1: {
                nValue = UnitSpan.applyFactor(this.value(), 10, 25);
                nFactor = 5;
                cValue[29] = 99;
                cValue[30] = 109;
                break;
            }
            case 17: {
                nValue = UnitSpan.applyFactor(this.value(), 10, 25);
                nFactor = 4;
                cValue[29] = 109;
                cValue[30] = 109;
                break;
            }
            case 2: {
                nFactor = 4;
                cValue[29] = 112;
                cValue[30] = 116;
                break;
            }
            case 3: {
                nValue = UnitSpan.convertReally(0, 3, this.value());
                nFactor = 6;
                cValue[29] = 105;
                cValue[30] = 110;
                break;
            }
            case 19: {
                nFactor = 3;
                cValue[29] = 112;
                cValue[30] = 116;
                break;
            }
            case 35: {
                nFactor = 0;
                cValue[29] = 109;
                cValue[30] = 112;
                break;
            }
            case 51: {
                nValue = UnitSpan.applyFactor(this.value(), 1, 12);
                nFactor = 3;
                cValue[29] = 112;
                cValue[30] = 99;
            }
        }
        int nPos = 28;
        if (bValuePerUnits) {
            if (this.units() == 17) {
                nFactor = 5;
                cValue[29] = 99;
                cValue[30] = 109;
            }
            if (this.units() == 2 || this.units() == 19 || this.units() == 35) {
                nValue = (int)((long)nValue / 12);
                cValue[29] = 112;
                cValue[30] = 99;
            }
            cValue[28] = 47;
            nPos = 27;
            double factor = Math.pow(10.0, nFactor);
            nValue = (int)(factor * factor / (double)nValue);
            nValue = (nValue + 50) / 100 * 100;
        }
        boolean bNegative = false;
        if (nValue < 0) {
            nValue *= -1;
            bNegative = true;
        }
        while (nFactor > nPrecision) {
            nValue = nFactor == nPrecision + 1 ? (nValue + 5) / 10 : (nValue /= 10);
            --nFactor;
        }
        boolean bZeroValue = (long)nValue == 0;
        boolean bAllZeros = true;
        if (bZeroValue) {
            cValue[nPos--] = 48;
        } else {
            while (nValue > 0) {
                int cDigit;
                if (nFactor == 0 && !bAllZeros) {
                    cValue[nPos--] = 46;
                }
                if ((cDigit = nValue % 10 + 48) != 48 || !bAllZeros || nFactor <= 0) {
                    cValue[nPos--] = (char)cDigit;
                    bAllZeros = false;
                }
                nValue /= 10;
                --nFactor;
            }
            while (nFactor > 0) {
                cValue[nPos--] = 48;
                --nFactor;
            }
            if (nFactor == 0 && !bAllZeros) {
                cValue[nPos--] = 46;
                cValue[nPos--] = 48;
            }
            if (bNegative && !bZeroValue) {
                cValue[nPos--] = 45;
            }
        }
        return new String(cValue, nPos + 1, 32 - nPos - 2);
    }

    public String toString() {
        return this.text(8, false, false);
    }

    public int units() {
        return this.meUnit;
    }

    public int unitsPerInch() {
        return UnitSpan.unitsPerInch(this.meUnit);
    }

    public static ParseData validatingParse(String sText, int eDefaultUnits, boolean bAllowNegative, boolean bForceValuePerUnit, boolean bAllowPercent) {
        boolean bNegative;
        int nFractionScale;
        char cUnit0;
        int nFraction;
        char cUnit1;
        boolean bValuePerUnit;
        int nValue;
        int eUnits;
        char cUnit2;
        boolean bPercent;
        UnitMapEntry unitMapEntry;
        int i;
        nValue = 0;
        nFraction = 0;
        nFractionScale = 1;
        cUnit0 = '\u0000';
        cUnit1 = '\u0000';
        cUnit2 = '\u0000';
        eUnits = 255;
        bValuePerUnit = bForceValuePerUnit;
        bPercent = false;
        State eState = State.PreSign;
        int MAX_FRACTION_SCALE = 10000000;
        bNegative = false;
        boolean bSlashSeen = false;
        int nUnitIndex = 0;
        block56 : for (int i2 = 0; i2 < sText.length(); ++i2) {
            char c = sText.charAt(i2);
            int cType = c;
            if ('0' <= c && c <= '9') {
                cType = 48;
            } else if ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') {
                cType = 97;
            }
            switch (eState) {
                case PreSign: {
                    switch (cType) {
                        case 45: {
                            if (!bAllowNegative) {
                                return null;
                            }
                            bNegative = true;
                            eState = State.PreNumber;
                            continue block56;
                        }
                        case 43: {
                            eState = State.PreNumber;
                            continue block56;
                        }
                        case 48: {
                            nValue = c - 48;
                            eState = State.WholePart;
                            continue block56;
                        }
                        case 46: {
                            eState = State.FractionStart;
                            continue block56;
                        }
                        case 32: {
                            continue block56;
                        }
                    }
                    return null;
                }
                case PreNumber: {
                    switch (cType) {
                        case 48: {
                            nValue = c - 48;
                            eState = State.WholePart;
                            continue block56;
                        }
                        case 46: {
                            eState = State.FractionStart;
                            continue block56;
                        }
                        case 32: {
                            continue block56;
                        }
                    }
                    return null;
                }
                case WholePart: {
                    switch (cType) {
                        case 48: {
                            if (nValue >= Integer.MAX_VALUE || (nValue = nValue * 10 + (c - 48)) >= 0) continue block56;
                            nValue = Integer.MAX_VALUE;
                            continue block56;
                        }
                        case 46: {
                            eState = State.FractionPart;
                            continue block56;
                        }
                        case 97: {
                            assert (nUnitIndex == 0);
                            cUnit0 = c;
                            ++nUnitIndex;
                            eState = State.Units;
                            continue block56;
                        }
                        case 37: {
                            if (!bAllowPercent) {
                                return null;
                            }
                            bPercent = true;
                            eState = State.PostValue;
                            continue block56;
                        }
                        case 47: {
                            bValuePerUnit = true;
                            bSlashSeen = true;
                            eState = State.PostNumber;
                            continue block56;
                        }
                        case 32: {
                            eState = State.PostNumber;
                            continue block56;
                        }
                    }
                    return null;
                }
                case FractionStart: {
                    switch (cType) {
                        case 48: {
                            nFraction = c - 48;
                            nFractionScale *= 10;
                            eState = State.FractionPart;
                            continue block56;
                        }
                    }
                    return null;
                }
                case FractionPart: {
                    switch (cType) {
                        case 48: {
                            if (nFractionScale > 10000000) continue block56;
                            nFraction = nFraction * 10 + (c - 48);
                            nFractionScale *= 10;
                            continue block56;
                        }
                        case 97: {
                            assert (nUnitIndex == 0);
                            cUnit0 = c;
                            ++nUnitIndex;
                            eState = State.Units;
                            continue block56;
                        }
                        case 37: {
                            if (!bAllowPercent) {
                                return null;
                            }
                            bPercent = true;
                            eState = State.PostValue;
                            continue block56;
                        }
                        case 47: {
                            bValuePerUnit = true;
                            bSlashSeen = true;
                            eState = State.PostNumber;
                            continue block56;
                        }
                        case 32: {
                            eState = State.PostNumber;
                            continue block56;
                        }
                    }
                    return null;
                }
                case PostNumber: {
                    switch (cType) {
                        case 97: {
                            assert (nUnitIndex == 0);
                            cUnit0 = c;
                            ++nUnitIndex;
                            eState = State.Units;
                            continue block56;
                        }
                        case 37: {
                            if (!bAllowPercent) {
                                return null;
                            }
                            bPercent = true;
                            eState = State.PostValue;
                            continue block56;
                        }
                        case 47: {
                            if (bSlashSeen) {
                                return null;
                            }
                            bValuePerUnit = true;
                            bSlashSeen = true;
                            continue block56;
                        }
                        case 32: {
                            continue block56;
                        }
                    }
                    return null;
                }
                case Units: {
                    switch (cType) {
                        case 97: {
                            if (nUnitIndex == 0) {
                                cUnit0 = c;
                            } else if (nUnitIndex == 1) {
                                cUnit1 = c;
                            } else if (nUnitIndex == 2) {
                                cUnit2 = c;
                            } else assert (cUnit2 != '\u0000');
                            ++nUnitIndex;
                            continue block56;
                        }
                        case 32: {
                            eState = State.PostValue;
                            continue block56;
                        }
                    }
                    return null;
                }
                case PostValue: {
                    switch (cType) {
                        case 32: {
                            continue block56;
                        }
                    }
                    return null;
                }
            }
        }
        switch (eState) {
            case PreSign: 
            case PreNumber: 
            case FractionStart: {
                return null;
            }
        }
        unitMapEntry = null;
        if (eState != State.Units && eState != State.PostValue) {
            if (eDefaultUnits == 255) {
                eDefaultUnits = UnitSpan.defaultUnits();
            }
            for (i = 0; i < gUnitMap.length; ++i) {
                UnitMapEntry oMapEntry = gUnitMap[i];
                if (oMapEntry.meUnits != eDefaultUnits) continue;
                unitMapEntry = oMapEntry;
                break;
            }
        } else if (!bPercent && nUnitIndex == 2) {
            for (i = 0; i < gUnitMap.length; ++i) {
                UnitMapEntry mapEntry = gUnitMap[i];
                if (cUnit0 != mapEntry.msLower.charAt(0) && cUnit0 != mapEntry.msUpper.charAt(0) || cUnit1 != mapEntry.msLower.charAt(1) && cUnit1 != mapEntry.msUpper.charAt(1)) continue;
                unitMapEntry = mapEntry;
                break;
            }
        }
        if (unitMapEntry != null) {
            eUnits = unitMapEntry.meUnits;
            if (nValue < Integer.MAX_VALUE) {
                long lResult = (long)nValue * (long)unitMapEntry.mnUnitScale;
                if (nFraction != 0) {
                    long lFraction = nFraction;
                    lFraction *= (long)unitMapEntry.mnUnitScale;
                    lFraction += (long)(nFractionScale / 2);
                    lResult += (lFraction /= (long)nFractionScale);
                }
                nValue = lResult > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int)lResult;
            }
            if (bValuePerUnit) {
                if (nValue == 0) {
                    return null;
                }
                double dUnitScale = unitMapEntry.mnUnitScale;
                nValue = (int)Math.round(dUnitScale * dUnitScale / (double)nValue);
            }
        }
        if (bNegative) {
            nValue = - nValue;
            nFraction = - nFraction;
        }
        return new ParseData(nValue, nFraction, nFractionScale, eUnits, cUnit0, cUnit1, cUnit2, bValuePerUnit, bPercent);
    }

    public int value() {
        return this.mnValue;
    }

    public int valueAsUnit(int eUnits) {
        return UnitSpan.convertUnit(eUnits, this.units(), this.value());
    }

    public static boolean match(UnitSpan u1, UnitSpan u2) {
        if (u1 == u2) {
            return true;
        }
        if (u1 == null || u2 == null) {
            return false;
        }
        return u1.equals(u2);
    }

    @Override
    public int compareTo(UnitSpan compare) {
        if (compare == null) {
            throw new NullPointerException();
        }
        int nTest = UnitSpan.convertUnit(this.units(), compare.units(), compare.value());
        if (this.value() < nTest) {
            return -1;
        }
        if (this.value() > nTest) {
            return 1;
        }
        return 0;
    }

    private static enum State {
        PreSign,
        PreNumber,
        WholePart,
        FractionStart,
        FractionPart,
        PostNumber,
        Units,
        PostValue;
        

        private State() {
        }
    }

    private static class UnitMapEntry {
        final String msLower;
        final String msUpper;
        final int meUnits;
        final int mnUnitScale;

        UnitMapEntry(String lower, String upper, int units, int unitScale) {
            this.msLower = lower;
            this.msUpper = upper;
            this.meUnits = units;
            this.mnUnitScale = unitScale;
        }
    }

    public static class ParseData {
        public final int mnValue;
        public final int mnFraction;
        public final int mnFractionScale;
        public final int meUnits;
        public final char mcUnit0;
        public final char mcUnit1;
        public final char mcUnit2;
        public final boolean mbValuePerUnit;
        public final boolean mbPercent;

        public ParseData(int nValue, int nFraction, int nFractionScale, int eUnits, char cUnit0, char cUnit1, char cUnit2, boolean bValuePerUnit, boolean bPercent) {
            this.mnValue = nValue;
            this.mnFraction = nFraction;
            this.mnFractionScale = nFractionScale;
            this.meUnits = eUnits;
            this.mcUnit0 = cUnit0;
            this.mcUnit1 = cUnit1;
            this.mcUnit2 = cUnit2;
            this.mbValuePerUnit = bValuePerUnit;
            this.mbPercent = bPercent;
        }
    }

}