Add utility methods to ObjectBigArray
Adds #getCapacity(), getAndSet, and setIfNull utility methods which
enables avoiding redundant ensureCapacity and get/set pairs in some
scenarios.
@@ -60,6 +60,14 @@ public final class ObjectBigArray<T>
return INSTANCE_SIZE + SizeOf.sizeOf(array) + (segments * SIZE_OF_SEGMENT);
}
+ /**
+ * Returns the current capacity of this big array
+ */
+ public long getCapacity()
+ {
+ return capacity;
+ }
+
/**
* Returns the element of this big array at specified index.
*
@@ -72,6 +80,24 @@ public final class ObjectBigArray<T>
return (T) array[segment(index)][offset(index)];
}
+ /**
+ * Gets the element of this big array at specified index and then sets the provided
+ * replacement value to the same index
+ *
+ * @param index a position in this big array.
+ *
+ * @return the previously stored value for the specified index
+ */
+ @SuppressWarnings("unchecked")
+ public T getAndSet(long index, T replacement)
+ {
+ Object[] segment = array[segment(index)];
+ int offset = offset(index);
+ T result = (T) segment[offset];
+ segment[offset] = replacement;
+ return result;
+ }
+
/**
* Sets the element of this big array at specified index.
*
@@ -82,6 +108,25 @@ public final class ObjectBigArray<T>
array[segment(index)][offset(index)] = value;
}
+ /**
+ * Sets the element of this big array at specified index if and only if the existing value
+ * at that index is null
+ *
+ * @param index a position in this big array.
+ *
+ * @return whether the previous value was null and the new value stored
+ */
+ public boolean setIfNull(long index, T value)
+ {
+ Object[] segment = array[segment(index)];
+ int offset = offset(index);
+ if (segment[offset] == null) {
+ segment[offset] = value;
+ return true;
+ }
+ return false;
+ }
+
/**
* Ensures this big array is at least the specified length. If the array is smaller, segments
* are added until the array is larger then the specified length.
