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

com.xiaoleilu.hutool.mutable.MutableDouble Maven / Gradle / Ivy

package com.xiaoleilu.hutool.mutable;

import com.xiaoleilu.hutool.util.NumberUtil;

/**
 * 可变 double 类型
 * 
 * @see Double
 * @since 3.0.1
 */
public class MutableDouble extends Number implements Comparable, Mutable {
	private static final long serialVersionUID = -7381592836008495052L;
	
	private double value;

	/**
	 * 构造,默认值0
	 */
	public MutableDouble() {
		super();
	}

	/**
	 * 构造
	 * @param value 值
	 */
	public MutableDouble(final double value) {
		super();
		this.value = value;
	}

	/**
	 * 构造
	 * @param value 值
	 */
	public MutableDouble(final Number value) {
		this(value.doubleValue());
	}

	/**
	 * 构造
	 * @param value String值
	 * @throws NumberFormatException 数字转换错误
	 */
	public MutableDouble(final String value) throws NumberFormatException {
		super();
		this.value = Double.parseDouble(value);
	}

	@Override
	public Double get() {
		return Double.valueOf(this.value);
	}

	/**
	 * 设置值
	 * @param value 值
	 */
	public void set(final double value) {
		this.value = value;
	}

	@Override
	public void set(final Number value) {
		this.value = value.doubleValue();
	}

	// -----------------------------------------------------------------------
	/**
	 * 值+1
	 * @return this
	 */
	public MutableDouble increment() {
		value++;
		return this;
	}

	/**
	 * 值减一
	 * @return this
	 */
	public MutableDouble decrement() {
		value--;
		return this;
	}

	// -----------------------------------------------------------------------
	/**
	 * 增加值
	 * @param operand 被增加的值
	 * @return this
	 */
	public MutableDouble add(final double operand) {
		this.value += operand;
		return this;
	}

	/**
	 * 增加值
	 * @param operand 被增加的值,非空
	 * @return this
	 */
	public MutableDouble add(final Number operand) {
		this.value += operand.doubleValue();
		return this;
	}

	/**
	 * 减去值
	 * 
	 * @param operand 被减的值
	 * @return this
	 */
	public MutableDouble subtract(final double operand) {
		this.value -= operand;
		return this;
	}

	/**
	 * 减去值
	 * 
	 * @param operand 被减的值,非空
	 * @return this
	 */
	public MutableDouble subtract(final Number operand) {
		this.value -= operand.doubleValue();
		return this;
	}

	// -----------------------------------------------------------------------
	@Override
	public int intValue() {
		return (int) value;
	}

	@Override
	public long longValue() {
		return (long) value;
	}

	@Override
	public float floatValue() {
		return (float) value;
	}

	@Override
	public double doubleValue() {
		return value;
	}

	// -----------------------------------------------------------------------
	/**
	 * 相等需同时满足如下条件:
	 * 
    *
  1. 非空
  2. *
  3. 类型为 {@link MutableDouble}
  4. *
  5. 值相等
  6. *
* * @param obj 比对的对象 * @return 相同返回true,否则 false */ @Override public boolean equals(final Object obj) { if (obj instanceof MutableDouble) { return (Double.doubleToLongBits(((MutableDouble)obj).value) == Double.doubleToLongBits(value)); } return false; } @Override public int hashCode() { final long bits = Double.doubleToLongBits(value); return (int) (bits ^ bits >>> 32); } // ----------------------------------------------------------------------- /** * 比较 * * @param other 其它 {@link MutableDouble} 对象 * @return x==y返回0,x<y返回-1,x>y返回1 */ @Override public int compareTo(final MutableDouble other) { return NumberUtil.compare(this.value, other.value); } // ----------------------------------------------------------------------- @Override public String toString() { return String.valueOf(value); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy