/** * {@inheritDoc} */ @SuppressWarnings("unchecked") @Override public E set(int idx, E o) { checkIndex(idx); int realIdx = getRealIndex(idx); Object old = items[realIdx]; items[realIdx] = o; return (E) old; }
@SuppressWarnings("unchecked") public E poll() { if (isEmpty()) { return null; } Object ret = items[first]; items[first] = null; decreaseSize(); if (first == last) { first = last = 0; } shrinkIfNeeded(); return (E) ret; }
/** * {@inheritDoc} */ @Override public void add(int idx, E o) { if (idx == size()) { offer(o); return; } checkIndex(idx); expandIfNeeded(); int realIdx = getRealIndex(idx); // Make a room for a new element. if (first < last) { System.arraycopy(items, realIdx, items, realIdx + 1, last - realIdx); } else { if (realIdx >= first) { System.arraycopy(items, 0, items, 1, last); items[0] = items[items.length - 1]; System.arraycopy(items, realIdx, items, realIdx + 1, items.length - realIdx - 1); } else { System.arraycopy(items, realIdx, items, realIdx + 1, last - realIdx); } } items[realIdx] = o; increaseSize(); }
/** * {@inheritDoc} */ @Override @SuppressWarnings("unchecked") public E remove(int idx) { if (idx == 0) { return poll(); } checkIndex(idx); int realIdx = getRealIndex(idx); Object removed = items[realIdx]; // Remove a room for the removed element. if (first < last) { System.arraycopy(items, first, items, first + 1, realIdx - first); } else { if (realIdx >= first) { System.arraycopy(items, first, items, first + 1, realIdx - first); } else { System.arraycopy(items, 0, items, 1, realIdx); items[0] = items[items.length - 1]; System.arraycopy(items, first, items, first + 1, items.length - first - 1); } } items[first] = null; decreaseSize(); shrinkIfNeeded(); return (E) removed; }
/** * {@inheritDoc} */ @Override public void clear() { if (!isEmpty()) { Arrays.fill(items, null); first = 0; last = 0; full = false; shrinkIfNeeded(); } }
/** * {@inheritDoc} */ @Override public boolean offer(E item) { if (item == null) { throw new IllegalArgumentException("item"); } expandIfNeeded(); items[last] = item; increaseSize(); return true; }
private void shrinkIfNeeded() { int size = size(); int newLen = normalizeCapacity(size);
/** * {@inheritDoc} */ @Override @SuppressWarnings("unchecked") public E peek() { if (isEmpty()) { return null; } return (E) items[first]; }
/** * {@inheritDoc} */ @Override public boolean add(E o) { return offer(o); }
/** * Construct a new circular queue with an initial capacity. * * @param initialCapacity The initial capacity of this circular queue */ public CircularQueue(int initialCapacity) { int actualCapacity = normalizeCapacity(initialCapacity); items = new Object[actualCapacity]; mask = actualCapacity - 1; this.initialCapacity = actualCapacity; this.shrinkThreshold = 0; }
/** * {@inheritDoc} */ @Override public void add(int idx, E o) { if (idx == size()) { offer(o); return; } checkIndex(idx); expandIfNeeded(); int realIdx = getRealIndex(idx); // Make a room for a new element. if (first < last) { System.arraycopy(items, realIdx, items, realIdx + 1, last - realIdx); } else { if (realIdx >= first) { System.arraycopy(items, 0, items, 1, last); items[0] = items[items.length - 1]; System.arraycopy(items, realIdx, items, realIdx + 1, items.length - realIdx - 1); } else { System.arraycopy(items, realIdx, items, realIdx + 1, last - realIdx); } } items[realIdx] = o; increaseSize(); }
/** * {@inheritDoc} */ @Override @SuppressWarnings("unchecked") public E remove(int idx) { if (idx == 0) { return poll(); } checkIndex(idx); int realIdx = getRealIndex(idx); Object removed = items[realIdx]; // Remove a room for the removed element. if (first < last) { System.arraycopy(items, first, items, first + 1, realIdx - first); } else { if (realIdx >= first) { System.arraycopy(items, first, items, first + 1, realIdx - first); } else { System.arraycopy(items, 0, items, 1, realIdx); items[0] = items[items.length - 1]; System.arraycopy(items, first, items, first + 1, items.length - first - 1); } } items[first] = null; decreaseSize(); shrinkIfNeeded(); return (E) removed; }
@Override public void clear() { if (!isEmpty()) { Arrays.fill(items, null); first = 0; last = 0; full = false; shrinkIfNeeded(); } }
public boolean offer(E item) { if (item == null) { throw new NullPointerException("item"); } expandIfNeeded(); items[last] = item; increaseSize(); return true; }
private void shrinkIfNeeded() { int size = size(); int newLen = normalizeCapacity(size);