Skip to content

Commit 4afb38a

Browse files
authored
Merge pull request #115 from Mast3rPlan/master
Fix performance issue
2 parents 7b61796 + 36b5670 commit 4afb38a

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

modules/API/src/main/java/com/comphenix/protocol/utility/CachedPackage.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.Map;
2121

22+
import com.google.common.base.Optional;
2223
import com.google.common.base.Preconditions;
2324
import com.google.common.base.Strings;
2425
import com.google.common.collect.Maps;
@@ -29,7 +30,7 @@
2930
* @author Kristian
3031
*/
3132
class CachedPackage {
32-
private final Map<String, Class<?>> cache;
33+
private final Map<String, Optional<Class<?>>> cache;
3334
private final String packageName;
3435
private final ClassSource source;
3536

@@ -50,7 +51,7 @@ public CachedPackage(String packageName, ClassSource source) {
5051
* @param clazz - type of class.
5152
*/
5253
public void setPackageClass(String className, Class<?> clazz) {
53-
cache.put(className, clazz);
54+
cache.put(className, Optional.<Class<?>>fromNullable(clazz));
5455
}
5556

5657
/**
@@ -61,19 +62,24 @@ public void setPackageClass(String className, Class<?> clazz) {
6162
*/
6263
public Class<?> getPackageClass(String className) {
6364
try {
64-
Class<?> result = cache.get(Preconditions.checkNotNull(className, "className cannot be NULL"));
65+
Optional<Class<?>> result = cache.get(Preconditions.checkNotNull(className, "className cannot be NULL"));
6566

6667
// Concurrency is not a problem - we don't care if we look up a class twice
6768
if (result == null) {
6869
// Look up the class dynamically
69-
result = source.loadClass(combine(packageName, className));
70-
if (result == null)
70+
result = Optional.<Class<?>>fromNullable(source.loadClass(combine(packageName, className)));
71+
if (!result.isPresent())
7172
throw new IllegalArgumentException("Source " + source + " returned NULL for " + className);
7273

7374
cache.put(className, result);
7475
}
7576

76-
return result;
77+
// Class has been looked for and hasn't been found in the past
78+
if (!result.isPresent()) {
79+
throw new ClassNotFoundException();
80+
}
81+
82+
return result.get();
7783
} catch (ClassNotFoundException e) {
7884
setPackageClass(className, null);
7985
throw new RuntimeException("Cannot find class " + combine(packageName, className), e);

0 commit comments

Comments
 (0)