2010-06-15 12:43:46 +08:00
|
|
|
package org.broadinstitute.sting.queue.function
|
|
|
|
|
|
|
|
|
|
import org.broadinstitute.sting.queue.util._
|
2010-06-23 02:39:20 +08:00
|
|
|
import java.lang.reflect.Field
|
2010-06-26 04:51:13 +08:00
|
|
|
import java.lang.annotation.Annotation
|
|
|
|
|
import org.broadinstitute.sting.commandline.{Input, Output, ArgumentDescription}
|
2010-06-15 12:43:46 +08:00
|
|
|
|
2010-06-23 02:39:20 +08:00
|
|
|
trait CommandLineFunction extends InputOutputFunction with DispatchFunction {
|
2010-06-26 04:51:13 +08:00
|
|
|
def inputFieldsWithValues = inputFields.filter(hasFieldValue(_))
|
|
|
|
|
def outputFieldsWithValues = outputFields.filter(hasFieldValue(_))
|
|
|
|
|
|
2010-06-15 12:43:46 +08:00
|
|
|
/**
|
|
|
|
|
* Repeats parameters with a prefix/suffix if they are set otherwise returns "".
|
|
|
|
|
* Skips null, Nil, None. Unwraps Some(x) to x. Everything else is called with x.toString.
|
|
|
|
|
*/
|
|
|
|
|
protected def repeat(prefix: String, params: Seq[_], suffix: String = "", separator: String = "") =
|
|
|
|
|
params.filter(param => hasValue(param)).map(param => prefix + toValue(param) + suffix).mkString(separator)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns parameter with a prefix/suffix if it is set otherwise returns "".
|
|
|
|
|
* Does not output null, Nil, None. Unwraps Some(x) to x. Everything else is called with x.toString.
|
|
|
|
|
*/
|
|
|
|
|
protected def optional(prefix: String, param: Any, suffix: String = "") =
|
|
|
|
|
if (hasValue(param)) prefix + toValue(param) + suffix else ""
|
|
|
|
|
|
2010-06-23 02:39:20 +08:00
|
|
|
def missingValues = {
|
2010-06-26 04:51:13 +08:00
|
|
|
val missingInputs = missingFields(inputFields, classOf[Input])
|
|
|
|
|
val missingOutputs = missingFields(outputFields, classOf[Output])
|
2010-06-15 12:43:46 +08:00
|
|
|
missingInputs | missingOutputs
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-26 04:51:13 +08:00
|
|
|
private def missingFields(fields: List[Field], annotation: Class[_ <: Annotation]) = {
|
2010-06-15 12:43:46 +08:00
|
|
|
var missing = Set.empty[String]
|
2010-06-23 02:39:20 +08:00
|
|
|
for (field <- fields) {
|
2010-06-26 04:51:13 +08:00
|
|
|
if (isRequired(field, annotation))
|
2010-06-23 02:39:20 +08:00
|
|
|
if (!hasValue(ReflectionUtils.getValue(this, field)))
|
|
|
|
|
missing += field.getName
|
2010-06-15 12:43:46 +08:00
|
|
|
}
|
|
|
|
|
missing
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-26 04:51:13 +08:00
|
|
|
private def isRequired(field: Field, annotation: Class[_ <: Annotation]) =
|
|
|
|
|
new ArgumentDescription(field.getAnnotation(annotation)).required
|
|
|
|
|
|
2010-06-23 02:39:20 +08:00
|
|
|
protected def hasFieldValue(field: Field) = hasValue(this.getFieldValue(field))
|
|
|
|
|
|
2010-06-15 12:43:46 +08:00
|
|
|
private def hasValue(param: Any) = param match {
|
|
|
|
|
case null => false
|
|
|
|
|
case Nil => false
|
|
|
|
|
case None => false
|
|
|
|
|
case _ => true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private def toValue(param: Any): String = param match {
|
|
|
|
|
case null => ""
|
|
|
|
|
case Nil => ""
|
|
|
|
|
case None => ""
|
|
|
|
|
case Some(x) => x.toString
|
|
|
|
|
case x => x.toString
|
|
|
|
|
}
|
|
|
|
|
}
|