diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/storage/LockBasedStorageManager.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/storage/LockBasedStorageManager.java index 5dc358225882439628618a43e0161be66aa7bbcc..c04fdef575fae52449e8b3b04b442290613f3ee2 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/storage/LockBasedStorageManager.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/lazy/storage/LockBasedStorageManager.java @@ -27,6 +27,7 @@ import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingTrace; import org.jetbrains.jet.util.slicedmap.ReadOnlySlice; import org.jetbrains.jet.util.slicedmap.WritableSlice; +import org.jetbrains.jet.utils.Nulls; import java.util.Collection; import java.util.concurrent.ConcurrentHashMap; @@ -107,23 +108,6 @@ public class LockBasedStorageManager implements StorageManager { return new LockProtectedTrace(lock, originalTrace); } - private static class Nulls { - private static final Object NULL_VALUE = new Object(); - - @Nullable - @SuppressWarnings("unchecked") - private static V unescape(@NotNull Object value) { - if (value == NULL_VALUE) return null; - return (V) value; - } - - @NotNull - private static Object escape(@Nullable V value) { - if (value == null) return NULL_VALUE; - return value; - } - } - private static class LockBasedLazyValue implements NullableLazyValue { private final Object lock; private final Computable computable; diff --git a/compiler/util/src/org/jetbrains/jet/utils/NotNullMemoizedFunction.java b/compiler/util/src/org/jetbrains/jet/utils/NotNullMemoizedFunction.java new file mode 100644 index 0000000000000000000000000000000000000000..a15accdf723beefe04472e0f8fad85789e8d388f --- /dev/null +++ b/compiler/util/src/org/jetbrains/jet/utils/NotNullMemoizedFunction.java @@ -0,0 +1,54 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.utils; + +import com.intellij.util.NotNullFunction; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public abstract class NotNullMemoizedFunction extends NullableMemoizedFunction implements NotNullFunction { + + public static NotNullFunction create(@NotNull final NotNullFunction compute) { + return new NotNullMemoizedFunction() { + @NotNull + @Override + protected V compute(@NotNull K input) { + return compute.fun(input); + } + }; + } + + public NotNullMemoizedFunction(@NotNull Map map) { + super(map); + } + + public NotNullMemoizedFunction() { + super(); + } + + @NotNull + @Override + public V fun(@NotNull K input) { + //noinspection ConstantConditions + return super.fun(input); + } + + @NotNull + @Override + protected abstract V compute(@NotNull K input); +} diff --git a/compiler/util/src/org/jetbrains/jet/utils/NullableMemoizedFunction.java b/compiler/util/src/org/jetbrains/jet/utils/NullableMemoizedFunction.java new file mode 100644 index 0000000000000000000000000000000000000000..2a49b2ab30dc35a1f03814d2c2f0dc8f7100014b --- /dev/null +++ b/compiler/util/src/org/jetbrains/jet/utils/NullableMemoizedFunction.java @@ -0,0 +1,65 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.utils; + +import com.intellij.util.Function; +import com.intellij.util.NullableFunction; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.HashMap; +import java.util.Map; + +public abstract class NullableMemoizedFunction implements NullableFunction { + + public static NullableFunction create(@NotNull final Function compute) { + return new NullableMemoizedFunction() { + @Nullable + @Override + protected V compute(@NotNull K input) { + return compute.fun(input); + } + }; + } + + private final Map cache; + + public NullableMemoizedFunction(@NotNull Map map) { + this.cache = map; + } + + public NullableMemoizedFunction() { + this(new HashMap()); + } + + @Override + @Nullable + public V fun(@NotNull K input) { + Object value = cache.get(input); + if (value != null) return Nulls.unescape(value); + + V typedValue = compute(input); + + Object oldValue = cache.put(input, Nulls.escape(typedValue)); + assert oldValue == null : "Race condition detected"; + + return typedValue; + } + + @Nullable + protected abstract V compute(@NotNull K input); +} diff --git a/compiler/util/src/org/jetbrains/jet/utils/Nulls.java b/compiler/util/src/org/jetbrains/jet/utils/Nulls.java new file mode 100644 index 0000000000000000000000000000000000000000..b5ce971d63621cf2fa3f8dcd08aa1e8eee4a5825 --- /dev/null +++ b/compiler/util/src/org/jetbrains/jet/utils/Nulls.java @@ -0,0 +1,37 @@ +/* + * Copyright 2010-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.utils; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class Nulls { + private static final Object NULL_VALUE = new Object(); + + @Nullable + @SuppressWarnings("unchecked") + public static V unescape(@NotNull Object value) { + if (value == NULL_VALUE) return null; + return (V) value; + } + + @NotNull + public static Object escape(@Nullable V value) { + if (value == null) return NULL_VALUE; + return value; + } +}