001package com.hfg.util.collection; 002 003 004import java.util.ArrayList; 005import java.util.Collection; 006import java.util.Collections; 007 008//------------------------------------------------------------------------------ 009/** 010 List of Comparable objects that stays sorted. 011 <div> 012 @author J. Alex Taylor, hairyfatguy.com 013 </div> 014 */ 015//------------------------------------------------------------------------------ 016// com.hfg XML/HTML Coding Library 017// 018// This library is free software; you can redistribute it and/or 019// modify it under the terms of the GNU Lesser General Public 020// License as published by the Free Software Foundation; either 021// version 2.1 of the License, or (at your option) any later version. 022// 023// This library is distributed in the hope that it will be useful, 024// but WITHOUT ANY WARRANTY; without even the implied warranty of 025// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 026// Lesser General Public License for more details. 027// 028// You should have received a copy of the GNU Lesser General Public 029// License along with this library; if not, write to the Free Software 030// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 031// 032// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 033// jataylor@hairyfatguy.com 034//------------------------------------------------------------------------------ 035 036public class SortedList<T extends Comparable<T>> extends ArrayList<T> 037{ 038 039 //########################################################################### 040 // CONSTRUCTORS 041 //########################################################################### 042 043 //--------------------------------------------------------------------------- 044 public SortedList() 045 { 046 super(); 047 } 048 049 //--------------------------------------------------------------------------- 050 public SortedList(int inInitialCapacity) 051 { 052 super(inInitialCapacity); 053 } 054 055 //--------------------------------------------------------------------------- 056 public SortedList(Collection<T> inCollection) 057 { 058 super(); 059 addAll(inCollection); 060 } 061 062 //########################################################################### 063 // PUBLIC METHODS 064 //########################################################################### 065 066 //--------------------------------------------------------------------------- 067 public T last() 068 { 069 return get(size() - 1); 070 } 071 072 //--------------------------------------------------------------------------- 073 public void removeLast() 074 { 075 remove(size() - 1); 076 } 077 078 //--------------------------------------------------------------------------- 079 public void truncate(int maxObjectsToRetain) 080 { 081 if (size() > maxObjectsToRetain) 082 { 083 removeRange(maxObjectsToRetain, size()); 084 } 085 } 086 087 //--------------------------------------------------------------------------- 088 @Override 089 public boolean add(T inObj) 090 { 091 int result = Collections.binarySearch(this, inObj); 092 super.add((result < 0 ? - result - 1 : result + 1), inObj); 093 return true; 094 } 095 096 //--------------------------------------------------------------------------- 097 @Override 098 public void add(int inIndex, T inObj) 099 { 100 add(inObj); 101 } 102 103 //--------------------------------------------------------------------------- 104 @Override 105 public boolean addAll(Collection<? extends T> inObjs) 106 { 107 if (inObjs != null) 108 { 109 super.addAll(inObjs); 110 Collections.sort(this); 111 } 112 113 return true; 114 } 115}