Mega Code Archive

 
Categories / Java / Development Class
 

The class that implements a simple LRU cache

/*  * Copyright 2008 the original author or authors.  *  * 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 org.dopdf.utils; import java.util.LinkedHashMap; import java.util.Map; /**  * The class that implements a simple LRU cache.  *  * @param <K> the type of key that will be held by this LRU cache.  * @param <V> the type of value held by this LRU cache.  * @author Deep Shah  */ public class LRUCache<K, V> {     /** The backing linked hash map object that will implement the LRU algorithm for us. */     private static final LinkedHashMap cache = new LinkedHashMap(Constants.Cache.MAX_CAPACITY,             Constants.Cache.LOAD_FACTOR, true) {         /**          * {@inheritDoc}          */         @Override         protected boolean removeEldestEntry(final Map.Entry eldest) {             return size() > Constants.Cache.SIZE;         }     };     /**      * Puts the key value pair in the LinkedHashmap.      * @param key the key value to put in the map.      * @param value the value to put in the map.      */     public void put(final K key, final V value) {         cache.put(key, value);     }     /**      * The method that gets the value from the LRU cache for the given key.  If the value is not found null is returned.      * @param key the key for which we have to find the value in the cache.      * @return The value matching the passed in key.  Null if no match is found.      */     public V get(final K key) {         return (V) cache.get(key);     }     /**      * The method that returns true if the cache holds the given key.      * @param key the key which we have to find in the cache.      * @return true if the value against the given key is found, false otherwise.      */     public boolean containsKey(final K key) {         return cache.containsKey(key);     } } class Constants {     /**      * The class that holds constatns for the cache class      *      * @author Deep Shah      */     public class Cache {         /**          * The load factor for the cache.          */         public static final float LOAD_FACTOR = 0.75f;         /**          * The max size of the cahce.          */         public static final int SIZE = 15;         /**          * The max capacity of the cahce.          */         public static final int MAX_CAPACITY = ((int) (SIZE / LOAD_FACTOR) + 1);     }     /**      * The class that holds the constants for the resources.      *      * @author Deep Shah      */     public class Resource {         /**          * The content type of jpeg images.          */         public static final String JPEG_CONTENT_TYPE = "image/jpeg";         /**          * The content type of png images.          */         public static final String PNG_CONTENT_TYPE = "image/png";         /**          * The content type of pdf document.          */         public static final String PDF_CONTENT_TYPE = "application/pdf";         /**          * The constant that represents the jpeg format.          */         public static final String JPEG_FORMAT = "jpeg";         /**          * The constant that represents the png format.          */         public static final String PNG_FORMAT = "png";         /**          * Text plain content type.          */         public static final String TEXT_CONTENT_TYPE = "text/plain";         /**          * The constant that represents the javascript format.          */         public static final String JAVASCRIPT_FORMAT = "js";         /**          * The constant that represents the css format.          */         public static final String CSS_FORMAT = "css";         /**          * The string pattern that identifies that the request is for a static resource.          */         public static final String STATIC_RESOURCE_PATTERN = ".+/static/.+(js|css|png)$";         /**          * The string pattern that identifies the download request.          */         public static final String DOWNLOAD_RESOURCE_PATTERN = ".+download/document*";         /**          * The pattern that identifies the pdf page request.          */         public static final String PDF_PAGE_RESOURCE_PATTERN = ".+/page/show*";     }     /**      * The class that holds he PDFPage related constants.      */     public class PDFPage {         /**          * The constant representing the id key.          */         public static final String ID_KEY = "id";         /**          * The constant representing the strategy key.          */         public static final String STRATEGY_KEY = "strategy";         /**          * The constant representing the Page key.          */         public static final String PAGE_KEY = "page";         /**          * The size of the page to be rendered.          */         public static final String SIZE_KEY = "size";     }     /**      * The constants for the DocumentHashingStrategy.      *      * @author Deep Shah      */     public enum PDFFetchingStrategy {         /**          * The constant that represents the LRU strategy.          */         LRU,         /**          * The constant that represents the SESSION strategy.          */         SESSION;         /**          * The method that returns an instance of PDFFetchingStrategy based on the strategy name passed as argument.          *          * @param pdfFetchingStrategyName the name of the strategy to use.  If null or empty is passed LRU will be returned.          * @return The reference of PDFFetchingStrategy that matches the name passed.          */         public static PDFFetchingStrategy parse(final String pdfFetchingStrategyName) {             if (pdfFetchingStrategyName == null                     || pdfFetchingStrategyName.equals("")                     || pdfFetchingStrategyName.equalsIgnoreCase(Constants.General.UNDEFINED)) return LRU;             return valueOf(pdfFetchingStrategyName.toUpperCase());         }     }     /**      * The enum that holds various page sizes.      *      * @author Deep Shah      */     public enum PageSize {         /**          * The small page size.          */         SMALL(392, 554),         /**          * The medium page size.          */         MEDIUM(617, 873),         /**          * The large page size.          */         LARGE(853, 1207);         /**          * The width represented by this size.          */         private int width;         /**          * The height represented by this size.          */         private int height;         /**          * The constructor that takes in width and height as argument.          *          * @param width  the width.          * @param height the height.          */         private PageSize(final int width, final int height) {             this.width = width;             this.height = height;         }         /**          * The method that will return an instance of page size based on the string value of size passed.          * If null of empty string is passed medium will be returned.          *          * @param size the string value of size to use.          * @return the PageSize reference that matches the size passed in.          */         public static PageSize parse(final String size) {             if (null == size || size.equals("") || size.equalsIgnoreCase(Constants.General.UNDEFINED)) return MEDIUM;             return valueOf(size.toUpperCase());         }         /**          * The getter for the width.          *          * @return the value held.          */         public int getWidth() {             return width;         }         /**          * The getter for the height.          *          * @return the value held.          */         public int getHeight() {             return height;         }     }     /**      * The class that holds information about the general constants.      *      * @author Deep Shah      */     public class General {         /**          * The undefined value constant.          */         public static final String UNDEFINED = "UNDEFINED";     } }