org.apache.fop.render.pdf.pdfbox.Cache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fop-pdf-images Show documentation
Show all versions of fop-pdf-images Show documentation
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