(二十四)Java工具类Mutable接口提供可变的存储改变值详解

原文链接:https://blog.csdn.net/yaomingyang/article/details/79295447

1.Mutable接口提供了一个通用的接口去实现可变数据类型的实现

所有已知实现Mutable接口的类有MutableBoolean, MutableByte, MutableDouble, MutableFloat, MutableInt, MutableLong, MutableObject, MutableShort,这些类都是可变的,也就是修改对象的值不需要重新创建新的对象; 
典型的用例是使用原始数据类型或字符串作为参数传递给一个方法并且允许方法修改原始数据或者字符串; 
另外一种典型的用例是存储经常变动的原始数据类型到容器中(例如:存入map)无需创建Integer/Long包装器;

package org.apache.commons.lang3.mutable;

public abstract interface Mutable<T>
{
  public abstract T getValue();

  public abstract void setValue(T paramT);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

2.MutableInt可变整型类详解

package org.apache.commons.lang3.mutable;

import org.apache.commons.lang3.math.NumberUtils;

public class MutableInt
  extends Number
  implements Comparable<MutableInt>, Mutable<Number>
{
  private static final long serialVersionUID = 512176391864L;
  private int value;
  /**
  * 声明一个可变整数,默认值为0
  **/
  public MutableInt() {}
  /**
  *声明一个参数为int类型的值初始化MutableInt构造函数
  */
  public MutableInt(int value)
  {
    this.value = value;
  }
  /**
  * 声明一个参数为Number类型的值初始化MutableInt构造函数
  */
  public MutableInt(Number value)
  {
    this.value = value.intValue();
  }
  /**
  *声明一个参数为String类型的值初始化MutableInt构造函数
  */
  public MutableInt(String value)
    throws NumberFormatException
  {
    this.value = Integer.parseInt(value);
  }
  /**
  * 获取MutableInt存储的值,并将值转换为Integer包装类型返回
  */
  public Integer getValue()
  {
    return Integer.valueOf(this.value);
  }
  /**
  * 传递一个int类型的参数来修改MutableInt存储的默认值
  */
  public void setValue(int value)
  {
    this.value = value;
  }
  /**
  * 传递一个Number类型额参数修改MutableInt存储的默认值
  */
  public void setValue(Number value)
  {
    this.value = value.intValue();
  }
  /**
  * MutableInt加1
  */
  public void increment()
  {
    this.value += 1;
  }
  /**
  * 该方法将MutableInt的值加一并返回未加一之前的数据,非线程安全的
  */
  public int getAndIncrement()
  {
    int last = this.value;
    this.value += 1;
    return last;
  }
  /**
  * 将MutableInt存储的值加一并返回
  */
  public int incrementAndGet()
  {
    this.value += 1;
    return this.value;
  }
  /**
  * 将MutableInt存储的值减一
  */
  public void decrement()
  {
    this.value -= 1;
  }
  /**
  * 将MutableInt存储的值减一并返回未减一之前的数据
  */
  public int getAndDecrement()
  {
    int last = this.value;
    this.value -= 1;
    return last;
  }
  /**
  * 将MutableInt存储的值减一并返回
  */
  public int decrementAndGet()
  {
    this.value -= 1;
    return this.value;
  }
  /**
  * 将MutableInt存储的值加上指定的int类型值
  */
  public void add(int operand)
  {
    this.value += operand;
  }
  /**
  * 将MutableInt存储的值加上指定的Number类型的值
  */
  public void add(Number operand)
  {
    this.value += operand.intValue();
  }
  /**
  * 将MutableInt存储的值减去指定的int类型值
  */
  public void subtract(int operand)
  {
    this.value -= operand;
  }
  /**
  * 将MutableInt存储的值减去指定的Number类型的值
  */
  public void subtract(Number operand)
  {
    this.value -= operand.intValue();
  }
  /**
  * 将MutableInt存储的值加上指定的int类型的值并返回结果
  */
  public int addAndGet(int operand)
  {
    this.value += operand;
    return this.value;
  }
  /**
  * 将MutableInt存储的值将上指定的Number类型的值并返回结果
  */
  public int addAndGet(Number operand)
  {
    this.value += operand.intValue();
    return this.value;
  }
  /**
  *  将MutableInt存储的值加上指定的int类型的值并返回之前的值
  */
  public int getAndAdd(int operand)
  {
    int last = this.value;
    this.value += operand;
    return last;
  }
  /**
  *  将MutableInt存储的值加上指定的Number类型的值并返回之前的值
  */
  public int getAndAdd(Number operand)
  {
    int last = this.value;
    this.value += operand.intValue();
    return last;
  }
  /**
  * 将MutableInt类型转换成int类型
  */
  public int intValue()
  {
    return this.value;
  }
  /**
  * 将MutableInt类型转换成long类型
  */
  public long longValue()
  {
    return this.value;
  }
  /**
  * 将MutableInt类型转换成float类型
  */
  public float floatValue()
  {
    return this.value;
  }
  /**
  * 将MutableInt类型转换成double类型
  */  
  public double doubleValue()
  {
    return this.value;
  }
  /**
  * 将MutableInt类型转换成Integer类型
  */
  public Integer toInteger()
  {
    return Integer.valueOf(intValue());
  }
  /**
  * 比较两个MutableInt类型数据是否相等
  */
  public boolean equals(Object obj)
  {
    if ((obj instanceof MutableInt)) {
      return this.value == ((MutableInt)obj).intValue();
    }
    return false;
  }
  /**
  *
  */
  public int hashCode()
  {
    return this.value;
  }
  /**
  * 比较两个MutableInt数据的大小
  */
  public int compareTo(MutableInt other)
  {
    return NumberUtils.compare(this.value, other.value);
  }
  /**
  *
  */
  public String toString()
  {
    return String.valueOf(this.value);
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235

其它几种可变的数据类型与上面的源码基本相似,一看就懂,就不在复述;


猜你喜欢

转载自blog.csdn.net/jarniyy/article/details/80429562