- All Implemented Interfaces:
Serializable
,Comparable<ResizeMethod>
,Constable
Defines ways to increase the capacity of backing arrays and other types of
internally managed buffers and caches. Can be used if you want to leave it to
clients of your class to decide upon a proper resizing strategy. If you use the
resize
methods to calculate a new size for the
cache, buffer or backing array, the increase in size will max out at MAX_INCREASE
for the MULTIPLY
and PERCENTAGE
resize methods.
After that, they will behave like the ADD
method, adding
MAX_INCREASE
to the backing array's current capacity each time it needs to be
resized. In addition, the resize
method will also take care of trapping
buffer overflows (the backing array's length reaching or exceeding Integer.MAX_VALUE
).
The following example illustrates how to use ResizeMethod
:
public class DIYArrayList<E> extends AbstractList<E> { private final ResizeMethod resizeMethod; private final double resizeAmount; private Object[] buf; private int size; public DIYArrayList(int initialCapacity, ResizeMethod resizeMethod, double resizeAmount) { this.buf = new Object[initialCapacity]; this.resizeMethod = resizeMethod; this.resizeAmount = resizeAmount; } @Override public boolean addAll(Collection<?> c) { int minIncrease = ResizeMethod.getMinIncrease(buf.length, size, c.size()); if(minIncrease > 0) { increaseCapacity(minIncrease); } // append the collection ... } private void increaseCapacity(int minIncrease) { int newCap = resizeMethod.resize(buf.length, resizeAmount, minIncrease); Object[] newBuf = new Object[newCap]; System.arraycopy(buf, 0, newBuf, 0, size); buf = newBuf; } }
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>>
-
Enum Constant Summary
Enum ConstantDescriptionIncrease the capacity by a fixed amount each time the buffer reaches full capacity.Increase the capacity by multiplying the current capacity by a fixed amount.Increase the capacity by adding a fixed percentage of the current capacity to the current capacity. -
Field Summary
Modifier and TypeFieldDescriptionstatic final int
The maximum increase in size allowed by theMULTIPLY
andPERCENTAGE
resize methods:8 * 1024 * 1024
. -
Method Summary
Modifier and TypeMethodDescriptionstatic int
getMinIncrease
(int curCapacity, int curSize, int itemCount) Calculates the extra capacity required to additemCount
items to an internally managed buffer, cache or backing array.int
resize
(int curCapacity, double amount) Calculates the new capacity for a buffer, cache or backing array using thisResizeMethod
and the specified resize amount (c.q. factor c.q. percentage) The capacity will increase by at least 1.int
resize
(int curCapacity, double amount, int minIncrease) Calculates the new capacity for a buffer, cache or backing array using thisResizeMethod
.static ResizeMethod
Returns the enum constant of this class with the specified name.static ResizeMethod[]
values()
Returns an array containing the constants of this enum class, in the order they are declared.
-
Enum Constant Details
-
ADD
Increase the capacity by a fixed amount each time the buffer reaches full capacity. -
MULTIPLY
Increase the capacity by multiplying the current capacity by a fixed amount. Using this resize method with a resize factor of 2 is the "classic" way of resizing a backing array. -
PERCENTAGE
Increase the capacity by adding a fixed percentage of the current capacity to the current capacity.
-
-
Field Details
-
MAX_INCREASE
public static final int MAX_INCREASEThe maximum increase in size allowed by theMULTIPLY
andPERCENTAGE
resize methods:8 * 1024 * 1024
.- See Also:
-
-
Method Details
-
values
Returns an array containing the constants of this enum class, in the order they are declared.- Returns:
- an array containing the constants of this enum class, in the order they are declared
-
valueOf
Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum class has no constant with the specified nameNullPointerException
- if the argument is null
-
getMinIncrease
public static int getMinIncrease(int curCapacity, int curSize, int itemCount) Calculates the extra capacity required to additemCount
items to an internally managed buffer, cache or backing array. If the result is zero or negative, no resizing is necessary. The return value of this method can be used as theminIncrease
argument for theresize
method.- Parameters:
curCapacity
- The current length or size of the buffer, cache or backing arraycurSize
- The number of items already in the buffer, cache or backing arrayitemCount
- The number of items to be added to the buffer, cache or backing array- Returns:
- If positive, the extra capacity required for the new items, else the remaining capacity once the new items have been added
-
resize
public int resize(int curCapacity, double amount) Calculates the new capacity for a buffer, cache or backing array using thisResizeMethod
and the specified resize amount (c.q. factor c.q. percentage) The capacity will increase by at least 1.- Parameters:
curCapacity
- The current capacityamount
- The amount, factor or percentage by which to increase the current capacity. This would typically be the value of a (final) instance variable in the class managing the backing array, and it would typically be passed in through the constructor, along with the resize method.- Returns:
- The value to be used as the length for the new backing array
-
resize
public int resize(int curCapacity, double amount, int minIncrease) Calculates the new capacity for a buffer, cache or backing array using thisResizeMethod
. The capacity will increase by at leastminIncrease
. ABufferOverflowException
is thrown if the current capacity isInteger.MAX_VALUE
, or ifcurCapacity + minIncrease
is greater thanInteger.MAX_VALUE
.- Parameters:
curCapacity
- The current capacity. It is allowed to start off with zero initial capacity. When resizing using theMULTIPLY
orPERCENTAGE
method, the initial capacity will first be tacitly increased to 1.amount
- The amount, factor or percentage by which to increase the current capacity. This would typically be the value of a (final) instance variable in the class managing the backing array, and it would typically be passed in through the constructor, along with the resize method.minIncrease
- The minimum amount by which to increase current capacity. This would typically be the size of an incoming block of data that necessitated the resizing of the backing array. The provided value will overrideMAX_INCREASE
and it must be at least 1.- Returns:
- The value to be used as the length for the new backing array
-