001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.utils.collections;
020
021import java.util.LinkedHashMap;
022import java.util.Map.Entry;
023
024/**
025 * A cache with an LRU eviction policy.
026 * @param <K> the type fo the keys.
027 * @param <V> the type fo the values.
028 * @author Laurent Cohen
029 */
030public class LRUMap<K, V> extends LinkedHashMap<K, V> {
031  /**
032   * Explicit serialVersionUID.
033   */
034  private static final long serialVersionUID = 1L;
035  /**
036   * The maximum cache capacity
037   */
038  private final int maxCapacity;
039
040  /**
041   * Initialize this cache with a default max capacity of 1024.
042   */
043  public LRUMap() {
044    this(1024);
045  }
046
047  /**
048   * Initialize this cache with the specified max capacity.
049   * @param maxCapacity the maximum capacity of this cache.
050   */
051  public LRUMap(final int maxCapacity) {
052    this.maxCapacity = maxCapacity;
053  }
054
055  @Override
056  protected boolean removeEldestEntry(final Entry<K, V> eldest) {
057    return size() >= maxCapacity;
058  }
059}