/** * Creates and returns a new evicting queue that will hold up to {@code maxSize} elements. * * <p>When {@code maxSize} is zero, elements will be evicted immediately after being added to the * queue. */ public static <E> EvictingQueue<E> create(int maxSize) { return new EvictingQueue<E>(maxSize); }
/** * Adds the given element to this queue. If the queue is currently full, the element at the head * of the queue is evicted to make room. * * @return {@code true} always */ @Override public boolean offer(E e) { return add(e); }
/** * Returns the number of additional elements that this queue can accept without evicting; * zero if the queue is currently full. * * @since 16.0 */ public int remainingCapacity() { return maxSize - size(); }
@Override public boolean addAll(Collection<? extends E> collection) { int size = collection.size(); if (size >= maxSize) { clear(); return Iterables.addAll(this, Iterables.skip(collection, size - maxSize)); } return standardAddAll(collection); }
@Override public boolean contains(Object object) { return delegate().contains(checkNotNull(object)); }
@Override public boolean remove(Object object) { return delegate().remove(checkNotNull(object)); }
/** * Adds the given element to this queue. If the queue is currently full, the element at the head * of the queue is evicted to make room. * * @return {@code true} always */ @Override public boolean add(E e) { checkNotNull(e); // check before removing if (maxSize == 0) { return true; } if (size() == maxSize) { delegate.remove(); } delegate.add(e); return true; }