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

jodd.mutable.MutableDouble Maven / Gradle / Ivy

Go to download

Jodd Core tools and utilities, including type converters, JDateTime, cache etc.

There is a newer version: 5.3.0
Show newest version
// Copyright (c) 2003-2014, Jodd Team (jodd.org). All Rights Reserved.

package jodd.mutable;

/**
 * A mutable double wrapper.
 */
public final class MutableDouble extends Number implements Comparable, Cloneable {

	public MutableDouble() {
	}

	public MutableDouble(double value) {
		this.value = value;
	}

	public MutableDouble(String value) {
		this.value = Double.parseDouble(value);
	}

	public MutableDouble(Number number) {
		this.value = number.doubleValue();
	}

	// ---------------------------------------------------------------- value

	/**
	 * The mutable value.
	 */
	public double value;

	/**
	 * Returns mutable value.
	 */
	public double getValue() {
		return value;
	}

	/**
	 * Sets mutable value.
	 */
	public void setValue(double value) {
		this.value = value;
	}

	/**
	 * Sets mutable value from a Number.
	 */
	public void setValue(Number value) {
		this.value = value.doubleValue();
	}

	// ---------------------------------------------------------------- object

	/**
	 * Stringify the value.
	 */
	@Override
	public String toString() {
		return Double.toString(value);
	}

	/**
	 * Returns a hashcode for this value.
	 */
	@Override
	public int hashCode() {
		long bits = Double.doubleToLongBits(value);
		return (int) (bits ^ (bits >>> 32));
	}

	/**
	 * Compares this object to the specified object.
	 *
	 * @param obj the object to compare with.
	 * @return true if the objects are the same;
	 *         false otherwise.
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj != null) {
			if (obj instanceof Double) {
				return Double.doubleToLongBits(value) == Double.doubleToLongBits(((Double) obj).doubleValue());
			}
			if (obj instanceof MutableDouble) {
				return Double.doubleToLongBits(value) == Double.doubleToLongBits(((MutableDouble) obj).value);
			}
		}
		return false;
	}

	// ---------------------------------------------------------------- number

	/**
	 * Returns the value as a int.
	 */
	@Override
	public int intValue() {
		return (int) value;
	}

	/**
	 * Returns the value as a long.
	 */
	@Override
	public long longValue() {
		return (long) value;
	}

	/**
	 * Returns the value as a float..
	 */
	@Override
	public float floatValue() {
		return (float) value;
	}

	/**
	 * Returns the value as a double.
	 */
	@Override
	public double doubleValue() {
		return value;
	}

	// ---------------------------------------------------------------- compare

	/**
	 * Checks whether the value is the special NaN value.
	 */
	public boolean isNaN() {
		return Double.isNaN(value);
	}

	/**
	 * Checks whether the double value is infinite.
	 */
	public boolean isInfinite() {
		return Double.isInfinite(value);
	}

	/**
	 * Compares value of two same instances.
	 */
	public int compareTo(MutableDouble other) {
		return Double.compare(value, other.value);
	}

	// ---------------------------------------------------------------- clone

	/**
	 * Clones object.
	 */
	@Override
	public MutableDouble clone() {
		return new MutableDouble(value);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy