Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ThreadLocal to StaticResources #342

Merged
merged 19 commits into from Apr 24, 2018
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
176 changes: 107 additions & 69 deletions src/main/java/org/verapdf/tools/StaticResources.java
Expand Up @@ -16,73 +16,111 @@
*/
public class StaticResources {

public static Map<String, CMap> cMapCache = new HashMap<>();
public static Map<COSKey, PDStructureNameSpace> structureNameSpaceCache = new HashMap<>();
public static Map<String, FontProgram> cachedFonts = new HashMap<>();

/**
* Caches CMap object.
*
* @param name is string key for cached CMap.
* @param cMap is CMap object for caching.
*/
public static void cacheCMap(String name, CMap cMap) {
cMapCache.put(name, cMap);
}

/**
* Gets CMap for this string key.
*
* @param name is key for CMap.
* @return cached CMap with this name or null if no CMap available.
*/
public static CMap getCMap(String name) {
return cMapCache.get(name);
}

/**
* Caches structure name space. Key is chosen to be indirect reference key
* of this namespace dictionary.
*
* @param nameSpace is PD structure name space to cache.
*/
public static void cacheStructureNameSpace(PDStructureNameSpace nameSpace) {
COSKey key = nameSpace.getObject().getObjectKey();
structureNameSpaceCache.put(key, nameSpace);
}

/**
* Gets cached pd structure name space.
* @param key is COSKey of namespace to get.
* @return cached namespace with this COSKey or null if no namespace
* available.
*/
public static PDStructureNameSpace getStructureNameSpace(COSKey key) {
return structureNameSpaceCache.get(key);
}

private StaticResources() {
}

public static void cacheFontProgram(String key, FontProgram font) {
if (key != null) {
cachedFonts.put(key, font);
}
}

public static FontProgram getCachedFont(String key) {
if (key == null) {
return null;
}
return cachedFonts.get(key);
}

/**
* Clears all cached static resources.
*/
public static void clear() {
cMapCache.clear();
structureNameSpaceCache.clear();
cachedFonts.clear();
}
private static ThreadLocal<Map<String, CMap>> cMapCache = new ThreadLocal<>();
private static ThreadLocal<Map<COSKey, PDStructureNameSpace>> structureNameSpaceCache = new ThreadLocal<>();
private static ThreadLocal<Map<String, FontProgram>> cachedFonts = new ThreadLocal<>();

private StaticResources() {
}

/**
* Caches CMap object.
*
* @param name is string key for cached CMap.
* @param cMap is CMap object for caching.
*/
public static void cacheCMap(String name, CMap cMap) {
checkForNull(cMapCache);
cMapCache.get().put(name, cMap);
}

/**
* Gets CMap for this string key.
*
* @param name is key for CMap.
* @return cached CMap with this name or null if no CMap available.
*/
public static CMap getCMap(String name) {
checkForNull(cMapCache);
return StaticResources.cMapCache.get().get(name);
}

/**
* Caches structure name space. Key is chosen to be indirect reference key
* of this namespace dictionary.
*
* @param nameSpace is PD structure name space to cache.
*/
public static void cacheStructureNameSpace(PDStructureNameSpace nameSpace) {
checkForNull(structureNameSpaceCache);

COSKey key = nameSpace.getObject().getObjectKey();
StaticResources.structureNameSpaceCache.get().put(key, nameSpace);
}

/**
* Gets cached pd structure name space.
*
* @param key is COSKey of namespace to get.
* @return cached namespace with this COSKey or null if no namespace
* available.
*/
public static PDStructureNameSpace getStructureNameSpace(COSKey key) {
checkForNull(structureNameSpaceCache);
return StaticResources.structureNameSpaceCache.get().get(key);
}

public static void cacheFontProgram(String key, FontProgram font) {
checkForNull(cachedFonts);
if (key != null) {
StaticResources.cachedFonts.get().put(key, font);
}
}

public static FontProgram getCachedFont(String key) {
checkForNull(cachedFonts);
if (key == null) {
return null;
}
return StaticResources.cachedFonts.get().get(key);
}

/**
* Clears all cached static resources.
*/
public static void clear() {
StaticResources.cMapCache.set(new HashMap<>());
StaticResources.structureNameSpaceCache.set(new HashMap<>());
StaticResources.cachedFonts.set(new HashMap<>());
}

private static void checkForNull(ThreadLocal variable) {
if (variable.get() == null) {
variable.set(new HashMap<>());
}
}

public static Map<String, CMap> getcMapCache() {
return cMapCache.get();
}

public static void setcMapCache(Map<String, CMap> cMapCache) {
StaticResources.cMapCache.set(cMapCache);
}

public static Map<COSKey, PDStructureNameSpace> getStructureNameSpaceCache() {
return structureNameSpaceCache.get();
}

public static void setStructureNameSpaceCache(Map<COSKey, PDStructureNameSpace> structureNameSpaceCache) {
StaticResources.structureNameSpaceCache.set(structureNameSpaceCache);
}

public static Map<String, FontProgram> getCachedFonts() {
return cachedFonts.get();
}

public static void setCachedFonts(Map<String, FontProgram> cachedFonts) {
StaticResources.cachedFonts.set(cachedFonts);
}
}