View Javadoc
1   /*
2    * Copyright (C) 2012 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import static com.google.common.base.Preconditions.checkArgument;
20  import static com.google.common.base.Preconditions.checkNotNull;
21  
22  import com.google.common.annotations.Beta;
23  import com.google.common.annotations.GwtIncompatible;
24  import com.google.common.annotations.VisibleForTesting;
25  
26  import java.io.Serializable;
27  import java.util.ArrayDeque;
28  import java.util.Collection;
29  import java.util.Queue;
30  
31  /**
32   * A non-blocking queue which automatically evicts elements from the head of the queue when
33   * attempting to add new elements onto the queue and it is full.
34   *
35   * <p>An evicting queue must be configured with a maximum size. Each time an element is added
36   * to a full queue, the queue automatically removes its head element. This is different from
37   * conventional bounded queues, which either block or reject new elements when full.
38   *
39   * <p>This class is not thread-safe, and does not accept null elements.
40   *
41   * @author Kurt Alfred Kluever
42   * @since 15.0
43   */
44  @Beta
45  @GwtIncompatible("java.util.ArrayDeque")
46  public final class EvictingQueue<E> extends ForwardingQueue<E> implements Serializable {
47  
48    private final Queue<E> delegate;
49  
50    @VisibleForTesting
51    final int maxSize;
52  
53    private EvictingQueue(int maxSize) {
54      checkArgument(maxSize >= 0, "maxSize (%s) must >= 0", maxSize);
55      this.delegate = new ArrayDeque<E>(maxSize);
56      this.maxSize = maxSize;
57    }
58  
59    /**
60     * Creates and returns a new evicting queue that will hold up to {@code maxSize} elements.
61     *
62     * <p>When {@code maxSize} is zero, elements will be evicted immediately after being added to the
63     * queue.
64     */
65    public static <E> EvictingQueue<E> create(int maxSize) {
66      return new EvictingQueue<E>(maxSize);
67    }
68  
69    /**
70     * Returns the number of additional elements that this queue can accept without evicting;
71     * zero if the queue is currently full.
72     *
73     * @since 16.0
74     */
75    public int remainingCapacity() {
76      return maxSize - size();
77    }
78  
79    @Override protected Queue<E> delegate() {
80      return delegate;
81    }
82  
83    /**
84     * Adds the given element to this queue. If the queue is currently full, the element at the head
85     * of the queue is evicted to make room.
86     *
87     * @return {@code true} always
88     */
89    @Override public boolean offer(E e) {
90      return add(e);
91    }
92  
93    /**
94     * Adds the given element to this queue. If the queue is currently full, the element at the head
95     * of the queue is evicted to make room.
96     *
97     * @return {@code true} always
98     */
99    @Override public boolean add(E e) {
100     checkNotNull(e);  // check before removing
101     if (maxSize == 0) {
102       return true;
103     }
104     if (size() == maxSize) {
105       delegate.remove();
106     }
107     delegate.add(e);
108     return true;
109   }
110 
111   @Override public boolean addAll(Collection<? extends E> collection) {
112     return standardAddAll(collection);
113   }
114 
115   @Override
116   public boolean contains(Object object) {
117     return delegate().contains(checkNotNull(object));
118   }
119 
120   @Override
121   public boolean remove(Object object) {
122     return delegate().remove(checkNotNull(object));
123   }
124 
125   // TODO(user): Do we want to checkNotNull each element in containsAll, removeAll, and retainAll?
126 
127   private static final long serialVersionUID = 0L;
128 }