001/* 002 * Copyright (C) 2012 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect; 018 019import static com.google.common.base.Preconditions.checkArgument; 020import static com.google.common.base.Preconditions.checkNotNull; 021 022import com.google.common.annotations.Beta; 023import com.google.common.annotations.GwtCompatible; 024import com.google.common.annotations.VisibleForTesting; 025import com.google.errorprone.annotations.CanIgnoreReturnValue; 026import java.io.Serializable; 027import java.util.ArrayDeque; 028import java.util.Collection; 029import java.util.Queue; 030 031/** 032 * A non-blocking queue which automatically evicts elements from the head of the queue when 033 * attempting to add new elements onto the queue and it is full. This queue orders elements FIFO 034 * (first-in-first-out). This data structure is logically equivalent to a circular buffer (i.e., 035 * cyclic buffer or ring buffer). 036 * 037 * <p>An evicting queue must be configured with a maximum size. Each time an element is added 038 * to a full queue, the queue automatically removes its head element. This is different from 039 * conventional bounded queues, which either block or reject new elements when full. 040 * 041 * <p>This class is not thread-safe, and does not accept null elements. 042 * 043 * @author Kurt Alfred Kluever 044 * @since 15.0 045 */ 046@Beta 047@GwtCompatible 048public final class EvictingQueue<E> extends ForwardingQueue<E> implements Serializable { 049 050 private final Queue<E> delegate; 051 052 @VisibleForTesting final int maxSize; 053 054 private EvictingQueue(int maxSize) { 055 checkArgument(maxSize >= 0, "maxSize (%s) must >= 0", maxSize); 056 this.delegate = new ArrayDeque<E>(maxSize); 057 this.maxSize = maxSize; 058 } 059 060 /** 061 * Creates and returns a new evicting queue that will hold up to {@code maxSize} elements. 062 * 063 * <p>When {@code maxSize} is zero, elements will be evicted immediately after being added to the 064 * queue. 065 */ 066 public static <E> EvictingQueue<E> create(int maxSize) { 067 return new EvictingQueue<E>(maxSize); 068 } 069 070 /** 071 * Returns the number of additional elements that this queue can accept without evicting; 072 * zero if the queue is currently full. 073 * 074 * @since 16.0 075 */ 076 public int remainingCapacity() { 077 return maxSize - size(); 078 } 079 080 @Override 081 protected Queue<E> delegate() { 082 return delegate; 083 } 084 085 /** 086 * Adds the given element to this queue. If the queue is currently full, the element at the head 087 * of the queue is evicted to make room. 088 * 089 * @return {@code true} always 090 */ 091 @Override 092 @CanIgnoreReturnValue 093 public boolean offer(E e) { 094 return add(e); 095 } 096 097 /** 098 * Adds the given element to this queue. If the queue is currently full, the element at the head 099 * of the queue is evicted to make room. 100 * 101 * @return {@code true} always 102 */ 103 @Override 104 @CanIgnoreReturnValue 105 public boolean add(E e) { 106 checkNotNull(e); // check before removing 107 if (maxSize == 0) { 108 return true; 109 } 110 if (size() == maxSize) { 111 delegate.remove(); 112 } 113 delegate.add(e); 114 return true; 115 } 116 117 @Override 118 @CanIgnoreReturnValue 119 public boolean addAll(Collection<? extends E> collection) { 120 int size = collection.size(); 121 if (size >= maxSize) { 122 clear(); 123 return Iterables.addAll(this, Iterables.skip(collection, size - maxSize)); 124 } 125 return standardAddAll(collection); 126 } 127 128 @Override 129 public boolean contains(Object object) { 130 return delegate().contains(checkNotNull(object)); 131 } 132 133 @Override 134 @CanIgnoreReturnValue 135 public boolean remove(Object object) { 136 return delegate().remove(checkNotNull(object)); 137 } 138 139 // TODO(kak): Do we want to checkNotNull each element in containsAll, removeAll, and retainAll? 140 141 private static final long serialVersionUID = 0L; 142}