sticky documentation popup [take 1]
[fedora-idea.git] / platform / platform-impl / src / com / intellij / util / CachedValueImpl.java
blobf6a9890319b4958c07b319e688db35fd2f01786c
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Created by IntelliJ IDEA.
19 * User: mike
20 * Date: Jun 6, 2002
21 * Time: 5:41:42 PM
22 * To change template for new class use
23 * Code Style | Class Templates options (Tools | IDE Options).
25 package com.intellij.util;
27 import com.intellij.psi.util.CachedValue;
28 import com.intellij.psi.util.CachedValueProvider;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
32 public abstract class CachedValueImpl<T> extends CachedValueBase<T> implements CachedValue<T> {
34 private final CachedValueProvider<T> myProvider;
36 public CachedValueImpl(@NotNull CachedValueProvider<T> provider) {
37 super();
38 myProvider = provider;
41 @Nullable
42 public T getValue() {
44 r.lock();
46 T value;
47 try {
48 value = getUpToDateOrNull();
49 if (value != null) {
50 return value == NULL ? null : value;
52 } finally {
53 r.unlock();
56 w.lock();
58 try {
59 value = getUpToDateOrNull();
60 if (value != null) {
61 return value == NULL ? null : value;
64 CachedValueProvider.Result<T> result = myProvider.compute();
65 value = result == null ? null : result.getValue();
67 setValue(value, result);
69 return value;
71 finally {
72 w.unlock();
76 public CachedValueProvider<T> getValueProvider() {
77 return myProvider;