Cache.java 1.8 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.pdfg.common;

import java.util.ArrayList;

public class Cache {
    protected ArrayList cacheKeys = null;
    protected ArrayList cacheValues = null;
    protected int maxEntries = 1;

    public Cache(int maxEntries) {
        if (maxEntries >= 1) {
            this.maxEntries = maxEntries;
        }
        this.cacheKeys = new ArrayList(maxEntries);
        this.cacheValues = new ArrayList(maxEntries);
    }

    public synchronized void clear() {
        this.cacheKeys.clear();
        this.cacheValues.clear();
    }

    public synchronized Object get(Object key) {
        int i;
        Object value = null;
        if (key != null && (i = this.cacheKeys.indexOf(key)) != -1) {
            value = this.cacheValues.get(i);
            this.cacheKeys.remove(i);
            this.cacheValues.remove(i);
            this.cacheKeys.add(key);
            this.cacheValues.add(value);
        }
        return value;
    }

    public synchronized void remove(Object key) {
        int i;
        if (key != null && (i = this.cacheKeys.indexOf(key)) != -1) {
            this.cacheKeys.remove(i);
            this.cacheValues.remove(i);
        }
    }

    public synchronized Object put(Object key, Object value) {
        Object oldValue = null;
        if (key != null) {
            int i = this.cacheKeys.indexOf(key);
            if (i != -1) {
                oldValue = this.cacheValues.get(i);
                this.cacheKeys.remove(i);
                this.cacheValues.remove(i);
            } else if (this.cacheKeys.size() >= this.maxEntries) {
                this.cacheKeys.remove(0);
                this.cacheValues.remove(0);
            }
            this.cacheKeys.add(key);
            this.cacheValues.add(value);
        }
        return oldValue;
    }
}