Skip to content

Commit

Permalink
Made class IdsMapCache thread-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
Argent77 committed Dec 27, 2015
1 parent a75fc2f commit 1ad71e6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/infinity/NearInfinity.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import infinity.updater.Updater;
import infinity.util.IdsMapCache;
import infinity.util.StringResource;
import infinity.util.Table2daCache;
import infinity.util.io.FileLookup;
import infinity.util.io.FileNI;

Expand Down Expand Up @@ -757,6 +758,7 @@ private static void clearCache()
{
FileLookup.getInstance().clearCache();
IdsMapCache.clearCache();
Table2daCache.clearCache();
SearchFrame.clearCache();
StringResource.close();
ProRef.clearCache();
Expand Down
33 changes: 20 additions & 13 deletions src/infinity/util/IdsMapCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,36 @@ public final class IdsMapCache

public static void cacheInvalid(ResourceEntry entry)
{
common.remove(entry.toString().toUpperCase(Locale.ENGLISH));
if (entry != null) {
common.remove(entry.toString().toUpperCase(Locale.ENGLISH));
}
}

public static void clearCache()
{
common.clear();
}

public static synchronized IdsMap get(String name) // name must be in UPPER CASE
public static synchronized IdsMap get(String name)
{
IdsMap map = common.get(name);
if (map == null) {
ResourceEntry resEntry = ResourceFactory.getResourceEntry(name);
if (resEntry == null && name.equalsIgnoreCase("ATTSTYLE.IDS"))
resEntry = ResourceFactory.getResourceEntry("ATTSTYL.IDS");
if (resEntry == null)
System.err.println("Could not find " + name);
else {
map = new IdsMap(resEntry);
common.put(name, map);
IdsMap retVal = null;
if (name != null) {
name = name.trim().toUpperCase(Locale.ENGLISH);
retVal = common.get(name);
if (retVal == null) {
ResourceEntry resEntry = ResourceFactory.getResourceEntry(name);
if (resEntry == null && name.equals("ATTSTYLE.IDS")) {
resEntry = ResourceFactory.getResourceEntry("ATTSTYL.IDS");
}
if (resEntry == null) {
System.err.println("Could not find " + name);
} else {
retVal = new IdsMap(resEntry);
common.put(name, retVal);
}
}
}
return map;
return retVal;
}

private IdsMapCache(){}
Expand Down
4 changes: 3 additions & 1 deletion src/infinity/util/Table2daCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public class Table2daCache
/** Removes the specified 2DA resource from the cache. */
public static synchronized void cacheInvalid(ResourceEntry entry)
{
map.remove(entry);
if (entry != null) {
map.remove(entry);
}
}

/** Removes all cached 2DA resources. */
Expand Down

0 comments on commit 1ad71e6

Please sign in to comment.