@@ -18,50 +18,74 @@ package com.ckkloverdos.convert
18
18
19
19
import select .ConverterSelectionStrategy
20
20
import com .ckkloverdos .maybe ._
21
+ import com .ckkloverdos .manifest .ManifestHelpers
22
+ import org .slf4j .LoggerFactory
21
23
22
24
/**
23
25
* An immutable registry for converters.
24
26
*
25
27
* @author Christos KK Loverdos <loverdos@gmail.com>.
26
28
*/
27
- class Converters (selector : ConverterSelectionStrategy ) extends CanConvert {
28
- def canConvertType (sm : Converter .AnyManifest , tm : Converter .AnyManifest ) = {
29
- selector.find(sm, tm).isJust
30
- }
29
+ class Converters (selector : ConverterSelectionStrategy ) extends ConverterBase {
30
+ def canConvertType [S : Manifest , T : Manifest ]: Boolean = selector.canConvertType[S , T ]
31
31
32
- def findConverter [S , T ](sm : Manifest [S ], tm : Manifest [T ]): Maybe [Converter [S , T ]] = {
33
- selector.find(sm, tm).asInstanceOf [Maybe [Converter [S , T ]]]
32
+ def findConverter [S : Manifest , T : Manifest ]: Maybe [Converter ] = {
33
+ val sm = manifest[S ]
34
+ val tm = manifest[T ]
35
+ // logger.debug("findConverter(%s, %s)".format(sm, tm))
36
+ selector.find(sm, tm)
34
37
}
35
38
36
39
/**
37
40
* Converts a value or throws an exception if the value cannot be converted.
38
41
*/
39
42
@ throws(classOf [ConverterException ])
40
- def convertValueEx [S : Manifest , T ](sourceValue : S , tm : Manifest [T ]): T = {
41
- val sm = manifest[S ]
43
+ def convertEx [T : Manifest ](sourceValue : Any ): T = {
44
+ val sm = ManifestHelpers .manifestOfAny(sourceValue)
45
+ val tm = manifest[T ]
46
+ // logger.debug("[1] Converters::convertEx(%s: %s)(tm=%s)".format(sourceValue, if(null eq sourceValue.asInstanceOf[AnyRef]) "Null" else sourceValue.getClass, tm))
47
+ // logger.debug("[2] Converters::convertEx(%s: %s): %s".format(sourceValue, sm, tm))
42
48
findConverter(sm, tm) match {
43
- case Just (cv) =>
44
- cv.convertEx(sourceValue)
45
- case NoVal =>
46
- ConverterException (" Could not find converter from %s -> %s for value %s" , sm, tm, sourceValue)
49
+ case Just (cv) ⇒
50
+ try {
51
+ // logger.debug("Converters::convertEx:: found %s".format(cv))
52
+ cv.convertEx[T ](sourceValue)
53
+ } catch {
54
+ case e : Exception =>
55
+ val errMsg = " Error converting %s -> %s for value %s" .format(sm, tm, sourceValue)
56
+ // logger.error("Converters::convertEx:: " + errMsg)
57
+ ConverterException (e, errMsg)
58
+ }
59
+ case NoVal ⇒
60
+ val errMsg = " Could not find converter %s -> %s for value %s" .format(sm, tm, sourceValue)
61
+ // logger.error("Converters::convertEx:: " + errMsg)
62
+ throw new ConverterException (errMsg)
47
63
case Failed (exception, explanation) =>
48
- ConverterException (exception, " Error [%s] trying to find converter from %s -> %s for value %s" , explanation, sm, tm, sourceValue)
64
+ val errMsg = " Could not find converter %s -> %s for value %s" .format(sm, tm, sourceValue)
65
+ // logger.error("Converters::convertEx:: " + errMsg)
66
+ throw new ConverterException (errMsg, exception)
49
67
}
50
68
}
51
69
52
- def convertValue [S : Manifest , T ](sourceValue : S , tm : Manifest [T ]): Maybe [T ] = {
53
- findConverter(manifest[S ], tm) flatMap (_.convert(sourceValue))
54
- }
55
-
56
- def convertValueToByte [S : Manifest ](sourceValue : S ): Maybe [Byte ] = convertValue(sourceValue, Manifest .Byte )
57
- def convertValueToShort [S : Manifest ](sourceValue : S ): Maybe [Short ] = convertValue(sourceValue, Manifest .Short )
58
- def convertValueToBoolean [S : Manifest ](sourceValue : S ): Maybe [Boolean ] = convertValue(sourceValue, Manifest .Boolean )
59
- def convertValueToInt [S : Manifest ](sourceValue : S ): Maybe [Int ] = convertValue(sourceValue, Manifest .Int )
60
- def convertValueToLong [S : Manifest ](sourceValue : S ): Maybe [Long ] = convertValue(sourceValue, Manifest .Long )
61
- def convertValueToFloat [S : Manifest ](sourceValue : S ): Maybe [Float ] = convertValue(sourceValue, Manifest .Float )
62
- def convertValueToDouble [S : Manifest ](sourceValue : S ): Maybe [Double ] = convertValue(sourceValue, Manifest .Double )
70
+ def convertToByte [S : Manifest ](sourceValue : S ): Maybe [Byte ] = convert[Byte ](sourceValue)
71
+ def convertToBoolean [S : Manifest ](sourceValue : S ): Maybe [Boolean ] = convert[Boolean ](sourceValue)
72
+ def convertToShort [S : Manifest ](sourceValue : S ): Maybe [Short ] = convert[Short ](sourceValue)
73
+ def convertToChar [S : Manifest ](sourceValue : S ): Maybe [Char ] = convert[Char ](sourceValue)
74
+ def convertToInt [S : Manifest ](sourceValue : S ): Maybe [Int ] = convert[Int ](sourceValue)
75
+ def convertToLong [S : Manifest ](sourceValue : S ): Maybe [Long ] = convert[Long ](sourceValue)
76
+ def convertToFloat [S : Manifest ](sourceValue : S ): Maybe [Float ] = convert[Float ](sourceValue)
77
+ def convertToDouble [S : Manifest ](sourceValue : S ): Maybe [Double ] = convert[Double ](sourceValue)
63
78
}
64
79
65
80
object Converters {
66
- val DefaultConverters = new StdConvertersBuilder ().registerDefaultConversions().build
81
+ final val DefaultConverters = new StdConvertersBuilder ().registerDefaultConversions().build
82
+
83
+ final def identityConverter : IdentityConverter .type =
84
+ IdentityConverter
85
+
86
+ final def justIdentityConverter : Just [IdentityConverter .type ] =
87
+ Just (IdentityConverter )
88
+
89
+ final def newSourceTargetConverter [S , T ](sm : Manifest [S ], tm : Manifest [T ], strictSource : Boolean )(f : S => T ): SourceTargetConverter [S , T ] =
90
+ new SourceTargetConverter [S , T ](sm, tm, strictSource, f)
67
91
}
0 commit comments