001package org.apache.commons.jcs.utils.struct;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.concurrent.atomic.AtomicInteger;
023
024/**
025 *
026 * @author Wiktor Niesiobędzki
027 *
028 *         Simple LRUMap implementation that keeps the number of the objects below or equal maxObjects
029 *
030 * @param <K>
031 * @param <V>
032 */
033public class LRUMap<K, V> extends AbstractLRUMap<K, V>
034{
035
036    /** if the max is less than 0, there is no limit! */
037    int maxObjects = -1;
038    AtomicInteger counter = new AtomicInteger(0);
039
040    public LRUMap()
041    {
042        super();
043    }
044
045    /**
046     *
047     * @param maxObjects
048     *            maximum number to keep in the map
049     */
050    public LRUMap(int maxObjects)
051    {
052        super();
053        this.maxObjects = maxObjects;
054    }
055
056    @Override
057    public boolean shouldRemove()
058    {
059        return maxObjects > 0 && this.size() > maxObjects;
060    }
061
062    public Object getMaxCounter()
063    {
064        return maxObjects;
065    }
066}