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
.psi
.impl
;
18 import com
.intellij
.psi
.PsiClass
;
19 import com
.intellij
.psi
.PsiField
;
20 import com
.intellij
.psi
.PsiFile
;
21 import com
.intellij
.psi
.PsiMethod
;
22 import com
.intellij
.psi
.search
.GlobalSearchScope
;
23 import com
.intellij
.psi
.search
.PsiShortNamesCache
;
24 import com
.intellij
.util
.ArrayUtil
;
25 import com
.intellij
.util
.containers
.HashSet
;
26 import gnu
.trove
.THashSet
;
27 import org
.jetbrains
.annotations
.NonNls
;
28 import org
.jetbrains
.annotations
.NotNull
;
30 import java
.util
.ArrayList
;
31 import java
.util
.Arrays
;
32 import java
.util
.List
;
35 public class CompositeShortNamesCache
extends PsiShortNamesCache
{
36 private final List
<PsiShortNamesCache
> myCaches
= new ArrayList
<PsiShortNamesCache
>();
37 private PsiShortNamesCache
[] myCacheArray
= new PsiShortNamesCache
[0];
39 public void addCache(PsiShortNamesCache cache
) {
41 myCacheArray
= myCaches
.toArray(new PsiShortNamesCache
[myCaches
.size()]);
44 public void runStartupActivity() {
45 for (PsiShortNamesCache cache
: myCaches
) {
46 cache
.runStartupActivity();
51 public PsiFile
[] getFilesByName(@NotNull String name
) {
52 Merger
<PsiFile
> merger
= null;
53 for (PsiShortNamesCache cache
: myCacheArray
) {
54 PsiFile
[] classes
= cache
.getFilesByName(name
);
55 if (classes
.length
!= 0) {
56 if (merger
== null) merger
= new Merger
<PsiFile
>();
60 PsiFile
[] result
= merger
== null ?
null : merger
.getResult();
61 return result
!= null ? result
: PsiFile
.EMPTY_ARRAY
;
65 public String
[] getAllFileNames() {
66 Merger
<String
> merger
= new Merger
<String
>();
67 for (PsiShortNamesCache cache
: myCacheArray
) {
68 merger
.add(cache
.getAllFileNames());
70 String
[] result
= merger
.getResult();
71 return result
!= null ? result
: ArrayUtil
.EMPTY_STRING_ARRAY
;
75 public PsiClass
[] getClassesByName(@NotNull String name
, @NotNull GlobalSearchScope scope
) {
76 Merger
<PsiClass
> merger
= null;
77 for (PsiShortNamesCache cache
: myCacheArray
) {
78 PsiClass
[] classes
= cache
.getClassesByName(name
, scope
);
79 if (classes
.length
!= 0) {
80 if (merger
== null) merger
= new Merger
<PsiClass
>();
84 PsiClass
[] result
= merger
== null ?
null : merger
.getResult();
85 return result
!= null ? result
: PsiClass
.EMPTY_ARRAY
;
89 public String
[] getAllClassNames() {
90 Merger
<String
> merger
= new Merger
<String
>();
91 for (PsiShortNamesCache cache
: myCacheArray
) {
92 merger
.add(cache
.getAllClassNames());
94 String
[] result
= merger
.getResult();
95 return result
!= null ? result
: ArrayUtil
.EMPTY_STRING_ARRAY
;
98 public void getAllClassNames(@NotNull HashSet
<String
> dest
) {
99 for (PsiShortNamesCache cache
: myCacheArray
) {
100 cache
.getAllClassNames(dest
);
105 public PsiMethod
[] getMethodsByName(@NotNull String name
, @NotNull GlobalSearchScope scope
) {
106 Merger
<PsiMethod
> merger
= null;
107 for (PsiShortNamesCache cache
: myCacheArray
) {
108 PsiMethod
[] classes
= cache
.getMethodsByName(name
, scope
);
109 if (classes
.length
!= 0) {
110 if (merger
== null) merger
= new Merger
<PsiMethod
>();
114 PsiMethod
[] result
= merger
== null ?
null : merger
.getResult();
115 return result
!= null ? result
: PsiMethod
.EMPTY_ARRAY
;
119 public PsiMethod
[] getMethodsByNameIfNotMoreThan(@NonNls @NotNull final String name
, @NotNull final GlobalSearchScope scope
, final int maxCount
) {
120 Merger
<PsiMethod
> merger
= null;
121 for (PsiShortNamesCache cache
: myCacheArray
) {
122 PsiMethod
[] classes
= cache
.getMethodsByNameIfNotMoreThan(name
, scope
, maxCount
);
123 if (classes
.length
!= 0) {
124 if (merger
== null) merger
= new Merger
<PsiMethod
>();
128 PsiMethod
[] result
= merger
== null ?
null : merger
.getResult();
129 return result
!= null ? result
: PsiMethod
.EMPTY_ARRAY
;
133 public String
[] getAllMethodNames() {
134 Merger
<String
> merger
= new Merger
<String
>();
135 for (PsiShortNamesCache cache
: myCacheArray
) {
136 merger
.add(cache
.getAllMethodNames());
138 String
[] result
= merger
.getResult();
139 return result
!= null ? result
: ArrayUtil
.EMPTY_STRING_ARRAY
;
142 public void getAllMethodNames(@NotNull HashSet
<String
> set
) {
143 for (PsiShortNamesCache cache
: myCacheArray
) {
144 cache
.getAllMethodNames(set
);
149 public PsiField
[] getFieldsByName(@NotNull String name
, @NotNull GlobalSearchScope scope
) {
150 Merger
<PsiField
> merger
= null;
151 for (PsiShortNamesCache cache
: myCacheArray
) {
152 PsiField
[] classes
= cache
.getFieldsByName(name
, scope
);
153 if (classes
.length
!= 0) {
154 if (merger
== null) merger
= new Merger
<PsiField
>();
158 PsiField
[] result
= merger
== null ?
null : merger
.getResult();
159 return result
!= null ? result
: PsiField
.EMPTY_ARRAY
;
163 public String
[] getAllFieldNames() {
164 Merger
<String
> merger
= new Merger
<String
>();
165 for (PsiShortNamesCache cache
: myCacheArray
) {
166 merger
.add(cache
.getAllFieldNames());
168 String
[] result
= merger
.getResult();
169 return result
!= null ? result
: ArrayUtil
.EMPTY_STRING_ARRAY
;
172 public void getAllFieldNames(@NotNull HashSet
<String
> set
) {
173 for (PsiShortNamesCache cache
: myCacheArray
) {
174 cache
.getAllFieldNames(set
);
178 private static class Merger
<T
> {
179 private T
[] mySingleItem
= null;
180 private Set
<T
> myAllItems
= null;
182 public void add(T
[] items
) {
183 if (items
== null || items
.length
== 0) return;
184 if (mySingleItem
== null) {
185 mySingleItem
= items
;
188 if (myAllItems
== null) {
189 myAllItems
= new THashSet
<T
>(Arrays
.asList(mySingleItem
));
191 myAllItems
.addAll(Arrays
.asList(items
));
194 public T
[] getResult() {
195 if (myAllItems
== null) return mySingleItem
;
196 return myAllItems
.toArray(mySingleItem
);
200 @SuppressWarnings({"HardCodedStringLiteral"})
202 public String
toString() {
203 return "Composite cache: " + myCaches
;