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 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 | package Torello.Java.Additional; import javax.json.*; import java.math.*; import static javax.json.JsonValue.ValueType.*; import static Torello.Java.Additional.JFlag.*; import java.util.function.Function; /** * Class which provides a series of helper functions for both {@link ReadJSON} and * {@link ReadJSON.XL}. * * <BR /><BR />The helpers for class {@link ReadArrJSON} are kept inside that class, and do not * appear in this one. * * <BR /><BR /><B CLASS=JDDescLabel>IMPORTANT:</B> * <BR />100% of the helper-methods that appear here are protected, and cannot be accessed * outside of this package. They are included in the documentation solely for the purposes of * (<I>if you happen to be interested</I>) letting you know how the JSON-Tools work. <I>It is not * intended that programmers would ever need to invoke, directly, any of the methods in this * class!</I> */ @Torello.JavaDoc.StaticFunctional public class RJInternal { private RJInternal() { } // ******************************************************************************************** // ******************************************************************************************** // "Helpers for the Helpers for the Helpers" // ******************************************************************************************** // ******************************************************************************************** /** * Generates a {@code JsonException} with a uniformly-consisten error-message. protected static void throwJsonBindingException(Class<?> c) { throw new JsonException( "The class which was passed to parameter 'c' [" + c.getName() + "] does not " + "appear to have a constructor with precisely one parameter of type JsonObject." ); } */ /** * Helper Method that generates an {@code ArithmeticException} with a uniformly-consistent * exception message * @param jn A Java {@code javax.json.JsonNumber} whose magnitude is too large. * @throws ArithmeticException */ protected static void throwAE_INFINITY (JsonNumber jn, String primTypeName, boolean positiveOrNegative) { throw new ArithmeticException( "When attempting to conver the JsonNumber [" + jn.toString() + "] to a " + primTypeName + " primitive, the number had a magnitude that was too large: " + (positiveOrNegative ? "Positive" : "Negative") + " Infinity was returned." ); } /** * Helper Method that generates an {@code ArithmeticException} with a uniformly-consistent * exception message * @param bd A Java {@code java.math.BigDecimal} whose magnitude is too large. * @throws ArithmeticException */ protected static void throwAE_INFINITY (BigDecimal bd, String primTypeName, boolean positiveOrNegative) { throw new ArithmeticException( "When attempting to conver the JsonNumber [" + bd.toString() + "] to a " + primTypeName + " primitive, the number had a magnitude that was too large: " + (positiveOrNegative ? "Positive" : "Negative") + " Infinity was returned." ); } /** * Converts a {@link JsonNumber} into a Java {@code double} * @param jn Any {@link JsonNumber} * @return java {@code double} primitive * @throws JsonArithmeticException If infinity is returned from the call to * {@code BigDecimal.doubleValue()} * @see JsonNumber#bigDecimalValue() */ protected static double DOUBLE_WITH_CHECK(JsonNumber jn) { return DOUBLE_WITH_CHECK(jn.bigDecimalValue()); } /** * Converts a {@code BigDecimal} into a Java {@code double} * @param bd Any {@code BigDecimal} * @return Java {@code double} primitive * @throws JsonArithmeticException If infinity is returned from the call to * {@code code BigDecimal.doubleValue()} */ protected static double DOUBLE_WITH_CHECK(BigDecimal bd) { double ret = bd.doubleValue(); if (ret == Double.NEGATIVE_INFINITY) throwAE_INFINITY(bd, "double", false); if (ret == Double.POSITIVE_INFINITY) throwAE_INFINITY(bd, "double", true); return ret; } /** * Converts a {@link JsonNumber} into a Java {@code float} * @param jn Any {@link JsonNumber} * @return java {@code float} primitive * @throws JsonArithmeticException If infinity is returned from the call to * {@code BigDecimal.floatValue()} * @see JsonNumber#bigDecimalValue() */ protected static float FLOAT_WITH_CHECK(JsonNumber jn) { return FLOAT_WITH_CHECK(jn.bigDecimalValue()); } /** * Converts a {@code BigDecimal} into a Java {@code float} * @param bd Any {@code BigDecimal} * @return Java {@code float} primitive * @throws JsonArithmeticException If infinity is returned from the call to * {@code code BigDecimal.floatValue()} */ protected static float FLOAT_WITH_CHECK(BigDecimal bd) { float ret = bd.floatValue(); if (ret == Float.NEGATIVE_INFINITY) throwAE_INFINITY(bd, "float", false); if (ret == Float.POSITIVE_INFINITY) throwAE_INFINITY(bd, "float", true); return ret; } /** * Converts any {@link JsonNumber} into one of the inheriting subclasses of Java class * {@code Number} * @param jn Any {@link JsonNumber} * @return The most appropriate intance of {@code java.lang.Number} * @see ReadJSON#getNUMBER(JsonObject, String, int, Number) * @see ReadJSON#getNUMBER(JsonArray, int, int, Number) * @see JsonNumber#isIntegral() * @see JsonNumber#bigIntegerValue() * @see JsonNumber#bigDecimalValue() */ protected static Number convertToNumber(JsonNumber jn) { if (jn.isIntegral()) { BigInteger bi = jn.bigIntegerValue(); int l = bi.bitLength(); if (l <= 32) return Integer.valueOf(bi.intValue()); if (l <= 64) return Long.valueOf(bi.longValue()); return bi; } else { BigDecimal bd = jn.bigDecimalValue(); // This probably isn't the most efficient thing I've ever written, but I do not // have the energy to stare at java.math.BigDecimal at the moment. The JavaDoc for // this JSON => Java-Type Conversion is quite intricate. I will figure this out at // at later date. float f = bd.floatValue(); if ((f != Float.NEGATIVE_INFINITY) && (f != Float.POSITIVE_INFINITY)) return f; double d = bd.doubleValue(); if ((f != Double.NEGATIVE_INFINITY) && (f != Double.POSITIVE_INFINITY)) return d; return bd; } } /** * Converts any {@code java.lang.String} into one of the inheriting subclasses of Java class * {@code Number} * @param s Any {@code String} * @return The most appropriate instance of {@code java.lang.Number} * @throws NumberFormatException If the input {@code String} isn't properly formatted as a * number. * @see ReadJSON#parseNUMBER(JsonObject, String, int, Number, Function) * @see ReadJSON#parseNUMBER(JsonArray, int, int, Number, Function) */ protected static Number convertToNumber(String s) { return convertToNumber(new BigDecimal(s.trim())); } /** * Converts any {@code java.math.BigDecimal} into one of the inheriting subclasses of * {@code Number}. * @param bd Any {@code BigDecimal} * @return The most appropriate instance of {@code java.lang.Number} */ protected static Number convertToNumber(BigDecimal bd) { if (bd.scale() == 0) { BigInteger bi = bd.toBigInteger(); int l = bi.bitLength(); if (l <= 32) return Integer.valueOf(bi.intValue()); if (l <= 64) return Long.valueOf(bi.longValue()); return bi; } else { // This probably isn't the most efficient thing I've ever written, but I do not // have the energy to stare at java.math.BigDecimal at the moment. The JavaDoc for // this JSON => Java-Type Conversion is quite intricate. I will figure this out at // at later date. float f = bd.floatValue(); if ((f != Float.NEGATIVE_INFINITY) && (f != Float.POSITIVE_INFINITY)) return f; double d = bd.doubleValue(); if ((f != Double.NEGATIVE_INFINITY) && (f != Double.POSITIVE_INFINITY)) return d; return bd; } } // ******************************************************************************************** // ******************************************************************************************** // PRIMARY FOUR "GET" METHODS FOR NUMBERS // ******************************************************************************************** // ******************************************************************************************** /** * This is an internal helper method for retrieving an element from a {@link JsonArray}, * and converting it to one of the standard <B STYLE='color: red;'>Java Types</B>. * * @param <T> <EMBED CLASS='external-html' DATA-FILE-ID=JR_GET_T> * @param ja Any instance of {@link JsonArray} * @param index Any index into the array which holds a {@link JsonNumber} * @param primitiveClass <EMBED CLASS='external-html' DATA-FILE-ID=JR_GET_PC> * @param jsonTypeToJavaType <EMBED CLASS='external-html' DATA-FILE-ID=JR_GET_JTTJT> * * @return The converted number, as an instance Generic-Parameter {@code 'T'} * * @throws JsonNullPrimitiveArrException <EMBED CLASS='external-html' * DATA-FILE-ID=JR_GET_JNPAEX> * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_GET_JTAEX> * @throws JsonArithmeticArrException If there any arithmetic problems during the conversion * @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'} * * @see ReadJSON#getInt(JsonArray, int) * @see ReadJSON#getLong(JsonArray, int) * @see ReadJSON#getShort(JsonArray, int) * @see ReadJSON#getByte(JsonArray, int) * @see ReadJSON#getDouble(JsonArray, int) * @see ReadJSON#getFloat(JsonArray, int) */ protected static <T> T GET( JsonArray ja, int index, Class<T> primitiveClass, Function<JsonNumber, T> jsonTypeToJavaType ) { // This will throw an IndexOutOfBoundsException if the index is out of bounds. JsonValue jv = ja.get(index); switch (jv.getValueType()) { // This method allows for null-returns. If Json-Null, return Java-Null. case NULL: throw new JsonNullPrimitiveArrException (ja, index, NUMBER, primitiveClass); // This will throw ArithmeticException if it cannot be converted case NUMBER: // REMEMBER: The primary reason for this class is that MEANINGFUL ERROR MESSAGES // make Json-Binding a lot easer... "JsonArithmeticException" has just // about everything that you need to know when debugging this stuff try { return jsonTypeToJavaType.apply((JsonNumber) jv); } catch (ArithmeticException ae) { throw new JsonArithmeticArrException (ae, ja, index, NUMBER, jv, primitiveClass); } // The JsonValue at the specified array-index does not contain an JsonNumber. default: throw new JsonTypeArrException (ja, index, NUMBER, jv, primitiveClass); } } /** * This is an internal helper method for retrieving an element from a {@link JsonArray}, * and converting it to one of the standard <B STYLE='color: red;'>Java Types</B>. * * @param <T> <EMBED CLASS='external-html' DATA-FILE-ID=JR_GET_T> * @param ja Any instance of {@link JsonArray} * @param index Any index into the array which holds a {@link JsonNumber} * @param jsonTypeToJavaType <EMBED CLASS='external-html' DATA-FILE-ID=JR_GET_JTTJT> * * @return The converted number, as an instance Generic-Parameter {@code 'T'} * * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_GET_JTAEX> * @throws JsonArithmeticArrException If there any arithmetic problems during the conversion * @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'} * * @see ReadJSON#getINTEGER(JsonArray, int) * @see ReadJSON#getLONG(JsonArray, int) * @see ReadJSON#getSHORT(JsonArray, int) * @see ReadJSON#getBYTE(JsonArray, int) * @see ReadJSON#getDOUBLE(JsonArray, int) * @see ReadJSON#getFLOAT(JsonArray, int) */ protected static <T extends java.lang.Number> T GET (JsonArray ja, int index, Function<JsonNumber, T> jsonTypeToJavaType, Class<T> returnClass) { // This will throw an IndexOutOfBoundsException if the index is out of bounds. // Since this *IS NOT* a method with FLAGS, the user has no way to avoid this exception // throw if, indeed, the index really is out of bounds! // // Using one of the 'FLAGS' variants of the 'GET' array-index, a user may request that // either null or a default-value be returned. Not with this version-of 'GET', though. JsonValue jv = ja.get(index); switch (jv.getValueType()) { // This method allows for null-returns. If Json-Null, return Java-Null. case NULL: return null; // This will throw ArithmeticException if it cannot be converted case NUMBER: // REMEMBER: The primary reason for this class is that MEANINGFUL ERROR MESSAGES // make Json-Binding a lot easer... "JsonArithmeticException" has just // about everything that you need to know when debugging this stuff try { return jsonTypeToJavaType.apply((JsonNumber) jv); } catch (ArithmeticException ae) { throw new JsonArithmeticArrException (ae, ja, index, NUMBER, jv, returnClass); } // The JsonValue at the specified array-index does not contain an JsonNumber. default: throw new JsonTypeArrException (ja, index, NUMBER, jv, returnClass); } } /** * This is an internal helper method for retrieving a property from a {@link JsonObject}, * and converting it to one of the standard <B STYLE='color: red;'>Java Types</B>. * * @param <T> <EMBED CLASS='external-html' DATA-FILE-ID=JR_GET_T> * @param jo Any instance of {@link JsonObject} * @param propertyName Any property name contained by {@code 'jo'} * @param jsonTypeToJavaType <EMBED CLASS='external-html' DATA-FILE-ID=JR_GET_JTTJT> * * @return The converted number, as an instance Generic-Parameter {@code 'T'} * * @throws JsonNullPrimitiveObjException <EMBED CLASS='external-html' * DATA-FILE-ID=JR_GET_JNPOEX> * @throws JsonPropMissingException If the property is missing, and {@code 'isOptional'} * is {@code FALSE}. * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_GET_JTAEX> * @throws JsonArithmeticObjException If there any arithmetic problems during the conversion * * @see ReadJSON#getInt(JsonObject, String) * @see ReadJSON#getLong(JsonObject, String) * @see ReadJSON#getShort(JsonObject, String) * @see ReadJSON#getByte(JsonObject, String) * @see ReadJSON#getDouble(JsonObject, String) * @see ReadJSON#getFloat(JsonObject, String) */ protected static <T> T GET( JsonObject jo, String propertyName, Class<T> primitiveClass, Function<JsonNumber, T> jsonTypeToJavaType ) { // Here, a 'get' request was made for a property that isn't actually listed among the // properties in the provided JsonObject. Since this internal 'GET' is used by methods // that are trying to return a Java Primitive (like 'int' or 'float'), then an exception // has to be thrown. The option of returning 'null' isn't possible here! if (! jo.containsKey(propertyName)) throw new JsonPropMissingException (jo, propertyName, NUMBER, primitiveClass); JsonValue jv = jo.get(propertyName); switch (jv.getValueType()) { // This method allows for null-returns. If Json-Null, return Java-Null. case NULL: throw new JsonNullPrimitiveObjException (jo, propertyName, NUMBER, primitiveClass); // This will throw ArithmeticException if this isn't a proper Java int case NUMBER: // REMEMBER: The primary reason for this class is that MEANINGFUL ERROR MESSAGES // make Json-Binding a lot easer... "JsonArithmeticException" has just // about everything that you need to know when debugging this stuff try { return jsonTypeToJavaType.apply((JsonNumber) jv); } catch (ArithmeticException ae) { throw new JsonArithmeticObjException (ae, jo, propertyName, NUMBER, jv, primitiveClass); } // The JsonObject property does not contain a JsonNumber. default: throw new JsonTypeObjException (jo, propertyName, NUMBER, jv, primitiveClass); } } /** * This is an internal helper method for retrieving a property from a {@link JsonObject}, * and converting it to one of the standard <B STYLE='color: red;'>Java Types</B>. * * @param <T> <EMBED CLASS='external-html' DATA-FILE-ID=JR_GET_T> * @param jo Any instance of {@link JsonObject} * @param propertyName Any property name contained by {@code 'jo'} * @param isOptional Indicates whether {@code 'propertyName'} may be missing from {@code 'jo'} * @param jsonTypeToJavaType <EMBED CLASS='external-html' DATA-FILE-ID=JR_GET_JTTJT> * * @return The converted number, as an instance Generic-Parameter {@code 'T'} * * @throws JsonPropMissingException If the property is missing, and {@code 'isOptional'} * is {@code FALSE}. * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_GET_JTAEX> * @throws JsonArithmeticObjException If there any arithmetic problems during the conversion * * @see ReadJSON#getINTEGER(JsonObject, String, boolean) * @see ReadJSON#getLONG(JsonObject, String, boolean) * @see ReadJSON#getSHORT(JsonObject, String, boolean) * @see ReadJSON#getBYTE(JsonObject, String, boolean) * @see ReadJSON#getDOUBLE(JsonObject, String, boolean) * @see ReadJSON#getFLOAT(JsonObject, String, boolean) */ protected static <T extends java.lang.Number> T GET( JsonObject jo, String propertyName, boolean isOptional, Function<JsonNumber, T> jsonTypeToJavaType, Class<T> returnClass ) { // Here, a 'get' request was made for a property that isn't actually listed among the // properties in the provided JsonObject. If 'isOptional' return null, otherwise throw if (! jo.containsKey(propertyName)) { if (isOptional) return null; throw new JsonPropMissingException (jo, propertyName, NUMBER, returnClass); } JsonValue jv = jo.get(propertyName); switch (jv.getValueType()) { // This method allows for null-returns. If Json-Null, return Java-Null. case NULL: return null; // This will throw ArithmeticException if this isn't a proper Java int case NUMBER: // REMEMBER: The primary reason for this class is that MEANINGFUL ERROR MESSAGES // make Json-Binding a lot easer... "JsonArithmeticException" has just // about everything that you need to know when debugging this stuff try { return jsonTypeToJavaType.apply((JsonNumber) jv); } catch (ArithmeticException ae) { throw new JsonArithmeticObjException (ae, jo, propertyName, NUMBER, jv, returnClass); } // The JsonObject property does not contain a JsonNumber. default: throw new JsonTypeObjException (jo, propertyName, NUMBER, jv, returnClass); } } // ******************************************************************************************** // ******************************************************************************************** // FLAG-CHECKER METHODS another section of "Helpers for the Helpers ..." // ******************************************************************************************** // ******************************************************************************************** /** * Flag Checker for {@code IndexOutOfBoundsException}. * * <BR /><BR />Checks whether the relevant flags were set in the users {@code FLAGS} parameter, * and either returns the appropriate value accordingly, or throws * {@code IndexOutOfBoundsException}. * * <EMBED CLASS='external-html' DATA-FILE-ID=FLAG_PRECEDENCE> * * @param <T> If requested, the default-value is returned, and this is its type. * * @return Can return either the user-provided default-value, or null depending on whether a * match was found in the user's request settings ({@code 'FLAGS'}). * * @throws IndexOutOfBoundsException If no flag was set specifying one of the two return-value * options. * * @see JFlag#RETURN_NULL_ON_IOB * @see JFlag#RETURN_DEFVAL_ON_IOB * @see JFlag#RETURN_NULL_ON_ANY_ALL * @see JFlag#RETURN_DEFVAL_ON_ANY_ALL */ protected static <T> T IOOBEX(JsonArray ja, int index, T defaultValue, int FLAGS) { if ((FLAGS & RETURN_NULL_ON_IOB) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_IOB) != 0) return defaultValue; if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue; ja.get(index); // Throws an IndexOutOfBoundsException // If you have reached this statment, this method was not applied properly throw new Torello.Java.UnreachableError(); } /** * Flag Checker for {@link JsonPropMissingException} * * <BR /><BR />Checks whether the relevant flags were set in the users {@code FLAGS} parameter, * and either returns the appropriate value accordingly, or throws * {@code JsonPropMissingException}. * * <EMBED CLASS='external-html' DATA-FILE-ID=FLAG_PRECEDENCE> * * @param <T> If requested, the default-value is returned, and this is its type. * * @return Can return either the user-provided default-value, or null depending on whether a * match was found in the user's request settings ({@code 'FLAGS'}). * * @throws JsonPropMissingException If no flag was set specifying one of the two return-value * options. * * @see JFlag#RETURN_NULL_ON_MISSING * @see JFlag#RETURN_DEFVAL_ON_MISSING * @see JFlag#RETURN_NULL_ON_ANY_ALL * @see JFlag#RETURN_DEFVAL_ON_ANY_ALL */ protected static <T> T JPMEX( JsonObject jo, String propertyName, T defaultValue, int FLAGS, JsonValue.ValueType expectedType, Class<T> returnClass ) { if ((FLAGS & RETURN_NULL_ON_MISSING) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_MISSING) != 0) return defaultValue; if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue; throw new JsonPropMissingException(jo, propertyName, expectedType, returnClass); } /** * Flag Checker for {@link JsonNullArrException} * * <BR /><BR />Checks whether the relevant flags were set in the users {@code FLAGS} parameter, * and either returns the appropriate value accordingly, or throws * {@code JsonNullArrException}. * * <EMBED CLASS='external-html' DATA-FILE-ID=FLAG_PRECEDENCE> * * @param <T> If requested, the default-value is returned, and this is its type. * * @return Can return either the user-provided default-value, or null depending on whether a * match was found in the user's request settings ({@code 'FLAGS'}). * * @throws JsonNullArrException If no flag was set specifying one of the two return-value * options. * * @see JFlag#RETURN_NULL_ON_NULL * @see JFlag#RETURN_DEFVAL_ON_NULL * @see JFlag#RETURN_NULL_ON_ANY_ALL * @see JFlag#RETURN_DEFVAL_ON_ANY_ALL */ protected static <T> T JNAEX( JsonArray ja, int index, T defaultValue, int FLAGS, JsonValue.ValueType expectedType, Class<T> returnClass ) { if ((FLAGS & RETURN_NULL_ON_NULL) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_NULL) != 0) return defaultValue; if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue; throw new JsonNullArrException(ja, index, expectedType, returnClass); } /** * Flag Checker for {@link JsonNullObjException} * * <BR /><BR />Checks whether the relevant flags were set in the users {@code FLAGS} parameter, * and either returns the appropriate value accordingly, or throws * {@code JsonNullObjException}. * * <EMBED CLASS='external-html' DATA-FILE-ID=FLAG_PRECEDENCE> * * @param <T> If requested, the default-value is returned, and this is its type. * * @return Can return either the user-provided default-value, or null depending on whether a * match was found in the user's request settings ({@code 'FLAGS'}). * * @throws JsonNullObjException If no flag was set specifying one of the two return-value * options. * * @see JFlag#RETURN_NULL_ON_NULL * @see JFlag#RETURN_DEFVAL_ON_NULL * @see JFlag#RETURN_NULL_ON_ANY_ALL * @see JFlag#RETURN_DEFVAL_ON_ANY_ALL */ protected static <T> T JNOEX( JsonObject jo, String propertyName, T defaultValue, int FLAGS, JsonValue.ValueType expectedType, Class<T> returnClass ) { if ((FLAGS & RETURN_NULL_ON_NULL) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_NULL) != 0) return defaultValue; if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue; throw new JsonNullObjException(jo, propertyName, expectedType, returnClass); } /** * Flag Checker for {@link JsonTypeArrException} * * <BR /><BR />Checks whether the relevant flags were set in the users {@code FLAGS} parameter, * and either returns the appropriate value accordingly, or throws * {@code JsonTypeArrException}. * * <EMBED CLASS='external-html' DATA-FILE-ID=FLAG_PRECEDENCE> * * @param <T> If requested, the default-value is returned, and this is its type. * * @return Can return either the user-provided default-value, or null depending on whether a * match was found in the user's request settings ({@code 'FLAGS'}). * * @throws JsonTypeArrException If no flag was set specifying one of the two return-value * options. * * @see JFlag#RETURN_NULL_ON_WRONG_JSONTYPE * @see JFlag#RETURN_DEFVAL_ON_WRONG_JSONTYPE * @see JFlag#RETURN_NULL_ON_ANY_ALL * @see JFlag#RETURN_DEFVAL_ON_ANY_ALL */ protected static <T> T JTAEX( JsonArray ja, int index, T defaultValue, int FLAGS, JsonValue.ValueType expectedType, JsonValue retrievedValue, Class<T> returnClass ) { if ((FLAGS & RETURN_NULL_ON_WRONG_JSONTYPE) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_WRONG_JSONTYPE) != 0) return defaultValue; if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue; throw new JsonTypeArrException(ja, index, expectedType, retrievedValue, returnClass); } /** * Flag Checker for {@link JsonTypeObjException} * * <BR /><BR />Checks whether the relevant flags were set in the users {@code FLAGS} parameter, * and either returns the appropriate value accordingly, or throws * {@code JsonNullObjException}. * * <EMBED CLASS='external-html' DATA-FILE-ID=FLAG_PRECEDENCE> * * @param <T> If requested, the default-value is returned, and this is its type. * * @return Can return either the user-provided default-value, or null depending on whether a * match was found in the user's request settings ({@code 'FLAGS'}). * * @throws JsonNullObjException If no flag was set specifying one of the two return-value * options. * * @see JFlag#RETURN_NULL_ON_WRONG_JSONTYPE * @see JFlag#RETURN_DEFVAL_ON_WRONG_JSONTYPE * @see JFlag#RETURN_NULL_ON_ANY_ALL * @see JFlag#RETURN_DEFVAL_ON_ANY_ALL */ protected static <T> T JTOEX( JsonObject jo, String propertyName, T defaultValue, int FLAGS, JsonValue.ValueType expectedType, JsonValue retrievedValue, Class<T> returnClass ) { if ((FLAGS & RETURN_NULL_ON_WRONG_JSONTYPE) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_WRONG_JSONTYPE) != 0) return defaultValue; if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue; throw new JsonTypeObjException (jo, propertyName, expectedType, retrievedValue, returnClass); } /** * Flag Checker for {@link JsonStrParseArrException} * * <BR /><BR />Checks whether the relevant flags were set in the users {@code FLAGS} parameter, * and either returns the appropriate value accordingly, or throws * {@code JsonStrParseArrException}. * * <EMBED CLASS='external-html' DATA-FILE-ID=FLAG_PRECEDENCE> * * @param <T> If requested, the default-value is returned, and this is its type. * * @return Can return either the user-provided default-value, or null depending on whether a * match was found in the user's request settings ({@code 'FLAGS'}). * * @throws JsonStrParseArrException If no flag was set specifying one of the two return-value * options. * * @see JFlag#RETURN_NULL_ON_SPEX * @see JFlag#RETURN_DEFVAL_ON_SPEX * @see JFlag#RETURN_NULL_ON_ANY_ALL * @see JFlag#RETURN_DEFVAL_ON_ANY_ALL */ protected static <T> T JSPAEX( Exception e, JsonArray ja, int index, T defaultValue, int FLAGS, JsonValue retrievedValue, Class<T> returnClass ) { if ((FLAGS & RETURN_NULL_ON_SPEX) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_SPEX) != 0) return defaultValue; if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue; throw new JsonStrParseArrException(e, ja, index, retrievedValue, returnClass); } /** * Flag Checker for {@link JsonStrParseObjException} * * <BR /><BR />Checks whether the relevant flags were set in the users {@code FLAGS} parameter, * and either returns the appropriate value accordingly, or throws * {@code JsonStrParseObjException}. * * <EMBED CLASS='external-html' DATA-FILE-ID=FLAG_PRECEDENCE> * * @param <T> If requested, the default-value is returned, and this is its type. * * @return Can return either the user-provided default-value, or null depending on whether a * match was found in the user's request settings ({@code 'FLAGS'}). * * @throws JsonStrParseObjException If no flag was set specifying one of the two return-value * options. * * @see JFlag#RETURN_NULL_ON_SPEX * @see JFlag#RETURN_DEFVAL_ON_SPEX * @see JFlag#RETURN_NULL_ON_ANY_ALL * @see JFlag#RETURN_DEFVAL_ON_ANY_ALL */ protected static <T> T JSPOEX( Exception e, JsonObject jo, String propertyName, T defaultValue, int FLAGS, JsonValue retrievedValue, Class<T> returnClass ) { if ((FLAGS & RETURN_NULL_ON_SPEX) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_SPEX) != 0) return defaultValue; if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue; throw new JsonStrParseObjException(e, jo, propertyName, retrievedValue, returnClass); } // ******************************************************************************************** // ******************************************************************************************** // GET: USES-FLAG METHODS // ******************************************************************************************** // ******************************************************************************************** /** * This is an internal helper method for retrieving an element from a {@link JsonArray}, * and converting it to a <B STYLE='color: red;'>Java Type</B>. * <EMBED CLASS=defs DATA-TYPE=number DATA-JTYPE=JsonNumber> * @param ja Any instance of {@link JsonArray} * @param index The array index containing the element to retrieve. * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} * @param defaultValue This is the 'Default Value' returned by this method, if there are any * problems converting or extracting the specified number, and the appropriate flags are set. * * @return On success, this method returns the converted number. * * @throws IndexOutOfBoundsException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_IOOBEX> * @throws JsonArithmeticArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> * @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNAEX> * @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTAEX> * * @see ReadJSON#getINTEGER(JsonArray, int, int, int) * @see ReadJSON#getLONG(JsonArray, int, int, long) * @see ReadJSON#getSHORT(JsonArray, int, int, short) * @see ReadJSON#getBYTE(JsonArray, int, int, byte) * @see ReadJSON#getDOUBLE(JsonArray, int, int, double) * @see ReadJSON#getFLOAT(JsonArray, int, int, float) * @see ReadJSON#getNUMBER(JsonArray, int, int, Number) */ protected static <T extends java.lang.Number> T GET( JsonArray ja, int index, int FLAGS, T defaultValue, Class<T> returnClass, Function<JsonNumber, T> jsonTypeToJavaType, Function<JsonNumber, T> typeToType2 ) { // When TRUE, the index provided turned out to be outside of the bounds of the array. The // IndexOutOfBounds "handler" (the method called here) will check the FLAGS, and: // // 1) return the defaultValue (if Requested by 'FLAGS' for IOOBEX) // 2) return null (if Requested by 'FLAGS' for IOOBEX) // 3) throw IndexOutOfBoundsException // // NOTE: It is probably a "little less efficient" to turn this into a method call, // since there are all these parameters that have to be passed, but this is // trading "readability" (less head-aches) in exchange for efficiency. // // This point applies to all of the "Exception Flag Handlers" used here if (index >= ja.size()) return IOOBEX(ja, index, defaultValue, FLAGS); JsonValue jv = ja.get(index); switch (jv.getValueType()) { // When a 'NULL' (Json-Null) JsonValue is present, the JsonNullArrException 'handler' // will do one of the following: // // 1) return the defaultValue (if Requested by 'FLAGS' for JNAEX) // 2) return null (if Requested by 'FLAGS' for JNAEX) // 3) throw JsonNullArrException case NULL: return JNAEX(ja, index, defaultValue, FLAGS, NUMBER, returnClass); case NUMBER: // Temp Variable, Used Twice (Just a Cast) JsonNumber n = (JsonNumber) jv; try { return jsonTypeToJavaType.apply(n); } // Because // // 1) A method for this code would only be invoked here, and... // 2) And because there would be 9 parameters to pass, // 3) the 'inline' version of "Flag Handler" is left here! // // NOTE: All four "JsonArithmetic Arr/Obj Exception" exception throws // are different for each of the 4 methods where they are used. catch (ArithmeticException ae) { if ((FLAGS & RETURN_NULL_ON_AEX) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_AEX) != 0) return defaultValue; if ((FLAGS & RETURN_JAPPROX_ON_AEX) != 0) return typeToType2.apply(n); if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue; throw new JsonArithmeticArrException (ae, ja, index, NUMBER, jv, returnClass); } // The JsonValue at the specified array-index does not contain an JsonNumber. // The "JsonTypeArrException Handler" will do one of these: // // 1) return the defaultValue (if Requested by 'FLAGS' for JTAEX) // 2) return null (if Requested by 'FLAGS' for JTAEX) // 3) throw JsonTypeArrException default: return JTAEX(ja, index, defaultValue, FLAGS, NUMBER, jv, returnClass); } } /** * This is an internal helper method for retrieving a property from a {@link JsonObject}, * and converting it to a <B STYLE='color: red;'>Java Type</B>. * <EMBED CLASS=defs DATA-TYPE=number DATA-JTYPE=JsonNumber> * * @param jo Any instance of {@link JsonObject} * @param propertyName The name of the property in {@code 'jo'} to retrieve. * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} * @param defaultValue This is the 'Default Value' returned by this method, if there are any * problems converting or extracting the specified number, and the appropriate flags are set * * @return On success, this method returns the converted number * * @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JPMEX> * @throws JsonArithmeticObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JAEX> * @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JNOEX> * @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=JRF_JTOEX> * * @see ReadJSON#getINTEGER(JsonObject, String, int, int) * @see ReadJSON#getLONG(JsonObject, String, int, long) * @see ReadJSON#getSHORT(JsonObject, String, int, short) * @see ReadJSON#getBYTE(JsonObject, String, int, byte) * @see ReadJSON#getDOUBLE(JsonObject, String, int, double) * @see ReadJSON#getFLOAT(JsonObject, String, int, float) * @see ReadJSON#getNUMBER(JsonObject, String, int, Number) */ protected static <T extends java.lang.Number> T GET( JsonObject jo, String propertyName, int FLAGS, T defaultValue, Class<T> returnClass, Function<JsonNumber, T> jsonTypeToJavaType, Function<JsonNumber, T> typeToType2 ) { JsonValue jv = jo.get(propertyName); // When TRUE, the user-specified 'property' (named by 'propertyName') isn't actually one // of the listed properties inside the JsonObject. The JsonPropMissingException "handler" // (the method called here) will check the FLAGS, and: // // 1) return the defaultValue (if Requested by 'FLAGS' for JPMEX) // 2) return null (if Requested by 'FLAGS' for JPMEX) // 3) throw JsonPropMissingException // // NOTE: It is probably a "little less efficient" to turn this into a method call, // since there are all these parameters that have to be passed, but this is // trading "readability" (less head-aches) in exchange for efficiency. // // This point applies to all of the "Exception Flag Handlers" used here if (jv == null) return JPMEX(jo, propertyName, defaultValue, FLAGS, NUMBER, returnClass); switch (jv.getValueType()) { // When a 'NULL' (Json-Null) JsonValue is present, the JsonNullObjException 'handler' // will do one of the following: // // 1) return the defaultValue (if Requested by 'FLAGS' for JNOEX) // 2) return null (if Requested by 'FLAGS' for JNOEX) // 3) throw JsonNullArrException case NULL: return JNOEX(jo, propertyName, defaultValue, FLAGS, NUMBER, returnClass); case NUMBER: // Temp Variable, Used Twice (Just a Cast) JsonNumber n = (JsonNumber) jv; try { return jsonTypeToJavaType.apply(n); } // Because // // 1) A method for this code would only be invoked here, and... // 2) And because there would be 9 parameters to pass, // 3) the 'inline' version of "Flag Handler" is left here! // // NOTE: All four "JsonArithmetic Arr/Obj Exception" exception throws // are different for each of the 4 methods where they are used. catch (ArithmeticException ae) { if ((FLAGS & RETURN_NULL_ON_AEX) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_AEX) != 0) return defaultValue; if ((FLAGS & RETURN_JAPPROX_ON_AEX) != 0) return typeToType2.apply(n); if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue; throw new JsonArithmeticObjException (ae, jo, propertyName, NUMBER, jv, returnClass); } // The JsonValue of 'propertyName' does not contain an JsonNumber. // The "JsonTypeObjException Handler" will do one of these: // // 1) return the defaultValue (if Requested by 'FLAGS' for JTOEX) // 2) return null (if Requested by 'FLAGS' for JTOEX) // 3) throw JsonTypeObjException default: return JTOEX(jo, propertyName, defaultValue, FLAGS, NUMBER, jv, returnClass); } } // ******************************************************************************************** // ******************************************************************************************** // HELPER PARSE - JsonString Inputs (also uses flags) // ******************************************************************************************** // ******************************************************************************************** /** * Retrieve a {@link JsonArray} element containing a {@link JsonString}, and transform it to * a <B STYLE='color: red'>Java Type</B>, with either a user-provided parser, or the standard * java parser for that class (passed as a parameter). * * @param <T> The type of the returned value * @param ja Any instance of {@link JsonArray} * @param index array-index containing the {@link JsonString} to retrieve. * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} * @param defaultValue User-provided default-value, only returned if flags are set. * @param parser A valid {@code String -> 'T'} parser. This parameter may be null. * @param defaultParser1 Default {@code String -> 'T'} parser. * @param defaultParser2 {@code String -> 'T'} parser, that will round on Arithmetic Exceptions * * @return On success, this method returns the converted type. * * @throws JsonPropMissingException {@code 'jo'} doesn't have {@code 'propertyName'}, unless * flags are set. * @throws JsonArithmeticArrException after parse, conversion fails, and flags aren't set * @throws JsonStrParseArrException parser-failure unless flags are set * @throws JsonNullArrException property contains null, unless flags are set * @throws JsonTypeArrException property doesn't contain {@code JsonString}, unless flags are * set. * * @see ReadJSON#parseINTEGER(JsonArray, int, int, int, Function) * @see ReadJSON#parseLONG(JsonArray, int, int, long, Function) * @see ReadJSON#parseSHORT(JsonArray, int, int, short, Function) * @see ReadJSON#parseBYTE(JsonArray, int, int, byte, Function) * @see ReadJSON#parseDOUBLE(JsonArray, int, int, double, Function) * @see ReadJSON#parseFLOAT(JsonArray, int, int, float, Function) * @see ReadJSON#parseNUMBER(JsonArray, int, int, Number, Function) */ protected static <T extends Number> T PARSE( JsonArray ja, int index, int FLAGS, T defaultValue, Class<T> returnClass, Function<String, T> parser, Function<BigDecimal, T> defaultParser1, Function<BigDecimal, T> defaultParser2 ) { // When TRUE, the index provided turned out to be outside of the bounds of the array. The // IndexOutOfBounds "handler" (the method called here) will check the FLAGS, and: // // 1) return the defaultValue (if Requested by 'FLAGS' for IOOBEX) // 2) return null (if Requested by 'FLAGS' for IOOBEX) // 3) throw IndexOutOfBoundsException // // NOTE: It is probably a "little less efficient" to turn this into a method call, // since there are all these parameters that have to be passed, but this is // trading "readability" (less head-aches) in exchange for efficiency. // // This point applies to all of the "Exception Flag Handlers" used here if (index >= ja.size()) return IOOBEX(ja, index, defaultValue, FLAGS); JsonValue jv = ja.get(index); switch (jv.getValueType()) { // When a 'NULL' (Json-Null) JsonValue is present, the JsonNullArrException 'handler' // will do one of the following: // // 1) return the defaultValue (if Requested by 'FLAGS' for JNAEX) // 2) return null (if Requested by 'FLAGS' for JNAEX) // 3) throw JsonNullArrException case NULL: return JNAEX(ja, index, defaultValue, FLAGS, STRING, returnClass); case STRING: String s = ((JsonString) jv).getString(); // NOTE: This isn't actually an "Exception Case", and if the user hasn't made // a request, the empty-string is passed to whatever parser is configured if (s.length() == 0) { if ((FLAGS & RETURN_NULL_ON_0LEN_STR) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_0LEN_STR) != 0) return defaultValue; if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue; } // Temp Variable, used in order not to invoke the BigDecimal contructor twice BigDecimal bd = null; try { return (parser != null) ? parser.apply(s) : defaultParser1.apply(bd = new BigDecimal(s.trim())); // NOTE: 'bd' will not be null if "ArithmeticException" is thrown... // new BigDecimal throws "NumberFormatException" is thrown // parser.applly can throw ArithmeticException } // Because // // 1) A method for this code would only be invoked here, and... // 2) And because there would be 9 parameters to pass, // 3) the 'inline' version of "Flag Handler" is left here! // // NOTE: All four "JsonArithmetic Arr/Obj Exception" exception throws // are different for each of the 4 methods where they are used. catch (ArithmeticException ae) { if ((FLAGS & RETURN_NULL_ON_AEX) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_AEX) != 0) return defaultValue; if ((FLAGS & RETURN_JAPPROX_ON_AEX) != 0) return defaultParser2.apply(bd); if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue; throw new JsonArithmeticArrException(ae, ja, index, STRING, jv, returnClass); } // HANDLER STRIKES AGAIN! - but this time for "JsonStrParseArrException" // RETURNS: null, or defaultValue, (otherwise throws JsonStrParseArrException) catch (Exception e) { return JSPAEX(e, ja, index, defaultValue, FLAGS, jv, returnClass); } // The JsonValue at the specified array-index does not contain an JsonString. // The "JsonTypeArrException Handler" will do one of these: // // 1) return the defaultValue (if Requested by 'FLAGS' for JTAEX) // 2) return null (if Requested by 'FLAGS' for JTAEX) // 3) throw JsonTypeArrException default: return JTAEX(ja, index, defaultValue, FLAGS, STRING, jv, returnClass); } } /** * Retrieve a {@link JsonObject} property containing a {@link JsonString}, and transform it to * a <B STYLE='color: red'>Java Type</B>, with either a user-provided parser, or the standard * java parser for that class (passed as a parameter). * * @param <T> The type of the returned value. * @param jo Any instance of {@link JsonObject} * @param propertyName propertyName containing the {@link JsonString} to retrieve. * @param FLAGS The return-value / exception-throw flag constants defined in {@link JFlag} * @param defaultValue User-provided default-value, only returned if flags are set. * @param parser A valid {@code String -> 'T'} parser. This parameter may be null. * @param defaultParser1 Default {@code String -> 'T'} parser. * @param defaultParser2 {@code String -> 'T'} parser, that will round on Arithmetic Exceptions * * @return On success, this method returns the converted type instance. * * @throws JsonPropMissingException {@code 'jo'} doesn't have {@code 'propertyName'}, unless * flags are set. * @throws JsonArithmeticObjException after parse, conversion fails, and flags aren't set * @throws JsonStrParseObjException parser-failure unless flags are set * @throws JsonNullObjException property contains null, unless flags are set * @throws JsonTypeObjException property doesn't contain {@code JsonString}, unless flags are * set. * * @see ReadJSON#parseINTEGER(JsonObject, String, int, int, Function) * @see ReadJSON#parseLONG(JsonObject, String, int, long, Function) * @see ReadJSON#parseSHORT(JsonObject, String, int, short, Function) * @see ReadJSON#parseBYTE(JsonObject, String, int, byte, Function) * @see ReadJSON#parseDOUBLE(JsonObject, String, int, double, Function) * @see ReadJSON#parseFLOAT(JsonObject, String, int, float, Function) * @see ReadJSON#parseNUMBER(JsonObject, String, int, Number, Function) */ protected static <T extends Number> T PARSE( JsonObject jo, String propertyName, int FLAGS, T defaultValue, Class<T> returnClass, Function<String, T> parser, Function<BigDecimal, T> defaultParser1, Function<BigDecimal, T> defaultParser2 ) { JsonValue jv = jo.get(propertyName); // When TRUE, the user-specified 'property' (named by 'propertyName') isn't actually one // of the listed properties inside the JsonObject. The JsonPropMissingException "handler" // (the method called here) will check the FLAGS, and: // // 1) return the defaultValue (if Requested by 'FLAGS' for JPMEX) // 2) return null (if Requested by 'FLAGS' for JPMEX) // 3) throw JsonPropMissingException // // NOTE: It is probably a "little less efficient" to turn this into a method call, // since there are all these parameters that have to be passed, but this is // trading "readability" (less head-aches) in exchange for efficiency. // // This point applies to all of the "Exception Flag Handlers" used here if (jv == null) return JPMEX(jo, propertyName, defaultValue, FLAGS, STRING, returnClass); switch (jv.getValueType()) { // When a 'NULL' (Json-Null) JsonValue is present, the JsonNullObjException 'handler' // will do one of the following: // // 1) return the defaultValue (if Requested by 'FLAGS' for JNOEX) // 2) return null (if Requested by 'FLAGS' for JNOEX) // 3) throw JsonNullArrException case NULL: return JNOEX(jo, propertyName, defaultValue, FLAGS, STRING, returnClass); case STRING: String s = ((JsonString) jv).getString(); // NOTE: This isn't actually an "Exception Case", and if the user hasn't made // a request, the empty-string is passed to whatever parser is configured if (s.length() == 0) { if ((FLAGS & RETURN_NULL_ON_0LEN_STR) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_0LEN_STR) != 0) return defaultValue; if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue; } // Temp Variable, used in order not to invoke the BigDecimal contructor twice BigDecimal bd = null; try { return (parser != null) ? parser.apply(s) : defaultParser1.apply(bd = new BigDecimal(s.trim())); // NOTE: 'bd' will not be null if "ArithmeticException" is thrown... // new BigDecimal throws "NumberFormatException" is thrown // parser.applly can throw ArithmeticException } // Because // // 1) A method for this code would only be invoked here, and... // 2) And because there would be 9 parameters to pass, // 3) the 'inline' version of "Flag Handler" is left here! // // NOTE: All four "JsonArithmetic Arr/Obj Exception" exception throws // are different for each of the 4 methods where they are used. catch (ArithmeticException ae) { if ((FLAGS & RETURN_NULL_ON_AEX) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_AEX) != 0) return defaultValue; if ((FLAGS & RETURN_JAPPROX_ON_AEX) != 0) return defaultParser2.apply(bd); if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue; throw new JsonArithmeticObjException (ae, jo, propertyName, STRING, jv, returnClass); } // HANDLER STRIKES AGAIN! - but this time for "JsonStrParseObjException" // RETURNS: null, or defaultValue, (otherwise throws JsonStrParseObjException) catch (Exception e) { return JSPOEX(e, jo, propertyName, defaultValue, FLAGS, jv, returnClass); } // The JsonValue of 'propertyName' does not contain an JsonString. // The "JsonTypeObjException Handler" will do one of these: // // 1) return the defaultValue (if Requested by 'FLAGS' for JTOEX) // 2) return null (if Requested by 'FLAGS' for JTOEX) // 3) throw JsonTypeObjException default: return JTOEX(jo, propertyName, defaultValue, FLAGS, STRING, jv, returnClass); } } } |