001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.util.set; 031 032import java.util.Collection; 033import java.util.Comparator; 034import java.util.TreeSet; 035 036/** 037 * Extension of a {@link TreeSet} that has a bounded upper size. 038 * Once this size is reached new objects will only be added if they 039 * are less than the last element in the set (according to the @{Comparable} 040 * interface or the given {@link Comparator}). If the new object 041 * is eligable to be added, the last item is automatically removed. 042 * 043 * Note: do not use this class as a form of priority queue if you 044 * expect items with equal priorities. Because this class is a form 045 * of set, items with equal priorities would be considered equal, 046 * an only one of them would be admitted to the set. 047 * 048 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 049 * 050 * @param <T> The type of object 051 */ 052public class BoundedTreeSet<T> extends TreeSet<T> { 053 private static final long serialVersionUID = 1L; 054 055 protected int maxSize; 056 057 /** 058 * Constructs a new, empty tree set, sorted according to the 059 * natural ordering of its elements. All elements inserted into 060 * the set must implement the {@link Comparable} interface. 061 * Furthermore, all such elements must be <i>mutually 062 * comparable</i>: {@code e1.compareTo(e2)} must not throw a 063 * {@code ClassCastException} for any elements {@code e1} and 064 * {@code e2} in the set. If the user attempts to add an element 065 * to the set that violates this constraint (for example, the user 066 * attempts to add a string element to a set whose elements are 067 * integers), the {@code add} call will throw a 068 * {@code ClassCastException}. 069 * 070 * The set additionally has a maximum size. Once this size is reached 071 * new objects will only be added if they are less than the last element 072 * in the set (according to the @{Comparable} interface). If the new object 073 * is eligable to be added, the last item is automatically removed. 074 * 075 * @param maxSize The maximum allowed number of elements. 076 */ 077 public BoundedTreeSet(int maxSize) { 078 super(); 079 this.maxSize = maxSize; 080 } 081 082 /** 083 * Constructs a new, empty tree set, sorted according to the specified 084 * comparator. All elements inserted into the set must be <i>mutually 085 * comparable</i> by the specified comparator: {@code comparator.compare(e1, 086 * e2)} must not throw a {@code ClassCastException} for any elements 087 * {@code e1} and {@code e2} in the set. If the user attempts to add 088 * an element to the set that violates this constraint, the 089 * {@code add} call will throw a {@code ClassCastException}. 090 * 091 * The set additionally has a maximum size. Once this size is reached 092 * new objects will only be added if they are less than the last element 093 * in the set (according to the @{Comparable} interface). If the new object 094 * is eligable to be added, the last item is automatically removed. 095 * 096 * @param maxSize The maximum allowed number of elements. 097 * @param comparator the comparator that will be used to order this set. 098 * If {@code null}, the {@linkplain Comparable natural 099 * ordering} of the elements will be used. 100 */ 101 public BoundedTreeSet(int maxSize, Comparator<? super T> comparator) { 102 super(comparator); 103 this.maxSize = maxSize; 104 } 105 106 @Override 107 public boolean add(T e) { 108 if (contains(e)) return false; 109 110 if (this.size() < maxSize) { 111 return super.add(e); 112 } 113 114 T last = this.last(); 115 116 if (this.comparator() == null) { 117 @SuppressWarnings("unchecked") 118 Comparable<? super T> key = (Comparable<? super T>) last; 119 120 if (key.compareTo(e) > 0) { 121 this.remove(last); 122 return super.add(e); 123 } 124 } else { 125 if (this.comparator().compare(last, e) > 0) { 126 this.remove(last); 127 return super.add(e); 128 } 129 } 130 131 return false; 132 } 133 134 @Override 135 public boolean addAll(Collection<? extends T> c) { 136 boolean changed = false; 137 138 for (T e : c) { 139 if (add(e)) 140 changed = true; 141 } 142 143 return changed; 144 } 145}