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.Set; 024 025public class SetMaxSize<E> implements Set<E> { 026 027 private int maxSize; 028 029 private final LinkedHashMapMaxSize<E, String> map; 030 031 public SetMaxSize(int maxSize){ 032 this(maxSize,new LinkedHashMapMaxSize<E, String>(maxSize)); 033 } 034 private SetMaxSize(int maxSize, LinkedHashMapMaxSize<E, String> map){ 035 this.maxSize=maxSize; 036 this.map=map; 037 } 038 039 @Override 040 public Iterator<E> iterator() { 041 return map.keySet().iterator(); 042 } 043 044 @Override 045 public int size() { 046 return map.size(); 047 } 048 049 @Override 050 public boolean isEmpty() { 051 return map.isEmpty(); 052 } 053 054 @Override 055 public boolean contains(Object o) { 056 return map.containsKey(o); 057 } 058 059 @Override 060 public boolean add(E e) { 061 map.put(e,""); 062 return true; 063 } 064 065 @Override 066 public boolean remove(Object o) { 067 return map.remove(o)!=null; 068 } 069 070 @Override 071 public void clear() { 072 map.clear(); 073 } 074 075 @Override 076 public Object clone() { 077 return new SetMaxSize<E>(maxSize,(LinkedHashMapMaxSize<E, String>)map.clone()); 078 } 079 080 @Override 081 public boolean equals(Object o) { 082 if(!(o instanceof SetMaxSize)) return false; 083 SetMaxSize other=(SetMaxSize) o; 084 return ((SetMaxSize)o).map.equals(map); 085 } 086 087 @Override 088 public int hashCode() { 089 return map.hashCode(); 090 } 091 092 @Override 093 public boolean removeAll(Collection<?> c) { 094 throw new UnsupportedOperationException(); 095 } 096 097 @Override 098 public Object[] toArray() { 099 return map.keySet().toArray(); 100 } 101 102 @Override 103 public <T> T[] toArray(T[] a) { 104 return map.keySet().toArray(a); 105 } 106 107 @Override 108 public boolean containsAll(Collection<?> c) { 109 throw new UnsupportedOperationException(); 110 } 111 112 @Override 113 public boolean addAll(Collection<? extends E> c) { 114 throw new UnsupportedOperationException(); 115 } 116 117 @Override 118 public boolean retainAll(Collection<?> c) { 119 throw new UnsupportedOperationException(); 120 } 121 122 @Override 123 public String toString() { 124 return map.keySet().toString(); 125 } 126}