litesoft
@ 809
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 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.core.util; import java.sql.*; import java.util.*; import java.util.Date; import org.litesoft.charstreams.*; import org.litesoft.core.simpletypes.nonpublic.*; import org.litesoft.core.simpletypes.temporal.*; import org.litesoft.core.simpletypes.temporal.nonpublic.*; import org.litesoft.core.typeutils.*; import org.litesoft.core.util.stringmatching.*; public class UtilsCommon extends EqualSupport { public static final String MUST_BE_POSITIVE = " must be positive"; public static final String CANNOT_BE_NEGATIVE = " cannot be negative"; public static final String NOT_ALLOWED_TO_BE_NULL = ": Not allowed to be null"; public static final String ASCII_127 = "DEL"; public static final String[] ASCII_LOW_32 = new String[]{"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL", "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI", "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US"}; protected UtilsCommon() { } public static Date now() { return new Date(); } public static Timestamp nowTS() { return new Timestamp( now().getTime() ); } public static <T extends Enum<T>> T enumFromString( Class<T> pClass, String pEnumAsString ) { if ( pClass != null ) { if ( null != (pEnumAsString = Strings.noEmpty( pEnumAsString )) ) { try { return Enum.valueOf( pClass, pEnumAsString ); } catch ( IllegalArgumentException ex ) { // Bad String } } } return null; } /** * Calculate the new "AccumulatedBitFlags" by Overriding the appropriate bit flags in * pPreviousAccumulatedBitFlags (based on the bits in pValidBitsOfNewBitFlags) from the pNewBitFlags. * * @param pPreviousAccumulatedBitFlags Bit Flags to be selectively overriden. * @param pValidBitsOfNewBitFlags The '1' bits will indicate which bids to override. * @param pNewBitFlags The source of the override Bit Flags. * * @return The new "AccumulatedBitFlags". */ public static int mutateAccumulatedBitFlags( int pPreviousAccumulatedBitFlags, int pValidBitsOfNewBitFlags, int pNewBitFlags ) { return (pPreviousAccumulatedBitFlags & (~pValidBitsOfNewBitFlags)) // | (pValidBitsOfNewBitFlags & pNewBitFlags); } public static Boolean toBoolean( String pInput ) { if ( "YES".equalsIgnoreCase( pInput ) || "Y".equalsIgnoreCase( pInput ) || "TRUE".equalsIgnoreCase( pInput ) || "T".equalsIgnoreCase( pInput ) ) { return Boolean.TRUE; } if ( "NO".equalsIgnoreCase( pInput ) || "N".equalsIgnoreCase( pInput ) || "FALSE".equalsIgnoreCase( pInput ) || "F".equalsIgnoreCase( pInput ) ) { return Boolean.FALSE; } return null; } /** * Returns a String that represents <i>pThis</i> with english code tail, * such as "3rd".<p> * <p/> * If <i>pThis</i> is non-negative, then append on the appropriate two * letter <i>english</i> code:<p> * <pre> * st,nd,rd,th * </pre><p> * * @param pThis the int to convert.<p> * * @return the String representation for <i>pThis</i> and if not negative, * then appended with the appropriate two letter <i>english</i> * junk.<p> */ public static String toNth( int pThis ) { String rv = Integer.toString( pThis ); if ( pThis < 0 ) { return rv; } if ( (pThis < 10) || ('1' != rv.charAt( rv.length() - 2 )) ) // not: 10-19 & 110-119, 210-219, ... 1010-1019, ... { switch ( rv.charAt( rv.length() - 1 ) ) { case '1': return rv + "st"; case '2': return rv + "nd"; case '3': return rv + "rd"; default: // 0th and n0th and 4th - 9th and n4th - n9th break; } } return rv + "th"; } public static String cvtTextForDisplay( String pText ) { if ( pText == null ) { return "[null]"; } int length = pText.length(); StringBuilder sb = new StringBuilder( length ); for ( int i = 0; i < length; i++ ) { char c = pText.charAt( i ); switch ( c ) { case '[': case ']': case '\\': case '"': sb.append( '\\' ); sb.append( c ); break; case '\n': sb.append( "\\n" ); break; case '\r': sb.append( "\\r" ); break; case '\t': sb.append( "\\t" ); break; case '\f': sb.append( "\\f" ); break; default: if ( (' ' <= c) && (c < 127) ) { sb.append( c ); break; } sb.append( '[' ); sb.append( cvtCharForDisplay( c ) ); sb.append( ']' ); break; } } return sb.toString(); } private static String cvtCharForDisplay( char pChar ) { if ( pChar < 0 ) // is this even possible? { return "-" + cvtCharForDisplay( (char) -pChar ); } if ( pChar < ' ' ) { return ASCII_LOW_32[pChar]; } if ( pChar < 127 ) { return String.valueOf( pChar ); } if ( pChar == 127 ) { return ASCII_127; } if ( pChar < 256 ) { return "HiBit-" + cvtCharForDisplay( (char) (pChar - 128) ); } return "x" + Integer.toString( pChar, 16 ); // toHex } public static Object[] convert( int pEntries, Enumeration<Object> pEnumeration ) { Object[] them = new Object[pEntries]; for ( int i = 0; (i < pEntries) && pEnumeration.hasMoreElements(); i++ ) { them[i] = pEnumeration.nextElement(); } return them; } public static boolean checkIndex( int pIndex, int pMax, boolean pInclusive ) { return ((0 <= pIndex) && (pIndex < pMax)) || (pInclusive && (pIndex == pMax)); } public static void validateIndex( int pIndex, int pMax, boolean pInclusive ) throws IllegalArgumentException { if ( !checkIndex( pIndex, pMax, pInclusive ) ) { throw new IllegalArgumentException( "!( 0 <= " + pIndex + " " + (pInclusive ? "<=" : "<") + " " + pMax + " )" ); } } /** * return pIndex constained between 0 (inclusive) and pMax (pInclusive). If the effective 'max' is less than 0, then 0 will be returned. */ public static int constrainIndex( int pIndex, int pMax, boolean pInclusive ) { if ( (pMax <= pIndex) || (pInclusive && (pIndex == pMax)) ) { pIndex = pInclusive ? pMax : pMax - 1; } return (pIndex < 0) ? 0 : pIndex; } public static char toHexDigit( int pZeroTo15 ) { pZeroTo15 &= 15; return (pZeroTo15 < 0) || (15 < pZeroTo15) ? '?' : HEX.charAt( pZeroTo15 ); } /** * Attempt to interpret (char) pHex as a Hex Character * * @return -1 if not a HEX character, otherwise the 0-15 */ public static int hexFor( char pHex ) { return HEX.indexOf( Character.toUpperCase( pHex ) ); } public static int fromHexDigit( char pHex ) throws IllegalArgumentException { int rv = hexFor( pHex ); if ( rv == -1 ) { throw new IllegalArgumentException( "Not a Hex digit '" + pHex + "': " + (int) pHex ); } return rv; } private static final String HEX = "0123456789ABCDEF"; public static char[] charToHex( char c ) { char[] rv = new char[4]; int cv = c; for ( int i = 3; i >= 0; i-- ) { rv[i] = toHexDigit( cv ); cv /= 16; } return rv; } public static char charFromHex( char[] pHex ) throws IllegalArgumentException { int rv = 0; for ( char c : pHex ) { rv = (rv * 16) + fromHexDigit( c ); } return (char) rv; } public static String toHex( byte[] pBytes ) { if ( pBytes == null ) { return null; } StringBuilder sb = new StringBuilder( pBytes.length + pBytes.length ); for ( byte zByte : pBytes ) { sb.append( toHexDigit( zByte >>> 4 ) ); sb.append( toHexDigit( zByte ) ); } return sb.toString(); } public static byte[] fromHex( String pString ) { if ( pString == null ) { return null; } if ( (pString.length() & 1) == 1 ) { throw new IllegalArgumentException( "Can't be HEX as it is 'Odd' Length: '" + pString + "'" ); } int zCharAt = 0; byte[] zBytes = new byte[pString.length() / 2]; for ( int i = 0; i < zBytes.length; i++ ) { int z1st = fromHexDigit( pString.charAt( zCharAt++ ) ); int z2nd = fromHexDigit( pString.charAt( zCharAt++ ) ); zBytes[i] = (byte) ((z1st << 4) + z2nd); } return zBytes; } public static String getMax2PlacePercentage( int pPortions ) { if ( pPortions == 0 ) { return null; } int portion = 10000 / pPortions; String s = "" + portion; while ( s.length() < 3 ) { s = "0" + s; } int wholeLen = s.length() - 2; s = s.substring( 0, wholeLen ) + "." + s.substring( wholeLen ); while ( s.charAt( s.length() - 1 ) == '0' ) { s = s.substring( 0, s.length() - 1 ); } if ( s.charAt( s.length() - 1 ) == '.' ) { s = s.substring( 0, s.length() - 1 ); } return s + "%"; } public static String appendNonEmpties( String pExisting, String pSeparator, String pToAppend ) { if ( Strings.isNullOrEmpty( pToAppend ) ) { return Strings.deNull( pExisting ).trim(); } if ( Strings.isNullOrEmpty( pExisting ) ) { return pToAppend.trim(); } return pExisting.trim() + Strings.deNull( pSeparator ) + pToAppend.trim(); } public static boolean areNonArraysEqual( boolean pThis, boolean pThat ) { return (pThis == pThat); } public static boolean areNonArraysEqual( int pThis, int pThat ) { return (pThis == pThat); } public static boolean isEmptyString( Object pObject ) { return pObject == null || ((pObject instanceof String) && Strings.isBlank( (String) pObject )); } public static boolean areBothEmptyStrings( Object pThis, Object pThat ) { return isEmptyString( pThis ) && isEmptyString( pThat ); } public static boolean isBooleanNotTrue( Object pObject ) { return pObject == null || Boolean.FALSE.equals( pObject ); } public static boolean areBothBooleanNotTrue( Object pThis, Object pThat ) { return isBooleanNotTrue( pThis ) && isBooleanNotTrue( pThat ); } public static boolean isNullEquivalent( Object pObject ) { return isEmptyString( pObject ) || isBooleanNotTrue( pObject ); } public static boolean areBothNullEquivalent( Object pThis, Object pThat ) { return isNullEquivalent( pThis ) && isNullEquivalent( pThat ); } public static boolean areEqual( Object pThis, Object pThat ) { if ( Objects.areNonArraysEqual( pThis, pThat ) ) { return true; } // Both CAN'T be null if ( (pThis instanceof Object[]) && (pThat instanceof Object[]) ) { return Objects.areArraysEqual( (Object[]) pThis, (Object[]) pThat ); } return (pThis instanceof int[]) && (pThat instanceof int[]) && Integers.areArraysEqual( (int[]) pThis, (int[]) pThat ); } public static int insureNonNegative( int pValue ) { return (pValue < 0) ? 0 : pValue; } public static String deNullToString( Object value, Object defaultValue ) { return Strings.deNull( noEmptyToString( value ), noEmptyToString( defaultValue ) ); } public static String noEmptyToString( Object value ) { return Strings.noEmpty( Strings.nullOKtoString( value ) ); } public static int intValue( Integer pInteger, int pDefault ) { return (pInteger != null) ? pInteger : pDefault; } public static String trimToLength( String pSource, int pMaxAcceptableLength ) throws IllegalArgumentException { pMaxAcceptableLength = assertNotNegative( "MaxAcceptableLength", pMaxAcceptableLength ); return ((pSource = Strings.deNull( pSource )).length() <= pMaxAcceptableLength) ? pSource : pSource.substring( 0, pMaxAcceptableLength ); } public static String trimToLength( String pSource, int pMaxAcceptableLength, String pTrimmedSuffix ) throws IllegalArgumentException { pMaxAcceptableLength = assertNotNegative( "MaxAcceptableLength", pMaxAcceptableLength ); if ( (pSource = Strings.deNull( pSource )).length() <= pMaxAcceptableLength ) { return pSource; } int zSuffixLength = (pTrimmedSuffix = Strings.deNull( pTrimmedSuffix )).length(); if ( zSuffixLength == 0 ) { return pSource.substring( 0, pMaxAcceptableLength ); } if ( pMaxAcceptableLength < zSuffixLength ) { pTrimmedSuffix = pTrimmedSuffix.substring( 0, zSuffixLength = pMaxAcceptableLength ); } pMaxAcceptableLength -= zSuffixLength; return pSource.substring( 0, pMaxAcceptableLength ) + pTrimmedSuffix; } public static String combineAsLines( String... pStrings ) { StringBuilder sb = new StringBuilder(); if ( pStrings != null ) { for ( String s : pStrings ) { sb.append( s ); sb.append( '\n' ); } } return sb.toString(); } public static String[] splitLines( String pString ) { pString = Strings.normalizeNewLines( pString ); return Strings.parseChar( pString, '\n' ); } public static String combine( String pSeparator, Object... pObjects ) { if ( Objects.isNullOrEmpty( pObjects ) ) { return null; } StringBuilder sb = new StringBuilder(); sb.append( pObjects[0] ); for ( int i = 1; i < pObjects.length; i++ ) { sb.append( pSeparator ); sb.append( pObjects[i] ); } return sb.toString(); } public static String combine( char pSeparator, String... pStrings ) { if ( Objects.isNullOrEmpty( pStrings ) ) { return null; } StringBuilder sb = new StringBuilder(); sb.append( pStrings[0] ); for ( int i = 1; i < pStrings.length; i++ ) { sb.append( pSeparator ); sb.append( pStrings[i] ); } return sb.toString(); } public static void assertAll7BitAsciiAlpha( String pObjectName, String pToBeAssert ) throws IllegalArgumentException { Objects.assertNotNull( pObjectName, pToBeAssert ); for ( int i = 0; i < pToBeAssert.length(); i++ ) { char c = pToBeAssert.charAt( i ); if ( !(is7BitAsciiAlpha( c )) ) { throw new IllegalArgumentException( pObjectName + ": '" + c + "' Not a 7 Bit Ascii Alpha at: " + i ); } } } public static void assertNotEqual( String pObjectName, long pNotExpected, long pActual ) throws IllegalArgumentException { if ( pNotExpected == pActual ) { throw new IllegalArgumentException( pObjectName + ": '" + pNotExpected + "' Not allowed" ); } } public static void assertTrue( String pValueDescription, boolean pActual ) throws IllegalArgumentException { if ( !pActual ) { throw new IllegalArgumentException( pValueDescription + ": Expected to be true" ); } } public static void assertNotEmptyIfNotNull( String pParamName, String pToBeAsserted ) throws IllegalArgumentException { if ( (pToBeAsserted != null) && (pToBeAsserted.length() == 0) ) { throw new IllegalArgumentException( pParamName + ": Not allowed to be empty ('')" ); } } public static void assertNotNullNotEmptyNoSpaces( String pErrorMessage, String pStringToAssert ) throws IllegalArgumentException { if ( Strings.isNullOrEmptyOrSpaces( pStringToAssert ) ) { Strings.errorNullOrEmptyOrSpace( pErrorMessage, "String" ); } } public static String assertNotNullNotEmpty( String pErrorMessage, String pStringToAssert ) throws IllegalArgumentException { String rv = Strings.noEmpty( pStringToAssert ); if ( rv == null ) { Strings.errorNullOrEmpty( pErrorMessage, "String" ); } return rv; } public static String assertNoLeadingOrTrailingWhiteSpace( String pErrorMessage, String pStringToAssert ) throws IllegalArgumentException { if ( pStringToAssert != null ) { String rv = pStringToAssert.trim(); if ( pStringToAssert.length() != rv.length() ) { Strings.error( "String", pErrorMessage, "No Leading or trailing whitespace allowed" ); } } return pStringToAssert; } public static String[] assertNotNullNotEmptyAndNoNullsOrEmptiesAndTrim( String pErrorMessage, String[] pStringArrayToAssert ) throws IllegalArgumentException { assertNotNullNotEmpty( pErrorMessage, pStringArrayToAssert ); return assertNoNullsOrEmptiesAndTrim( pErrorMessage, pStringArrayToAssert ); } public static void assertNotNullNotEmpty( String pErrorMessage, String[] pStringArrayToAssert ) throws IllegalArgumentException { if ( Objects.isNullOrEmpty( pStringArrayToAssert ) ) { Strings.errorNullOrEmpty( pErrorMessage, "String[]" ); } } public static String[] assertNoNullsOrEmptiesAndTrim( String pErrorMessage, String[] pStringArrayToAssert ) throws IllegalArgumentException { String[] rv = new String[pStringArrayToAssert.length]; for ( int i = 0; i < pStringArrayToAssert.length; i++ ) { String s = Strings.noEmpty( pStringArrayToAssert[i] ); if ( s == null ) { Strings.errorNullOrEmpty( pErrorMessage + "[" + i + "]", "String[]" ); } rv[i] = s; } return rv; } /** * Assert that "value" is not null or when converted to a String is not * empty and then return resulting "value" trimmed. * * @param what "field" was the problem on * @param value to check * * @return value converted to a String minus any leading and trailing spaces */ public static String assertNoEmptyToString( String what, Object value ) { String strValue = noEmptyToString( value ); if ( strValue == null ) { Strings.errorNullOrEmpty( what, String.valueOf( justClassNameOf( value ) ) ); } return strValue; } /** * Assert that "value" is not null or empty (if it is a String) and return * "value" trimmed (if it is a String). * * @param what "field" was the problem on * @param value to check * * @return value minus any leading and trailing spaces */ public static <T> T assertNotNullOrEmpty( String what, T value ) { if ( value == null ) { Strings.errorNullOrEmpty( what, String.valueOf( justClassNameOf( value ) ) ); } if ( value instanceof String ) { value = cast( assertNotNullNotEmpty( what, value.toString() ) ); } return value; } public static Long assertNotNegative( String pName, Long pValue ) throws IllegalArgumentException { if ( pValue != null ) { assertNotNegative( pName, pValue.longValue() ); } return pValue; } public static long assertNotNegative( String pName, long pValue ) throws IllegalArgumentException { if ( pValue < 0 ) { throw new IllegalArgumentException( pName + CANNOT_BE_NEGATIVE ); } return pValue; } public static Integer assertNotNegative( String pName, Integer pValue ) throws IllegalArgumentException { if ( pValue != null ) { assertNotNegative( pName, pValue.intValue() ); } return pValue; } public static int assertNotNegative( String pName, int pValue ) throws IllegalArgumentException { if ( pValue < 0 ) { throw new IllegalArgumentException( pName + CANNOT_BE_NEGATIVE ); } return pValue; } public static int assertPositive( String pName, int pValue ) throws IllegalArgumentException { if ( pValue <= 0 ) { throw new IllegalArgumentException( pName + MUST_BE_POSITIVE ); } return pValue; } public static String displayFormatFlag( boolean pFlag, String pTrueString ) { return pFlag ? pTrueString : ""; } public static boolean endsWithPathSep( String pPath ) { return (pPath != null) && isPathSep( pPath.charAt( pPath.length() - 1 ) ); } public static int lastPathSepAt( String pPath ) { if ( pPath != null ) { for ( int at = pPath.length(); --at >= 0; ) { if ( isPathSep( pPath.charAt( at ) ) ) { return at; } } } return -1; } public static boolean isPathSep( char pChar ) { return (pChar == '/') || (pChar == '\\') || (pChar == ':'); } public static String forwardSlashPath( String pPath ) { return pPath.replace( '\\', '/' ); } public static String multiLineHelper( String pLine, String pAppend ) { return Strings.isNullOrEmpty( pLine ) ? "" : (pLine + pAppend); } // Groups // Object Group: public static String padIt( int pMinDesiredLength, Object pIt ) { return Strings.padIt( pMinDesiredLength, "" + pIt ); } public static String iTpad( Object pIt, int pMinDesiredLength ) { return Strings.iTpad( "" + pIt, pMinDesiredLength ); } public static boolean isAnyTrue( Boolean... pToSearch ) { if ( pToSearch != null ) { for ( Boolean zBoolean : pToSearch ) { if ( Boolean.TRUE.equals( zBoolean ) ) { return true; } } } return false; } public static boolean isOneOf( String pToFind, String[] pToSearch ) { return isObjectOneOf( pToFind, pToSearch ); } public static boolean isOneOf( Object pToFind, Object[] pToSearch ) { return isObjectOneOf( pToFind, pToSearch ); } private static boolean isObjectOneOf( Object pToFind, Object[] pToSearch ) { if ( pToSearch != null ) { for ( int i = pToSearch.length; --i >= 0; ) { if ( areEqual( pToFind, pToSearch[i] ) ) { return true; } } } return false; } /** * Is the string 'pInQuestion' made up of the 'pParts' where if only 1, then must be equal, otherwise * the pInQuestion.startsWith(first) && pInQuestion.endsWith(last) && the middle must be found in * order with no overlap between. * * @param pInQuestion - null will always return false * @param pParts - Null treated as empty & Null Elements treated as "" */ public static boolean isMadeUpFromParts( String pInQuestion, String[] pParts ) { StringMatcher zSM = StringMatcherFactory.createEquals( pParts ); return (zSM != null) && zSM.matches( pInQuestion ); } public static int indexOfOrLength( String pToSearch, char pToFind ) { return helperIndexOfOrLength( pToSearch, pToSearch.indexOf( pToFind ) ); } public static int indexOfOrLength( String pToSearch, String pToFind ) { return helperIndexOfOrLength( pToSearch, pToSearch.indexOf( pToFind ) ); } private static int helperIndexOfOrLength( String pToSearch, int pAt ) { return (pAt != -1) ? pAt : pToSearch.length(); } // String Group public static String[] removeStringFromArray( String[] pArray, int pIndexToRemove ) { if ( Objects.isNullOrEmpty( pArray ) || (pIndexToRemove < 0) || (pArray.length <= pIndexToRemove) ) { return pArray; } int zNewLength = pArray.length - 1; String[] zNewArray = new String[zNewLength]; if ( pIndexToRemove != 0 ) { System.arraycopy( pArray, 0, zNewArray, 0, pIndexToRemove ); } if ( pIndexToRemove != zNewLength ) { System.arraycopy( pArray, pIndexToRemove + 1, zNewArray, pIndexToRemove, zNewLength - pIndexToRemove ); } return zNewArray; } public static String[] appendStringArrays( String[] pArray1, String[] pArray2 ) { if ( Objects.isNullOrEmpty( pArray2 ) ) { return pArray1; } if ( Objects.isNullOrEmpty( pArray1 ) ) { return pArray2; } String[] joined = new String[pArray1.length + pArray2.length]; System.arraycopy( pArray1, 0, joined, 0, pArray1.length ); System.arraycopy( pArray2, 0, joined, pArray1.length, pArray2.length ); return joined; } public static String[] prependString( String pNewFirst, String[] pTheRest ) { return appendStringArrays( new String[]{pNewFirst}, pTheRest ); } public static String[] appendString( String[] pCurArray, String pNewLast ) { return appendStringArrays( pCurArray, new String[]{pNewLast} ); } public static String toStringNullToEmpty( Object pObject ) { if ( pObject != null ) { String rv = pObject.toString(); if ( rv != null ) { return rv; } } return ""; } public static String toStringOrNull( Object pObject ) { return Strings.noEmpty( (pObject != null) ? pObject.toString() : null ); } public static String toString( Object pObject ) { if ( pObject != null ) { String rv = pObject.toString(); if ( rv != null ) { return rv; } } return null; } public static String optionsToString( Object[] pObjects ) { if ( pObjects == null ) { return "No Options Available"; } StringBuilder sb = new StringBuilder( "Valid Options are" ); String prefix = ": "; //noinspection ForLoopReplaceableByForEach for ( int i = 0; i < pObjects.length; i++ ) { sb.append( prefix ).append( pObjects[i] ); prefix = ", "; } return sb.toString(); } public static String toString( Object[] pObjects ) { if ( pObjects == null ) { return "null"; } return new StringBuilder().append( '<' ).append( pObjects.length ).append( ':' ).append( toString( pObjects, "," ) ).append( '>' ).toString(); } public static String toString( Object[] pObjects, String separator ) { if ( pObjects == null ) { return "null"; } StringBuilder sb = new StringBuilder(); if ( pObjects.length > 0 ) { sb.append( pObjects[0] ); //noinspection ForLoopReplaceableByForEach for ( int i = 1; i < pObjects.length; i++ ) { sb.append( separator ).append( pObjects[i] ); } } return sb.toString(); } public static Map<String, String> createHashMap( String... pKeyValuePairs ) throws IllegalArgumentException { return populate( new HashMap<String, String>(), pKeyValuePairs ); } public static Map<String, String> populate( Map<String, String> pToPopulate, String... pNameValuePairs ) throws IllegalArgumentException { if ( pNameValuePairs != null ) { if ( (pNameValuePairs.length & 1) == 1 ) { throw new IllegalArgumentException( "NameValuePairs not paired" ); } for ( int i = 0; i < pNameValuePairs.length; ) { String name = pNameValuePairs[i++]; String value = pNameValuePairs[i++]; // noinspection unchecked pToPopulate.put( name, value ); } } return pToPopulate; } public static void populate( Map pToPopulate, Map pNewContents ) { if ( pNewContents != null ) { //noinspection unchecked pToPopulate.putAll( pNewContents ); } } public static String[] getSortedKeysAsStrings( Map pMap ) { String[] names = toStringArray( pMap.keySet().toArray() ); Arrays.sort( names ); return names; } public static String[] arrayOfOne( String pString ) { return new String[]{pString}; } public static String[] toStringArray( Collection pCollection ) { return (pCollection == null) ? null : toStringArray( pCollection.toArray() ); } public static String[] toStringArray( Object[] pObjects ) { if ( pObjects == null ) { return null; } String[] zStrings = new String[pObjects.length]; for ( int i = 0; i < pObjects.length; i++ ) { Object o = pObjects[i]; zStrings[i] = (o == null) ? null : o.toString(); } return zStrings; } public static List<String> toStringList( Collection pCollection ) { return (pCollection == null) ? null : toStringList( pCollection.toArray() ); } public static List<String> toStringList( Object[] pObjects ) { if ( pObjects == null ) { return null; } List<String> strings = new ArrayList<String>( pObjects.length ); for ( Object o : pObjects ) { strings.add( (o == null) ? null : o.toString() ); } return strings; } public static Map<String, String> addPropertiesTo( Map<String, String> pMap, String... pProperty_NameValues ) throws IllegalArgumentException { if ( Objects.isNotNullOrEmpty( pProperty_NameValues ) ) { if ( (pProperty_NameValues.length & 1) != 0 ) { throw new IllegalArgumentException( "Attempt to add Properties that were NOT Name/Value pairs" ); } for ( int i = 0; i < pProperty_NameValues.length; i += 2 ) { pMap.put( pProperty_NameValues[i], pProperty_NameValues[i + 1] ); } } return pMap; } public static boolean isAll7BitAsciiAlpha( String pString, int pMinLength ) { if ( (pString == null) || (pString.length() < pMinLength) ) { return false; } for ( int i = 0; i < pString.length(); i++ ) { if ( !is7BitAsciiAlpha( pString.charAt( i ) ) ) { return false; } } return true; } public static boolean isAlphaNumeric( String pString, int pMinLength ) { if ( (pString == null) || (pString.length() < pMinLength) ) { return false; } for ( int i = 0; i < pString.length(); i++ ) { if ( !isAlphaNumeric( pString.charAt( i ) ) ) { return false; } } return true; } public static boolean isNumeric( String pString, int pMinLength ) { if ( (pString == null) || (pString.length() < pMinLength) ) { return false; } for ( int i = 0; i < pString.length(); i++ ) { if ( !isNumeric( pString.charAt( i ) ) ) { return false; } } return true; } public static boolean isValidAttributeIdentifier( String pIdentifier ) { return isValidAttributeIdentifier( pIdentifier, 2 ); } public static boolean isValidAttributeIdentifier( String pIdentifier, int pMinLength ) { if ( (pIdentifier == null) || (pIdentifier.length() < pMinLength) ) { return false; } if ( !isValidAttributeIdentifierStartCharacter( pIdentifier.charAt( 0 ) ) ) { return false; } for ( int i = 1; i < pIdentifier.length(); i++ ) { if ( !isValidAttributeIdentifierRestCharacter( pIdentifier.charAt( i ) ) ) { return false; } } return true; } private static boolean isValidAttributeIdentifierStartCharacter( char pChar ) { // return Character.isJavaIdentifierStart( pChar ); return (('A' <= pChar) && (pChar <= 'Z')); } private static boolean isValidAttributeIdentifierRestCharacter( char pChar ) { // return Character.isJavaIdentifierPart( pChar ); return isAlphaNumeric( pChar ); } public static boolean isAlphaNumeric( char pChar ) { return isAlpha( pChar ) || isNumeric( pChar ); } public static boolean isAlpha( char pChar ) { return is7BitAsciiAlpha( pChar ); } public static boolean isNumeric( char pChar ) { return ('0' <= pChar) && (pChar <= '9'); } public static boolean is7BitAsciiAlpha( char pChar ) { return (('A' <= pChar) && (pChar <= 'Z')) || (('a' <= pChar) && (pChar <= 'z')); } @SuppressWarnings({"unchecked", "ManualArrayToCollectionCopy", "MismatchedQueryAndUpdateOfCollection"}) public static <T> Collection<T> convert( Collection<T> pCollectionToFill, Object[] pObjects ) { Collection<Object> toFill = (Collection<Object>) pCollectionToFill; for ( Object object : pObjects ) { toFill.add( object ); } return pCollectionToFill; } /** * This method strips the package name off the fully qualified class name of the pObject returning just the substring * beginning one character beyond the last "." (And removes the wrapping Class names if Any). * * @return the substring beginning one character beyond the last "$"; null or no "$" just returns justClassName( pFullyQualifiedClassName ) */ public static String justSimpleName( Object pObject ) { return justSimpleName( (pObject != null) ? pObject.getClass() : null ); } /** * This method strips the package name off the fully qualified class name of the pObject returning just the substring * beginning one character beyond the last "." (And removes the wrapping Class names if Any). * * @return the substring beginning one character beyond the last "$"; null or no "$" just returns justClassName( pFullyQualifiedClassName ) */ public static String justSimpleName( Class pClass ) { return justSimpleName( (pClass != null) ? pClass.getName() : null ); } /** * This method strips the package name off a fully qualified class name returning just the substring * beginning one character beyond the last "." (And removes the wrapping Class names if Any). * * @return the substring beginning one character beyond the last "$"; null or no "$" just returns justClassName( pFullyQualifiedClassName ) */ public static String justSimpleName( String pFullyQualifiedClassName ) { int zAt = (pFullyQualifiedClassName != null) ? pFullyQualifiedClassName.lastIndexOf( '$' ) : -1; return (zAt != -1) ? pFullyQualifiedClassName.substring( zAt + 1 ) : justClassName( pFullyQualifiedClassName ); } /** * This method strips the package name off a fully qualified class name returning just the substring * beginning one character beyond the last ".". * * @return the substring beginning one character beyond the last "."; null or no "." just returns pFullyQualifiedClassName */ public static String justClassName( String pFullyQualifiedClassName ) { int zAt = (pFullyQualifiedClassName != null) ? pFullyQualifiedClassName.lastIndexOf( '.' ) : -1; return (zAt != -1) ? pFullyQualifiedClassName.substring( zAt + 1 ) : pFullyQualifiedClassName; } /** * This method strips the package name off a fully qualified class name returning just the substring * beginning one character beyond the last ".". * * @return the substring beginning one character beyond the last "."; null or no "." just returns */ public static String justClassName( Class<?> pClass ) { return (pClass != null) ? justClassName( pClass.getName() ) : null; } /** * This method strips the package name off the class name of the pObject returning just the substring * beginning one character beyond the last ".". * * @see #justClassName(Class) */ public static String justClassNameOf( Object pObject ) { return (pObject != null) ? justClassName( pObject.getClass() ) : null; } /** * This method strips the package name off the class name of the pObject returning just the substring * beginning one character beyond the last ".", and if it ends with "Impl" remove that. * * @see #justClassName(Class) */ public static String justClassNameNoImplOf( Object pObject ) { String zName = justClassNameOf( pObject ); return ((zName != null) && zName.endsWith( "Impl" )) ? zName.substring( 0, zName.length() - 4 ) : zName; } public static String justClassNameIfPackage( Class<?> pClass, String pPackage ) { return (pClass != null) ? justClassNameIfPackage( pClass.getName(), pPackage ) : null; } public static String justClassNameIfPackage( String pClassName, String pPackage ) { if ( pClassName != null ) { if ( (pPackage != null) && pPackage.endsWith( "." ) && (pPackage.length() < pClassName.length()) ) { String zSimpleName = justClassName( pClassName ); if ( pClassName.equals( pPackage + zSimpleName ) ) { return zSimpleName; } } } return pClassName; } public static String classNameOf( Object pObject ) { return (pObject != null) ? pObject.getClass().getName() : null; } public static void appendClassName( StringBuilder pBuffer, int pTabLevel, String pLabel, Object pValue ) { append( pBuffer, pTabLevel, pLabel, (pValue == null) ? null : pValue.getClass().getName() ); } public static void append( StringBuilder pBuffer, int pTabLevel, String pLabel, Object pValue ) { append( pBuffer, pTabLevel, pLabel ); pBuffer.append( ':' ); pBuffer.append( (pValue == null) ? "null" : pValue.toString() ); } public static void append( StringBuilder pBuffer, int pTabLevel, String pValue ) { pBuffer.append( '\n' ); while ( pTabLevel-- >= 1 ) { pBuffer.append( '\t' ); } pBuffer.append( (pValue == null) ? "null" : pValue ); } public static <T> List<T> convert( Enumeration<T> pEnumeration ) { ArrayList<T> rv = new ArrayList<T>(); while ( pEnumeration.hasMoreElements() ) { rv.add( pEnumeration.nextElement() ); } return rv; } /** * @param pTo - is NOT extended * * @return pTo */ public static Object[] copyArrayTo( Object[] pFrom, Object[] pTo ) { if ( (pFrom != null) && (pTo != null) ) { copySubArrayTo( Math.min( pFrom.length, pTo.length ), pFrom, 0, pTo, 0 ); } return pTo; } /** * @param pTo - is NOT extended * @param pToIndex - 0 based, < 0 indicates from end (eg -1 == Last) * * @return pTo */ public static Object[] copyArrayTo( Object[] pFrom, Object[] pTo, int pToIndex ) { if ( (pFrom != null) && (pTo != null) ) { copySubArrayTo( Math.min( pFrom.length, pTo.length - pToIndex ), pFrom, 0, pTo, pToIndex ); } return pTo; } /** * @param pCount - < 1 means copy nothing * @param pTo - is NOT extended * @param pFromIndex - 0 based, < 0 indicates from end (eg -1 == Last) * @param pToIndex - 0 based, < 0 indicates from end (eg -1 == Last) * * @return pTo */ public static Object[] copySubArrayTo( int pCount, // Object[] pFrom, int pFromIndex, // Object[] pTo, int pToIndex ) { if ( (pCount > 0) && // Objects.isNotNullOrEmpty( pFrom ) && (pFromIndex < pFrom.length) && // Objects.isNotNullOrEmpty( pTo ) && (pToIndex < pTo.length) ) { pCount = lesserOf( pCount, pTo, pToIndex = unNegateIndex( pTo, pToIndex ) ); pCount = lesserOf( pCount, pFrom, pFromIndex = unNegateIndex( pFrom, pFromIndex ) ); while ( pCount-- > 0 ) { pTo[pToIndex++] = pFrom[pFromIndex++]; } } return pTo; } private static int lesserOf( int pCount, Object[] pObjects, int pIndex ) { if ( pCount > 0 ) { int zMoveLen = pObjects.length - pIndex; if ( zMoveLen < pCount ) { return zMoveLen; } } return pCount; } private static int unNegateIndex( Object[] pFrom, int pFromIndex ) { if ( pFromIndex < 0 ) { if ( (pFromIndex = pFrom.length - pFromIndex) < 0 ) { pFromIndex = 0; } } return pFromIndex; } public static String[] parseOptions( String pOptionsAsString ) throws IllegalArgumentException { if ( null == (pOptionsAsString = Strings.noEmpty( pOptionsAsString )) ) { return null; } char sep = pOptionsAsString.charAt( 0 ); return Character.isLetterOrDigit( sep ) ? // parseOptions( pOptionsAsString, ',' ) : // parseOptions( pOptionsAsString.substring( 1 ).trim(), sep ); } private static String[] parseOptions( String pStringToParse, char pSeparator ) throws IllegalArgumentException { List<String> zParts = new ArrayList<String>(); int from = 0; for ( int at; -1 != (at = pStringToParse.indexOf( pSeparator, from )); from = at + 1 ) { addNotEmpty( zParts, pStringToParse.substring( from, at ) ); } addNotEmpty( zParts, pStringToParse.substring( from ) ); if ( zParts.size() < 2 ) { throw new IllegalArgumentException( "Not TWO options, given: " + pStringToParse ); } return toStringArray( zParts ); } private static void addNotEmpty( List<String> pParts, String pPart ) { if ( (pPart = pPart.trim()).length() != 0 ) { pParts.add( pPart ); } } public static String printStackTraceToString( Throwable pThrowable ) { ExceptionStackTracePrintStream ps = new ExceptionStackTracePrintStream(); pThrowable.printStackTrace( ps ); return ps.toString(); } public static String normalizeCtrlChars( String pText ) { if ( pText != null ) { for ( int i = pText.length(); i-- > 0; ) { if ( pText.charAt( i ) < ' ' ) { StringBuilder sb = new StringBuilder( pText ); for (; i >= 0; i-- ) { char c = sb.charAt( i ); if ( c < ' ' ) { switch ( c ) { case '\n': case '\r': case '\f': break; case '\t': sb.setCharAt( i, ' ' ); break; default: sb.setCharAt( i, '.' ); break; } } pText = sb.toString(); } break; } } } return pText; } /** * Given path(s) that may be system dependent, create a path * that is converted to a system independent form (forward Slashes). * Note: Ignore null entries (null, or all nulls returns null). */ public static String forwardSlashPath( String... pPaths ) { StringBuilder sb = null; if ( pPaths != null ) { for ( String path : pPaths ) { if ( path != null ) { if ( sb == null ) { sb = new StringBuilder(); } if ( path.length() != 0 ) { String fsp = forwardSlashPath( path ); if ( sb.length() == 0 ) { sb.append( fsp ); } else { boolean pStartsWithSlash = (fsp.charAt( 0 ) == '/'); if ( sb.charAt( sb.length() - 1 ) == '/' ) { sb.append( pStartsWithSlash ? fsp.substring( 1 ) : fsp ); } else { if ( !pStartsWithSlash ) { sb.append( '/' ); } sb.append( fsp ); } } } } } } return (sb == null) ? null : sb.toString(); } /** * @param pText !null */ public static void formatToJavaSourceString( StringBuilder pSB, String pText ) { LowLevelEncode( pSB, new CharSourceFromSequence( pText ), true ); } public static String encodeToConstrainedASCII( String pText ) { if ( Strings.isNullOrEmpty( pText ) ) { return pText; } return LowLevelEncode( new StringBuilder(), new CharSourceFromSequence( pText, 0 ), true ).toString(); } public static String decodeFromConstrainedASCII( String pText ) { if ( Strings.isNullOrEmpty( pText ) ) { return pText; } return LowLevelDecode( new StringBuilder(), new CharSourceFromSequence( pText, 0 ), false ).toString(); } private static StringBuilder LowLevelEncode( StringBuilder pSB, CharSource pSource, boolean pDoubleQuoteSpecial ) { for ( int c; -1 != (c = pSource.get()); ) { if ( pDoubleQuoteSpecial && (c == '"') ) { pSB.append( '\\' ); pSB.append( (char) c ); } else { switch ( c ) { case '\\': pSB.append( '\\' ); pSB.append( '\\' ); break; case '\n': pSB.append( '\\' ); pSB.append( 'n' ); break; case '\r': pSB.append( '\\' ); pSB.append( 'r' ); break; case '\t': pSB.append( '\\' ); pSB.append( 't' ); break; default: if ( (' ' <= c) && (c < 127) ) { pSB.append( (char) c ); break; } pSB.append( '\\' ); pSB.append( 'x' ); pSB.append( toHexDigit( c >> 24 ) ); pSB.append( toHexDigit( c >> 16 ) ); pSB.append( toHexDigit( c >> 8 ) ); pSB.append( toHexDigit( c ) ); break; } } } return pSB; } private static StringBuilder LowLevelDecode( StringBuilder pSB, CharSource pSource, boolean pDoubleQuoteSpecial ) { for ( int c; -1 != (c = pSource.get()); ) { if ( (c < ' ') || (127 <= c) ) { throw new IllegalArgumentException( "Decoding Error: Char[" + pSource.getLastOffset() + "] Not between ' ' & Ascii 126 (inclusive)" ); } if ( c != '\\' ) { pSB.append( (char) c ); continue; } if ( -1 == (c = pSource.get()) ) { throw new IllegalArgumentException( "Decoding Error: No Char after escape \\" ); } if ( pDoubleQuoteSpecial && (c == '"') ) { pSB.append( (char) c ); } else { switch ( c ) { case '\\': pSB.append( (char) c ); break; case 'n': pSB.append( '\n' ); break; case 'r': pSB.append( '\r' ); break; case 't': pSB.append( '\t' ); break; case 'x': int c1 = pSource.get(); int c2 = pSource.get(); int c3 = pSource.get(); int c4 = pSource.get(); if ( c4 == -1 ) { throw new IllegalArgumentException( "Decoding Error: Not enough Chars after \\x" ); } try { int zChar = (fromHexDigit( (char) c1 ) << 24) + // (fromHexDigit( (char) c2 ) << 16) + // (fromHexDigit( (char) c3 ) << 8) + // fromHexDigit( (char) c4 ); pSB.append( (char) zChar ); } catch ( IllegalArgumentException e ) { throw new IllegalArgumentException( "Decoding Error: After \\x expected 4 hex digits: " + e.getMessage() ); } break; default: throw new IllegalArgumentException( "Decoding Error: Invalid Char '" + c + "' after escape \\" ); } } } return pSB; } // Array / varArgs public static String combineAsLines( List<String> pStrings ) { return combineAsLines( (pStrings == null) ? null : pStrings.toArray( new String[pStrings.size()] ) ); } public static String combine( String pSeparator, List<String> pStrings ) { //noinspection SuspiciousToArrayCall return combine( pSeparator, (Object[]) pStrings.toArray( new String[pStrings.size()] ) ); } public static String combineObjects( String pSeparator, List<?> pObjects ) { return combine( pSeparator, pObjects.toArray() ); } public static String[] removeAllBlankOrCommentLines( String[] pLines ) { List<String> lines = new LinkedList<String>(); for ( String line : Strings.deNull( pLines ) ) { String s = line.trim(); if ( (s.length() != 0) && !(s.startsWith( "#" ) || s.startsWith( "//" )) ) { lines.add( line ); } } return lines.toArray( new String[lines.size()] ); } // Objects: // Object Group: public static boolean areNotNull( Object... pToChecks ) { if ( pToChecks == null ) { return false; } for ( Object toCheck : pToChecks ) { if ( toCheck == null ) { return false; } } return true; } public static String merge( String[] pLines ) { if ( (pLines == null) || (pLines.length == 0) ) { return ""; } if ( pLines.length == 1 ) { return pLines[0]; } int length = 0; for ( String line : pLines ) { if ( line != null ) { length += line.length() + 2; } } StringBuilder sb = new StringBuilder( length ); for ( String line : pLines ) { if ( line != null ) { sb.append( line ).append( '\r' ).append( '\n' ); } } return sb.toString(); } public static boolean isAllUppercaseOrSpaces( String pToTest ) { if ( Strings.isNotNullOrEmpty( pToTest ) ) { for ( int i = 0; i < pToTest.length(); i++ ) { char c = pToTest.charAt( i ); if ( (c != ' ') && !Character.isUpperCase( c ) ) { return false; } } } return true; } public static boolean isAllUppercase( String pToTest ) { if ( Strings.isNotNullOrEmpty( pToTest ) ) { for ( int i = 0; i < pToTest.length(); i++ ) { if ( !Character.isUpperCase( pToTest.charAt( i ) ) ) { return false; } } } return true; } public static boolean isAsciiIdentifier( String pToTest ) { if ( !Strings.isNullOrEmpty( pToTest ) ) { if ( isFirstCharAsciiIdentifier( pToTest.charAt( 0 ) ) ) { for ( int i = 1; i < pToTest.length(); i++ ) { if ( !isNonFirstCharAsciiIdentifier( pToTest.charAt( i ) ) ) { return false; } } return true; } } return false; } public static boolean isFirstCharAsciiIdentifier( char pToTest ) { return isAsciiLetter( pToTest ) || (pToTest == '_'); } public static boolean isNonFirstCharAsciiIdentifier( char pToTest ) { return isAsciiLetter( pToTest ) || (pToTest == '_') || Character.isDigit( pToTest ); } public static boolean isAsciiLetter( char pToTest ) { return (('A' <= pToTest) && (pToTest <= 'Z')) || (('a' <= pToTest) && (pToTest <= 'z')); } public static boolean isNoSpaceAscii( String pToTest ) { if ( !Strings.isNullOrEmpty( pToTest ) ) { for ( int i = 0; i < pToTest.length(); i++ ) { if ( !isNoSpaceAscii( pToTest.charAt( i ) ) ) { return false; } } return true; } return false; } public static boolean isNoSpaceAscii( char pToTest ) { return ((' ' < pToTest) && (pToTest < 127)); } public static boolean isNoCtrlAscii( String pToTest ) { if ( pToTest != null ) { for ( int i = 0; i < pToTest.length(); i++ ) { if ( !isNoCtrlAscii( pToTest.charAt( i ) ) ) { return false; } } return true; } return false; } public static boolean isNoCtrlAscii( char pToTest ) { return ((' ' <= pToTest) && (pToTest < 127)); } public static boolean hasText( Object pValue ) { return (pValue != null) && (pValue.toString().trim().length() != 0); } public static SimpleDate forceEndOfMonth( SimpleDate pSimpleDate ) { if ( pSimpleDate != null ) { int zDaysInMonth = CalendarSupport.daysInMonth( pSimpleDate.getYear(), pSimpleDate.getMonth() ); if ( pSimpleDate.getDay() != zDaysInMonth ) { pSimpleDate.setDay( zDaysInMonth ); } } return pSimpleDate; } public static String singleQuoteToString( List<String> pStrings ) { StringBuilder sb = new StringBuilder(); if ( pStrings != null ) { char prefix = '['; if ( pStrings.size() == 0 ) { sb.append( prefix ); } else { for ( String zString : pStrings ) { sb.append( prefix ); sb.append( '\'' ); sb.append( zString ); sb.append( '\'' ); prefix = ','; } } sb.append( ']' ); } return sb.toString(); } @SuppressWarnings({"unchecked"}) public static <T> T cast( Object o ) { return (T) o; } } |
Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/util/UtilsCommon.java
Revision | Author | Commited | Message |
---|---|---|---|
809 Diff | GeorgeS | Thu 16 Aug, 2012 04:10:46 +0000 | |
806 Diff | GeorgeS | Thu 16 Aug, 2012 03:48:22 +0000 | |
805 Diff | GeorgeS | Wed 15 Aug, 2012 12:53:09 +0000 | |
804 Diff | GeorgeS | Wed 15 Aug, 2012 12:48:51 +0000 | |
803 Diff | GeorgeS | Wed 15 Aug, 2012 04:08:34 +0000 | |
802 Diff | GeorgeS | Wed 15 Aug, 2012 04:04:47 +0000 | |
801 Diff | GeorgeS | Wed 15 Aug, 2012 03:59:02 +0000 | |
800 Diff | GeorgeS | Wed 15 Aug, 2012 03:33:38 +0000 | |
799 Diff | GeorgeS | Wed 15 Aug, 2012 03:32:04 +0000 | |
772 | GeorgeS | Sun 15 Jul, 2012 16:55:51 +0000 | ! |