Simple implementations for lazy values and memoized functions
This commit is contained in:
committed by
Alexander Udalov
parent
c1cef019ce
commit
364ed19c4b
+43
@@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* 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.lang.resolve.lazy.storage;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public abstract class MemoizedFunctionToNotNullImpl<K, V> extends MemoizedFunctionToNullableImpl<K, V> implements MemoizedFunctionToNotNull<K, V> {
|
||||||
|
|
||||||
|
public MemoizedFunctionToNotNullImpl() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public MemoizedFunctionToNotNullImpl(@NotNull Map<K, Object> map) {
|
||||||
|
super(map);
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public V fun(K input) {
|
||||||
|
V result = super.fun(input);
|
||||||
|
assert result != null : "compute() returned null";
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
protected abstract V doCompute(K input);
|
||||||
|
}
|
||||||
+53
@@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* 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.lang.resolve.lazy.storage;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.jet.utils.WrappedValues;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public abstract class MemoizedFunctionToNullableImpl<K, V> implements MemoizedFunctionToNullable<K, V> {
|
||||||
|
private final Map<K, Object> cache;
|
||||||
|
|
||||||
|
public MemoizedFunctionToNullableImpl() {
|
||||||
|
this(new HashMap<K, Object>());
|
||||||
|
}
|
||||||
|
|
||||||
|
public MemoizedFunctionToNullableImpl(@NotNull Map<K, Object> map) {
|
||||||
|
this.cache = map;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public V fun(K input) {
|
||||||
|
Object value = cache.get(input);
|
||||||
|
if (value != null) return WrappedValues.unescapeNull(value);
|
||||||
|
|
||||||
|
V typedValue = doCompute(input);
|
||||||
|
|
||||||
|
Object oldValue = cache.put(input, WrappedValues.escapeNull(typedValue));
|
||||||
|
assert oldValue == null : "Race condition detected";
|
||||||
|
|
||||||
|
return typedValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
protected abstract V doCompute(K input);
|
||||||
|
}
|
||||||
+34
@@ -0,0 +1,34 @@
|
|||||||
|
/*
|
||||||
|
* 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.lang.resolve.lazy.storage;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
public abstract class NotNullLazyValueImpl<T> extends NullableLazyValueImpl<T> implements NotNullLazyValue<T> {
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public T compute() {
|
||||||
|
T result = super.compute();
|
||||||
|
assert result != null : "compute() returned null";
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
protected abstract T doCompute();
|
||||||
|
}
|
||||||
+44
@@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* 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.lang.resolve.lazy.storage;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.jet.utils.WrappedValues;
|
||||||
|
|
||||||
|
public abstract class NullableLazyValueImpl<T> implements NullableLazyValue<T> {
|
||||||
|
@Nullable
|
||||||
|
private Object value = null;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T compute() {
|
||||||
|
Object _value = value;
|
||||||
|
if (_value != null) return WrappedValues.unescapeNull(_value);
|
||||||
|
|
||||||
|
T typedValue = doCompute();
|
||||||
|
value = WrappedValues.escapeNull(typedValue);
|
||||||
|
|
||||||
|
postCompute(typedValue);
|
||||||
|
|
||||||
|
return typedValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract T doCompute();
|
||||||
|
|
||||||
|
protected void postCompute(T value) {
|
||||||
|
// Doing something in post-compute helps prevent infinite recursion
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user