less memory

This commit is contained in:
Alexey Kudravtsev
2012-04-26 13:45:00 +04:00
parent f5497b06e9
commit ace29cd9a3
3 changed files with 22 additions and 30 deletions
@@ -21,9 +21,12 @@ import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.NotNullLazyKey;
import com.intellij.openapi.util.UserDataHolder;
import com.intellij.openapi.util.UserDataHolderEx;
import org.jetbrains.annotations.NonNls;
import com.intellij.util.ConcurrencyUtil;
import com.intellij.util.containers.ConcurrentHashMap;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.ConcurrentMap;
public abstract class CachedValuesManager {
private static final NotNullLazyKey<CachedValuesManager, Project> INSTANCE_KEY = ServiceManager.createLazyKey(CachedValuesManager.class);
@@ -83,31 +86,21 @@ public abstract class CachedValuesManager {
* @return up-to-date value.
*/
public abstract <T, D extends UserDataHolder> T getCachedValue(@NotNull D dataHolder,
@NotNull Key<CachedValue<T>> key,
@NotNull CachedValueProvider<T> provider,
boolean trackValue);
@NotNull Key<CachedValue<T>> key,
@NotNull CachedValueProvider<T> provider,
boolean trackValue);
public <T, D extends UserDataHolder> T getCachedValue(@NotNull D dataHolder,
@NotNull CachedValueProvider<T> provider) {
return getCachedValue(dataHolder, new MemoizationKey<CachedValue<T>>(provider.getClass().getName()), provider, false);
public <T, D extends UserDataHolder> T getCachedValue(@NotNull D dataHolder, @NotNull CachedValueProvider<T> provider) {
return getCachedValue(dataHolder, getKey(provider), provider, false);
}
public static class MemoizationKey<T> extends Key<T> {
private final String myName;
public MemoizationKey(@NotNull @NonNls String name) {
super(name);
myName = name;
}
public int hashCode() {
return myName.hashCode();
}
@Override
public boolean equals(Object obj) {
return obj instanceof MemoizationKey && myName.equals(((MemoizationKey)obj).myName);
private final ConcurrentMap<String, Key<CachedValue>> keyForProvider = new ConcurrentHashMap<String, Key<CachedValue>>();
private <T> Key<CachedValue<T>> getKey(@NotNull CachedValueProvider<T> provider) {
String name = provider.getClass().getName();
Key<CachedValue> key = keyForProvider.get(name);
if (key == null) {
key = ConcurrencyUtil.cacheOrGet(keyForProvider, name, Key.<CachedValue>create(name));
}
return (Key)key;
}
}
@@ -49,10 +49,9 @@ public class CachedValuesManagerImpl extends CachedValuesManager {
@Override
@Nullable
public <T, D extends UserDataHolder> T getCachedValue(@NotNull D dataHolder,
@NotNull Key<CachedValue<T>> key,
@NotNull CachedValueProvider<T> provider,
boolean trackValue) {
@NotNull Key<CachedValue<T>> key,
@NotNull CachedValueProvider<T> provider,
boolean trackValue) {
CachedValue<T> value;
if (dataHolder instanceof UserDataHolderEx) {
UserDataHolderEx dh = (UserDataHolderEx)dataHolder;
@@ -38,12 +38,12 @@ public class Key<T> {
myName = name;
}
public int hashCode() {
public final int hashCode() {
return myIndex;
}
@Override
public boolean equals(Object obj) {
public final boolean equals(Object obj) {
return obj == this;
}
@@ -61,7 +61,7 @@ public class Key<T> {
}
@Nullable
public T get(@Nullable Map<Key, Object> holder) {
public T get(@Nullable Map<Key, ?> holder) {
return holder == null ? null : (T)holder.get(this);
}