Skip to content

Commit

Permalink
review fixes 9
Browse files Browse the repository at this point in the history
  • Loading branch information
Łukasz Bigorajski committed Oct 30, 2024
1 parent 956f6be commit 5562246
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,35 @@ object Conversion {
}

trait ConversionExt extends ExtensionMethodsHandler with Conversion {
val definitions: List[MethodDefinition]
private lazy val definitionsByName = definitions().groupBy(_.name)

def definitions(): List[MethodDefinition] = {
val targetTypeSimpleName = resultTypeClass.simpleName()
List(
definition(
Typed.typedClass[JBoolean],
s"is$targetTypeSimpleName",
Some(s"Check whether the value can be convert to a $targetTypeSimpleName")
),
definition(
typingResult,
s"to$targetTypeSimpleName",
Some(s"Convert the value to $targetTypeSimpleName or throw exception in case of failure")
),
definition(
typingResult,
s"to${targetTypeSimpleName}OrNull",
Some(s"Convert the value to $targetTypeSimpleName or null in case of failure")
),
)
}

// Conversion extension should be available for every class in a runtime
override def appliesToClassInRuntime(clazz: Class[_]): Boolean = true

override def extractDefinitions(clazz: Class[_], set: ClassDefinitionSet): Map[String, List[MethodDefinition]] = {
if (appliesToConversion(clazz)) {
definitions.groupBy(_.name)
definitionsByName
} else {
Map.empty
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package pl.touk.nussknacker.engine.extension

import org.springframework.util.NumberUtils
import pl.touk.nussknacker.engine.api.typed.typing.Typed
import pl.touk.nussknacker.engine.definition.clazz.{ClassDefinitionSet, MethodDefinition}
import pl.touk.nussknacker.engine.definition.clazz.ClassDefinitionSet

import java.lang.{Boolean => JBoolean}
import java.math.{BigDecimal => JBigDecimal, BigInteger => JBigInteger}
Expand All @@ -21,20 +20,6 @@ object ToBigDecimalConversionExt extends ConversionExt with ToNumericConversion
override type ResultType = JBigDecimal
override val resultTypeClass: Class[JBigDecimal] = classOf[JBigDecimal]

override val definitions: List[MethodDefinition] = List(
definition(Typed.typedClass[JBoolean], "isBigDecimal", Some("Check whether can be convert to a BigDecimal")),
definition(
Typed.typedClass[JBigDecimal],
"toBigDecimal",
Some("Convert to BigDecimal or throw exception in case of failure")
),
definition(
Typed.typedClass[JBigDecimal],
"toBigDecimalOrNull",
Some("Convert to BigDecimal or null in case of failure")
),
)

override def createConverter(
set: ClassDefinitionSet
): ToExtensionMethodInvocationTargetConverter[ToBigDecimalConversionExt] =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package pl.touk.nussknacker.engine.extension

import pl.touk.nussknacker.engine.api.typed.typing
import pl.touk.nussknacker.engine.api.typed.typing.Typed
import pl.touk.nussknacker.engine.definition.clazz.{ClassDefinitionSet, StaticMethodDefinition}
import pl.touk.nussknacker.engine.definition.clazz.ClassDefinitionSet

import java.lang.{Boolean => JBoolean}

Expand All @@ -16,25 +14,17 @@ object ToBooleanConversionExt extends ConversionExt {
private val cannotConvertException = (value: Any) =>
new IllegalArgumentException(s"Cannot convert: $value to Boolean")
private val allowedClassesForConversion: Set[Class[_]] = Set(classOf[String], classOf[Object])
private val booleanTyping: typing.TypedClass = Typed.typedClass[JBoolean]

override val definitions: List[StaticMethodDefinition] = List(
definition(booleanTyping, "isBoolean", Some("Check whether can be convert to a Boolean")),
definition(booleanTyping, "toBoolean", Some("Convert to Boolean or throw exception in case of failure")),
definition(booleanTyping, "toBooleanOrNull", Some("Convert to Boolean or null in case of failure")),
)

override type ExtensionMethodInvocationTarget = ToBooleanConversionExt
override val invocationTargetClass: Class[ToBooleanConversionExt] = classOf[ToBooleanConversionExt]
override type ResultType = JBoolean
override val resultTypeClass: Class[JBoolean] = classOf[JBoolean]

override def createConverter(
set: ClassDefinitionSet
): ToExtensionMethodInvocationTargetConverter[ToBooleanConversionExt] =
(target: Any) => new ToBooleanConversionExt(target)

override type ResultType = JBoolean
override val resultTypeClass: Class[JBoolean] = classOf[JBoolean]

override def appliesToConversion(clazz: Class[_]): Boolean = allowedClassesForConversion.contains(clazz)

override def convertEither(value: Any): Either[Throwable, JBoolean] = value match {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package pl.touk.nussknacker.engine.extension

import org.springframework.util.NumberUtils
import pl.touk.nussknacker.engine.api.typed.typing.Typed
import pl.touk.nussknacker.engine.definition.clazz.{ClassDefinitionSet, MethodDefinition}
import pl.touk.nussknacker.engine.definition.clazz.ClassDefinitionSet
import pl.touk.nussknacker.engine.extension.Conversion.toNumberEither

import java.lang.{Boolean => JBoolean, Double => JDouble}
Expand All @@ -20,12 +19,6 @@ object ToDoubleConversionExt extends ConversionExt with ToNumericConversion {
override type ResultType = JDouble
override val resultTypeClass: Class[JDouble] = classOf[JDouble]

override val definitions: List[MethodDefinition] = List(
definition(Typed.typedClass[JBoolean], "isDouble", Some("Check whether can be convert to a Double")),
definition(Typed.typedClass[JDouble], "toDouble", Some("Convert to Double or throw exception in case of failure")),
definition(Typed.typedClass[JDouble], "toDoubleOrNull", Some("Convert to Double or null in case of failure")),
)

override def createConverter(
set: ClassDefinitionSet
): ToExtensionMethodInvocationTargetConverter[ToDoubleConversionExt] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ object ToListConversionExt extends ConversionExt with ToCollectionConversion {
description = Option("Convert to a list or null in case of failure")
)

override val definitions: List[MethodDefinition] = List(
override type ExtensionMethodInvocationTarget = ToListConversionExt
override val invocationTargetClass: Class[ToListConversionExt] = classOf[ToListConversionExt]

override def definitions(): List[MethodDefinition] = List(
isListMethodDefinition,
toListDefinition,
toListOrNullDefinition,
)

override type ExtensionMethodInvocationTarget = ToListConversionExt
override val invocationTargetClass: Class[ToListConversionExt] = classOf[ToListConversionExt]

override def createConverter(
set: ClassDefinitionSet
): ToExtensionMethodInvocationTargetConverter[ToListConversionExt] =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package pl.touk.nussknacker.engine.extension

import org.springframework.util.NumberUtils
import pl.touk.nussknacker.engine.api.typed.typing.Typed
import pl.touk.nussknacker.engine.definition.clazz.{ClassDefinitionSet, MethodDefinition}
import pl.touk.nussknacker.engine.definition.clazz.ClassDefinitionSet
import pl.touk.nussknacker.engine.extension.Conversion.toNumberEither

import java.lang.{Boolean => JBoolean, Long => JLong}
Expand All @@ -21,12 +20,6 @@ object ToLongConversionExt extends ConversionExt with ToNumericConversion {
override type ResultType = JLong
override val resultTypeClass: Class[JLong] = classOf[JLong]

override val definitions: List[MethodDefinition] = List(
definition(Typed.typedClass[JBoolean], "isLong", Some("Check whether can be convert to a Long")),
definition(Typed.typedClass[JLong], "toLong", Some("Convert to Long or throw exception in case of failure")),
definition(Typed.typedClass[JLong], "toLongOrNull", Some("Convert to Long or null in case of failure")),
)

override def createConverter(
set: ClassDefinitionSet
): ToExtensionMethodInvocationTargetConverter[ToLongConversionExt] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object ToMapConversionExt extends ConversionExt with ToCollectionConversion {
description = Option("Convert to a map or null in case of failure")
)

override val definitions: List[MethodDefinition] = List(
override def definitions(): List[MethodDefinition] = List(
isMapMethodDefinition,
toMapDefinition,
toMapOrNullDefinition,
Expand Down

0 comments on commit 5562246

Please sign in to comment.