Package org.chaidb.db.helper.cache.algorithm

Source Code of org.chaidb.db.helper.cache.algorithm.FIFO

/*
* Copyright (C) 2006  http://www.chaidb.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
*/

package org.chaidb.db.helper.cache.algorithm;

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

public class FIFO implements ReplacementAlgorithm {
    public static final String ALGORITHM_NAME = "FIFO";

    public FIFO(int cacheSize) {
        if (cacheSize <= 0) {
            throw new IllegalArgumentException("cacheSize must be larger than 0");
        } else {
            this.sizeBoundary = cacheSize;
            this.map = new LinkedHashMap(cacheSize + 1, 1, false) {
                protected boolean removeEldestEntry(Map.Entry eldest) {
                    return size() > sizeBoundary;
                }
            };
        }
    }

    public int getSizeBoundary() {
        return this.sizeBoundary;
    }

    public boolean containsKey(Object key) {
        return map.containsKey(key);
    }

    public Object put(Object key, Object value) {
        return map.put(key, value);
    }

    public Object get(Object key) {
        return map.get(key);
    }

    public Object remove(Object key) {
        return map.remove(key);
    }

    public void clear() {
        map.clear();
    }

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

    private final int sizeBoundary;
    private LinkedHashMap map;
}
TOP

Related Classes of org.chaidb.db.helper.cache.algorithm.FIFO

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.