001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  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     */
017    package org.apache.activemq.util;
018    
019    import java.util.AbstractSet;
020    import java.util.Iterator;
021    import java.util.Set;
022    
023    /**
024     * A Simple LRU Set
025     * 
026     * 
027     * @param <K>
028     * @param <V>
029     */
030    
031    public class LRUSet<E>
032    extends AbstractSet<E>
033    implements Set<E>, Cloneable, java.io.Serializable{
034       
035        private static final Object IGNORE = new Object();
036       
037        private final LRUCache cache;
038    
039        /**
040         * Default constructor for an LRU Cache The default capacity is 10000
041         */
042        public LRUSet() {
043            this(0,10000, 0.75f, true);
044        }
045    
046        /**
047         * Constructs a LRUCache with a maximum capacity
048         * 
049         * @param maximumCacheSize
050         */
051        public LRUSet(int maximumCacheSize) {
052            this(0, maximumCacheSize, 0.75f, true);
053        }
054    
055        /**
056         * Constructs an empty <tt>LRUCache</tt> instance with the specified
057         * initial capacity, maximumCacheSize,load factor and ordering mode.
058         * 
059         * @param initialCapacity
060         *            the initial capacity.
061         * @param maximumCacheSize
062         * @param loadFactor
063         *            the load factor.
064         * @param accessOrder
065         *            the ordering mode - <tt>true</tt> for access-order,
066         *            <tt>false</tt> for insertion-order.
067         * @throws IllegalArgumentException
068         *             if the initial capacity is negative or the load factor is
069         *             non-positive.
070         */
071    
072        public LRUSet(int initialCapacity, int maximumCacheSize, float loadFactor, boolean accessOrder) {
073            this.cache = new LRUCache<E,Object>(initialCapacity,maximumCacheSize,loadFactor,accessOrder);
074        }
075    
076       
077        public Iterator<E> iterator() {
078        return cache.keySet().iterator();
079        }
080    
081       
082        public int size() {
083        return cache.size();
084        }
085    
086       
087        public boolean isEmpty() {
088        return cache.isEmpty();
089        }
090    
091        public boolean contains(Object o) {
092        return cache.containsKey(o);
093        }
094    
095       
096        public boolean add(E o) {
097        return cache.put(o, IGNORE)==null;
098        }
099    
100        public boolean remove(Object o) {
101        return cache.remove(o)==IGNORE;
102        }
103    
104        
105        public void clear() {
106        cache.clear();
107        }
108    
109        
110    
111        
112          
113    }