001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.AbstractQueue;
018    import java.util.Iterator;
019    import java.util.LinkedList;
020    
021    /**
022     * @author Michael C. Han
023     */
024    public class LimitedFIFOQueue<E> extends AbstractQueue<E> {
025    
026            public LimitedFIFOQueue(int capacity) {
027                    _capacity = capacity;
028            }
029    
030            @Override
031            public Iterator<E> iterator() {
032                    return _linkedList.iterator();
033            }
034    
035            @Override
036            public boolean offer(E e) {
037                    if (size() >= _capacity) {
038                            _linkedList.removeFirst();
039                    }
040    
041                    _linkedList.offerLast(e);
042    
043                    return true;
044            }
045    
046            @Override
047            public E peek() {
048                    return _linkedList.peek();
049            }
050    
051            @Override
052            public E poll() {
053                    return _linkedList.poll();
054            }
055    
056            @Override
057            public int size() {
058                    return _linkedList.size();
059            }
060    
061            private final int _capacity;
062            private final LinkedList<E> _linkedList = new LinkedList<>();
063    
064    }