diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /scalalib/src/main/scala/com/yahoo |
Publish
Diffstat (limited to 'scalalib/src/main/scala/com/yahoo')
3 files changed, 81 insertions, 0 deletions
diff --git a/scalalib/src/main/scala/com/yahoo/vespa/scalalib/arm/Using.scala b/scalalib/src/main/scala/com/yahoo/vespa/scalalib/arm/Using.scala new file mode 100644 index 00000000000..41585b193cf --- /dev/null +++ b/scalalib/src/main/scala/com/yahoo/vespa/scalalib/arm/Using.scala @@ -0,0 +1,17 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.scalalib.arm + +import scala.language.reflectiveCalls + +/** + * @author tonytv + */ +object Using { + def using[RESOURCE <: { def close() }, RETURN](resource: RESOURCE)(f: RESOURCE => RETURN) = { + try { + f(resource) + } finally { + if (resource != null) resource.close() + } + } +} diff --git a/scalalib/src/main/scala/com/yahoo/vespa/scalalib/java/function/FunctionConverters.scala b/scalalib/src/main/scala/com/yahoo/vespa/scalalib/java/function/FunctionConverters.scala new file mode 100644 index 00000000000..012f9b4bbe5 --- /dev/null +++ b/scalalib/src/main/scala/com/yahoo/vespa/scalalib/java/function/FunctionConverters.scala @@ -0,0 +1,20 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.scalalib.java.function + +import java.util.function.Predicate + +import scala.language.implicitConversions + +/** + * For using scala functions in Java APIs, such as the stream API. + * @author tonytv + */ +object FunctionConverters { + implicit class JavaPredicate[T](f: T => Boolean) extends Predicate[T] { + override def test(t: T): Boolean = f(t) + } + + implicit class JavaFunction[T, R](f: T => R) extends java.util.function.Function[T, R] { + override def apply(t: T): R = f(t) + } +} diff --git a/scalalib/src/main/scala/com/yahoo/vespa/scalalib/osgi/maven/ProjectBundleClassPaths.scala b/scalalib/src/main/scala/com/yahoo/vespa/scalalib/osgi/maven/ProjectBundleClassPaths.scala new file mode 100644 index 00000000000..9bf35e06ce5 --- /dev/null +++ b/scalalib/src/main/scala/com/yahoo/vespa/scalalib/osgi/maven/ProjectBundleClassPaths.scala @@ -0,0 +1,44 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.scalalib.osgi.maven + +import java.nio.charset.StandardCharsets +import java.nio.file.{Files, Path} + +import org.json4s.NoTypeHints +import org.json4s.native.Serialization + +import ProjectBundleClassPaths._ + +/** + * Represents the bundles in a maven project and the classpath elements + * corresponding to code that would end up in the bundle. + * @author tonytv + */ +case class ProjectBundleClassPaths(mainBundle: BundleClasspathMapping, + providedDependencies: List[BundleClasspathMapping]) + + +object ProjectBundleClassPaths { + //Written by bundle-plugin in the test classes directory. + val classPathMappingsFileName = "bundle-plugin.bundle-classpath-mappings.json" + + case class BundleClasspathMapping(bundleSymbolicName: String, classPathElements: List[String]) + + //Used by Serialization.* methods. See https://github.com/json4s/json4s + private implicit val serializationFormat = Serialization.formats(NoTypeHints) + + def save(path: Path, mappings: ProjectBundleClassPaths) { + Files.createDirectories(path.getParent) + val outputFile = Files.newBufferedWriter(path, StandardCharsets.UTF_8) + try { + Serialization.write(mappings, outputFile) + } finally { + outputFile.close() + } + } + + def load(path: Path): ProjectBundleClassPaths = { + val inputFile = Files.newBufferedReader(path, StandardCharsets.UTF_8) + Serialization.read[ProjectBundleClassPaths](inputFile) + } +} |