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

io.github.palexdev.imcache.cache.Cache Maven / Gradle / Ivy

package io.github.palexdev.imcache.cache;

import java.util.*;

public abstract class Cache implements ICache {
    //================================================================================
    // Properties
    //================================================================================
    protected final SequencedMap cache;
    protected int capacity = 10;

    //================================================================================
    // Constructors
    //================================================================================
    public Cache() {
        this.cache = new LinkedHashMap<>();
    }

    protected Cache(SequencedMap cache) {
        this.cache = cache;
    }

    //================================================================================
    // Overridden Methods
    //================================================================================
    @Override
    public Iterator> iterator() {
        return asMap().entrySet().iterator();
    }

    @Override
    public void clear() {
        cache.clear();
    }

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

    @Override
    public Map asMap() {
        return Collections.unmodifiableMap(cache);
    }

    //================================================================================
    // Getters/Setters
    //================================================================================
    @Override
    public int getCapacity() {
        return capacity;
    }

    @Override
    public ICache setCapacity(int capacity) {
        while (size() > capacity) removeOldest();
        this.capacity = capacity;
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy