Removed use of Guava's Cache because Bukkit uses an ancient version of Guava.

This commit is contained in:
sk89q 2014-04-05 14:44:41 -07:00
parent 24f8fbc92a
commit e0a2873c99
3 changed files with 109 additions and 40 deletions

View File

@ -0,0 +1,33 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Marks features that should be replaced with Google Guava but cannot
* yet because Bukkit uses such an old version of Guava.
*/
@Retention(RetentionPolicy.SOURCE)
@Documented
public @interface RequiresNewerGuava {
}

View File

@ -20,16 +20,14 @@
package com.sk89q.worldedit.util.eventbus;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.collect.*;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;
import com.google.common.eventbus.DeadEvent;
import com.sk89q.worldedit.util.annotation.RequiresNewerGuava;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -66,35 +64,8 @@ public class EventBus {
*/
private final SubscriberFindingStrategy finder = new AnnotatedSubscriberFinder();
/**
* A thread-safe cache for flattenHierarchy(). The Class class is immutable.
*/
private Cache<Class<?>, Set<Class<?>>> flattenHierarchyCache =
CacheBuilder.newBuilder()
.weakKeys()
.build(new CacheLoader<Class<?>, Set<Class<?>>>() {
@Override
public Set<Class<?>> load(Class<?> concreteClass) throws Exception {
List<Class<?>> parents = Lists.newLinkedList();
Set<Class<?>> classes = Sets.newHashSet();
parents.add(concreteClass);
while (!parents.isEmpty()) {
Class<?> clazz = parents.remove(0);
classes.add(clazz);
Class<?> parent = clazz.getSuperclass();
if (parent != null) {
parents.add(parent);
}
Collections.addAll(parents, clazz.getInterfaces());
}
return classes;
}
});
@RequiresNewerGuava
private HierarchyCache flattenHierarchyCache = new HierarchyCache();
/**
* Registers the given handler for the given class to receive events.
@ -251,11 +222,7 @@ public class EventBus {
* @return {@code clazz}'s complete type hierarchy, flattened and uniqued.
*/
Set<Class<?>> flattenHierarchy(Class<?> concreteClass) {
try {
return flattenHierarchyCache.get(concreteClass);
} catch (ExecutionException e) {
throw Throwables.propagate(e.getCause());
}
}
}

View File

@ -0,0 +1,69 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.util.eventbus;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.sk89q.worldedit.util.annotation.RequiresNewerGuava;
import java.util.*;
/**
* Holds a cache of class hierarcy.
* </p>
* This exists because Bukkit has an ancient version of Guava and the cache
* library in Guava has since changed.
*/
@RequiresNewerGuava
class HierarchyCache {
private final Map<Class<?>, Set<Class<?>>> cache = new WeakHashMap<Class<?>, Set<Class<?>>>();
public Set<Class<?>> get(Class<?> concreteClass) {
Set<Class<?>> ret = cache.get(concreteClass);
if (ret == null) {
ret = build(concreteClass);
cache.put(concreteClass, ret);
}
return ret;
}
protected Set<Class<?>> build(Class<?> concreteClass) {
List<Class<?>> parents = Lists.newLinkedList();
Set<Class<?>> classes = Sets.newHashSet();
parents.add(concreteClass);
while (!parents.isEmpty()) {
Class<?> clazz = parents.remove(0);
classes.add(clazz);
Class<?> parent = clazz.getSuperclass();
if (parent != null) {
parents.add(parent);
}
Collections.addAll(parents, clazz.getInterfaces());
}
return classes;
}
}