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

org.apache.fop.render.pdf.pdfbox.Cache Maven / Gradle / Ivy

Go to download

Apache FOP PDF Images plug-in extends FOP in order to add support for using PDF images in fo:external-graphic elements when generating PDF files.

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.fop.render.pdf.pdfbox;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

abstract class Cache {

    public enum Type {
        WEAK, SOFT, STRONG;
    }

    public abstract V getValue(K key, ValueMaker valueMaker) throws Exception;

    public static  Cache createCache(Type cacheType) {
        switch (cacheType) {
            case WEAK:
                return new WeakDocumentCache();
            case SOFT:
                return new SoftDocumentCache();
            case STRONG:
                return new StrongDocumentCache();
            default:
                return createDefaultCache();
        }
    }

    private static  Cache createDefaultCache() {
        return new WeakDocumentCache();
    }

    private static class StrongDocumentCache extends Cache {

        private final Map cache = new HashMap();

        @Override
        public V getValue(K key, ValueMaker valueMaker) throws Exception {
            V value = cache.get(key);
            if (value == null) {
                value = valueMaker.make();
                cache.put(key, value);
            }
            return value;
        }
    }

    private static class SoftDocumentCache extends Cache {

        private final Map> softKeys = new HashMap>();

        private final Map cache = new WeakHashMap();

        @Override
        public V getValue(K key, ValueMaker valueMaker) throws Exception {
            SoftReference reference = softKeys.get(key);
            Object softKey;
            if (reference == null || reference.get() == null) {
                softKey = new Object();
                reference = new SoftReference(softKey);
                softKeys.put(key, reference);
            } else {
                softKey = reference.get();
            }
            V value = cache.get(softKey);
            if (value == null) {
                value = valueMaker.make();
                cache.put(softKey, value);
            }
            return value;
        }
    }

    private static class WeakDocumentCache extends Cache {

        private V currentValue;

        private K currentKey;

        @Override
        public V getValue(K key, ValueMaker valueMaker) throws Exception {
            if (!key.equals(currentKey)) {
                currentKey = key;
                currentValue = valueMaker.make();
            }
            return currentValue;
        }
    }

    public interface ValueMaker {
        V make() throws Exception;
    }
}