Update #0 - First Release

This commit is contained in:
LAX1DUDE
2022-12-25 01:12:28 -08:00
commit e7179fad45
2154 changed files with 256324 additions and 0 deletions

View File

@ -0,0 +1,53 @@
/*
* 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} 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.
*
* @param <T> the type to set and get
* @since 2.1
*/
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);
}

View File

@ -0,0 +1,213 @@
/*
* 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;
import org.apache.commons.lang3.BooleanUtils;
/**
* A mutable {@code boolean} wrapper.
* <p>
* Note that as MutableBoolean does not extend Boolean, it is not treated by
* HString.format as a Boolean parameter.
*
* @see Boolean
* @since 2.2
*/
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() {
}
/**
* Constructs a new MutableBoolean with the specified value.
*
* @param value the initial value to store
*/
public MutableBoolean(final boolean value) {
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(final Boolean value) {
this.value = value.booleanValue();
}
// -----------------------------------------------------------------------
/**
* Gets the value as a Boolean instance.
*
* @return the value as a Boolean, never null
*/
@Override
public Boolean getValue() {
return Boolean.valueOf(this.value);
}
/**
* Sets the value.
*
* @param value the value to set
*/
public void setValue(final boolean value) {
this.value = value;
}
/**
* Sets the value to false.
*
* @since 3.3
*/
public void setFalse() {
this.value = false;
}
/**
* Sets the value to true.
*
* @since 3.3
*/
public void setTrue() {
this.value = true;
}
/**
* Sets the value from any Boolean instance.
*
* @param value the value to set, not null
* @throws NullPointerException if the object is null
*/
@Override
public void setValue(final Boolean value) {
this.value = value.booleanValue();
}
// -----------------------------------------------------------------------
/**
* Checks if the current value is {@code true}.
*
* @return {@code true} if the current value is {@code true}
* @since 2.5
*/
public boolean isTrue() {
return value;
}
/**
* Checks if the current value is {@code false}.
*
* @return {@code true} if the current value is {@code false}
* @since 2.5
*/
public boolean isFalse() {
return !value;
}
// -----------------------------------------------------------------------
/**
* 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} if
* and only if the argument is not {@code null} and is an {@code MutableBoolean}
* object that contains the same {@code boolean} value as this object.
*
* @param obj the object to compare with, null returns false
* @return {@code true} if the objects are the same; {@code false} otherwise.
*/
@Override
public boolean equals(final 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} or
* {@code Boolean.FALSE}
*/
@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
*/
@Override
public int compareTo(final MutableBoolean other) {
return BooleanUtils.compare(this.value, other.value);
}
// -----------------------------------------------------------------------
/**
* Returns the String value of this mutable.
*
* @return the mutable value as a string
*/
@Override
public String toString() {
return String.valueOf(value);
}
}

View File

@ -0,0 +1,404 @@
/*
* 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 org.apache.commons.lang3.math.NumberUtils;
/**
* A mutable {@code byte} wrapper.
* <p>
* Note that as MutableByte does not extend Byte, it is not treated by
* HString.format as a Byte parameter.
*
* @see Byte
* @since 2.1
*/
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() {
}
/**
* Constructs a new MutableByte with the specified value.
*
* @param value the initial value to store
*/
public MutableByte(final byte value) {
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(final Number value) {
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(final String value) {
this.value = Byte.parseByte(value);
}
// -----------------------------------------------------------------------
/**
* Gets the value as a Byte instance.
*
* @return the value as a Byte, never null
*/
@Override
public Byte getValue() {
return Byte.valueOf(this.value);
}
/**
* Sets the value.
*
* @param value the value to set
*/
public void setValue(final 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
*/
@Override
public void setValue(final Number value) {
this.value = value.byteValue();
}
// -----------------------------------------------------------------------
/**
* Increments the value.
*
* @since 2.2
*/
public void increment() {
value++;
}
/**
* Increments this instance's value by 1; this method returns the value
* associated with the instance immediately prior to the increment operation.
* This method is not thread safe.
*
* @return the value associated with the instance before it was incremented
* @since 3.5
*/
public byte getAndIncrement() {
final byte last = value;
value++;
return last;
}
/**
* Increments this instance's value by 1; this method returns the value
* associated with the instance immediately after the increment operation. This
* method is not thread safe.
*
* @return the value associated with the instance after it is incremented
* @since 3.5
*/
public byte incrementAndGet() {
value++;
return value;
}
/**
* Decrements the value.
*
* @since 2.2
*/
public void decrement() {
value--;
}
/**
* Decrements this instance's value by 1; this method returns the value
* associated with the instance immediately prior to the decrement operation.
* This method is not thread safe.
*
* @return the value associated with the instance before it was decremented
* @since 3.5
*/
public byte getAndDecrement() {
final byte last = value;
value--;
return last;
}
/**
* Decrements this instance's value by 1; this method returns the value
* associated with the instance immediately after the decrement operation. This
* method is not thread safe.
*
* @return the value associated with the instance after it is decremented
* @since 3.5
*/
public byte decrementAndGet() {
value--;
return value;
}
// -----------------------------------------------------------------------
/**
* Adds a value to the value of this instance.
*
* @param operand the value to add, not null
* @since 2.2
*/
public void add(final 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 2.2
*/
public void add(final Number operand) {
this.value += operand.byteValue();
}
/**
* Subtracts a value from the value of this instance.
*
* @param operand the value to subtract, not null
* @since 2.2
*/
public void subtract(final 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 2.2
*/
public void subtract(final Number operand) {
this.value -= operand.byteValue();
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately after the addition operation.
* This method is not thread safe.
*
* @param operand the quantity to add, not null
* @return the value associated with this instance after adding the operand
* @since 3.5
*/
public byte addAndGet(final byte operand) {
this.value += operand;
return value;
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately after the addition operation.
* This method is not thread safe.
*
* @param operand the quantity to add, not null
* @throws NullPointerException if {@code operand} is null
* @return the value associated with this instance after adding the operand
* @since 3.5
*/
public byte addAndGet(final Number operand) {
this.value += operand.byteValue();
return value;
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately prior to the addition
* operation. This method is not thread safe.
*
* @param operand the quantity to add, not null
* @return the value associated with this instance immediately before the
* operand was added
* @since 3.5
*/
public byte getAndAdd(final byte operand) {
final byte last = value;
this.value += operand;
return last;
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately prior to the addition
* operation. This method is not thread safe.
*
* @param operand the quantity to add, not null
* @throws NullPointerException if {@code operand} is null
* @return the value associated with this instance immediately before the
* operand was added
* @since 3.5
*/
public byte getAndAdd(final Number operand) {
final byte last = value;
this.value += operand.byteValue();
return last;
}
// -----------------------------------------------------------------------
// 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} if
* and only if the argument is not {@code null} and is a {@code MutableByte}
* object that contains the same {@code byte} value as this object.
*
* @param obj the object to compare with, null returns false
* @return {@code true} if the objects are the same; {@code false} otherwise.
*/
@Override
public boolean equals(final 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
*/
@Override
public int compareTo(final MutableByte other) {
return NumberUtils.compare(this.value, other.value);
}
// -----------------------------------------------------------------------
/**
* Returns the String value of this mutable.
*
* @return the mutable value as a string
*/
@Override
public String toString() {
return String.valueOf(value);
}
}

View File

@ -0,0 +1,434 @@
/*
* 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} wrapper.
* <p>
* Note that as MutableDouble does not extend Double, it is not treated by
* HString.format as a Double parameter.
*
* @see Double
* @since 2.1
*/
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() {
}
/**
* Constructs a new MutableDouble with the specified value.
*
* @param value the initial value to store
*/
public MutableDouble(final double value) {
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(final Number value) {
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(final String value) {
this.value = Double.parseDouble(value);
}
// -----------------------------------------------------------------------
/**
* Gets the value as a Double instance.
*
* @return the value as a Double, never null
*/
@Override
public Double getValue() {
return Double.valueOf(this.value);
}
/**
* Sets the value.
*
* @param value the value to set
*/
public void setValue(final 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
*/
@Override
public void setValue(final 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 2.2
*/
public void increment() {
value++;
}
/**
* Increments this instance's value by 1; this method returns the value
* associated with the instance immediately prior to the increment operation.
* This method is not thread safe.
*
* @return the value associated with the instance before it was incremented
* @since 3.5
*/
public double getAndIncrement() {
final double last = value;
value++;
return last;
}
/**
* Increments this instance's value by 1; this method returns the value
* associated with the instance immediately after the increment operation. This
* method is not thread safe.
*
* @return the value associated with the instance after it is incremented
* @since 3.5
*/
public double incrementAndGet() {
value++;
return value;
}
/**
* Decrements the value.
*
* @since 2.2
*/
public void decrement() {
value--;
}
/**
* Decrements this instance's value by 1; this method returns the value
* associated with the instance immediately prior to the decrement operation.
* This method is not thread safe.
*
* @return the value associated with the instance before it was decremented
* @since 3.5
*/
public double getAndDecrement() {
final double last = value;
value--;
return last;
}
/**
* Decrements this instance's value by 1; this method returns the value
* associated with the instance immediately after the decrement operation. This
* method is not thread safe.
*
* @return the value associated with the instance after it is decremented
* @since 3.5
*/
public double decrementAndGet() {
value--;
return value;
}
// -----------------------------------------------------------------------
/**
* Adds a value to the value of this instance.
*
* @param operand the value to add
* @since 2.2
*/
public void add(final 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 2.2
*/
public void add(final Number operand) {
this.value += operand.doubleValue();
}
/**
* Subtracts a value from the value of this instance.
*
* @param operand the value to subtract, not null
* @since 2.2
*/
public void subtract(final 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 2.2
*/
public void subtract(final Number operand) {
this.value -= operand.doubleValue();
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately after the addition operation.
* This method is not thread safe.
*
* @param operand the quantity to add, not null
* @return the value associated with this instance after adding the operand
* @since 3.5
*/
public double addAndGet(final double operand) {
this.value += operand;
return value;
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately after the addition operation.
* This method is not thread safe.
*
* @param operand the quantity to add, not null
* @throws NullPointerException if {@code operand} is null
* @return the value associated with this instance after adding the operand
* @since 3.5
*/
public double addAndGet(final Number operand) {
this.value += operand.doubleValue();
return value;
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately prior to the addition
* operation. This method is not thread safe.
*
* @param operand the quantity to add, not null
* @return the value associated with this instance immediately before the
* operand was added
* @since 3.5
*/
public double getAndAdd(final double operand) {
final double last = value;
this.value += operand;
return last;
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately prior to the addition
* operation. This method is not thread safe.
*
* @param operand the quantity to add, not null
* @throws NullPointerException if {@code operand} is null
* @return the value associated with this instance immediately before the
* operand was added
* @since 3.5
*/
public double getAndAdd(final Number operand) {
final double last = value;
this.value += operand.doubleValue();
return last;
}
// -----------------------------------------------------------------------
// 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}
* if and only if the argument is not {@code null} and is a {@code Double}
* 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} 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 d1}
* and {@code d2}, the value of {@code d1.equals(d2)} is {@code true} if and
* only if <blockquote>
*
* <pre>
* d1.doubleValue() == d2.doubleValue()
* </pre>
*
* </blockquote>
* <p>
* also has the value {@code true}. However, there are two exceptions:
* <ul>
* <li>If {@code d1} and {@code d2} both represent {@code Double.NaN}, then the
* {@code equals} method returns {@code true}, even though
* {@code Double.NaN==Double.NaN} has the value {@code false}.
* <li>If {@code d1} represents {@code +0.0} while {@code d2} represents
* {@code -0.0}, or vice versa, the {@code equal} test has the value
* {@code false}, even though {@code +0.0==-0.0} has the value {@code true}.
* This allows hashtables to operate properly.
* </ul>
*
* @param obj the object to compare with, null returns false
* @return {@code true} if the objects are the same; {@code false} otherwise.
*/
@Override
public boolean equals(final 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() {
final 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
*/
@Override
public int compareTo(final MutableDouble other) {
return Double.compare(this.value, other.value);
}
// -----------------------------------------------------------------------
/**
* Returns the String value of this mutable.
*
* @return the mutable value as a string
*/
@Override
public String toString() {
return String.valueOf(value);
}
}

View File

@ -0,0 +1,434 @@
/*
* 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} wrapper.
* <p>
* Note that as MutableFloat does not extend Float, it is not treated by
* HString.format as a Float parameter.
*
* @see Float
* @since 2.1
*/
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() {
}
/**
* Constructs a new MutableFloat with the specified value.
*
* @param value the initial value to store
*/
public MutableFloat(final float value) {
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(final Number value) {
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(final String value) {
this.value = Float.parseFloat(value);
}
// -----------------------------------------------------------------------
/**
* Gets the value as a Float instance.
*
* @return the value as a Float, never null
*/
@Override
public Float getValue() {
return Float.valueOf(this.value);
}
/**
* Sets the value.
*
* @param value the value to set
*/
public void setValue(final 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
*/
@Override
public void setValue(final 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 2.2
*/
public void increment() {
value++;
}
/**
* Increments this instance's value by 1; this method returns the value
* associated with the instance immediately prior to the increment operation.
* This method is not thread safe.
*
* @return the value associated with the instance before it was incremented
* @since 3.5
*/
public float getAndIncrement() {
final float last = value;
value++;
return last;
}
/**
* Increments this instance's value by 1; this method returns the value
* associated with the instance immediately after the increment operation. This
* method is not thread safe.
*
* @return the value associated with the instance after it is incremented
* @since 3.5
*/
public float incrementAndGet() {
value++;
return value;
}
/**
* Decrements the value.
*
* @since 2.2
*/
public void decrement() {
value--;
}
/**
* Decrements this instance's value by 1; this method returns the value
* associated with the instance immediately prior to the decrement operation.
* This method is not thread safe.
*
* @return the value associated with the instance before it was decremented
* @since 3.5
*/
public float getAndDecrement() {
final float last = value;
value--;
return last;
}
/**
* Decrements this instance's value by 1; this method returns the value
* associated with the instance immediately after the decrement operation. This
* method is not thread safe.
*
* @return the value associated with the instance after it is decremented
* @since 3.5
*/
public float decrementAndGet() {
value--;
return value;
}
// -----------------------------------------------------------------------
/**
* Adds a value to the value of this instance.
*
* @param operand the value to add, not null
* @since 2.2
*/
public void add(final 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 2.2
*/
public void add(final Number operand) {
this.value += operand.floatValue();
}
/**
* Subtracts a value from the value of this instance.
*
* @param operand the value to subtract
* @since 2.2
*/
public void subtract(final 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 2.2
*/
public void subtract(final Number operand) {
this.value -= operand.floatValue();
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately after the addition operation.
* This method is not thread safe.
*
* @param operand the quantity to add, not null
* @return the value associated with this instance after adding the operand
* @since 3.5
*/
public float addAndGet(final float operand) {
this.value += operand;
return value;
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately after the addition operation.
* This method is not thread safe.
*
* @param operand the quantity to add, not null
* @throws NullPointerException if {@code operand} is null
* @return the value associated with this instance after adding the operand
* @since 3.5
*/
public float addAndGet(final Number operand) {
this.value += operand.floatValue();
return value;
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately prior to the addition
* operation. This method is not thread safe.
*
* @param operand the quantity to add, not null
* @return the value associated with this instance immediately before the
* operand was added
* @since 3.5
*/
public float getAndAdd(final float operand) {
final float last = value;
this.value += operand;
return last;
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately prior to the addition
* operation. This method is not thread safe.
*
* @param operand the quantity to add, not null
* @throws NullPointerException if {@code operand} is null
* @return the value associated with this instance immediately before the
* operand was added
* @since 3.5
*/
public float getAndAdd(final Number operand) {
final float last = value;
this.value += operand.floatValue();
return last;
}
// -----------------------------------------------------------------------
// 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} if
* and only if the argument is not {@code null} and is a {@code Float} object
* that represents a {@code float} that has the identical bit pattern to the bit
* pattern of the {@code float} 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 f1}
* and {@code f2}, the value of {@code f1.equals(f2)} is {@code true} if and
* only if <blockquote>
*
* <pre>
* f1.floatValue() == f2.floatValue()
* </pre>
*
* </blockquote>
* <p>
* also has the value {@code true}. However, there are two exceptions:
* <ul>
* <li>If {@code f1} and {@code f2} both represent {@code Float.NaN}, then the
* {@code equals} method returns {@code true}, even though
* {@code Float.NaN==Float.NaN} has the value {@code false}.
* <li>If {@code f1} represents {@code +0.0f} while {@code f2} represents
* {@code -0.0f}, or vice versa, the {@code equal} test has the value
* {@code false}, even though {@code 0.0f==-0.0f} has the value {@code true}.
* </ul>
* This definition allows hashtables to operate properly.
*
* @param obj the object to compare with, null returns false
* @return {@code true} if the objects are the same; {@code false} otherwise.
* @see java.lang.Float#floatToIntBits(float)
*/
@Override
public boolean equals(final 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
*/
@Override
public int compareTo(final MutableFloat other) {
return Float.compare(this.value, other.value);
}
// -----------------------------------------------------------------------
/**
* Returns the String value of this mutable.
*
* @return the mutable value as a string
*/
@Override
public String toString() {
return String.valueOf(value);
}
}

View File

@ -0,0 +1,393 @@
/*
* 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 org.apache.commons.lang3.math.NumberUtils;
/**
* A mutable {@code int} wrapper.
* <p>
* Note that as MutableInt does not extend Integer, it is not treated by
* HString.format as an Integer parameter.
*
* @see Integer
* @since 2.1
*/
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() {
}
/**
* Constructs a new MutableInt with the specified value.
*
* @param value the initial value to store
*/
public MutableInt(final int value) {
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(final Number value) {
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(final String value) {
this.value = Integer.parseInt(value);
}
// -----------------------------------------------------------------------
/**
* Gets the value as a Integer instance.
*
* @return the value as a Integer, never null
*/
@Override
public Integer getValue() {
return Integer.valueOf(this.value);
}
/**
* Sets the value.
*
* @param value the value to set
*/
public void setValue(final 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
*/
@Override
public void setValue(final Number value) {
this.value = value.intValue();
}
// -----------------------------------------------------------------------
/**
* Increments the value.
*
* @since 2.2
*/
public void increment() {
value++;
}
/**
* Increments this instance's value by 1; this method returns the value
* associated with the instance immediately prior to the increment operation.
* This method is not thread safe.
*
* @return the value associated with the instance before it was incremented
* @since 3.5
*/
public int getAndIncrement() {
final int last = value;
value++;
return last;
}
/**
* Increments this instance's value by 1; this method returns the value
* associated with the instance immediately after the increment operation. This
* method is not thread safe.
*
* @return the value associated with the instance after it is incremented
* @since 3.5
*/
public int incrementAndGet() {
value++;
return value;
}
/**
* Decrements the value.
*
* @since 2.2
*/
public void decrement() {
value--;
}
/**
* Decrements this instance's value by 1; this method returns the value
* associated with the instance immediately prior to the decrement operation.
* This method is not thread safe.
*
* @return the value associated with the instance before it was decremented
* @since 3.5
*/
public int getAndDecrement() {
final int last = value;
value--;
return last;
}
/**
* Decrements this instance's value by 1; this method returns the value
* associated with the instance immediately after the decrement operation. This
* method is not thread safe.
*
* @return the value associated with the instance after it is decremented
* @since 3.5
*/
public int decrementAndGet() {
value--;
return value;
}
// -----------------------------------------------------------------------
/**
* Adds a value to the value of this instance.
*
* @param operand the value to add, not null
* @since 2.2
*/
public void add(final 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 2.2
*/
public void add(final Number operand) {
this.value += operand.intValue();
}
/**
* Subtracts a value from the value of this instance.
*
* @param operand the value to subtract, not null
* @since 2.2
*/
public void subtract(final 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 2.2
*/
public void subtract(final Number operand) {
this.value -= operand.intValue();
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately after the addition operation.
* This method is not thread safe.
*
* @param operand the quantity to add, not null
* @return the value associated with this instance after adding the operand
* @since 3.5
*/
public int addAndGet(final int operand) {
this.value += operand;
return value;
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately after the addition operation.
* This method is not thread safe.
*
* @param operand the quantity to add, not null
* @throws NullPointerException if {@code operand} is null
* @return the value associated with this instance after adding the operand
* @since 3.5
*/
public int addAndGet(final Number operand) {
this.value += operand.intValue();
return value;
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately prior to the addition
* operation. This method is not thread safe.
*
* @param operand the quantity to add, not null
* @return the value associated with this instance immediately before the
* operand was added
* @since 3.5
*/
public int getAndAdd(final int operand) {
final int last = value;
this.value += operand;
return last;
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately prior to the addition
* operation. This method is not thread safe.
*
* @param operand the quantity to add, not null
* @throws NullPointerException if {@code operand} is null
* @return the value associated with this instance immediately before the
* operand was added
* @since 3.5
*/
public int getAndAdd(final Number operand) {
final int last = value;
this.value += operand.intValue();
return last;
}
// -----------------------------------------------------------------------
// 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} if
* and only if the argument is not {@code null} and is a {@code MutableInt}
* object that contains the same {@code int} value as this object.
*
* @param obj the object to compare with, null returns false
* @return {@code true} if the objects are the same; {@code false} otherwise.
*/
@Override
public boolean equals(final 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
*/
@Override
public int compareTo(final MutableInt other) {
return NumberUtils.compare(this.value, other.value);
}
// -----------------------------------------------------------------------
/**
* Returns the String value of this mutable.
*
* @return the mutable value as a string
*/
@Override
public String toString() {
return String.valueOf(value);
}
}

View File

@ -0,0 +1,393 @@
/*
* 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 org.apache.commons.lang3.math.NumberUtils;
/**
* A mutable {@code long} wrapper.
* <p>
* Note that as MutableLong does not extend Long, it is not treated by
* HString.format as a Long parameter.
*
* @see Long
* @since 2.1
*/
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() {
}
/**
* Constructs a new MutableLong with the specified value.
*
* @param value the initial value to store
*/
public MutableLong(final long value) {
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(final Number value) {
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(final String value) {
this.value = Long.parseLong(value);
}
// -----------------------------------------------------------------------
/**
* Gets the value as a Long instance.
*
* @return the value as a Long, never null
*/
@Override
public Long getValue() {
return Long.valueOf(this.value);
}
/**
* Sets the value.
*
* @param value the value to set
*/
public void setValue(final 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
*/
@Override
public void setValue(final Number value) {
this.value = value.longValue();
}
// -----------------------------------------------------------------------
/**
* Increments the value.
*
* @since 2.2
*/
public void increment() {
value++;
}
/**
* Increments this instance's value by 1; this method returns the value
* associated with the instance immediately prior to the increment operation.
* This method is not thread safe.
*
* @return the value associated with the instance before it was incremented
* @since 3.5
*/
public long getAndIncrement() {
final long last = value;
value++;
return last;
}
/**
* Increments this instance's value by 1; this method returns the value
* associated with the instance immediately after the increment operation. This
* method is not thread safe.
*
* @return the value associated with the instance after it is incremented
* @since 3.5
*/
public long incrementAndGet() {
value++;
return value;
}
/**
* Decrements the value.
*
* @since 2.2
*/
public void decrement() {
value--;
}
/**
* Decrements this instance's value by 1; this method returns the value
* associated with the instance immediately prior to the decrement operation.
* This method is not thread safe.
*
* @return the value associated with the instance before it was decremented
* @since 3.5
*/
public long getAndDecrement() {
final long last = value;
value--;
return last;
}
/**
* Decrements this instance's value by 1; this method returns the value
* associated with the instance immediately after the decrement operation. This
* method is not thread safe.
*
* @return the value associated with the instance after it is decremented
* @since 3.5
*/
public long decrementAndGet() {
value--;
return value;
}
// -----------------------------------------------------------------------
/**
* Adds a value to the value of this instance.
*
* @param operand the value to add, not null
* @since 2.2
*/
public void add(final 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 2.2
*/
public void add(final Number operand) {
this.value += operand.longValue();
}
/**
* Subtracts a value from the value of this instance.
*
* @param operand the value to subtract, not null
* @since 2.2
*/
public void subtract(final 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 2.2
*/
public void subtract(final Number operand) {
this.value -= operand.longValue();
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately after the addition operation.
* This method is not thread safe.
*
* @param operand the quantity to add, not null
* @return the value associated with this instance after adding the operand
* @since 3.5
*/
public long addAndGet(final long operand) {
this.value += operand;
return value;
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately after the addition operation.
* This method is not thread safe.
*
* @param operand the quantity to add, not null
* @throws NullPointerException if {@code operand} is null
* @return the value associated with this instance after adding the operand
* @since 3.5
*/
public long addAndGet(final Number operand) {
this.value += operand.longValue();
return value;
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately prior to the addition
* operation. This method is not thread safe.
*
* @param operand the quantity to add, not null
* @return the value associated with this instance immediately before the
* operand was added
* @since 3.5
*/
public long getAndAdd(final long operand) {
final long last = value;
this.value += operand;
return last;
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately prior to the addition
* operation. This method is not thread safe.
*
* @param operand the quantity to add, not null
* @throws NullPointerException if {@code operand} is null
* @return the value associated with this instance immediately before the
* operand was added
* @since 3.5
*/
public long getAndAdd(final Number operand) {
final long last = value;
this.value += operand.longValue();
return last;
}
// -----------------------------------------------------------------------
// 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} if
* and only if the argument is not {@code null} and is a {@code MutableLong}
* object that contains the same {@code long} value as this object.
*
* @param obj the object to compare with, null returns false
* @return {@code true} if the objects are the same; {@code false} otherwise.
*/
@Override
public boolean equals(final 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
*/
@Override
public int compareTo(final MutableLong other) {
return NumberUtils.compare(this.value, other.value);
}
// -----------------------------------------------------------------------
/**
* Returns the String value of this mutable.
*
* @return the mutable value as a string
*/
@Override
public String toString() {
return String.valueOf(value);
}
}

View File

@ -0,0 +1,125 @@
/*
* 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} wrapper.
*
* @param <T> the type to set and get
* @since 2.1
*/
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}.
*/
public MutableObject() {
}
/**
* Constructs a new MutableObject with the specified value.
*
* @param value the initial value to store
*/
public MutableObject(final T value) {
this.value = value;
}
// -----------------------------------------------------------------------
/**
* Gets the value.
*
* @return the value, may be null
*/
@Override
public T getValue() {
return this.value;
}
/**
* Sets the value.
*
* @param value the value to set
*/
@Override
public void setValue(final T value) {
this.value = value;
}
// -----------------------------------------------------------------------
/**
* <p>
* Compares this object against the specified object. The result is {@code true}
* if and only if the argument is not {@code null} and is a
* {@code MutableObject} object that contains the same {@code T} value as this
* object.
* </p>
*
* @param obj the object to compare with, {@code null} returns {@code false}
* @return {@code true} if the objects are the same; {@code true} if the objects
* have equivalent {@code value} fields; {@code false} otherwise.
*/
@Override
public boolean equals(final Object obj) {
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
if (this.getClass() == obj.getClass()) {
final MutableObject<?> that = (MutableObject<?>) obj;
return this.value.equals(that.value);
}
return false;
}
/**
* Returns the value's hash code or {@code 0} if the value is {@code null}.
*
* @return the value's hash code or {@code 0} if the value is {@code null}.
*/
@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();
}
}

View File

@ -0,0 +1,404 @@
/*
* 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 org.apache.commons.lang3.math.NumberUtils;
/**
* A mutable {@code short} wrapper.
* <p>
* Note that as MutableShort does not extend Short, it is not treated by
* HString.format as a Short parameter.
*
* @see Short
* @since 2.1
*/
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() {
}
/**
* Constructs a new MutableShort with the specified value.
*
* @param value the initial value to store
*/
public MutableShort(final short value) {
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(final Number value) {
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(final String value) {
this.value = Short.parseShort(value);
}
// -----------------------------------------------------------------------
/**
* Gets the value as a Short instance.
*
* @return the value as a Short, never null
*/
@Override
public Short getValue() {
return Short.valueOf(this.value);
}
/**
* Sets the value.
*
* @param value the value to set
*/
public void setValue(final 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
*/
@Override
public void setValue(final Number value) {
this.value = value.shortValue();
}
// -----------------------------------------------------------------------
/**
* Increments the value.
*
* @since 2.2
*/
public void increment() {
value++;
}
/**
* Increments this instance's value by 1; this method returns the value
* associated with the instance immediately prior to the increment operation.
* This method is not thread safe.
*
* @return the value associated with the instance before it was incremented
* @since 3.5
*/
public short getAndIncrement() {
final short last = value;
value++;
return last;
}
/**
* Increments this instance's value by 1; this method returns the value
* associated with the instance immediately after the increment operation. This
* method is not thread safe.
*
* @return the value associated with the instance after it is incremented
* @since 3.5
*/
public short incrementAndGet() {
value++;
return value;
}
/**
* Decrements the value.
*
* @since 2.2
*/
public void decrement() {
value--;
}
/**
* Decrements this instance's value by 1; this method returns the value
* associated with the instance immediately prior to the decrement operation.
* This method is not thread safe.
*
* @return the value associated with the instance before it was decremented
* @since 3.5
*/
public short getAndDecrement() {
final short last = value;
value--;
return last;
}
/**
* Decrements this instance's value by 1; this method returns the value
* associated with the instance immediately after the decrement operation. This
* method is not thread safe.
*
* @return the value associated with the instance after it is decremented
* @since 3.5
*/
public short decrementAndGet() {
value--;
return value;
}
// -----------------------------------------------------------------------
/**
* Adds a value to the value of this instance.
*
* @param operand the value to add, not null
* @since 2.2
*/
public void add(final 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 2.2
*/
public void add(final Number operand) {
this.value += operand.shortValue();
}
/**
* Subtracts a value from the value of this instance.
*
* @param operand the value to subtract, not null
* @since 2.2
*/
public void subtract(final 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 2.2
*/
public void subtract(final Number operand) {
this.value -= operand.shortValue();
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately after the addition operation.
* This method is not thread safe.
*
* @param operand the quantity to add, not null
* @return the value associated with this instance after adding the operand
* @since 3.5
*/
public short addAndGet(final short operand) {
this.value += operand;
return value;
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately after the addition operation.
* This method is not thread safe.
*
* @param operand the quantity to add, not null
* @throws NullPointerException if {@code operand} is null
* @return the value associated with this instance after adding the operand
* @since 3.5
*/
public short addAndGet(final Number operand) {
this.value += operand.shortValue();
return value;
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately prior to the addition
* operation. This method is not thread safe.
*
* @param operand the quantity to add, not null
* @return the value associated with this instance immediately before the
* operand was added
* @since 3.5
*/
public short getAndAdd(final short operand) {
final short last = value;
this.value += operand;
return last;
}
/**
* Increments this instance's value by {@code operand}; this method returns the
* value associated with the instance immediately prior to the addition
* operation. This method is not thread safe.
*
* @param operand the quantity to add, not null
* @throws NullPointerException if {@code operand} is null
* @return the value associated with this instance immediately before the
* operand was added
* @since 3.5
*/
public short getAndAdd(final Number operand) {
final short last = value;
this.value += operand.shortValue();
return last;
}
// -----------------------------------------------------------------------
// 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} if
* and only if the argument is not {@code null} and is a {@code MutableShort}
* object that contains the same {@code short} value as this object.
*
* @param obj the object to compare with, null returns false
* @return {@code true} if the objects are the same; {@code false} otherwise.
*/
@Override
public boolean equals(final 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
*/
@Override
public int compareTo(final MutableShort other) {
return NumberUtils.compare(this.value, other.value);
}
// -----------------------------------------------------------------------
/**
* Returns the String value of this mutable.
*
* @return the mutable value as a string
*/
@Override
public String toString() {
return String.valueOf(value);
}
}

View File

@ -0,0 +1,27 @@
/*
* 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.
*/
/**
* <p>
* Provides typed mutable wrappers to primitive values and Object. These
* wrappers are similar to the wrappers provided by the Java API, but allow the
* wrapped value to be changed without needing to create a separate wrapper
* object. These classes are not thread-safe.
* </p>
*
* @since 2.1
*/
package org.apache.commons.lang3.mutable;