|
| 1 | +/* |
| 2 | + * Copyright 2025 Aiven Oy |
| 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 io.aiven.commons.collections; |
| 18 | + |
| 19 | +import java.util.Objects; |
| 20 | + |
| 21 | +import org.apache.commons.collections4.queue.CircularFifoQueue; |
| 22 | +import org.apache.commons.collections4.queue.SynchronizedQueue; |
| 23 | + |
| 24 | +/** |
| 25 | + * Implements a ring buffer of items. Items are inserted until maximum size is reached and then the earliest items are |
| 26 | + * removed when newer items are added. |
| 27 | + * |
| 28 | + * @param <K> |
| 29 | + * the type of item in the queue. Must support equality check. |
| 30 | + */ |
| 31 | +public final class RingBuffer<K> { |
| 32 | + /** How to handle the duplicates in the buffer. */ |
| 33 | + public enum DuplicateHandling { |
| 34 | + /** Allow duplicates in the buffer. */ |
| 35 | + ALLOW, |
| 36 | + /** Reject (do not add) duplicates to the buffer. */ |
| 37 | + REJECT, |
| 38 | + /** Move the duplicate entry to the tail of the buffer. */ |
| 39 | + DELETE |
| 40 | + } |
| 41 | + |
| 42 | + /** The wrapped queue. */ |
| 43 | + private final SynchronizedQueue<K> queue; |
| 44 | + |
| 45 | + private final CircularFifoQueue<K> wrappedQueue; |
| 46 | + |
| 47 | + /** Flag to indicate ring buffer should always be empty. */ |
| 48 | + private final boolean alwaysEmpty; |
| 49 | + |
| 50 | + /** Flag to allow duplicates in the buffer. */ |
| 51 | + private final DuplicateHandling duplicateHandling; |
| 52 | + |
| 53 | + /** |
| 54 | + * Create a Ring Buffer of a maximum size that rejects duplicates. If the size is less than or equal to 0 then the |
| 55 | + * buffer is always empty. |
| 56 | + * |
| 57 | + * @param size |
| 58 | + * The maximum size of the ring buffer |
| 59 | + * @see DuplicateHandling#REJECT |
| 60 | + */ |
| 61 | + public RingBuffer(final int size) { |
| 62 | + this(size, DuplicateHandling.REJECT); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Create a Ring Buffer of specified maximum size and potentially allowing duplicates. If the size is less than or |
| 67 | + * equal to 0 then the buffer is always empty. |
| 68 | + * |
| 69 | + * @param size |
| 70 | + * The maximum size of the ring buffer |
| 71 | + * @param duplicateHandling |
| 72 | + * defines how to handle duplicate values in the buffer. |
| 73 | + */ |
| 74 | + public RingBuffer(final int size, final DuplicateHandling duplicateHandling) { |
| 75 | + wrappedQueue = new CircularFifoQueue<>(size > 0 ? size : 1); |
| 76 | + queue = SynchronizedQueue.synchronizedQueue(wrappedQueue); |
| 77 | + alwaysEmpty = size <= 0; |
| 78 | + this.duplicateHandling = duplicateHandling; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public String toString() { |
| 83 | + return String.format("RingBuffer[%s, load %s/%s]", duplicateHandling, queue.size(), wrappedQueue.maxSize()); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Adds a new item if it is not already present. |
| 88 | + * |
| 89 | + * <ul> |
| 90 | + * <li>If the buffer is always empty the item is ignored and not enqueued. |
| 91 | + * <li>If the buffer already contains the item it is ignored and not enqueued. |
| 92 | + * <li>If the buffer is full the oldest entry in the buffer is ejected. |
| 93 | + * </ul> |
| 94 | + * |
| 95 | + * @param item |
| 96 | + * Item T which is to be added to the Queue |
| 97 | + * @return The item that was ejected. May be {@code null}. |
| 98 | + */ |
| 99 | + public K add(final K item) { |
| 100 | + Objects.requireNonNull(item, "item"); |
| 101 | + if (!alwaysEmpty && checkDuplicates(item)) { |
| 102 | + final K result = isFull() ? queue.poll() : null; |
| 103 | + queue.add(item); |
| 104 | + return result; |
| 105 | + } |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Removes a single instance of the item from the buffer. |
| 111 | + * |
| 112 | + * @param item |
| 113 | + * the item to remove. |
| 114 | + */ |
| 115 | + public void remove(final K item) { |
| 116 | + queue.remove(item); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Determines if the item is in the buffer. |
| 121 | + * |
| 122 | + * @param item |
| 123 | + * the item to look for. |
| 124 | + * @return {@code true} if the item is in the buffer, {@code false} othersie. |
| 125 | + */ |
| 126 | + public boolean contains(final K item) { |
| 127 | + return queue.contains(item); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Returns but does not remove the head of the buffer. |
| 132 | + * |
| 133 | + * @return the item at the head of the buffer. May be {@code null}. |
| 134 | + */ |
| 135 | + public K head() { |
| 136 | + return queue.peek(); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Returns but does not remove the teal of the buffer. |
| 141 | + * |
| 142 | + * @return the item at the tail of the buffer. May be {@code null}. |
| 143 | + */ |
| 144 | + public K tail() { |
| 145 | + final int size = wrappedQueue.size(); |
| 146 | + return size == 0 ? null : wrappedQueue.get(size - 1); |
| 147 | + } |
| 148 | + |
| 149 | + private boolean checkDuplicates(final K item) { |
| 150 | + switch (duplicateHandling) { |
| 151 | + case ALLOW : |
| 152 | + return true; |
| 153 | + case REJECT : |
| 154 | + return !queue.contains(item); |
| 155 | + case DELETE : |
| 156 | + queue.remove(item); |
| 157 | + return true; |
| 158 | + default : |
| 159 | + throw new IllegalStateException("Unsupported duplicate handling: " + duplicateHandling); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Returns {@code true} if the buffer is full. |
| 165 | + * |
| 166 | + * @return {@code true} if the buffer is full. |
| 167 | + */ |
| 168 | + public boolean isFull() { |
| 169 | + return wrappedQueue.isAtFullCapacity(); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Gets the next item to be ejected. If the buffer is full this will return the oldest value in the buffer. If the |
| 174 | + * buffer is not full this method will return {@code null}. |
| 175 | + * |
| 176 | + * @return A value T from the last place in the buffer, returns null if buffer is not full. |
| 177 | + */ |
| 178 | + public K getNextEjected() { |
| 179 | + return isFull() ? queue.peek() : null; |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public boolean equals(final Object object) { |
| 184 | + if (object == this) { |
| 185 | + return true; |
| 186 | + } |
| 187 | + return super.equals(object); |
| 188 | + } |
| 189 | + |
| 190 | + @SuppressWarnings("PMD.UselessOverridingMethod") |
| 191 | + @Override |
| 192 | + public int hashCode() { |
| 193 | + return super.hashCode(); |
| 194 | + } |
| 195 | +} |
0 commit comments