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

org.kernelab.basis.Variable Maven / Gradle / Ivy

There is a newer version: 0.0.19-RELEASE
Show newest version
package org.kernelab.basis;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * Variable class which can change its value.
 * 
 * @author Dilly King
 * 
 * @param 
 *            The generic type of number.
 */
public class Variable> extends java.lang.Number implements
		Comparable>, Serializable, Copieable>
{
	/**
     * 
     */
	private static final long	serialVersionUID	= -4353893839356953344L;

	/**
	 * Try to convert the string to a Byte value, if could not convert then
	 * return null.
	 * 
	 * @param string
	 *            the number string.
	 * @return The Byte value.
	 */
	public static Byte asByte(String string)
	{
		return asByte(string, null);
	}

	/**
	 * Try to convert the string to a Byte value, if could not convert then use
	 * the defaultValue instead.
	 * 
	 * @param string
	 *            the number string.
	 * @param defaultValue
	 *            the default value which would be used if the string could not
	 *            be converted to a Byte.
	 * @return The Byte value.
	 */
	public static Byte asByte(String string, Byte defaultValue)
	{
		try
		{
			return Byte.valueOf(string);
		}
		catch (Exception e)
		{
			return defaultValue;
		}
	}

	/**
	 * Try to convert the string to a Double value, if could not convert then
	 * return null.
	 * 
	 * @param string
	 *            the number string.
	 * @return The Double value.
	 */
	public static Double asDouble(String string)
	{
		return asDouble(string, null);
	}

	/**
	 * Try to convert the string to a Double value, if could not convert then
	 * use the defaultValue instead.
	 * 
	 * @param string
	 *            the number string.
	 * @param defaultValue
	 *            the default value which would be used if the string could not
	 *            be converted to a Double.
	 * @return The Double value.
	 */
	public static Double asDouble(String string, Double defaultValue)
	{
		try
		{
			return Double.valueOf(string);
		}
		catch (Exception e)
		{
			return defaultValue;
		}
	}

	/**
	 * Try to convert the string to a Float value, if could not convert then
	 * return null.
	 * 
	 * @param string
	 *            the number string.
	 * @return The Float value.
	 */
	public static Float asFloat(String string)
	{
		return asFloat(string, null);
	}

	/**
	 * Try to convert the string to a Float value, if could not convert then use
	 * the defaultValue instead.
	 * 
	 * @param string
	 *            the number string.
	 * @param defaultValue
	 *            the default value which would be used if the string could not
	 *            be converted to a Float.
	 * @return The Float value.
	 */
	public static Float asFloat(String string, Float defaultValue)
	{
		try
		{
			return Float.valueOf(string);
		}
		catch (Exception e)
		{
			return defaultValue;
		}
	}

	/**
	 * Try to convert the string to a Integer value, if could not convert then
	 * return null.
	 * 
	 * @param string
	 *            the number string.
	 * @return The Integer value.
	 */
	public static Integer asInteger(String string)
	{
		return asInteger(string, null);
	}

	/**
	 * Try to convert the string to an Integer value, if could not convert then
	 * use the defaultValue instead.
	 * 
	 * @param string
	 *            the number string.
	 * @param defaultValue
	 *            the default value which would be used if the string could not
	 *            be converted to an Integer.
	 * @return The Integer value.
	 */
	public static Integer asInteger(String string, Integer defaultValue)
	{
		try
		{
			return Integer.valueOf(string);
		}
		catch (Exception e)
		{
			return defaultValue;
		}
	}

	/**
	 * Try to convert the string to a Long value, if could not convert then
	 * return null.
	 * 
	 * @param string
	 *            the number string.
	 * @return The Long value.
	 */
	public static Long asLong(String string)
	{
		return asLong(string, null);
	}

	/**
	 * Try to convert the string to a Long value, if could not convert then use
	 * the defaultValue instead.
	 * 
	 * @param string
	 *            the number string.
	 * @param defaultValue
	 *            the default value which would be used if the string could not
	 *            be converted to a Long.
	 * @return The Long value.
	 */
	public static Long asLong(String string, Long defaultValue)
	{
		try
		{
			return Long.valueOf(string);
		}
		catch (Exception e)
		{
			return defaultValue;
		}
	}

	/**
	 * Try to convert the string to a Short value, if could not convert then
	 * return null.
	 * 
	 * @param string
	 *            the number string.
	 * @return The Short value.
	 */
	public static Short asShort(String string)
	{
		return asShort(string, null);
	}

	/**
	 * Try to convert the string to a Short value, if could not convert then use
	 * the defaultValue instead.
	 * 
	 * @param string
	 *            the number string.
	 * @param defaultValue
	 *            the default value which would be used if the string could not
	 *            be converted to a Short.
	 * @return The Short value.
	 */
	public static Short asShort(String string, Short defaultValue)
	{
		try
		{
			return Short.valueOf(string);
		}
		catch (Exception e)
		{
			return defaultValue;
		}
	}

	/**
	 * Get the Byte value of a java.lang.Number
	 * 
	 * @param number
	 *            The java.lang.Number
	 * @return The Byte value
	 */
	public static Byte byteValue(java.lang.Number number)
	{
		Byte value = null;

		if (number != null)
		{
			if (number instanceof Byte)
			{
				value = (Byte) number;
			}
			else
			{
				try
				{
					value = Byte.valueOf(number.toString());
				}
				catch (Exception e)
				{
					value = Variable.doubleValue(number).byteValue();
				}
			}
		}

		return value;
	}

	/**
	 * Get the Double value of a java.lang.Number
	 * 
	 * @param number
	 *            The java.lang.Number
	 * @return The Double value
	 */
	public static Double doubleValue(java.lang.Number number)
	{
		Double value = null;

		if (number != null)
		{
			if (number instanceof Double)
			{
				value = (Double) number;
			}
			else
			{
				value = Double.valueOf(number.toString());
			}
		}

		return value;
	}

	/**
	 * Get the Float value of a java.lang.Number
	 * 
	 * @param number
	 *            The java.lang.Number
	 * @return The Float value
	 */
	public static Float floatValue(java.lang.Number number)
	{
		Float value = null;

		if (number != null)
		{
			if (number instanceof Float)
			{
				value = (Float) number;
			}
			else
			{
				try
				{
					value = Float.valueOf(number.toString());
				}
				catch (Exception e)
				{
					value = Variable.doubleValue(number).floatValue();
				}
			}
		}

		return value;
	}

	/**
	 * Get the format string of a BigDecimal number.
	 * 
	 * 
	 * Example:
	 * 
	 * number: new BigDecimal("1234.5")
	 * format: "#,##0.00"
	 * string: "1,234.50"
	 * 
* * @param number * the BigDecimal number to be formatted. * @param pattern * the number pattern. * @return the formatted String. */ public static String format(BigDecimal number, String pattern) { return number == null ? null : new DecimalFormat(pattern == null ? "" : pattern).format(number); } /** * Get the format string of a number. * *
	 * Example:
	 * 
	 * number: 1234.5
	 * format: "#,##0.00"
	 * string: "1,234.50"
	 * 
* * @param * the generic number type. * @param number * the number to be formatted. * @param pattern * the number pattern. * @return the formatted String. * @see java.text.DecimalFormat */ public static String format(N number, String pattern) { return number == null ? null : new DecimalFormat(pattern == null ? "" : pattern).format(number); } /** * Get the format string of a number represented by a String. * *
	 * Example:
	 * 
	 * number: "1234.5"
	 * format: "#,##0.00"
	 * string: "1,234.50"
	 * 
* * @param number * the String number to be formatted. * @param pattern * the number pattern. * @return the formatted String. */ public static String format(String number, String pattern) { return number == null ? null : new DecimalFormat(pattern == null ? "" : pattern).format(new BigDecimal(number)); } /** * Get the Integer value of a java.lang.Number * * @param number * The java.lang.Number * @return The Integer value */ public static Integer integerValue(java.lang.Number number) { Integer value = null; if (number != null) { if (number instanceof Integer) { value = (Integer) number; } else { try { value = Integer.valueOf(number.toString()); } catch (Exception e) { value = Variable.doubleValue(number).intValue(); } } } return value; } /** * Determine whether the String represents a float number. * * @param string * the number string. * @return TRUE if the string is Double otherwise * FALSE. */ public static boolean isFloatNumber(String string) { try { Double.parseDouble(string); return true; } catch (Exception e) { return false; } } /** * Determine whether the String represents a integer number. * * @param string * the number string. * @return TRUE if the string is Integer otherwise * FALSE. */ public static boolean isIntegerNumber(String string) { try { Long.parseLong(string); return true; } catch (Exception ex) { return false; } } /** * Determine whether a Char is a number letter. * * @param c * the number char. * @return TRUE if the char is number otherwise * FALSE. */ public static boolean isNumber(char c) { return '0' <= c && c <= '9'; } /** * Determine whether a String is a number. * *
	 * Example:
	 * 
	 * the String "12.97" is a number.
	 * the String "8.4.5" is not a number.
	 * the String "3p2.5" is not a number.
	 * 
* * @param string * the number string. * @return TRUE if the string is number otherwise * FALSE. */ public static boolean isNumber(String string) { return isFloatNumber(string); } /** * Get the Long value of a java.lang.Number * * @param number * The java.lang.Number * @return The Long value */ public static Long longValue(java.lang.Number number) { Long value = null; if (number != null) { if (number instanceof Long) { value = (Long) number; } else { try { value = Long.valueOf(number.toString()); } catch (Exception e) { value = Variable.doubleValue(number).longValue(); } } } return value; } /** * @param args */ public static void main(String[] args) { Tools.debug(Variable.format("1234.5", "#,##0.00")); } /** * Get the instance of Variable. This method is no need to assign the * generic type of the number. * *
	 * Example:
	 * 
	 * Variable.instance(2);
	 * 
	 * equals to 
	 * 
	 * new Variable<Integer>(2);
	 * 
* * @param * The number type. * @param number * The value of the number. * @return The instance of Variable. */ public static > Variable newInstance(N number) { return new Variable(number); } /** * Get the Short value of a java.lang.Number * * @param number * The java.lang.Number * @return The Short value */ public static Short shortValue(java.lang.Number number) { Short value = null; if (number != null) { if (number instanceof Short) { value = (Short) number; } else { try { value = Short.valueOf(number.toString()); } catch (Exception e) { value = Variable.doubleValue(number).shortValue(); } } } return value; } public N value; public Variable(N value) { this.value = value; } protected Variable(Variable number) { this.value = number.value; } @Override public Variable clone() { return new Variable(this); } public int compareTo(Variable number) { return this.getValue().compareTo(number.getValue()); } public double doubleValue() { return Variable.doubleValue(value); } public float floatValue() { return (float) doubleValue(); } public String format(String format) { return Variable.format(this.getValue(), format); } /** * Get the value of this Variable into the parameter. * * @param variable * The Variable which would holds the value of this Variable. */ public Variable get(Variable variable) { variable.value = this.value; return variable; } public N getValue() { return value; } public int intValue() { return Variable.integerValue(value); } public long longValue() { return Variable.longValue(value); } /** * To set the value of this Variable according to the given parameter. * * @param variable * The Variable according to which the value of this Variable * would be set to. */ public Variable set(Variable variable) { this.value = variable.value; return this; } public Variable setValue(N value) { this.value = value; return this; } @Override public String toString() { return value.toString(); } public String toString(String format) { return format(this, format); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy