001package com.hfg.util.collection; 002 003import java.util.ArrayList; 004import java.util.Collection; 005import java.util.Set; 006 007//------------------------------------------------------------------------------ 008/** 009 * Set that preserves the addition order. 010 * @author J. Alex Taylor, hairyfatguy.com 011 */ 012//------------------------------------------------------------------------------ 013// com.hfg Library 014// 015// This library is free software; you can redistribute it and/or 016// modify it under the terms of the GNU Lesser General Public 017// License as published by the Free Software Foundation; either 018// version 2.1 of the License, or (at your option) any later version. 019// 020// This library is distributed in the hope that it will be useful, 021// but WITHOUT ANY WARRANTY; without even the implied warranty of 022// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 023// Lesser General Public License for more details. 024// 025// You should have received a copy of the GNU Lesser General Public 026// License along with this library; if not, write to the Free Software 027// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 028// 029// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 030// jataylor@hairyfatguy.com 031//------------------------------------------------------------------------------ 032 033public class OrderedSet<E> extends ArrayList<E> implements Set<E> 034{ 035 //########################################################################## 036 // CONSTRUCTORS 037 //########################################################################## 038 039 //-------------------------------------------------------------------------- 040 public OrderedSet() 041 { 042 super(); 043 } 044 045 //-------------------------------------------------------------------------- 046 public OrderedSet(int inInitialCapacity) 047 { 048 super(inInitialCapacity); 049 } 050 051 //-------------------------------------------------------------------------- 052 public OrderedSet(Collection<? extends E> inValues) 053 { 054 super(inValues != null ? inValues.size() : 99); 055 addAll(inValues); 056 } 057 058 //########################################################################## 059 // PUBLIC METHODS 060 //########################################################################## 061 062 063 //-------------------------------------------------------------------------- 064 public boolean add(E inObj) 065 { 066 boolean result = false; 067 068 if (! contains(inObj)) 069 { 070 super.add(inObj); 071 result = true; 072 } 073 074 return result; 075 } 076 077 //-------------------------------------------------------------------------- 078 public void add(int inIndex, E inObj) 079 { 080 if (! contains(inObj)) 081 { 082 super.add(inIndex, inObj); 083 } 084 } 085 086 //-------------------------------------------------------------------------- 087 public boolean addAll(Collection<? extends E> inCollection) 088 { 089 boolean changed = false; 090 if (CollectionUtil.hasValues(inCollection)) 091 { 092 for (E value : inCollection) 093 { 094 if (add(value)) changed = true; 095 } 096 } 097 098 return changed; 099 } 100 101 //-------------------------------------------------------------------------- 102 @Override 103 public boolean addAll(int inIndex, Collection<? extends E> inCollection) 104 { 105 boolean changed = false; 106 for (E value : inCollection) 107 { 108 changed = ! contains(value); 109 add(inIndex++, value); 110 } 111 112 return changed; 113 } 114 115 //-------------------------------------------------------------------------- 116 @Override 117 public E set(int inIndex, E inElement) 118 { 119 int existingIndex = indexOf(inElement); 120 121 super.set(inIndex, inElement); 122 123 if (existingIndex >= 0) 124 { 125 remove(existingIndex); 126 } 127 128 return null; 129 } 130 131 //-------------------------------------------------------------------------- 132 public E first() 133 { 134 return (CollectionUtil.hasValues(this) ? get(0) : null); 135 } 136 137 //-------------------------------------------------------------------------- 138 public E last() 139 { 140 return (CollectionUtil.hasValues(this) ? get(size() - 1) : null); 141 } 142 143}