cn.hutool.core.lang.mutable.MutableInt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hutool-all Show documentation
Show all versions of hutool-all Show documentation
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。
package cn.hutool.core.lang.mutable;
import cn.hutool.core.util.NumberUtil;
/**
* 可变 int
类型
*
* @see Integer
* @since 3.0.1
*/
public class MutableInt extends Number implements Comparable, Mutable {
private static final long serialVersionUID = 1L;
private int value;
/**
* 构造,默认值0
*/
public MutableInt() {
}
/**
* 构造
* @param value 值
*/
public MutableInt(final int value) {
this.value = value;
}
/**
* 构造
* @param value 值
*/
public MutableInt(final Number value) {
this(value.intValue());
}
/**
* 构造
* @param value String值
* @throws NumberFormatException 数字转换错误
*/
public MutableInt(final String value) throws NumberFormatException {
this.value = Integer.parseInt(value);
}
@Override
public Integer get() {
return this.value;
}
/**
* 设置值
* @param value 值
*/
public void set(final int value) {
this.value = value;
}
@Override
public void set(final Number value) {
this.value = value.intValue();
}
// -----------------------------------------------------------------------
/**
* 值+1
* @return this
*/
public MutableInt increment() {
value++;
return this;
}
/**
* 值减一
* @return this
*/
public MutableInt decrement() {
value--;
return this;
}
// -----------------------------------------------------------------------
/**
* 增加值
* @param operand 被增加的值
* @return this
*/
public MutableInt add(final int operand) {
this.value += operand;
return this;
}
/**
* 增加值
* @param operand 被增加的值,非空
* @return this
* @throws NullPointerException if the object is null
*/
public MutableInt add(final Number operand) {
this.value += operand.intValue();
return this;
}
/**
* 减去值
*
* @param operand 被减的值
* @return this
*/
public MutableInt subtract(final int operand) {
this.value -= operand;
return this;
}
/**
* 减去值
*
* @param operand 被减的值,非空
* @return this
* @throws NullPointerException if the object is null
*/
public MutableInt subtract(final Number operand) {
this.value -= operand.intValue();
return this;
}
// -----------------------------------------------------------------------
@Override
public int intValue() {
return value;
}
@Override
public long longValue() {
return value;
}
@Override
public float floatValue() {
return value;
}
@Override
public double doubleValue() {
return value;
}
// -----------------------------------------------------------------------
/**
* 相等需同时满足如下条件:
*
* - 非空
* - 类型为 MutableInt
* - 值相等
*
*
* @param obj 比对的对象
* @return 相同返回true
,否则 {@code false}
*/
@Override
public boolean equals(final Object obj) {
if (obj instanceof MutableInt) {
return value == ((MutableInt) obj).intValue();
}
return false;
}
@Override
public int hashCode() {
return this.value;
}
// -----------------------------------------------------------------------
/**
* 比较
*
* @param other 其它 MutableInt 对象
* @return x==y返回0,x<y返回-1,x>y返回1
*/
@Override
public int compareTo(final MutableInt other) {
return NumberUtil.compare(this.value, other.value);
}
// -----------------------------------------------------------------------
@Override
public String toString() {
return String.valueOf(value);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy