filetypemanager perf fix
[fedora-idea.git] / plugins / properties / src / com / intellij / lang / properties / ResourceBundleImpl.java
blobc6e2bb22487c9765e621ee73e4406d6c53ac0db6
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 /**
18 * @author Alexey
20 package com.intellij.lang.properties;
22 import com.intellij.lang.properties.psi.PropertiesFile;
23 import com.intellij.openapi.fileTypes.FileTypeManager;
24 import com.intellij.openapi.fileTypes.StdFileTypes;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.util.Comparing;
27 import com.intellij.openapi.vfs.VirtualFile;
28 import com.intellij.openapi.vfs.VirtualFileManager;
29 import com.intellij.psi.PsiFile;
30 import com.intellij.psi.PsiManager;
31 import com.intellij.util.SmartList;
32 import com.intellij.util.containers.ContainerUtil;
33 import org.jetbrains.annotations.NonNls;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
36 import java.util.Collections;
37 import java.util.Comparator;
38 import java.util.List;
40 public class ResourceBundleImpl implements ResourceBundle {
41 @NotNull private final VirtualFile myBaseDirectory;
42 @NotNull private final String myBaseName;
43 @NonNls private static final String RESOURCE_BUNDLE_PREFIX = "resourceBundle:";
45 public ResourceBundleImpl(@NotNull VirtualFile baseDirectory, @NotNull String baseName) {
46 myBaseDirectory = baseDirectory;
47 myBaseName = baseName;
50 public static final ResourceBundle NULL = new ResourceBundle() {
51 @NotNull
52 public List<PropertiesFile> getPropertiesFiles(final Project project) {
53 return Collections.emptyList();
56 @NotNull
57 public PropertiesFile getDefaultPropertiesFile(final Project project) {
58 throw new IllegalStateException();
61 @NotNull
62 public String getBaseName() {
63 return "";
66 @NotNull
67 public VirtualFile getBaseDirectory() {
68 throw new IllegalStateException();
72 @NotNull
73 public List<PropertiesFile> getPropertiesFiles(final Project project) {
74 VirtualFile[] children = myBaseDirectory.getChildren();
75 List<PropertiesFile> result = new SmartList<PropertiesFile>();
76 FileTypeManager fileTypeManager = FileTypeManager.getInstance();
77 PsiManager psiManager = PsiManager.getInstance(project);
78 for (VirtualFile file : children) {
79 if (!file.isValid() || !fileTypeManager.isFileOfType(file, StdFileTypes.PROPERTIES)) continue;
80 if (Comparing.strEqual(PropertiesUtil.getBaseName(file), myBaseName)) {
81 PsiFile psiFile = psiManager.findFile(file);
82 if (psiFile instanceof PropertiesFile) {
83 result.add((PropertiesFile)psiFile);
87 return result;
90 @NotNull
91 public PropertiesFile getDefaultPropertiesFile(final Project project) {
92 List<PropertiesFile> files = getPropertiesFiles(project);
93 // put default properties file first
94 ContainerUtil.quickSort(files, new Comparator<PropertiesFile>() {
95 public int compare(final PropertiesFile o1, final PropertiesFile o2) {
96 return Comparing.compare(o1.getName(), o2.getName());
98 });
99 return files.get(0);
102 @NotNull
103 public String getBaseName() {
104 return myBaseName;
107 public boolean equals(final Object o) {
108 if (this == o) return true;
109 if (o == null || getClass() != o.getClass()) return false;
111 final ResourceBundleImpl resourceBundle = (ResourceBundleImpl)o;
113 if (!myBaseDirectory.equals(resourceBundle.myBaseDirectory)) return false;
114 if (!myBaseName.equals(resourceBundle.myBaseName)) return false;
116 return true;
119 public int hashCode() {
120 int result = myBaseDirectory.hashCode();
121 result = 29 * result + myBaseName.hashCode();
122 return result;
125 @Nullable
126 public static ResourceBundle createByUrl(String url) {
127 if (!url.startsWith(RESOURCE_BUNDLE_PREFIX)) return null;
129 String defaultPropertiesUrl = url.substring(RESOURCE_BUNDLE_PREFIX.length());
130 final int idx = defaultPropertiesUrl.lastIndexOf('/');
131 if (idx == -1) return null;
132 String baseDir = defaultPropertiesUrl.substring(0, idx);
133 String baseName = defaultPropertiesUrl.substring(idx + 1);
134 VirtualFile baseDirectory = VirtualFileManager.getInstance().findFileByUrl(baseDir);
135 if (baseDirectory != null) {
136 return new ResourceBundleImpl(baseDirectory, baseName);
138 return null;
141 public String getUrl() {
142 return RESOURCE_BUNDLE_PREFIX +getBaseDirectory() + "/" + getBaseName();
145 @NotNull
146 public VirtualFile getBaseDirectory() {
147 return myBaseDirectory;