/** * {@inheritDoc} */ @Override public KType remove(int index) { assert (index >= 0 && index < size()) : "Index " + index + " out of bounds [" + 0 + ", " + size() + ")."; final KType v = (KType) buffer[index]; if (index + 1 < elementsCount) { System.arraycopy(buffer, index + 1, buffer, index, elementsCount - index - 1); } elementsCount--; buffer[elementsCount] = null; return v; }
/** * {@inheritDoc} */ @Override public void removeRange(int fromIndex, int toIndex) { assert (fromIndex >= 0 && fromIndex <= size()) : "Index " + fromIndex + " out of bounds [" + 0 + ", " + size() + ")."; assert (toIndex >= 0 && toIndex <= size()) : "Index " + toIndex + " out of bounds [" + 0 + ", " + size() + "]."; assert fromIndex <= toIndex : "fromIndex must be <= toIndex: " + fromIndex + ", " + toIndex; System.arraycopy(buffer, toIndex, buffer, fromIndex, elementsCount - toIndex); final int count = toIndex - fromIndex; elementsCount -= count; Arrays.fill(buffer, elementsCount, elementsCount + count, null); }
/** * Applies <code>procedure</code> to a slice of the list, * <code>fromIndex</code>, inclusive, to <code>toIndex</code>, exclusive. */ public <T extends ObjectProcedure<? super KType>> T forEach(T procedure, int fromIndex, final int toIndex) { assert (fromIndex >= 0 && fromIndex <= size()) : "Index " + fromIndex + " out of bounds [" + 0 + ", " + size() + ")."; assert (toIndex >= 0 && toIndex <= size()) : "Index " + toIndex + " out of bounds [" + 0 + ", " + size() + "]."; assert fromIndex <= toIndex : "fromIndex must be <= toIndex: " + fromIndex + ", " + toIndex; final KType [] buffer = (KType[]) this.buffer; for (int i = fromIndex; i < toIndex; i++) { procedure.apply(buffer[i]); } return procedure; }
/** * Applies <code>predicate</code> to a slice of the list, * <code>fromIndex</code>, inclusive, to <code>toIndex</code>, exclusive, or * until predicate returns <code>false</code>. */ public <T extends ObjectPredicate<? super KType>> T forEach(T predicate, int fromIndex, final int toIndex) { assert (fromIndex >= 0 && fromIndex <= size()) : "Index " + fromIndex + " out of bounds [" + 0 + ", " + size() + ")."; assert (toIndex >= 0 && toIndex <= size()) : "Index " + toIndex + " out of bounds [" + 0 + ", " + size() + "]."; assert fromIndex <= toIndex : "fromIndex must be <= toIndex: " + fromIndex + ", " + toIndex; final KType[] buffer = (KType[]) this.buffer; for (int i = fromIndex; i < toIndex; i++) { if (!predicate.apply(buffer[i])) break; } return predicate; }
/** * {@inheritDoc} */ @Override public void insert(int index, KType e1) { assert (index >= 0 && index <= size()) : "Index " + index + " out of bounds [" + 0 + ", " + size() + "]."; ensureBufferSpace(1); System.arraycopy(buffer, index, buffer, index + 1, elementsCount - index); buffer[index] = e1; elementsCount++; }
/** * {@inheritDoc} */ @Override public Iterator<ObjectCursor<KType>> iterator() { return new ValueIterator<KType>((KType[]) buffer, size()); }
/** * {@inheritDoc} */ @Override public <T extends ObjectProcedure<? super KType>> T forEach(T procedure) { return forEach(procedure, 0, size()); }
/** * Trim the internal buffer to the current size. */ public void trimToSize() { if (size() != this.buffer.length) { this.buffer = (KType[]) toArray(); } }
/** * {@inheritDoc} */ @Override public <T extends ObjectPredicate<? super KType>> T forEach(T predicate) { return forEach(predicate, 0, size()); }
/** * Ensure this container can hold at least the given number of elements * without resizing its buffers. * * @param expectedElements * The total number of elements, inclusive. */ @Override public void ensureCapacity(int expectedElements) { final int bufferLen = (buffer == null ? 0 : buffer.length); if (expectedElements > bufferLen) { ensureBufferSpace(expectedElements - size()); } }
}.sort(0, array.size());
/** * Compare index-aligned elements against another * {@link ObjectIndexedContainer}. * Equality comparison is performed with this object's {@link #equals(Object, Object)} * method. */ protected boolean equalElements(ObjectArrayList<?> other) { int max = size(); if (other.size() != max) { return false; } for (int i = 0; i < max; i++) { if (!this.equals(other.get(i), get(i))) { return false; } } return true; }
public static void sortAndDedup(final ObjectArrayList<byte[]> array) { int len = array.size(); if (len > 1) { sort(array); int uniqueCount = 1; for (int i = 1; i < len; ++i) { if (!Arrays.equals(array.get(i), array.get(i - 1))) { array.set(uniqueCount++, array.get(i)); } } array.elementsCount = uniqueCount; } }
@Override public BytesRef binaryValue() { try { CollectionUtils.sortAndDedup(bytesList); int size = bytesList.size(); final byte[] bytes = new byte[totalSize + (size + 1) * 5]; ByteArrayDataOutput out = new ByteArrayDataOutput(bytes); out.writeVInt(size); // write total number of values for (int i = 0; i < size; i ++) { final byte[] value = bytesList.get(i); int valueLength = value.length; out.writeVInt(valueLength); out.writeBytes(value, 0, valueLength); } return new BytesRef(bytes, 0, out.getPosition()); } catch (IOException e) { throw new ElasticsearchException("Failed to get binary value", e); } } }
/** * {@inheritDoc} */ @Override public Iterator<ObjectCursor<KType>> iterator() { return new ValueIterator<KType>((KType[]) buffer, size()); }
/** * Trim the internal buffer to the current size. */ public void trimToSize() { if (size() != this.buffer.length) { this.buffer = (KType[]) toArray(); } }
/** * {@inheritDoc} */ @Override public <T extends ObjectProcedure<? super KType>> T forEach(T procedure) { return forEach(procedure, 0, size()); }