com.xceptance.common.collection.LRUHashMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xlt Show documentation
Show all versions of xlt Show documentation
XLT (Xceptance LoadTest) is an extensive load and performance test tool developed and maintained by Xceptance.
The newest version!
/*
* Copyright (c) 2005-2024 Xceptance Software Technologies GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xceptance.common.collection;
import java.util.LinkedHashMap;
import java.util.Map;
/**
*
* LRUHashMap implementation that behaves exactly like a normal HashMap. The size of the map is always limited to
* maxSize. This prevents the map from growing. Any new entry will remove the least frequently used one from the map
* (LRU).
*
*
* An entry is moved to the top of the list at any get or put operation. The get operation does not structurally modify
* the map and therefore does not throw an exception in case of iterating and doing gets on values.
*
*
* @param
* type of map entry keys
* @param
* type of map entry values
* @author Rene Schwietzke
*/
public class LRUHashMap extends LinkedHashMap
{
/**
* The serialVersionUID.
*/
private static final long serialVersionUID = -6633745418108562019L;
/**
* The initial size of the new map.
*/
private static final int INITIAL_SIZE = 3;
/**
* The rehashing default fill size.
*/
private static final float INITIAL_REHASHING_SIZE = 0.75f;
/**
* Maximum size of hashmap, limits the amount of content.
*/
private final int maxSize;
/**
* Constructs a new, empty map with the specified maxSize.
*
* @param max
* the size limit of the map
*/
public LRUHashMap(final int max)
{
super(INITIAL_SIZE, INITIAL_REHASHING_SIZE, true);
this.maxSize = max;
}
/**
* Constructs a new map with data from the another map.
*
* @param map
* another map to copy data from
* @param max
* the size limit of the map
*/
public LRUHashMap(final Map map, final int max)
{
super(INITIAL_SIZE, INITIAL_REHASHING_SIZE, true);
this.maxSize = max;
putAll(map);
}
/**
* Returns true if this map should remove its eldest entry.
*
* @param eldest
* The least recently inserted entry in the map, or if this is an access-ordered map, the least recently
* accessed entry. This is the entry that will be removed if this method returns true. If the
* map was empty prior to the put or putAll invocation resulting in this invocation,
* this will be the entry that was just inserted; in other words, if the map contains a single entry, the
* eldest entry is also the newest.
* @return true if the eldest entry should be removed from the map; false if it should be
* retained.
*/
@Override
protected final boolean removeEldestEntry(final Map.Entry eldest)
{
return size() > maxSize;
}
}