| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| SimpleFixedSizeFIFO |
|
| 1.2;1.2 |
| 1 | /** | |
| 2 | * Copyright 2009 Martin Vysny. | |
| 3 | * | |
| 4 | * This file is part of WebVM. | |
| 5 | * | |
| 6 | * WebVM is free software: you can redistribute it and/or modify | |
| 7 | * it under the terms of the GNU General Public License as published by | |
| 8 | * the Free Software Foundation, either version 3 of the License, or | |
| 9 | * (at your option) any later version. | |
| 10 | * | |
| 11 | * WebVM is distributed in the hope that it will be useful, | |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | * GNU General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU General Public License | |
| 17 | * along with WebVM. If not, see <http://www.gnu.org/licenses/>. | |
| 18 | */ | |
| 19 | package sk.baka.webvm.misc; | |
| 20 | ||
| 21 | import java.util.ArrayList; | |
| 22 | import java.util.List; | |
| 23 | import java.util.Queue; | |
| 24 | import java.util.concurrent.ConcurrentLinkedQueue; | |
| 25 | ||
| 26 | /** | |
| 27 | * A simple object that holds a fixed-size history. A fixed-size FIFO implementation. | |
| 28 | * @param <T> type of items contained in this history object. | |
| 29 | * @author Martin Vysny | |
| 30 | */ | |
| 31 | public final class SimpleFixedSizeFIFO<T> { | |
| 32 | ||
| 33 | private final int maxLength; | |
| 34 | 4 | private int historyLength = 0; |
| 35 | 4 | private final Queue<T> history = new ConcurrentLinkedQueue<T>(); |
| 36 | ||
| 37 | /** | |
| 38 | * Creates new instance. | |
| 39 | * @param maxLength maximum length of the FIFO queue. Old items are dropped automatically. | |
| 40 | */ | |
| 41 | public SimpleFixedSizeFIFO(final int maxLength) { | |
| 42 | 4 | super(); |
| 43 | 4 | this.maxLength = maxLength; |
| 44 | 4 | } |
| 45 | ||
| 46 | /** | |
| 47 | * Adds an item to the history, discarding oldest items when the maximum length has been reached. Not thread-safe - may be called from one thread instance only. | |
| 48 | * @param item the item to add | |
| 49 | */ | |
| 50 | public void add(T item) { | |
| 51 | 4 | while (historyLength >= maxLength) { |
| 52 | 0 | history.poll(); |
| 53 | 0 | historyLength--; |
| 54 | } | |
| 55 | 4 | history.add(item); |
| 56 | 4 | historyLength++; |
| 57 | 4 | newest = item; |
| 58 | 4 | } |
| 59 | ||
| 60 | /** | |
| 61 | * Returns a snapshot of the FIFO. Thread-safe. | |
| 62 | * @return a snapshot, first item is the oldest one. | |
| 63 | */ | |
| 64 | public List<T> toList() { | |
| 65 | 15 | return new ArrayList<T>(history); |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * Clears the history. | |
| 70 | */ | |
| 71 | public void clear() { | |
| 72 | 0 | history.clear(); |
| 73 | 0 | newest = null; |
| 74 | 0 | } |
| 75 | 4 | private T newest = null; |
| 76 | ||
| 77 | /** | |
| 78 | * Returns the newest item put into this queue. Not thread-safe - may be called from the same thread which is {@link #add(java.lang.Object) adding} items only. | |
| 79 | * @return newest item or null if the FIFO is empty. | |
| 80 | */ | |
| 81 | public T getNewest() { | |
| 82 | 12 | return newest; |
| 83 | } | |
| 84 | } |