blob: c4420ef81141ff2b2c75bf792300bd682de59b78 [file] [log] [blame]
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
package org.apache.griffin.measure.utils
import scala.reflect.ClassTag
object ParamUtil {
implicit class ParamMap(params: Map[String, Any]) {
def getAny(key: String, defValue: Any): Any = {
params.get(key) match {
case Some(v) => v
case _ => defValue
}
}
def getAnyRef[T: ClassTag](key: String, defValue: T): T = {
params.get(key) match {
case Some(v: T) => v
case _ => defValue
}
}
def getString(key: String, defValue: String): String = {
try {
params.get(key) match {
case Some(v: String) => v
case Some(v) => v.toString
case _ => defValue
}
} catch {
case _: NumberFormatException => defValue
}
}
def getLazyString(key: String, defValue: () => String): String = {
try {
params.get(key) match {
case Some(v: String) => v
case Some(v) => v.toString
case _ => defValue()
}
} catch {
case _: NumberFormatException => defValue()
}
}
def getStringOrKey(key: String): String = getString(key, key)
def getByte(key: String, defValue: Byte): Byte = {
try {
params.get(key) match {
case Some(v: String) => v.toByte
case Some(v: Byte) => v
case Some(v: Short) => v.toByte
case Some(v: Int) => v.toByte
case Some(v: Long) => v.toByte
case Some(v: Float) => v.toByte
case Some(v: Double) => v.toByte
case _ => defValue
}
} catch {
case _: NumberFormatException => defValue
}
}
def getShort(key: String, defValue: Short): Short = {
try {
params.get(key) match {
case Some(v: String) => v.toShort
case Some(v: Byte) => v.toShort
case Some(v: Short) => v
case Some(v: Int) => v.toShort
case Some(v: Long) => v.toShort
case Some(v: Float) => v.toShort
case Some(v: Double) => v.toShort
case _ => defValue
}
} catch {
case _: NumberFormatException => defValue
}
}
def getInt(key: String, defValue: Int): Int = {
try {
params.get(key) match {
case Some(v: String) => v.toInt
case Some(v: Byte) => v.toInt
case Some(v: Short) => v.toInt
case Some(v: Int) => v
case Some(v: Long) => v.toInt
case Some(v: Float) => v.toInt
case Some(v: Double) => v.toInt
case _ => defValue
}
} catch {
case _: NumberFormatException => defValue
}
}
def getLong(key: String, defValue: Long): Long = {
try {
params.get(key) match {
case Some(v: String) => v.toLong
case Some(v: Byte) => v.toLong
case Some(v: Short) => v.toLong
case Some(v: Int) => v.toLong
case Some(v: Long) => v
case Some(v: Float) => v.toLong
case Some(v: Double) => v.toLong
case _ => defValue
}
} catch {
case _: NumberFormatException => defValue
}
}
def getFloat(key: String, defValue: Float): Float = {
try {
params.get(key) match {
case Some(v: String) => v.toFloat
case Some(v: Byte) => v.toFloat
case Some(v: Short) => v.toFloat
case Some(v: Int) => v.toFloat
case Some(v: Long) => v.toFloat
case Some(v: Float) => v
case Some(v: Double) => v.toFloat
case _ => defValue
}
} catch {
case _: NumberFormatException => defValue
}
}
def getDouble(key: String, defValue: Double): Double = {
try {
params.get(key) match {
case Some(v: String) => v.toDouble
case Some(v: Byte) => v.toDouble
case Some(v: Short) => v.toDouble
case Some(v: Int) => v.toDouble
case Some(v: Long) => v.toDouble
case Some(v: Float) => v.toDouble
case Some(v: Double) => v
case _ => defValue
}
} catch {
case _: NumberFormatException => defValue
}
}
def getBoolean(key: String, defValue: Boolean): Boolean = {
try {
params.get(key) match {
case Some(v: String) => v.toBoolean
case Some(v: Boolean) => v
case _ => defValue
}
} catch {
case _: NumberFormatException => defValue
}
}
case class StringAnyMap(values: Map[String, Any])
def getParamMap(key: String, defValue: Map[String, Any]
= Map[String, Any]()): Map[String, Any] = {
params.get(key) match {
case Some(v: StringAnyMap) => v.values
case _ => defValue
}
}
def getParamMapOpt(key: String): Option[Map[String, Any]] = {
params.get(key) match {
case Some(v: StringAnyMap) => Some(v.values)
case _ => None
}
}
def getArr[T](key: String): Seq[T] = {
case class TSeqs(values: Seq[T])
params.get(key) match {
case Some(seq: TSeqs) => seq.values
case _ => Nil
}
}
def addIfNotExist(key: String, value: Any): Map[String, Any] = {
params.get(key) match {
case Some(v) => params
case _ => params + (key -> value)
}
}
def removeKeys(keys: Iterable[String]): Map[String, Any] = {
params -- keys
}
}
}