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

org.boon.cache.SimpleCache Maven / Gradle / Ivy

Go to download

Simple opinionated Java for the novice to expert level Java Programmer. Low Ceremony. High Productivity. A real boon to Java to developers!

There is a newer version: 0.34
Show newest version
package org.boon.cache;


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


/* This supports both LRU and FIFO */
public class SimpleCache implements Cache {

    Map map = new LinkedHashMap();


    private static class InternalCacheLinkedList extends LinkedHashMap {
        final int limit;

        InternalCacheLinkedList( final int limit, final boolean lru ) {
            super( 16, 0.75f, lru );
            this.limit = limit;
        }

        protected final boolean removeEldestEntry( final Map.Entry eldest ) {
            return super.size() > limit;
        }
    }


    public SimpleCache( final int limit, CacheType type ) {

        if ( type.equals( CacheType.LRU ) ) {
            map = new InternalCacheLinkedList<>( limit, true );
        } else {
            map = new InternalCacheLinkedList<>( limit, false );
        }
    }

    public SimpleCache( final int limit ) {

        map = new InternalCacheLinkedList<>( limit, true );

    }

    @Override
    public void put( K key, V value ) {
        map.put( key, value );
    }

    @Override
    public V get( K key ) {
        return map.get( key );
    }

    //For testing only
    @Override
    public V getSilent( K key ) {
        V value = map.get( key );
        if ( value != null ) {
            map.remove( key );
            map.put( key, value );
        }
        return value;
    }

    @Override
    public void remove( K key ) {
        map.remove( key );
    }

    @Override
    public int size() {
        return map.size();
    }

    public String toString() {
        return map.toString();
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy