Advertisement
bobmarley12345

java primitive try parse helper

Apr 10th, 2022
1,220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.24 KB | None | 0 0
  1. package reghzy.api.utils;
  2.  
  3. import javax.annotation.Nonnull;
  4. import java.util.LinkedList;
  5. import java.util.Queue;
  6.  
  7. /**
  8.  * A queue-based parsing helper
  9.  */
  10. public class Parsing {
  11.     private static final Queue<Float>   stackF = new LinkedList<Float>();
  12.     private static final Queue<Double>  stackD = new LinkedList<Double>();
  13.     private static final Queue<Integer> stackI = new LinkedList<Integer>();
  14.     private static final Queue<Long>    stackL = new LinkedList<Long>();
  15.  
  16.     // private static float    valueF;
  17.     // private static double   valueD;
  18.     // private static int      valueI;
  19.     // private static long     valueL;
  20.  
  21.     public static boolean parseInt(String value) {
  22.         if (value == null || value.length() == 0) {
  23.             return false;
  24.         }
  25.  
  26.         final int radix = 10;
  27.         int result = 0;
  28.         boolean isNegative = false;
  29.         int i = 0, len = value.length();
  30.         int limit = -Integer.MAX_VALUE;
  31.         int radixMinLimit;
  32.         int digit;
  33.  
  34.         char firstChar = value.charAt(0);
  35.         if (firstChar < '0') { // Possible leading "+" or "-"
  36.             if (firstChar == '-') {
  37.                 isNegative = true;
  38.                 limit = Integer.MIN_VALUE;
  39.             }
  40.             else if (firstChar != '+')
  41.                 return false;
  42.  
  43.             if (len == 1) // Cannot have lone "+" or "-"
  44.                 return false;
  45.  
  46.             i++;
  47.         }
  48.  
  49.         radixMinLimit = limit / radix;
  50.         while (i < len) {
  51.             // Accumulating negatively avoids surprises near MAX_VALUE
  52.             digit = Character.digit(value.charAt(i++), radix);
  53.             if (digit < 0) {
  54.                 return false;
  55.             }
  56.             if (result < radixMinLimit) {
  57.                 return false;
  58.             }
  59.  
  60.             result *= radix;
  61.             if (result < limit + digit) {
  62.                 return false;
  63.             }
  64.  
  65.             result -= digit;
  66.         }
  67.  
  68.         stackI.add(Integer.valueOf(isNegative ? result : -result));
  69.         // valueI = isNegative ? result : -result;
  70.         return true;
  71.     }
  72.  
  73.     public static boolean parseInt(char[] value, int startIndex, int endIndex) {
  74.         if (value == null || value.length == 0) {
  75.             return false;
  76.         }
  77.  
  78.         int radix = 10;
  79.         int result = 0;
  80.         boolean isNegative = false;
  81.         int i = startIndex, len = (endIndex - startIndex);
  82.         int limit = -Integer.MAX_VALUE;
  83.         int radixMinLimit;
  84.         int digit;
  85.  
  86.         char firstChar = value[startIndex];
  87.         if (firstChar < '0') { // Possible leading "+" or "-"
  88.             if (firstChar == '-') {
  89.                 isNegative = true;
  90.                 limit = Integer.MIN_VALUE;
  91.             }
  92.             else if (firstChar != '+')
  93.                 return false;
  94.  
  95.             if (len == 1) // Cannot have lone "+" or "-"
  96.                 return false;
  97.             i++;
  98.         }
  99.  
  100.         radixMinLimit = limit / radix;
  101.         while (i < endIndex) {
  102.             // Accumulating negatively avoids surprises near MAX_VALUE
  103.             digit = Character.digit(value[i++], radix);
  104.             if (digit < 0) {
  105.                 return false;
  106.             }
  107.             if (result < radixMinLimit) {
  108.                 return false;
  109.             }
  110.  
  111.             result *= radix;
  112.             if (result < limit + digit) {
  113.                 return false;
  114.             }
  115.  
  116.             result -= digit;
  117.         }
  118.  
  119.         stackI.add(Integer.valueOf(isNegative ? result : -result));
  120.         // valueI = isNegative ? result : -result;
  121.         return true;
  122.     }
  123.  
  124.     public static boolean parseInt(String value, int startIndex) {
  125.         if (value == null) {
  126.             return false;
  127.         }
  128.  
  129.         return parseInt(value, startIndex, value.length());
  130.     }
  131.  
  132.     public static boolean parseInt(String value, int startIndex, int endIndex) {
  133.         if (value == null || value.length() == 0) {
  134.             return false;
  135.         }
  136.  
  137.         int radix = 10;
  138.         int result = 0;
  139.         boolean isNegative = false;
  140.         int i = startIndex, len = (endIndex - startIndex);
  141.         int limit = -Integer.MAX_VALUE;
  142.         int radixMinLimit;
  143.         int digit;
  144.  
  145.         char firstChar = value.charAt(startIndex);
  146.         if (firstChar < '0') { // Possible leading "+" or "-"
  147.             if (firstChar == '-') {
  148.                 isNegative = true;
  149.                 limit = Integer.MIN_VALUE;
  150.             }
  151.             else if (firstChar != '+')
  152.                 return false;
  153.  
  154.             if (len == 1) // Cannot have lone "+" or "-"
  155.                 return false;
  156.             i++;
  157.         }
  158.  
  159.         radixMinLimit = limit / radix;
  160.         while (i < endIndex) {
  161.             // Accumulating negatively avoids surprises near MAX_VALUE
  162.             digit = Character.digit(value.charAt(i++), radix);
  163.             if (digit < 0) {
  164.                 return false;
  165.             }
  166.             if (result < radixMinLimit) {
  167.                 return false;
  168.             }
  169.  
  170.             result *= radix;
  171.             if (result < limit + digit) {
  172.                 return false;
  173.             }
  174.  
  175.             result -= digit;
  176.         }
  177.  
  178.         stackI.add(Integer.valueOf(isNegative ? result : -result));
  179.         // valueI = isNegative ? result : -result;
  180.         return true;
  181.     }
  182.  
  183.     public static boolean parseLong(String value) {
  184.         int radix = 10;
  185.         long result = 0;
  186.         boolean negative = false;
  187.         int i = 0, len = value.length();
  188.         long limit = -Long.MAX_VALUE;
  189.         long radixMinLimit;
  190.         int digit;
  191.  
  192.         if (len == 0) {
  193.             return false;
  194.         }
  195.         else {
  196.             char firstChar = value.charAt(0);
  197.             if (firstChar < '0') { // Possible leading "+" or "-"
  198.                 if (firstChar == '-') {
  199.                     negative = true;
  200.                     limit = Long.MIN_VALUE;
  201.                 }
  202.                 else if (firstChar != '+')
  203.                     return false;
  204.  
  205.                 if (len == 1) // Cannot have lone "+" or "-"
  206.                     return false;
  207.                 i++;
  208.             }
  209.  
  210.             radixMinLimit = limit / radix;
  211.             while (i < len) {
  212.                 // Accumulating negatively avoids surprises near MAX_VALUE
  213.                 digit = Character.digit(value.charAt(i++), radix);
  214.                 if (digit < 0) {
  215.                     return false;
  216.                 }
  217.                 if (result < radixMinLimit) {
  218.                     return false;
  219.                 }
  220.                 result *= radix;
  221.                 if (result < limit + digit) {
  222.                     return false;
  223.                 }
  224.                 result -= digit;
  225.             }
  226.         }
  227.  
  228.         stackL.add(Long.valueOf(negative ? result : -result));
  229.         // valueL = negative ? result : -result;
  230.         return true;
  231.     }
  232.  
  233.     public static boolean parseFloat(String value) {
  234.         try {
  235.             stackF.add(Float.parseFloat(value));
  236.             // valueF = Float.parseFloat(value);
  237.             return true;
  238.         }
  239.         catch (Throwable throwable) {
  240.             return false;
  241.         }
  242.     }
  243.  
  244.     public static boolean parseDouble(String value) {
  245.         try {
  246.             stackD.add(Double.parseDouble(value));
  247.             // valueD = Double.parseDouble(value);
  248.             return true;
  249.         }
  250.         catch (Throwable throwable) {
  251.             return false;
  252.         }
  253.     }
  254.  
  255.     // public static float popF() {
  256.     //     return stackF.pop();
  257.     // }
  258.     // public static double popD() {
  259.     //     return stackD.pop();
  260.     // }
  261.     // public static int popI() {
  262.     //     return stackI.pop();
  263.     // }
  264.     // public static long popL() {
  265.     //     return stackL.pop();
  266.     // }
  267.  
  268.  
  269.     /**
  270.      * Pops the first element from the float queue
  271.      */
  272.     @Nonnull
  273.     public static Float getFloat() {
  274.         return stackF.remove();
  275.         // return valueF;
  276.     }
  277.  
  278.     /**
  279.      * Pops the first element from the double queue
  280.      */
  281.     @Nonnull
  282.     public static Double getDouble() {
  283.         return stackD.remove();
  284.         // return valueD;
  285.     }
  286.  
  287.     /**
  288.      * Pops the first element from the int queue
  289.      */
  290.     @Nonnull
  291.     public static Integer getInt() {
  292.         return stackI.remove();
  293.         // return valueI;
  294.     }
  295.  
  296.     /**
  297.      * Pops the first element from the long queue
  298.      */
  299.     @Nonnull
  300.     public static Long getLong() {
  301.         return stackL.remove();
  302.         // return valueL;
  303.     }
  304.  
  305.     public static void clearFloat() {
  306.         stackF.clear();
  307.     }
  308.  
  309.     public static void clearFloat(int count) {
  310.         clear(stackF, count);
  311.     }
  312.  
  313.     public static void clearDouble() {
  314.         stackD.clear();
  315.     }
  316.  
  317.     public static void clearDouble(int count) {
  318.         clear(stackD, count);
  319.     }
  320.  
  321.     public static void clearInt() {
  322.         stackI.clear();
  323.     }
  324.  
  325.     public static void clearInt(int count) {
  326.         clear(stackI, count);
  327.     }
  328.  
  329.     public static void clearLong() {
  330.         stackL.clear();
  331.     }
  332.  
  333.     public static void clearLong(int count) {
  334.         clear(stackL, count);
  335.     }
  336.  
  337.     private static void clear(Queue<?> queue, int count) {
  338.         for (int i = 0; i < count; i++) {
  339.             queue.poll();
  340.         }
  341.     }
  342. }
  343.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement