All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.cli.jvm.index.JvmDependenciesDynamicCompoundIndex.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * Copyright 2010-2016 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jetbrains.kotlin.cli.jvm.index

import com.intellij.openapi.vfs.VirtualFile
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.read
import kotlin.concurrent.write

class JvmDependenciesDynamicCompoundIndex : JvmDependenciesIndex {
    private val indices = arrayListOf()
    private val lock = ReentrantReadWriteLock()

    fun addIndex(index: JvmDependenciesIndex) {
        lock.write {
            indices.add(index)
        }
    }

    fun addNewIndexForRoots(roots: Iterable): JvmDependenciesIndex? =
        lock.read {
            val alreadyIndexed = indexedRoots.toHashSet()
            val newRoots = roots.filter { root -> root !in alreadyIndexed }
            if (newRoots.isEmpty()) null
            else JvmDependenciesIndexImpl(newRoots).also(this::addIndex)
        }

    override val indexedRoots: Sequence get() = indices.asSequence().flatMap { it.indexedRoots }

    override fun  findClass(
        classId: ClassId,
        acceptedRootTypes: Set,
        findClassGivenDirectory: (VirtualFile, JavaRoot.RootType) -> T?
    ): T? = lock.read {
        indices.asSequence().mapNotNull { it.findClass(classId, acceptedRootTypes, findClassGivenDirectory) }.firstOrNull()
    }

    override fun traverseDirectoriesInPackage(
        packageFqName: FqName,
        acceptedRootTypes: Set,
        continueSearch: (VirtualFile, JavaRoot.RootType) -> Boolean
    ) = lock.read {
        indices.forEach { it.traverseDirectoriesInPackage(packageFqName, acceptedRootTypes, continueSearch) }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy