All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xqbase.util.Numbers Maven / Gradle / Ivy

There is a newer version: 0.2.18
Show newest version
package com.xqbase.util;

/**
 * Help to parse numbers (int, long, float and double)
 * and prevent unchecked {@link NumberFormatException}
 */
public class Numbers {
	/**
	 * @return an int between min and max (inclusive)
	 */
	public static int minMax(int min, int mid, int max) {
		return mid < min ? min : mid > max ? max : mid;
	}

	/**
	 * Parse an int with default value 0
	 *
	 * @return default value if null or not parsable
	 */
	public static int parseInt(String s) {
		return parseInt(s, 0);
	}

	/**
	 * Parse an int with a given default value i
	 *
	 * @return default value if null or not parsable
	 */
	public static int parseInt(String s, int i) {
		if (s == null) {
			return i;
		}
		try {
			return Integer.parseInt(s.trim());
		} catch (NumberFormatException e) {
			return i;
		}
	}

	/**
	 * Parse an int with a given range
	 * between min and max (inclusive)
	 *
	 * @return default value if null or not parsable
	 */
	public static int parseInt(String s, int min, int max) {
		return parseInt(s, 0, min, max);
	}

	/**
	 * Parse an int with a given default value i and
	 * a given range between min and max (inclusive)
	 *
	 * @return default value if null or not parsable
	 */
	public static int parseInt(String s, int i, int min, int max) {
		return minMax(min, parseInt(s, i), max);
	}

	/**
	 * Parse a long with default value 0
	 *
	 * @return default value if null or not parsable
	 */
	public static long parseLong(String s) {
		return parseLong(s, 0);
	}

	/**
	 * Parse a long with a given default value l
	 *
	 * @return default value if null or not parsable
	 */
	public static long parseLong(String s, long l) {
		if (s == null) {
			return l;
		}
		try {
			return Long.parseLong(s.trim());
		} catch (NumberFormatException e) {
			return l;
		}
	}

	/**
	 * Parse a float with default value 0
	 *
	 * @return default value if null or not parsable
	 */
	public static float parseFloat(String s) {
		return parseFloat(s, 0);
	}

	/**
	 * Parse a float with a given default value f
	 *
	 * @return default value if null or not parsable
	 */
	public static float parseFloat(String s, float f) {
		if (s == null) {
			return f;
		}
		try {
			return Float.parseFloat(s.trim());
		} catch (NumberFormatException e) {
			return f;
		}
	}

	/**
	 * Parse a double with default value 0
	 *
	 * @return default value if null or not parsable
	 */
	public static double parseDouble(String s) {
		return parseDouble(s, 0);
	}

	/**
	 * Parse a double with a given default value d
	 *
	 * @return default value if null or not parsable
	 */
	public static double parseDouble(String s, double d) {
		if (s == null) {
			return d;
		}
		try {
			return Double.parseDouble(s.trim());
		} catch (NumberFormatException e) {
			return d;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy