001/*-------------------------------------------------------------------------+ 002| | 003| Copyright 2005-2011 The ConQAT Project | 004| | 005| Licensed under the Apache License, Version 2.0 (the "License"); | 006| you may not use this file except in compliance with the License. | 007| You may obtain a copy of the License at | 008| | 009| http://www.apache.org/licenses/LICENSE-2.0 | 010| | 011| Unless required by applicable law or agreed to in writing, software | 012| distributed under the License is distributed on an "AS IS" BASIS, | 013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 014| See the License for the specific language governing permissions and | 015| limitations under the License. | 016+-------------------------------------------------------------------------*/ 017package org.conqat.lib.commons.collections; 018 019import java.util.Comparator; 020import java.util.PriorityQueue; 021 022import org.conqat.lib.commons.assertion.CCSMAssert; 023 024/** 025 * Priority queue that retains only the n last/largest elements. Order is 026 * determined by the employed comparator. 027 */ 028public class BoundedPriorityQueue<T> extends PriorityQueue<T> { 029 030 /** Version used for serialization. */ 031 private static final long serialVersionUID = 1; 032 033 /** Capacity of the collection */ 034 private int capacityBound; 035 036 /** 037 * Constructor using natural order 038 * 039 * @param capacityBound 040 * maximal number of elements that are retained 041 */ 042 public BoundedPriorityQueue(int capacityBound) { 043 super(capacityBound); 044 setCapacityBound(capacityBound); 045 } 046 047 /** Assert that capacityBound is positive and store in field */ 048 private void setCapacityBound(int capacityBound) throws AssertionError { 049 CCSMAssert.isTrue(capacityBound > 0, "Capacity bound must be positive but was: " + capacityBound); 050 this.capacityBound = capacityBound; 051 } 052 053 /** 054 * Constructor. 055 * 056 * @param capacityBound 057 * maximal number of elements that are retained 058 * 059 * @param comparator 060 * Comparator used to compare elements 061 */ 062 public BoundedPriorityQueue(int capacityBound, Comparator<T> comparator) { 063 super(capacityBound, comparator); 064 setCapacityBound(capacityBound); 065 } 066 067 /** Add a single element */ 068 @Override 069 public boolean offer(T element) { 070 boolean result = super.offer(element); 071 072 if (size() > capacityBound) { 073 remove(); 074 } 075 CCSMAssert.isTrue(size() <= capacityBound, "Size exceeds capacity bound"); 076 077 return result; 078 } 079}