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.
16 package com
.intellij
.util
;
18 import com
.intellij
.openapi
.util
.UserDataHolder
;
19 import com
.intellij
.openapi
.util
.Key
;
20 import com
.intellij
.openapi
.util
.UserDataHolderEx
;
21 import com
.intellij
.openapi
.project
.Project
;
22 import com
.intellij
.psi
.util
.*;
23 import org
.jetbrains
.annotations
.NotNull
;
24 import org
.jetbrains
.annotations
.Nullable
;
29 public class CachedValuesManagerImpl
extends CachedValuesManager
{
31 private final Project myProject
;
32 private final CachedValuesFactory myFactory
;
34 public CachedValuesManagerImpl(Project project
, CachedValuesFactory factory
) {
36 myFactory
= factory
== null ?
new DefaultCachedValuesFactory(project
) : factory
;
39 public <T
> CachedValue
<T
> createCachedValue(@NotNull CachedValueProvider
<T
> provider
, boolean trackValue
) {
40 return myFactory
.createCachedValue(provider
, trackValue
);
43 public <T
,P
> ParameterizedCachedValue
<T
,P
> createParameterizedCachedValue(@NotNull ParameterizedCachedValueProvider
<T
,P
> provider
, boolean trackValue
) {
44 return myFactory
.createParameterizedCachedValue(provider
, trackValue
);
49 public <T
, D
extends UserDataHolder
> T
getCachedValue(@NotNull D dataHolder
,
50 @NotNull Key
<CachedValue
<T
>> key
,
51 @NotNull CachedValueProvider
<T
> provider
,
55 if (dataHolder
instanceof UserDataHolderEx
) {
56 UserDataHolderEx dh
= (UserDataHolderEx
)dataHolder
;
57 value
= dh
.getUserData(key
);
58 if (value
instanceof CachedValueBase
&& !((CachedValueBase
)value
).isFromMyProject(myProject
)) {
60 dataHolder
.putUserData(key
, null);
63 value
= createCachedValue(provider
, trackValue
);
64 assert ((CachedValueBase
)value
).isFromMyProject(myProject
);
65 value
= dh
.putUserDataIfAbsent(key
, value
);
69 synchronized (dataHolder
) {
70 value
= dataHolder
.getUserData(key
);
71 if (value
instanceof CachedValueBase
&& !((CachedValueBase
)value
).isFromMyProject(myProject
)) {
75 value
= createCachedValue(provider
, trackValue
);
76 dataHolder
.putUserData(key
, value
);
80 return value
.getValue();
83 public Project
getProject() {