Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package reghzy.api.utils;
- import javax.annotation.Nonnull;
- import java.util.LinkedList;
- import java.util.Queue;
- /**
- * A queue-based parsing helper
- */
- public class Parsing {
- private static final Queue<Float> stackF = new LinkedList<Float>();
- private static final Queue<Double> stackD = new LinkedList<Double>();
- private static final Queue<Integer> stackI = new LinkedList<Integer>();
- private static final Queue<Long> stackL = new LinkedList<Long>();
- // private static float valueF;
- // private static double valueD;
- // private static int valueI;
- // private static long valueL;
- public static boolean parseInt(String value) {
- if (value == null || value.length() == 0) {
- return false;
- }
- final int radix = 10;
- int result = 0;
- boolean isNegative = false;
- int i = 0, len = value.length();
- int limit = -Integer.MAX_VALUE;
- int radixMinLimit;
- int digit;
- char firstChar = value.charAt(0);
- if (firstChar < '0') { // Possible leading "+" or "-"
- if (firstChar == '-') {
- isNegative = true;
- limit = Integer.MIN_VALUE;
- }
- else if (firstChar != '+')
- return false;
- if (len == 1) // Cannot have lone "+" or "-"
- return false;
- i++;
- }
- radixMinLimit = limit / radix;
- while (i < len) {
- // Accumulating negatively avoids surprises near MAX_VALUE
- digit = Character.digit(value.charAt(i++), radix);
- if (digit < 0) {
- return false;
- }
- if (result < radixMinLimit) {
- return false;
- }
- result *= radix;
- if (result < limit + digit) {
- return false;
- }
- result -= digit;
- }
- stackI.add(Integer.valueOf(isNegative ? result : -result));
- // valueI = isNegative ? result : -result;
- return true;
- }
- public static boolean parseInt(char[] value, int startIndex, int endIndex) {
- if (value == null || value.length == 0) {
- return false;
- }
- int radix = 10;
- int result = 0;
- boolean isNegative = false;
- int i = startIndex, len = (endIndex - startIndex);
- int limit = -Integer.MAX_VALUE;
- int radixMinLimit;
- int digit;
- char firstChar = value[startIndex];
- if (firstChar < '0') { // Possible leading "+" or "-"
- if (firstChar == '-') {
- isNegative = true;
- limit = Integer.MIN_VALUE;
- }
- else if (firstChar != '+')
- return false;
- if (len == 1) // Cannot have lone "+" or "-"
- return false;
- i++;
- }
- radixMinLimit = limit / radix;
- while (i < endIndex) {
- // Accumulating negatively avoids surprises near MAX_VALUE
- digit = Character.digit(value[i++], radix);
- if (digit < 0) {
- return false;
- }
- if (result < radixMinLimit) {
- return false;
- }
- result *= radix;
- if (result < limit + digit) {
- return false;
- }
- result -= digit;
- }
- stackI.add(Integer.valueOf(isNegative ? result : -result));
- // valueI = isNegative ? result : -result;
- return true;
- }
- public static boolean parseInt(String value, int startIndex) {
- if (value == null) {
- return false;
- }
- return parseInt(value, startIndex, value.length());
- }
- public static boolean parseInt(String value, int startIndex, int endIndex) {
- if (value == null || value.length() == 0) {
- return false;
- }
- int radix = 10;
- int result = 0;
- boolean isNegative = false;
- int i = startIndex, len = (endIndex - startIndex);
- int limit = -Integer.MAX_VALUE;
- int radixMinLimit;
- int digit;
- char firstChar = value.charAt(startIndex);
- if (firstChar < '0') { // Possible leading "+" or "-"
- if (firstChar == '-') {
- isNegative = true;
- limit = Integer.MIN_VALUE;
- }
- else if (firstChar != '+')
- return false;
- if (len == 1) // Cannot have lone "+" or "-"
- return false;
- i++;
- }
- radixMinLimit = limit / radix;
- while (i < endIndex) {
- // Accumulating negatively avoids surprises near MAX_VALUE
- digit = Character.digit(value.charAt(i++), radix);
- if (digit < 0) {
- return false;
- }
- if (result < radixMinLimit) {
- return false;
- }
- result *= radix;
- if (result < limit + digit) {
- return false;
- }
- result -= digit;
- }
- stackI.add(Integer.valueOf(isNegative ? result : -result));
- // valueI = isNegative ? result : -result;
- return true;
- }
- public static boolean parseLong(String value) {
- int radix = 10;
- long result = 0;
- boolean negative = false;
- int i = 0, len = value.length();
- long limit = -Long.MAX_VALUE;
- long radixMinLimit;
- int digit;
- if (len == 0) {
- return false;
- }
- else {
- char firstChar = value.charAt(0);
- if (firstChar < '0') { // Possible leading "+" or "-"
- if (firstChar == '-') {
- negative = true;
- limit = Long.MIN_VALUE;
- }
- else if (firstChar != '+')
- return false;
- if (len == 1) // Cannot have lone "+" or "-"
- return false;
- i++;
- }
- radixMinLimit = limit / radix;
- while (i < len) {
- // Accumulating negatively avoids surprises near MAX_VALUE
- digit = Character.digit(value.charAt(i++), radix);
- if (digit < 0) {
- return false;
- }
- if (result < radixMinLimit) {
- return false;
- }
- result *= radix;
- if (result < limit + digit) {
- return false;
- }
- result -= digit;
- }
- }
- stackL.add(Long.valueOf(negative ? result : -result));
- // valueL = negative ? result : -result;
- return true;
- }
- public static boolean parseFloat(String value) {
- try {
- stackF.add(Float.parseFloat(value));
- // valueF = Float.parseFloat(value);
- return true;
- }
- catch (Throwable throwable) {
- return false;
- }
- }
- public static boolean parseDouble(String value) {
- try {
- stackD.add(Double.parseDouble(value));
- // valueD = Double.parseDouble(value);
- return true;
- }
- catch (Throwable throwable) {
- return false;
- }
- }
- // public static float popF() {
- // return stackF.pop();
- // }
- // public static double popD() {
- // return stackD.pop();
- // }
- // public static int popI() {
- // return stackI.pop();
- // }
- // public static long popL() {
- // return stackL.pop();
- // }
- /**
- * Pops the first element from the float queue
- */
- @Nonnull
- public static Float getFloat() {
- return stackF.remove();
- // return valueF;
- }
- /**
- * Pops the first element from the double queue
- */
- @Nonnull
- public static Double getDouble() {
- return stackD.remove();
- // return valueD;
- }
- /**
- * Pops the first element from the int queue
- */
- @Nonnull
- public static Integer getInt() {
- return stackI.remove();
- // return valueI;
- }
- /**
- * Pops the first element from the long queue
- */
- @Nonnull
- public static Long getLong() {
- return stackL.remove();
- // return valueL;
- }
- public static void clearFloat() {
- stackF.clear();
- }
- public static void clearFloat(int count) {
- clear(stackF, count);
- }
- public static void clearDouble() {
- stackD.clear();
- }
- public static void clearDouble(int count) {
- clear(stackD, count);
- }
- public static void clearInt() {
- stackI.clear();
- }
- public static void clearInt(int count) {
- clear(stackI, count);
- }
- public static void clearLong() {
- stackL.clear();
- }
- public static void clearLong(int count) {
- clear(stackL, count);
- }
- private static void clear(Queue<?> queue, int count) {
- for (int i = 0; i < count; i++) {
- queue.poll();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement