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; 025 026import java.io.Serializable; 027import java.util.Collection; 028import java.util.Queue; 029 030/** 031 * A non-blocking queue which automatically evicts elements from the head of the queue when 032 * attempting to add new elements onto the queue and it is full. This data structure is logically 033 * equivalent to a circular buffer (i.e., cyclic buffer or ring buffer). 034 * 035 * <p>An evicting queue must be configured with a maximum size. Each time an element is added 036 * to a full queue, the queue automatically removes its head element. This is different from 037 * conventional bounded queues, which either block or reject new elements when full. 038 * 039 * <p>This class is not thread-safe, and does not accept null elements. 040 * 041 * @author Kurt Alfred Kluever 042 * @since 15.0 043 */ 044@Beta 045@GwtCompatible 046public final class EvictingQueue<E> extends ForwardingQueue<E> implements Serializable { 047 048 private final Queue<E> delegate; 049 050 @VisibleForTesting final int maxSize; 051 052 private EvictingQueue(int maxSize) { 053 checkArgument(maxSize >= 0, "maxSize (%s) must >= 0", maxSize); 054 this.delegate = Platform.newFastestQueue(maxSize); 055 this.maxSize = maxSize; 056 } 057 058 /** 059 * Creates and returns a new evicting queue that will hold up to {@code maxSize} elements. 060 * 061 * <p>When {@code maxSize} is zero, elements will be evicted immediately after being added to the 062 * queue. 063 */ 064 public static <E> EvictingQueue<E> create(int maxSize) { 065 return new EvictingQueue<E>(maxSize); 066 } 067 068 /** 069 * Returns the number of additional elements that this queue can accept without evicting; 070 * zero if the queue is currently full. 071 * 072 * @since 16.0 073 */ 074 public int remainingCapacity() { 075 return maxSize - size(); 076 } 077 078 @Override 079 protected Queue<E> delegate() { 080 return delegate; 081 } 082 083 /** 084 * Adds the given element to this queue. If the queue is currently full, the element at the head 085 * of the queue is evicted to make room. 086 * 087 * @return {@code true} always 088 */ 089 @Override 090 public boolean offer(E e) { 091 return add(e); 092 } 093 094 /** 095 * Adds the given element to this queue. If the queue is currently full, the element at the head 096 * of the queue is evicted to make room. 097 * 098 * @return {@code true} always 099 */ 100 @Override 101 public boolean add(E e) { 102 checkNotNull(e); // check before removing 103 if (maxSize == 0) { 104 return true; 105 } 106 if (size() == maxSize) { 107 delegate.remove(); 108 } 109 delegate.add(e); 110 return true; 111 } 112 113 @Override 114 public boolean addAll(Collection<? extends E> collection) { 115 return standardAddAll(collection); 116 } 117 118 @Override 119 public boolean contains(Object object) { 120 return delegate().contains(checkNotNull(object)); 121 } 122 123 @Override 124 public boolean remove(Object object) { 125 return delegate().remove(checkNotNull(object)); 126 } 127 128 // TODO(kak): Do we want to checkNotNull each element in containsAll, removeAll, and retainAll? 129 130 private static final long serialVersionUID = 0L; 131}