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

com.jetbrains.plugin.structure.classes.resolvers.CacheResolver.kt Maven / Gradle / Ivy

Go to download

Base library for resolving class files and resources. Used by other JetBrains Plugins Structure Classes libraries.

The newest version!
/*
 * Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
 */

package com.jetbrains.plugin.structure.classes.resolvers

import com.github.benmanes.caffeine.cache.Caffeine
import com.github.benmanes.caffeine.cache.LoadingCache
import org.objectweb.asm.tree.ClassNode
import java.util.*
import java.util.concurrent.ExecutionException

class CacheResolver(
  private val delegate: Resolver,
  cacheSize: Int = DEFAULT_CACHE_SIZE
) : Resolver() {

  private data class BundleCacheKey(val baseName: String, val locale: Locale)

  private val classCache: LoadingCache> =
    Caffeine.newBuilder()
      .maximumSize(cacheSize.toLong())
      .build { key -> delegate.resolveClass(key) }

  private val propertyBundleCache: LoadingCache> =
    Caffeine.newBuilder()
      .maximumSize(cacheSize.toLong())
      .build { key -> delegate.resolveExactPropertyResourceBundle(key.baseName, key.locale) }

  override val allClasses
    get() = delegate.allClasses

  override val allBundleNameSet
    get() = delegate.allBundleNameSet

  override val allPackages
    get() = delegate.allPackages

  override val readMode
    get() = delegate.readMode

  override fun resolveClass(className: String): ResolutionResult = try {
    classCache.get(className)
  } catch (e: ExecutionException) {
    throw e.cause ?: e
  }

  override fun resolveExactPropertyResourceBundle(baseName: String, locale: Locale): ResolutionResult = try {
    propertyBundleCache.get(BundleCacheKey(baseName, locale))
  } catch (e: ExecutionException) {
    throw e.cause ?: e
  }

  override fun toString() = "Caching resolver for $delegate"

  override fun containsClass(className: String) =
    delegate.containsClass(className)

  override fun containsPackage(packageName: String) =
    delegate.containsPackage(packageName)

  override fun close() {
    delegate.close()
  }

  override fun processAllClasses(processor: (ResolutionResult) -> Boolean) =
    delegate.processAllClasses(processor)

  private companion object {
    private const val DEFAULT_CACHE_SIZE = 1024
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy