All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.helger.commons.collection.impl.SoftLinkedHashMap Maven / Gradle / Ivy

/**
 * Copyright (C) 2014-2015 Philip Helger (www.helger.com)
 * philip[at]helger[dot]com
 *
 * 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.helger.commons.collection.impl;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;

import com.helger.commons.annotation.OverrideOnDemand;

/**
 * Soft {@link HashMap} implementation based on
 * http://www.javaspecialists.eu/archive/Issue015.html
* The entrySet implementation is from * org.hypergraphdb.util * * @author Philip Helger * @param * Key type * @param * Value type */ public class SoftLinkedHashMap extends AbstractSoftMap { private final int m_nMaxSize; private static interface IRemoveEldest { boolean removeEldestSoftEntry (Map.Entry aEntry); } private static class InternalLinkedHashMap extends LinkedHashMap > { // Note: 0.75f is the same as HashMap.DEFAULT_LOAD_FACTOR private static final float DEFAULT_LOAD_FACTOR = 0.75f; private IRemoveEldest m_aCallback; public InternalLinkedHashMap (@Nonnegative final int nMaxSize) { super (nMaxSize, DEFAULT_LOAD_FACTOR, true); } @Override protected final boolean removeEldestEntry (@Nonnull final Map.Entry > aEldest) { final MapEntry aEntry = new MapEntry (aEldest.getKey (), aEldest.getValue ().get ()); return m_aCallback.removeEldestSoftEntry (aEntry); } } public SoftLinkedHashMap (@Nonnegative final int nMaxSize) { super (new InternalLinkedHashMap (nMaxSize)); ((InternalLinkedHashMap ) m_aSrcMap).m_aCallback = new IRemoveEldest () { public boolean removeEldestSoftEntry (final Map.Entry aEldest) { final int nSize = size (); if (nSize <= m_nMaxSize) { // No need to remove anything return false; } // Invoke protected method onRemoveEldestEntry (nSize, aEldest); return true; } }; m_nMaxSize = nMaxSize; } /** * @return The maximum number of elements that can reside inside this object. * Never < 0. */ @Nonnegative public final int getMaxSize () { return m_nMaxSize; } /** * Protected method that is invoked every time the oldest entry is removed. * * @param nSize * Current size of the map. Always ≥ 0. * @param aEldest * The map entry that is removed. Never null. */ @OverrideOnDemand protected void onRemoveEldestEntry (@Nonnegative final int nSize, @Nonnull final Map.Entry aEldest) {} }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy