diff options
Diffstat (limited to 'src/org/apache/commons/lang3/mutable')
| -rw-r--r-- | src/org/apache/commons/lang3/mutable/Mutable.java | 54 | ||||
| -rw-r--r-- | src/org/apache/commons/lang3/mutable/MutableBoolean.java | 193 | ||||
| -rw-r--r-- | src/org/apache/commons/lang3/mutable/MutableByte.java | 283 | ||||
| -rw-r--r-- | src/org/apache/commons/lang3/mutable/MutableDouble.java | 312 | ||||
| -rw-r--r-- | src/org/apache/commons/lang3/mutable/MutableFloat.java | 313 | ||||
| -rw-r--r-- | src/org/apache/commons/lang3/mutable/MutableInt.java | 273 | ||||
| -rw-r--r-- | src/org/apache/commons/lang3/mutable/MutableLong.java | 273 | ||||
| -rw-r--r-- | src/org/apache/commons/lang3/mutable/MutableObject.java | 126 | ||||
| -rw-r--r-- | src/org/apache/commons/lang3/mutable/MutableShort.java | 283 |
9 files changed, 2110 insertions, 0 deletions
diff --git a/src/org/apache/commons/lang3/mutable/Mutable.java b/src/org/apache/commons/lang3/mutable/Mutable.java new file mode 100644 index 0000000..64514f0 --- /dev/null +++ b/src/org/apache/commons/lang3/mutable/Mutable.java @@ -0,0 +1,54 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.lang3.mutable;
+
+/**
+ * Provides mutable access to a value.
+ * <p>
+ * <code>Mutable</code> is used as a generic interface to the implementations in this package.
+ * <p>
+ * A typical use case would be to enable a primitive or string to be passed to a method and allow that method to
+ * effectively change the value of the primitive/string. Another use case is to store a frequently changing primitive in
+ * a collection (for example a total in a map) without needing to create new Integer/Long wrapper objects.
+ *
+ * @since 2.1
+ * @param <T> the type to set and get
+ * @version $Id: Mutable.java 1153213 2011-08-02 17:35:39Z ggregory $
+ */
+public interface Mutable<T> {
+
+ /**
+ * Gets the value of this mutable.
+ *
+ * @return the stored value
+ */
+ T getValue();
+
+ /**
+ * Sets the value of this mutable.
+ *
+ * @param value
+ * the value to store
+ * @throws NullPointerException
+ * if the object is null and null is invalid
+ * @throws ClassCastException
+ * if the type is invalid
+ */
+ void setValue(T value);
+
+}
diff --git a/src/org/apache/commons/lang3/mutable/MutableBoolean.java b/src/org/apache/commons/lang3/mutable/MutableBoolean.java new file mode 100644 index 0000000..f2e0a48 --- /dev/null +++ b/src/org/apache/commons/lang3/mutable/MutableBoolean.java @@ -0,0 +1,193 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.lang3.mutable;
+
+import java.io.Serializable;
+
+/**
+ * A mutable <code>boolean</code> wrapper.
+ * <p>
+ * Note that as MutableBoolean does not extend Boolean, it is not treated by String.format as a Boolean parameter.
+ *
+ * @see Boolean
+ * @since 2.2
+ * @version $Id: MutableBoolean.java 1160571 2011-08-23 07:36:08Z bayard $
+ */
+public class MutableBoolean implements Mutable<Boolean>, Serializable, Comparable<MutableBoolean> {
+
+ /**
+ * Required for serialization support.
+ *
+ * @see java.io.Serializable
+ */
+ private static final long serialVersionUID = -4830728138360036487L;
+
+ /** The mutable value. */
+ private boolean value;
+
+ /**
+ * Constructs a new MutableBoolean with the default value of false.
+ */
+ public MutableBoolean() {
+ super();
+ }
+
+ /**
+ * Constructs a new MutableBoolean with the specified value.
+ *
+ * @param value the initial value to store
+ */
+ public MutableBoolean(boolean value) {
+ super();
+ this.value = value;
+ }
+
+ /**
+ * Constructs a new MutableBoolean with the specified value.
+ *
+ * @param value the initial value to store, not null
+ * @throws NullPointerException if the object is null
+ */
+ public MutableBoolean(Boolean value) {
+ super();
+ this.value = value.booleanValue();
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets the value as a Boolean instance.
+ *
+ * @return the value as a Boolean, never null
+ */
+ public Boolean getValue() {
+ return Boolean.valueOf(this.value);
+ }
+
+ /**
+ * Sets the value.
+ *
+ * @param value the value to set
+ */
+ public void setValue(boolean value) {
+ this.value = value;
+ }
+
+ /**
+ * Sets the value from any Boolean instance.
+ *
+ * @param value the value to set, not null
+ * @throws NullPointerException if the object is null
+ */
+ public void setValue(Boolean value) {
+ this.value = value.booleanValue();
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Checks if the current value is <code>true</code>.
+ *
+ * @return <code>true</code> if the current value is <code>true</code>
+ * @since 2.5
+ */
+ public boolean isTrue() {
+ return value == true;
+ }
+
+ /**
+ * Checks if the current value is <code>false</code>.
+ *
+ * @return <code>true</code> if the current value is <code>false</code>
+ * @since 2.5
+ */
+ public boolean isFalse() {
+ return value == false;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Returns the value of this MutableBoolean as a boolean.
+ *
+ * @return the boolean value represented by this object.
+ */
+ public boolean booleanValue() {
+ return value;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets this mutable as an instance of Boolean.
+ *
+ * @return a Boolean instance containing the value from this mutable, never null
+ * @since 2.5
+ */
+ public Boolean toBoolean() {
+ return Boolean.valueOf(booleanValue());
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is
+ * not <code>null</code> and is an <code>MutableBoolean</code> object that contains the same
+ * <code>boolean</code> value as this object.
+ *
+ * @param obj the object to compare with, null returns false
+ * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (obj instanceof MutableBoolean) {
+ return value == ((MutableBoolean) obj).booleanValue();
+ }
+ return false;
+ }
+
+ /**
+ * Returns a suitable hash code for this mutable.
+ *
+ * @return the hash code returned by <code>Boolean.TRUE</code> or <code>Boolean.FALSE</code>
+ */
+ @Override
+ public int hashCode() {
+ return value ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode();
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Compares this mutable to another in ascending order.
+ *
+ * @param other the other mutable to compare to, not null
+ * @return negative if this is less, zero if equal, positive if greater
+ * where false is less than true
+ */
+ public int compareTo(MutableBoolean other) {
+ boolean anotherVal = other.value;
+ return value == anotherVal ? 0 : (value ? 1 : -1);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Returns the String value of this mutable.
+ *
+ * @return the mutable value as a string
+ */
+ @Override
+ public String toString() {
+ return String.valueOf(value);
+ }
+
+}
diff --git a/src/org/apache/commons/lang3/mutable/MutableByte.java b/src/org/apache/commons/lang3/mutable/MutableByte.java new file mode 100644 index 0000000..129d86d --- /dev/null +++ b/src/org/apache/commons/lang3/mutable/MutableByte.java @@ -0,0 +1,283 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3.mutable;
+
+/**
+ * A mutable <code>byte</code> wrapper.
+ * <p>
+ * Note that as MutableByte does not extend Byte, it is not treated by String.format as a Byte parameter.
+ *
+ * @see Byte
+ * @since 2.1
+ * @version $Id: MutableByte.java 1160571 2011-08-23 07:36:08Z bayard $
+ */
+public class MutableByte extends Number implements Comparable<MutableByte>, Mutable<Number> {
+
+ /**
+ * Required for serialization support.
+ *
+ * @see java.io.Serializable
+ */
+ private static final long serialVersionUID = -1585823265L;
+
+ /** The mutable value. */
+ private byte value;
+
+ /**
+ * Constructs a new MutableByte with the default value of zero.
+ */
+ public MutableByte() {
+ super();
+ }
+
+ /**
+ * Constructs a new MutableByte with the specified value.
+ *
+ * @param value the initial value to store
+ */
+ public MutableByte(byte value) {
+ super();
+ this.value = value;
+ }
+
+ /**
+ * Constructs a new MutableByte with the specified value.
+ *
+ * @param value the initial value to store, not null
+ * @throws NullPointerException if the object is null
+ */
+ public MutableByte(Number value) {
+ super();
+ this.value = value.byteValue();
+ }
+
+ /**
+ * Constructs a new MutableByte parsing the given string.
+ *
+ * @param value the string to parse, not null
+ * @throws NumberFormatException if the string cannot be parsed into a byte
+ * @since 2.5
+ */
+ public MutableByte(String value) throws NumberFormatException {
+ super();
+ this.value = Byte.parseByte(value);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets the value as a Byte instance.
+ *
+ * @return the value as a Byte, never null
+ */
+ public Byte getValue() {
+ return Byte.valueOf(this.value);
+ }
+
+ /**
+ * Sets the value.
+ *
+ * @param value the value to set
+ */
+ public void setValue(byte value) {
+ this.value = value;
+ }
+
+ /**
+ * Sets the value from any Number instance.
+ *
+ * @param value the value to set, not null
+ * @throws NullPointerException if the object is null
+ */
+ public void setValue(Number value) {
+ this.value = value.byteValue();
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Increments the value.
+ *
+ * @since Commons Lang 2.2
+ */
+ public void increment() {
+ value++;
+ }
+
+ /**
+ * Decrements the value.
+ *
+ * @since Commons Lang 2.2
+ */
+ public void decrement() {
+ value--;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Adds a value to the value of this instance.
+ *
+ * @param operand the value to add, not null
+ * @since Commons Lang 2.2
+ */
+ public void add(byte operand) {
+ this.value += operand;
+ }
+
+ /**
+ * Adds a value to the value of this instance.
+ *
+ * @param operand the value to add, not null
+ * @throws NullPointerException if the object is null
+ * @since Commons Lang 2.2
+ */
+ public void add(Number operand) {
+ this.value += operand.byteValue();
+ }
+
+ /**
+ * Subtracts a value from the value of this instance.
+ *
+ * @param operand the value to subtract, not null
+ * @since Commons Lang 2.2
+ */
+ public void subtract(byte operand) {
+ this.value -= operand;
+ }
+
+ /**
+ * Subtracts a value from the value of this instance.
+ *
+ * @param operand the value to subtract, not null
+ * @throws NullPointerException if the object is null
+ * @since Commons Lang 2.2
+ */
+ public void subtract(Number operand) {
+ this.value -= operand.byteValue();
+ }
+
+ //-----------------------------------------------------------------------
+ // shortValue relies on Number implementation
+ /**
+ * Returns the value of this MutableByte as a byte.
+ *
+ * @return the numeric value represented by this object after conversion to type byte.
+ */
+ @Override
+ public byte byteValue() {
+ return value;
+ }
+
+ /**
+ * Returns the value of this MutableByte as an int.
+ *
+ * @return the numeric value represented by this object after conversion to type int.
+ */
+ @Override
+ public int intValue() {
+ return value;
+ }
+
+ /**
+ * Returns the value of this MutableByte as a long.
+ *
+ * @return the numeric value represented by this object after conversion to type long.
+ */
+ @Override
+ public long longValue() {
+ return value;
+ }
+
+ /**
+ * Returns the value of this MutableByte as a float.
+ *
+ * @return the numeric value represented by this object after conversion to type float.
+ */
+ @Override
+ public float floatValue() {
+ return value;
+ }
+
+ /**
+ * Returns the value of this MutableByte as a double.
+ *
+ * @return the numeric value represented by this object after conversion to type double.
+ */
+ @Override
+ public double doubleValue() {
+ return value;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets this mutable as an instance of Byte.
+ *
+ * @return a Byte instance containing the value from this mutable
+ */
+ public Byte toByte() {
+ return Byte.valueOf(byteValue());
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is
+ * not <code>null</code> and is a <code>MutableByte</code> object that contains the same <code>byte</code> value
+ * as this object.
+ *
+ * @param obj the object to compare with, null returns false
+ * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (obj instanceof MutableByte) {
+ return value == ((MutableByte) obj).byteValue();
+ }
+ return false;
+ }
+
+ /**
+ * Returns a suitable hash code for this mutable.
+ *
+ * @return a suitable hash code
+ */
+ @Override
+ public int hashCode() {
+ return value;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Compares this mutable to another in ascending order.
+ *
+ * @param other the other mutable to compare to, not null
+ * @return negative if this is less, zero if equal, positive if greater
+ */
+ public int compareTo(MutableByte other) {
+ byte anotherVal = other.value;
+ return value < anotherVal ? -1 : (value == anotherVal ? 0 : 1);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Returns the String value of this mutable.
+ *
+ * @return the mutable value as a string
+ */
+ @Override
+ public String toString() {
+ return String.valueOf(value);
+ }
+
+}
diff --git a/src/org/apache/commons/lang3/mutable/MutableDouble.java b/src/org/apache/commons/lang3/mutable/MutableDouble.java new file mode 100644 index 0000000..f4acfa9 --- /dev/null +++ b/src/org/apache/commons/lang3/mutable/MutableDouble.java @@ -0,0 +1,312 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3.mutable;
+
+/**
+ * A mutable <code>double</code> wrapper.
+ * <p>
+ * Note that as MutableDouble does not extend Double, it is not treated by String.format as a Double parameter.
+ *
+ * @see Double
+ * @since 2.1
+ * @version $Id: MutableDouble.java 1160571 2011-08-23 07:36:08Z bayard $
+ */
+public class MutableDouble extends Number implements Comparable<MutableDouble>, Mutable<Number> {
+
+ /**
+ * Required for serialization support.
+ *
+ * @see java.io.Serializable
+ */
+ private static final long serialVersionUID = 1587163916L;
+
+ /** The mutable value. */
+ private double value;
+
+ /**
+ * Constructs a new MutableDouble with the default value of zero.
+ */
+ public MutableDouble() {
+ super();
+ }
+
+ /**
+ * Constructs a new MutableDouble with the specified value.
+ *
+ * @param value the initial value to store
+ */
+ public MutableDouble(double value) {
+ super();
+ this.value = value;
+ }
+
+ /**
+ * Constructs a new MutableDouble with the specified value.
+ *
+ * @param value the initial value to store, not null
+ * @throws NullPointerException if the object is null
+ */
+ public MutableDouble(Number value) {
+ super();
+ this.value = value.doubleValue();
+ }
+
+ /**
+ * Constructs a new MutableDouble parsing the given string.
+ *
+ * @param value the string to parse, not null
+ * @throws NumberFormatException if the string cannot be parsed into a double
+ * @since 2.5
+ */
+ public MutableDouble(String value) throws NumberFormatException {
+ super();
+ this.value = Double.parseDouble(value);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets the value as a Double instance.
+ *
+ * @return the value as a Double, never null
+ */
+ public Double getValue() {
+ return Double.valueOf(this.value);
+ }
+
+ /**
+ * Sets the value.
+ *
+ * @param value the value to set
+ */
+ public void setValue(double value) {
+ this.value = value;
+ }
+
+ /**
+ * Sets the value from any Number instance.
+ *
+ * @param value the value to set, not null
+ * @throws NullPointerException if the object is null
+ */
+ public void setValue(Number value) {
+ this.value = value.doubleValue();
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Checks whether the double value is the special NaN value.
+ *
+ * @return true if NaN
+ */
+ public boolean isNaN() {
+ return Double.isNaN(value);
+ }
+
+ /**
+ * Checks whether the double value is infinite.
+ *
+ * @return true if infinite
+ */
+ public boolean isInfinite() {
+ return Double.isInfinite(value);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Increments the value.
+ *
+ * @since Commons Lang 2.2
+ */
+ public void increment() {
+ value++;
+ }
+
+ /**
+ * Decrements the value.
+ *
+ * @since Commons Lang 2.2
+ */
+ public void decrement() {
+ value--;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Adds a value to the value of this instance.
+ *
+ * @param operand the value to add
+ * @since Commons Lang 2.2
+ */
+ public void add(double operand) {
+ this.value += operand;
+ }
+
+ /**
+ * Adds a value to the value of this instance.
+ *
+ * @param operand the value to add, not null
+ * @throws NullPointerException if the object is null
+ * @since Commons Lang 2.2
+ */
+ public void add(Number operand) {
+ this.value += operand.doubleValue();
+ }
+
+ /**
+ * Subtracts a value from the value of this instance.
+ *
+ * @param operand the value to subtract, not null
+ * @since Commons Lang 2.2
+ */
+ public void subtract(double operand) {
+ this.value -= operand;
+ }
+
+ /**
+ * Subtracts a value from the value of this instance.
+ *
+ * @param operand the value to subtract, not null
+ * @throws NullPointerException if the object is null
+ * @since Commons Lang 2.2
+ */
+ public void subtract(Number operand) {
+ this.value -= operand.doubleValue();
+ }
+
+ //-----------------------------------------------------------------------
+ // shortValue and byteValue rely on Number implementation
+ /**
+ * Returns the value of this MutableDouble as an int.
+ *
+ * @return the numeric value represented by this object after conversion to type int.
+ */
+ @Override
+ public int intValue() {
+ return (int) value;
+ }
+
+ /**
+ * Returns the value of this MutableDouble as a long.
+ *
+ * @return the numeric value represented by this object after conversion to type long.
+ */
+ @Override
+ public long longValue() {
+ return (long) value;
+ }
+
+ /**
+ * Returns the value of this MutableDouble as a float.
+ *
+ * @return the numeric value represented by this object after conversion to type float.
+ */
+ @Override
+ public float floatValue() {
+ return (float) value;
+ }
+
+ /**
+ * Returns the value of this MutableDouble as a double.
+ *
+ * @return the numeric value represented by this object after conversion to type double.
+ */
+ @Override
+ public double doubleValue() {
+ return value;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets this mutable as an instance of Double.
+ *
+ * @return a Double instance containing the value from this mutable, never null
+ */
+ public Double toDouble() {
+ return Double.valueOf(doubleValue());
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Compares this object against the specified object. The result is <code>true</code> if and only if the argument
+ * is not <code>null</code> and is a <code>Double</code> object that represents a double that has the identical
+ * bit pattern to the bit pattern of the double represented by this object. For this purpose, two
+ * <code>double</code> values are considered to be the same if and only if the method
+ * {@link Double#doubleToLongBits(double)}returns the same long value when applied to each.
+ * <p>
+ * Note that in most cases, for two instances of class <code>Double</code>,<code>d1</code> and <code>d2</code>,
+ * the value of <code>d1.equals(d2)</code> is <code>true</code> if and only if <blockquote>
+ *
+ * <pre>
+ * d1.doubleValue() == d2.doubleValue()
+ * </pre>
+ *
+ * </blockquote>
+ * <p>
+ * also has the value <code>true</code>. However, there are two exceptions:
+ * <ul>
+ * <li>If <code>d1</code> and <code>d2</code> both represent <code>Double.NaN</code>, then the
+ * <code>equals</code> method returns <code>true</code>, even though <code>Double.NaN==Double.NaN</code> has
+ * the value <code>false</code>.
+ * <li>If <code>d1</code> represents <code>+0.0</code> while <code>d2</code> represents <code>-0.0</code>,
+ * or vice versa, the <code>equal</code> test has the value <code>false</code>, even though
+ * <code>+0.0==-0.0</code> has the value <code>true</code>. This allows hashtables to operate properly.
+ * </ul>
+ *
+ * @param obj the object to compare with, null returns false
+ * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ return (obj instanceof MutableDouble)
+ && (Double.doubleToLongBits(((MutableDouble) obj).value) == Double.doubleToLongBits(value));
+ }
+
+ /**
+ * Returns a suitable hash code for this mutable.
+ *
+ * @return a suitable hash code
+ */
+ @Override
+ public int hashCode() {
+ long bits = Double.doubleToLongBits(value);
+ return (int) (bits ^ (bits >>> 32));
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Compares this mutable to another in ascending order.
+ *
+ * @param other the other mutable to compare to, not null
+ * @return negative if this is less, zero if equal, positive if greater
+ */
+ public int compareTo(MutableDouble other) {
+ double anotherVal = other.value;
+ return Double.compare(value, anotherVal);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Returns the String value of this mutable.
+ *
+ * @return the mutable value as a string
+ */
+ @Override
+ public String toString() {
+ return String.valueOf(value);
+ }
+
+}
diff --git a/src/org/apache/commons/lang3/mutable/MutableFloat.java b/src/org/apache/commons/lang3/mutable/MutableFloat.java new file mode 100644 index 0000000..b7e0cd0 --- /dev/null +++ b/src/org/apache/commons/lang3/mutable/MutableFloat.java @@ -0,0 +1,313 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3.mutable;
+
+/**
+ * A mutable <code>float</code> wrapper.
+ * <p>
+ * Note that as MutableFloat does not extend Float, it is not treated by String.format as a Float parameter.
+ *
+ * @see Float
+ * @since 2.1
+ * @version $Id: MutableFloat.java 1160571 2011-08-23 07:36:08Z bayard $
+ */
+public class MutableFloat extends Number implements Comparable<MutableFloat>, Mutable<Number> {
+
+ /**
+ * Required for serialization support.
+ *
+ * @see java.io.Serializable
+ */
+ private static final long serialVersionUID = 5787169186L;
+
+ /** The mutable value. */
+ private float value;
+
+ /**
+ * Constructs a new MutableFloat with the default value of zero.
+ */
+ public MutableFloat() {
+ super();
+ }
+
+ /**
+ * Constructs a new MutableFloat with the specified value.
+ *
+ * @param value the initial value to store
+ */
+ public MutableFloat(float value) {
+ super();
+ this.value = value;
+ }
+
+ /**
+ * Constructs a new MutableFloat with the specified value.
+ *
+ * @param value the initial value to store, not null
+ * @throws NullPointerException if the object is null
+ */
+ public MutableFloat(Number value) {
+ super();
+ this.value = value.floatValue();
+ }
+
+ /**
+ * Constructs a new MutableFloat parsing the given string.
+ *
+ * @param value the string to parse, not null
+ * @throws NumberFormatException if the string cannot be parsed into a float
+ * @since 2.5
+ */
+ public MutableFloat(String value) throws NumberFormatException {
+ super();
+ this.value = Float.parseFloat(value);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets the value as a Float instance.
+ *
+ * @return the value as a Float, never null
+ */
+ public Float getValue() {
+ return Float.valueOf(this.value);
+ }
+
+ /**
+ * Sets the value.
+ *
+ * @param value the value to set
+ */
+ public void setValue(float value) {
+ this.value = value;
+ }
+
+ /**
+ * Sets the value from any Number instance.
+ *
+ * @param value the value to set, not null
+ * @throws NullPointerException if the object is null
+ */
+ public void setValue(Number value) {
+ this.value = value.floatValue();
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Checks whether the float value is the special NaN value.
+ *
+ * @return true if NaN
+ */
+ public boolean isNaN() {
+ return Float.isNaN(value);
+ }
+
+ /**
+ * Checks whether the float value is infinite.
+ *
+ * @return true if infinite
+ */
+ public boolean isInfinite() {
+ return Float.isInfinite(value);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Increments the value.
+ *
+ * @since Commons Lang 2.2
+ */
+ public void increment() {
+ value++;
+ }
+
+ /**
+ * Decrements the value.
+ *
+ * @since Commons Lang 2.2
+ */
+ public void decrement() {
+ value--;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Adds a value to the value of this instance.
+ *
+ * @param operand the value to add, not null
+ * @since Commons Lang 2.2
+ */
+ public void add(float operand) {
+ this.value += operand;
+ }
+
+ /**
+ * Adds a value to the value of this instance.
+ *
+ * @param operand the value to add, not null
+ * @throws NullPointerException if the object is null
+ * @since Commons Lang 2.2
+ */
+ public void add(Number operand) {
+ this.value += operand.floatValue();
+ }
+
+ /**
+ * Subtracts a value from the value of this instance.
+ *
+ * @param operand the value to subtract
+ * @since Commons Lang 2.2
+ */
+ public void subtract(float operand) {
+ this.value -= operand;
+ }
+
+ /**
+ * Subtracts a value from the value of this instance.
+ *
+ * @param operand the value to subtract, not null
+ * @throws NullPointerException if the object is null
+ * @since Commons Lang 2.2
+ */
+ public void subtract(Number operand) {
+ this.value -= operand.floatValue();
+ }
+
+ //-----------------------------------------------------------------------
+ // shortValue and byteValue rely on Number implementation
+ /**
+ * Returns the value of this MutableFloat as an int.
+ *
+ * @return the numeric value represented by this object after conversion to type int.
+ */
+ @Override
+ public int intValue() {
+ return (int) value;
+ }
+
+ /**
+ * Returns the value of this MutableFloat as a long.
+ *
+ * @return the numeric value represented by this object after conversion to type long.
+ */
+ @Override
+ public long longValue() {
+ return (long) value;
+ }
+
+ /**
+ * Returns the value of this MutableFloat as a float.
+ *
+ * @return the numeric value represented by this object after conversion to type float.
+ */
+ @Override
+ public float floatValue() {
+ return value;
+ }
+
+ /**
+ * Returns the value of this MutableFloat as a double.
+ *
+ * @return the numeric value represented by this object after conversion to type double.
+ */
+ @Override
+ public double doubleValue() {
+ return value;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets this mutable as an instance of Float.
+ *
+ * @return a Float instance containing the value from this mutable, never null
+ */
+ public Float toFloat() {
+ return Float.valueOf(floatValue());
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Compares this object against some other object. The result is <code>true</code> if and only if the argument is
+ * not <code>null</code> and is a <code>Float</code> object that represents a <code>float</code> that has the
+ * identical bit pattern to the bit pattern of the <code>float</code> represented by this object. For this
+ * purpose, two float values are considered to be the same if and only if the method
+ * {@link Float#floatToIntBits(float)}returns the same int value when applied to each.
+ * <p>
+ * Note that in most cases, for two instances of class <code>Float</code>,<code>f1</code> and <code>f2</code>,
+ * the value of <code>f1.equals(f2)</code> is <code>true</code> if and only if <blockquote>
+ *
+ * <pre>
+ * f1.floatValue() == f2.floatValue()
+ * </pre>
+ *
+ * </blockquote>
+ * <p>
+ * also has the value <code>true</code>. However, there are two exceptions:
+ * <ul>
+ * <li>If <code>f1</code> and <code>f2</code> both represent <code>Float.NaN</code>, then the
+ * <code>equals</code> method returns <code>true</code>, even though <code>Float.NaN==Float.NaN</code> has
+ * the value <code>false</code>.
+ * <li>If <code>f1</code> represents <code>+0.0f</code> while <code>f2</code> represents <code>-0.0f</code>,
+ * or vice versa, the <code>equal</code> test has the value <code>false</code>, even though
+ * <code>0.0f==-0.0f</code> has the value <code>true</code>.
+ * </ul>
+ * This definition allows hashtables to operate properly.
+ *
+ * @param obj the object to compare with, null returns false
+ * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
+ * @see java.lang.Float#floatToIntBits(float)
+ */
+ @Override
+ public boolean equals(Object obj) {
+ return (obj instanceof MutableFloat)
+ && (Float.floatToIntBits(((MutableFloat) obj).value) == Float.floatToIntBits(value));
+ }
+
+ /**
+ * Returns a suitable hash code for this mutable.
+ *
+ * @return a suitable hash code
+ */
+ @Override
+ public int hashCode() {
+ return Float.floatToIntBits(value);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Compares this mutable to another in ascending order.
+ *
+ * @param other the other mutable to compare to, not null
+ * @return negative if this is less, zero if equal, positive if greater
+ */
+ public int compareTo(MutableFloat other) {
+ float anotherVal = other.value;
+ return Float.compare(value, anotherVal);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Returns the String value of this mutable.
+ *
+ * @return the mutable value as a string
+ */
+ @Override
+ public String toString() {
+ return String.valueOf(value);
+ }
+
+}
diff --git a/src/org/apache/commons/lang3/mutable/MutableInt.java b/src/org/apache/commons/lang3/mutable/MutableInt.java new file mode 100644 index 0000000..eb12dad --- /dev/null +++ b/src/org/apache/commons/lang3/mutable/MutableInt.java @@ -0,0 +1,273 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3.mutable;
+
+/**
+ * A mutable <code>int</code> wrapper.
+ * <p>
+ * Note that as MutableInt does not extend Integer, it is not treated by String.format as an Integer parameter.
+ *
+ * @see Integer
+ * @since 2.1
+ * @version $Id: MutableInt.java 1160571 2011-08-23 07:36:08Z bayard $
+ */
+public class MutableInt extends Number implements Comparable<MutableInt>, Mutable<Number> {
+
+ /**
+ * Required for serialization support.
+ *
+ * @see java.io.Serializable
+ */
+ private static final long serialVersionUID = 512176391864L;
+
+ /** The mutable value. */
+ private int value;
+
+ /**
+ * Constructs a new MutableInt with the default value of zero.
+ */
+ public MutableInt() {
+ super();
+ }
+
+ /**
+ * Constructs a new MutableInt with the specified value.
+ *
+ * @param value the initial value to store
+ */
+ public MutableInt(int value) {
+ super();
+ this.value = value;
+ }
+
+ /**
+ * Constructs a new MutableInt with the specified value.
+ *
+ * @param value the initial value to store, not null
+ * @throws NullPointerException if the object is null
+ */
+ public MutableInt(Number value) {
+ super();
+ this.value = value.intValue();
+ }
+
+ /**
+ * Constructs a new MutableInt parsing the given string.
+ *
+ * @param value the string to parse, not null
+ * @throws NumberFormatException if the string cannot be parsed into an int
+ * @since 2.5
+ */
+ public MutableInt(String value) throws NumberFormatException {
+ super();
+ this.value = Integer.parseInt(value);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets the value as a Integer instance.
+ *
+ * @return the value as a Integer, never null
+ */
+ public Integer getValue() {
+ return Integer.valueOf(this.value);
+ }
+
+ /**
+ * Sets the value.
+ *
+ * @param value the value to set
+ */
+ public void setValue(int value) {
+ this.value = value;
+ }
+
+ /**
+ * Sets the value from any Number instance.
+ *
+ * @param value the value to set, not null
+ * @throws NullPointerException if the object is null
+ */
+ public void setValue(Number value) {
+ this.value = value.intValue();
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Increments the value.
+ *
+ * @since Commons Lang 2.2
+ */
+ public void increment() {
+ value++;
+ }
+
+ /**
+ * Decrements the value.
+ *
+ * @since Commons Lang 2.2
+ */
+ public void decrement() {
+ value--;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Adds a value to the value of this instance.
+ *
+ * @param operand the value to add, not null
+ * @since Commons Lang 2.2
+ */
+ public void add(int operand) {
+ this.value += operand;
+ }
+
+ /**
+ * Adds a value to the value of this instance.
+ *
+ * @param operand the value to add, not null
+ * @throws NullPointerException if the object is null
+ * @since Commons Lang 2.2
+ */
+ public void add(Number operand) {
+ this.value += operand.intValue();
+ }
+
+ /**
+ * Subtracts a value from the value of this instance.
+ *
+ * @param operand the value to subtract, not null
+ * @since Commons Lang 2.2
+ */
+ public void subtract(int operand) {
+ this.value -= operand;
+ }
+
+ /**
+ * Subtracts a value from the value of this instance.
+ *
+ * @param operand the value to subtract, not null
+ * @throws NullPointerException if the object is null
+ * @since Commons Lang 2.2
+ */
+ public void subtract(Number operand) {
+ this.value -= operand.intValue();
+ }
+
+ //-----------------------------------------------------------------------
+ // shortValue and byteValue rely on Number implementation
+ /**
+ * Returns the value of this MutableInt as an int.
+ *
+ * @return the numeric value represented by this object after conversion to type int.
+ */
+ @Override
+ public int intValue() {
+ return value;
+ }
+
+ /**
+ * Returns the value of this MutableInt as a long.
+ *
+ * @return the numeric value represented by this object after conversion to type long.
+ */
+ @Override
+ public long longValue() {
+ return value;
+ }
+
+ /**
+ * Returns the value of this MutableInt as a float.
+ *
+ * @return the numeric value represented by this object after conversion to type float.
+ */
+ @Override
+ public float floatValue() {
+ return value;
+ }
+
+ /**
+ * Returns the value of this MutableInt as a double.
+ *
+ * @return the numeric value represented by this object after conversion to type double.
+ */
+ @Override
+ public double doubleValue() {
+ return value;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets this mutable as an instance of Integer.
+ *
+ * @return a Integer instance containing the value from this mutable, never null
+ */
+ public Integer toInteger() {
+ return Integer.valueOf(intValue());
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is
+ * not <code>null</code> and is a <code>MutableInt</code> object that contains the same <code>int</code> value
+ * as this object.
+ *
+ * @param obj the object to compare with, null returns false
+ * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (obj instanceof MutableInt) {
+ return value == ((MutableInt) obj).intValue();
+ }
+ return false;
+ }
+
+ /**
+ * Returns a suitable hash code for this mutable.
+ *
+ * @return a suitable hash code
+ */
+ @Override
+ public int hashCode() {
+ return value;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Compares this mutable to another in ascending order.
+ *
+ * @param other the other mutable to compare to, not null
+ * @return negative if this is less, zero if equal, positive if greater
+ */
+ public int compareTo(MutableInt other) {
+ int anotherVal = other.value;
+ return value < anotherVal ? -1 : (value == anotherVal ? 0 : 1);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Returns the String value of this mutable.
+ *
+ * @return the mutable value as a string
+ */
+ @Override
+ public String toString() {
+ return String.valueOf(value);
+ }
+
+}
diff --git a/src/org/apache/commons/lang3/mutable/MutableLong.java b/src/org/apache/commons/lang3/mutable/MutableLong.java new file mode 100644 index 0000000..eea862d --- /dev/null +++ b/src/org/apache/commons/lang3/mutable/MutableLong.java @@ -0,0 +1,273 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3.mutable;
+
+/**
+ * A mutable <code>long</code> wrapper.
+ * <p>
+ * Note that as MutableLong does not extend Long, it is not treated by String.format as a Long parameter.
+ *
+ * @see Long
+ * @since 2.1
+ * @version $Id: MutableLong.java 1160571 2011-08-23 07:36:08Z bayard $
+ */
+public class MutableLong extends Number implements Comparable<MutableLong>, Mutable<Number> {
+
+ /**
+ * Required for serialization support.
+ *
+ * @see java.io.Serializable
+ */
+ private static final long serialVersionUID = 62986528375L;
+
+ /** The mutable value. */
+ private long value;
+
+ /**
+ * Constructs a new MutableLong with the default value of zero.
+ */
+ public MutableLong() {
+ super();
+ }
+
+ /**
+ * Constructs a new MutableLong with the specified value.
+ *
+ * @param value the initial value to store
+ */
+ public MutableLong(long value) {
+ super();
+ this.value = value;
+ }
+
+ /**
+ * Constructs a new MutableLong with the specified value.
+ *
+ * @param value the initial value to store, not null
+ * @throws NullPointerException if the object is null
+ */
+ public MutableLong(Number value) {
+ super();
+ this.value = value.longValue();
+ }
+
+ /**
+ * Constructs a new MutableLong parsing the given string.
+ *
+ * @param value the string to parse, not null
+ * @throws NumberFormatException if the string cannot be parsed into a long
+ * @since 2.5
+ */
+ public MutableLong(String value) throws NumberFormatException {
+ super();
+ this.value = Long.parseLong(value);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets the value as a Long instance.
+ *
+ * @return the value as a Long, never null
+ */
+ public Long getValue() {
+ return Long.valueOf(this.value);
+ }
+
+ /**
+ * Sets the value.
+ *
+ * @param value the value to set
+ */
+ public void setValue(long value) {
+ this.value = value;
+ }
+
+ /**
+ * Sets the value from any Number instance.
+ *
+ * @param value the value to set, not null
+ * @throws NullPointerException if the object is null
+ */
+ public void setValue(Number value) {
+ this.value = value.longValue();
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Increments the value.
+ *
+ * @since Commons Lang 2.2
+ */
+ public void increment() {
+ value++;
+ }
+
+ /**
+ * Decrements the value.
+ *
+ * @since Commons Lang 2.2
+ */
+ public void decrement() {
+ value--;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Adds a value to the value of this instance.
+ *
+ * @param operand the value to add, not null
+ * @since Commons Lang 2.2
+ */
+ public void add(long operand) {
+ this.value += operand;
+ }
+
+ /**
+ * Adds a value to the value of this instance.
+ *
+ * @param operand the value to add, not null
+ * @throws NullPointerException if the object is null
+ * @since Commons Lang 2.2
+ */
+ public void add(Number operand) {
+ this.value += operand.longValue();
+ }
+
+ /**
+ * Subtracts a value from the value of this instance.
+ *
+ * @param operand the value to subtract, not null
+ * @since Commons Lang 2.2
+ */
+ public void subtract(long operand) {
+ this.value -= operand;
+ }
+
+ /**
+ * Subtracts a value from the value of this instance.
+ *
+ * @param operand the value to subtract, not null
+ * @throws NullPointerException if the object is null
+ * @since Commons Lang 2.2
+ */
+ public void subtract(Number operand) {
+ this.value -= operand.longValue();
+ }
+
+ //-----------------------------------------------------------------------
+ // shortValue and byteValue rely on Number implementation
+ /**
+ * Returns the value of this MutableLong as an int.
+ *
+ * @return the numeric value represented by this object after conversion to type int.
+ */
+ @Override
+ public int intValue() {
+ return (int) value;
+ }
+
+ /**
+ * Returns the value of this MutableLong as a long.
+ *
+ * @return the numeric value represented by this object after conversion to type long.
+ */
+ @Override
+ public long longValue() {
+ return value;
+ }
+
+ /**
+ * Returns the value of this MutableLong as a float.
+ *
+ * @return the numeric value represented by this object after conversion to type float.
+ */
+ @Override
+ public float floatValue() {
+ return value;
+ }
+
+ /**
+ * Returns the value of this MutableLong as a double.
+ *
+ * @return the numeric value represented by this object after conversion to type double.
+ */
+ @Override
+ public double doubleValue() {
+ return value;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets this mutable as an instance of Long.
+ *
+ * @return a Long instance containing the value from this mutable, never null
+ */
+ public Long toLong() {
+ return Long.valueOf(longValue());
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Compares this object to the specified object. The result is <code>true</code> if and only if the argument
+ * is not <code>null</code> and is a <code>MutableLong</code> object that contains the same <code>long</code>
+ * value as this object.
+ *
+ * @param obj the object to compare with, null returns false
+ * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (obj instanceof MutableLong) {
+ return value == ((MutableLong) obj).longValue();
+ }
+ return false;
+ }
+
+ /**
+ * Returns a suitable hash code for this mutable.
+ *
+ * @return a suitable hash code
+ */
+ @Override
+ public int hashCode() {
+ return (int) (value ^ (value >>> 32));
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Compares this mutable to another in ascending order.
+ *
+ * @param other the other mutable to compare to, not null
+ * @return negative if this is less, zero if equal, positive if greater
+ */
+ public int compareTo(MutableLong other) {
+ long anotherVal = other.value;
+ return value < anotherVal ? -1 : (value == anotherVal ? 0 : 1);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Returns the String value of this mutable.
+ *
+ * @return the mutable value as a string
+ */
+ @Override
+ public String toString() {
+ return String.valueOf(value);
+ }
+
+}
diff --git a/src/org/apache/commons/lang3/mutable/MutableObject.java b/src/org/apache/commons/lang3/mutable/MutableObject.java new file mode 100644 index 0000000..74c4f35 --- /dev/null +++ b/src/org/apache/commons/lang3/mutable/MutableObject.java @@ -0,0 +1,126 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.lang3.mutable;
+
+import java.io.Serializable;
+
+/**
+ * A mutable <code>Object</code> wrapper.
+ *
+ * @since 2.1
+ * @version $Id: MutableObject.java 1088899 2011-04-05 05:31:27Z bayard $
+ */
+public class MutableObject<T> implements Mutable<T>, Serializable {
+
+ /**
+ * Required for serialization support.
+ *
+ * @see java.io.Serializable
+ */
+ private static final long serialVersionUID = 86241875189L;
+
+ /** The mutable value. */
+ private T value;
+
+ /**
+ * Constructs a new MutableObject with the default value of <code>null</code>.
+ */
+ public MutableObject() {
+ super();
+ }
+
+ /**
+ * Constructs a new MutableObject with the specified value.
+ *
+ * @param value the initial value to store
+ */
+ public MutableObject(T value) {
+ super();
+ this.value = value;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets the value.
+ *
+ * @return the value, may be null
+ */
+ public T getValue() {
+ return this.value;
+ }
+
+ /**
+ * Sets the value.
+ *
+ * @param value the value to set
+ */
+ public void setValue(T value) {
+ this.value = value;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * <p>
+ * Compares this object against the specified object. The result is <code>true</code> if and only if the argument
+ * is not <code>null</code> and is a <code>MutableObject</code> object that contains the same <code>T</code>
+ * value as this object.
+ * </p>
+ *
+ * @param obj the object to compare with, <code>null</code> returns <code>false</code>
+ * @return <code>true</code> if the objects are the same;
+ * <code>true</code> if the objects have equivalent <code>value</code> fields;
+ * <code>false</code> otherwise.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == null) {
+ return false;
+ }
+ if (this == obj) {
+ return true;
+ }
+ if (this.getClass() == obj.getClass()) {
+ MutableObject<?> that = (MutableObject<?>) obj;
+ return this.value.equals(that.value);
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Returns the value's hash code or <code>0</code> if the value is <code>null</code>.
+ *
+ * @return the value's hash code or <code>0</code> if the value is <code>null</code>.
+ */
+ @Override
+ public int hashCode() {
+ return value == null ? 0 : value.hashCode();
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Returns the String value of this mutable.
+ *
+ * @return the mutable value as a string
+ */
+ @Override
+ public String toString() {
+ return value == null ? "null" : value.toString();
+ }
+
+}
diff --git a/src/org/apache/commons/lang3/mutable/MutableShort.java b/src/org/apache/commons/lang3/mutable/MutableShort.java new file mode 100644 index 0000000..2dfcb00 --- /dev/null +++ b/src/org/apache/commons/lang3/mutable/MutableShort.java @@ -0,0 +1,283 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.lang3.mutable;
+
+/**
+ * A mutable <code>short</code> wrapper.
+ * <p>
+ * Note that as MutableShort does not extend Short, it is not treated by String.format as a Short parameter.
+ *
+ * @see Short
+ * @since 2.1
+ * @version $Id: MutableShort.java 1160571 2011-08-23 07:36:08Z bayard $
+ */
+public class MutableShort extends Number implements Comparable<MutableShort>, Mutable<Number> {
+
+ /**
+ * Required for serialization support.
+ *
+ * @see java.io.Serializable
+ */
+ private static final long serialVersionUID = -2135791679L;
+
+ /** The mutable value. */
+ private short value;
+
+ /**
+ * Constructs a new MutableShort with the default value of zero.
+ */
+ public MutableShort() {
+ super();
+ }
+
+ /**
+ * Constructs a new MutableShort with the specified value.
+ *
+ * @param value the initial value to store
+ */
+ public MutableShort(short value) {
+ super();
+ this.value = value;
+ }
+
+ /**
+ * Constructs a new MutableShort with the specified value.
+ *
+ * @param value the initial value to store, not null
+ * @throws NullPointerException if the object is null
+ */
+ public MutableShort(Number value) {
+ super();
+ this.value = value.shortValue();
+ }
+
+ /**
+ * Constructs a new MutableShort parsing the given string.
+ *
+ * @param value the string to parse, not null
+ * @throws NumberFormatException if the string cannot be parsed into a short
+ * @since 2.5
+ */
+ public MutableShort(String value) throws NumberFormatException {
+ super();
+ this.value = Short.parseShort(value);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets the value as a Short instance.
+ *
+ * @return the value as a Short, never null
+ */
+ public Short getValue() {
+ return Short.valueOf(this.value);
+ }
+
+ /**
+ * Sets the value.
+ *
+ * @param value the value to set
+ */
+ public void setValue(short value) {
+ this.value = value;
+ }
+
+ /**
+ * Sets the value from any Number instance.
+ *
+ * @param value the value to set, not null
+ * @throws NullPointerException if the object is null
+ */
+ public void setValue(Number value) {
+ this.value = value.shortValue();
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Increments the value.
+ *
+ * @since Commons Lang 2.2
+ */
+ public void increment() {
+ value++;
+ }
+
+ /**
+ * Decrements the value.
+ *
+ * @since Commons Lang 2.2
+ */
+ public void decrement() {
+ value--;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Adds a value to the value of this instance.
+ *
+ * @param operand the value to add, not null
+ * @since Commons Lang 2.2
+ */
+ public void add(short operand) {
+ this.value += operand;
+ }
+
+ /**
+ * Adds a value to the value of this instance.
+ *
+ * @param operand the value to add, not null
+ * @throws NullPointerException if the object is null
+ * @since Commons Lang 2.2
+ */
+ public void add(Number operand) {
+ this.value += operand.shortValue();
+ }
+
+ /**
+ * Subtracts a value from the value of this instance.
+ *
+ * @param operand the value to subtract, not null
+ * @since Commons Lang 2.2
+ */
+ public void subtract(short operand) {
+ this.value -= operand;
+ }
+
+ /**
+ * Subtracts a value from the value of this instance.
+ *
+ * @param operand the value to subtract, not null
+ * @throws NullPointerException if the object is null
+ * @since Commons Lang 2.2
+ */
+ public void subtract(Number operand) {
+ this.value -= operand.shortValue();
+ }
+
+ //-----------------------------------------------------------------------
+ // byteValue relies on Number implementation
+ /**
+ * Returns the value of this MutableShort as a short.
+ *
+ * @return the numeric value represented by this object after conversion to type short.
+ */
+ @Override
+ public short shortValue() {
+ return value;
+ }
+
+ /**
+ * Returns the value of this MutableShort as an int.
+ *
+ * @return the numeric value represented by this object after conversion to type int.
+ */
+ @Override
+ public int intValue() {
+ return value;
+ }
+
+ /**
+ * Returns the value of this MutableShort as a long.
+ *
+ * @return the numeric value represented by this object after conversion to type long.
+ */
+ @Override
+ public long longValue() {
+ return value;
+ }
+
+ /**
+ * Returns the value of this MutableShort as a float.
+ *
+ * @return the numeric value represented by this object after conversion to type float.
+ */
+ @Override
+ public float floatValue() {
+ return value;
+ }
+
+ /**
+ * Returns the value of this MutableShort as a double.
+ *
+ * @return the numeric value represented by this object after conversion to type double.
+ */
+ @Override
+ public double doubleValue() {
+ return value;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets this mutable as an instance of Short.
+ *
+ * @return a Short instance containing the value from this mutable, never null
+ */
+ public Short toShort() {
+ return Short.valueOf(shortValue());
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Compares this object to the specified object. The result is <code>true</code> if and only if the argument
+ * is not <code>null</code> and is a <code>MutableShort</code> object that contains the same <code>short</code>
+ * value as this object.
+ *
+ * @param obj the object to compare with, null returns false
+ * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (obj instanceof MutableShort) {
+ return value == ((MutableShort) obj).shortValue();
+ }
+ return false;
+ }
+
+ /**
+ * Returns a suitable hash code for this mutable.
+ *
+ * @return a suitable hash code
+ */
+ @Override
+ public int hashCode() {
+ return value;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Compares this mutable to another in ascending order.
+ *
+ * @param other the other mutable to compare to, not null
+ * @return negative if this is less, zero if equal, positive if greater
+ */
+ public int compareTo(MutableShort other) {
+ short anotherVal = other.value;
+ return value < anotherVal ? -1 : (value == anotherVal ? 0 : 1);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Returns the String value of this mutable.
+ *
+ * @return the mutable value as a string
+ */
+ @Override
+ public String toString() {
+ return String.valueOf(value);
+ }
+
+}
|
