update copyright
[fedora-idea.git] / xml / dom-openapi / src / com / intellij / util / xml / model / DomModelCache.java
blob97dcd6cf201c3cb7cbec51de51b3e52d8a1229eb
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.
17 package com.intellij.util.xml.model;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.util.Key;
21 import com.intellij.openapi.util.UserDataHolder;
22 import com.intellij.psi.PsiManager;
23 import com.intellij.psi.util.CachedValue;
24 import com.intellij.psi.util.CachedValueProvider;
25 import com.intellij.psi.util.CachedValuesManager;
26 import org.jetbrains.annotations.NonNls;
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
30 /**
31 * @author Dmitry Avdeev
33 public abstract class DomModelCache<T, H extends UserDataHolder> {
35 private final Key<CachedValue<T>> myKey;
36 private final Project myProject;
38 public DomModelCache(Project project, @NonNls String keyName) {
39 myProject = project;
40 myKey = new Key<CachedValue<T>>(keyName);
43 @Nullable
44 public T getCachedValue(final @NotNull H dataHolder) {
45 CachedValue<T> cachedValue = dataHolder.getUserData(myKey);
46 if (cachedValue == null) {
47 final CachedValueProvider<T> myProvider = new CachedValueProvider<T>() {
48 @Nullable
49 public Result<T> compute() {
50 return computeValue(dataHolder);
53 final CachedValuesManager manager = PsiManager.getInstance(myProject).getCachedValuesManager();
54 cachedValue = manager.createCachedValue(myProvider, false);
55 dataHolder.putUserData(myKey, cachedValue);
57 return cachedValue.getValue();
60 @NotNull
61 protected abstract CachedValueProvider.Result<T> computeValue(@NotNull H dataHolder);