001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.commons.collection; 020 021import java.util.Collection; 022import java.util.Iterator; 023import java.util.LinkedList; 024import java.util.Queue; 025 026public class QueueMaxSize<E> implements Queue<E> { 027 028 private int maxSize; 029 private LinkedList<E> list=new LinkedList<E>(); 030 031 public QueueMaxSize(int maxSize){ 032 this.maxSize=maxSize; 033 } 034 035 036 @Override 037 public boolean add(E e) { 038 if(!list.add(e)) return false; 039 while (size() > maxSize) { list.remove(); } 040 return true; 041 } 042 043 public int size() { 044 return list.size(); 045 } 046 047 public boolean contains(Object o) { 048 return list.contains(o); 049 } 050 051 public Iterator<E> iterator() { 052 return list.iterator(); 053 } 054 055 public <T> T[] toArray(T[] a) { 056 return list.toArray(a); 057 } 058 059 public boolean remove(Object o) { 060 return list.remove(o); 061 } 062 063 public void clear() { 064 list.clear(); 065 } 066 067 public E remove() { 068 return list.remove(); 069 } 070 071 public E poll() { 072 return list.poll(); 073 } 074 075 public E element() { 076 return list.element(); 077 } 078 079 public E peek() { 080 throw new UnsupportedOperationException(); 081 } 082 083 084 @Override 085 public boolean isEmpty() { 086 throw new UnsupportedOperationException(); 087 } 088 089 090 @Override 091 public Object[] toArray() { 092 throw new UnsupportedOperationException(); 093 } 094 095 096 @Override 097 public boolean containsAll(Collection<?> c) { 098 throw new UnsupportedOperationException(); 099 } 100 101 102 @Override 103 public boolean addAll(Collection<? extends E> c) { 104 throw new UnsupportedOperationException(); 105 } 106 107 108 @Override 109 public boolean removeAll(Collection<?> c) { 110 throw new UnsupportedOperationException(); 111 } 112 113 114 @Override 115 public boolean retainAll(Collection<?> c) { 116 throw new UnsupportedOperationException(); 117 } 118 119 120 @Override 121 public boolean offer(E e) { 122 throw new UnsupportedOperationException(); 123 } 124}