Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
nh13 committed Apr 21, 2022
1 parent 3247e78 commit 45b68d6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/main/scala/com/fulcrumgenomics/util/MetricBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import java.io.{PrintWriter, StringWriter}
import scala.reflect.runtime.{universe => ru}
import scala.util.{Failure, Success}

/** Class for building metrics of type [[T]]
/** Class for building metrics of type [[T]].
*
* This is not thread-safe.
*
* @param source optionally, the source of reading (e.g. file)
* @tparam T the metric type
Expand Down Expand Up @@ -77,6 +79,8 @@ class MetricBuilder[T <: Metric](source: Option[String] = None)(implicit tt: ru.
* @return a new instance of type [[T]]
*/
def fromArgMap(argMap: Map[String, String], lineNumber: Option[Int] = None): T = {
reflectiveBuilder.reset() // reset the arguments to their initial values

val names = argMap.keys.toIndexedSeq
forloop(from = 0, until = names.length) { i =>
reflectiveBuilder.argumentLookup.forField(names(i)) match {
Expand Down Expand Up @@ -107,7 +111,8 @@ class MetricBuilder[T <: Metric](source: Option[String] = None)(implicit tt: ru.
// build it. NB: if arguments are missing values, then an exception will be thrown here
// Also, we don't use the default "build()" method since if a collection or option is empty, it will be treated as
// missing.
reflectiveBuilder.build(reflectiveBuilder.argumentLookup.ordered.map(arg => arg.value getOrElse unreachable(s"Arguments not set: ${arg.name}")))
val params = reflectiveBuilder.argumentLookup.ordered.map(arg => arg.value getOrElse unreachable(s"Arguments not set: ${arg.name}"))
reflectiveBuilder.build(params)
}

/** Logs the throwable, if given, and throws a [[FailureException]] with information about when reading metrics fails
Expand Down
43 changes: 43 additions & 0 deletions src/test/scala/com/fulcrumgenomics/util/MetricBuilderTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* The MIT License
*
* Copyright (c) 2022 Fulcrum Genomics
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package com.fulcrumgenomics.util

import com.fulcrumgenomics.testing.UnitSpec


case class MetricBuilderTestMetric(name: String, count: Long = 1) extends Metric

class MetricBuilderTest extends UnitSpec {
private val builder = new MetricBuilder[MetricBuilderTestMetric]()

"MetricBuilder.fromArgMap" should "build a metric from an argmap with all value specified" in {
builder.fromArgMap(Map("name" -> "foo", "count" -> "2")) shouldBe MetricBuilderTestMetric(name="foo", count=2)
}

it should "build a metric from an argmap with only required values specified" in {
builder.fromArgMap(Map("name" -> "foo")) shouldBe MetricBuilderTestMetric(name="foo")
}
}

0 comments on commit 45b68d6

Please sign in to comment.