diff options
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/util/LRUMap.java')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/util/LRUMap.java | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/LRUMap.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/LRUMap.java new file mode 100644 index 0000000000..14f7f1f181 --- /dev/null +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/LRUMap.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2018, Konrad Windszus <konrad_w@gmx.de> and others + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +package org.eclipse.jgit.util; + +import java.util.LinkedHashMap; + +/** + * Map with only up to n entries. If a new entry is added so that the map + * contains more than those n entries the least-recently used entry is removed + * from the map. + * + * @param <K> + * the type of keys maintained by this map + * @param <V> + * the type of mapped values + * + * @since 5.4 + */ +public class LRUMap<K, V> extends LinkedHashMap<K, V> { + + private static final long serialVersionUID = 4329609127403759486L; + + private final int limit; + + /** + * Constructs an empty map which may contain at most the given amount of + * entries. + * + * @param initialCapacity + * the initial capacity + * @param limit + * the number of entries the map should have at most + */ + public LRUMap(int initialCapacity, int limit) { + super(initialCapacity, 0.75f, true); + this.limit = limit; + } + + @Override + protected boolean removeEldestEntry(java.util.Map.Entry<K, V> eldest) { + return size() > limit; + } +} |