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

io.fintrospect.templating.CachingClasspathViews.scala Maven / Gradle / Ivy

There is a newer version: 12.21.1
Show newest version
package io.fintrospect.templating

import java.util.concurrent.ConcurrentHashMap

import scala.collection.JavaConverters._
import scala.io.Source.fromInputStream

class CachingClasspathViews[X](fn: String => X, suffix: String) {
  private val classToTemplate = new ConcurrentHashMap[Class[_], X]().asScala

  def loadView[T <: View](view: T): X = classToTemplate.getOrElseUpdate(view.getClass,
    Option(getClass.getResourceAsStream("/" + view.template + suffix))
      .map(fromInputStream)
      .map(t => fn(t.mkString))
      .getOrElse({
        throw new ViewNotFound(view)
      })
  )
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy