001 /*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.jet.lang.resolve.kotlin;
018
019 import com.intellij.openapi.vfs.VirtualFile;
020 import com.intellij.util.containers.SLRUCache;
021 import org.jetbrains.annotations.NotNull;
022 import org.jetbrains.annotations.Nullable;
023 import org.jetbrains.jet.lang.resolve.name.FqName;
024 import org.jetbrains.jet.storage.LockBasedStorageManager;
025
026 public abstract class VirtualFileKotlinClassFinder implements VirtualFileFinder {
027
028 // This cache must be small: we only query the same file a few times in a row (from different places)
029 private final SLRUCache<VirtualFile, KotlinJvmBinaryClass> cache = new SLRUCache<VirtualFile, KotlinJvmBinaryClass>(2, 2) {
030 @NotNull
031 @Override
032 public KotlinJvmBinaryClass createValue(VirtualFile virtualFile) {
033 // Operations under this lock are not supposed to involve other locks
034 return new VirtualFileKotlinClass(new LockBasedStorageManager(), virtualFile);
035 }
036 };
037
038 @Nullable
039 @Override
040 public KotlinJvmBinaryClass findKotlinClass(@NotNull FqName fqName) {
041 VirtualFile file = findVirtualFileWithHeader(fqName);
042 return file == null ? null : createKotlinClass(file);
043 }
044
045 @Override
046 @NotNull
047 public KotlinJvmBinaryClass createKotlinClass(@NotNull VirtualFile file) {
048 synchronized (cache) {
049 return cache.get(file);
050 }
051 }
052 }