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

com.ibm.cloud.objectstorage.internal.FIFOCache Maven / Gradle / Ivy

Go to download

A single bundled dependency that includes all service and dependent JARs with third-party libraries relocated to different namespaces.

There is a newer version: 2.13.4
Show newest version
/*
 * Copyright 2014-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file 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.ibm.cloud.objectstorage.internal;

import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;

import com.ibm.cloud.objectstorage.annotation.ThreadSafe;

/**
 * A bounded cache that has a FIFO eviction policy when the cache is full.
 *
 * @param 
 *            value type
 */
@ThreadSafe
public final class FIFOCache {
    private final BoundedLinkedHashMap map;
    private final ReadLock rlock;
    private final WriteLock wlock;

    /**
     * @param maxSize
     *            the maximum number of entries of the cache
     */
    public FIFOCache(final int maxSize) {
        if (maxSize < 1) {
            throw new IllegalArgumentException("maxSize " + maxSize
                    + " must be at least 1");
        }
        map = new BoundedLinkedHashMap(maxSize);
        ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
        rlock = lock.readLock();
        wlock = lock.writeLock();
    }

    /**
     * Adds an entry to the cache, evicting the earliest entry if necessary.
     */
    public T add(String key, T value) {
        wlock.lock();
        try {
            return map.put(key, value);
        } finally {
            wlock.unlock();
        }
    }

    /** Returns the value of the given key; or null of no such entry exists. */
    public T get(String key) {
        rlock.lock();
        try {
            return map.get(key);
        } finally {
            rlock.unlock();
        }
    }

    /**
     * Returns the current size of the cache.
     */
    public int size() {
        rlock.lock();
        try {
            return map.size();
        } finally {
            rlock.unlock();
        }
    }

    /**
     * Returns the maximum size of the cache.
     */
    public int getMaxSize() {
        return map.getMaxSize();
    }

    @Override
    public String toString() {
        rlock.lock();
        try {
            return map.toString();
        } finally {
            rlock.unlock();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy