Skip to content
This repository was archived by the owner on Aug 17, 2019. It is now read-only.

Commit 0017cf8

Browse files
committed
Use our Type instead of Manifest
1 parent 044a4c1 commit 0017cf8

9 files changed

+160
-73
lines changed

src/main/scala/com/ckkloverdos/convert/ConverterBase.scala

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@ import org.slf4j.LoggerFactory
2727
trait ConverterBase {
2828
protected val logger = LoggerFactory.getLogger(getClass)
2929

30-
def canConvertValueToType[S: Manifest, T: Manifest](sourceValue: S): Boolean =
30+
def canConvertValueToType[S: Type, T: Type](sourceValue: S): Boolean =
3131
canConvertType[S, T]
3232

33-
def canConvertValueToValue[S: Manifest, T: Manifest](sourceValue: S, targetValue: T): Boolean =
33+
def canConvertValueToValue[S: Type, T: Type](sourceValue: S, targetValue: T): Boolean =
3434
canConvertType[S, T]
3535

36-
def canConvertType[S: Manifest, T: Manifest]: Boolean
36+
def canConvertType[S: Type, T: Type]: Boolean
3737

3838
/**
3939
* Convert or throw an exception.
4040
*
4141
* This is a low-level function.
4242
*/
4343
@throws(classOf[ConverterException])
44-
def convertEx[T: Manifest](sourceValue: Any): T
44+
def convertEx[T: Type](sourceValue: Any): T
4545

46-
def convert[T: Manifest](sourceValue: Any): Maybe[T] = Maybe {
46+
def convert[T: Type](sourceValue: Any): Maybe[T] = Maybe {
4747
// logger.debug("ConverterBase::convert(%s: %s): %s".format(sourceValue, sourceValue.getClass, manifest[T]))
4848
convertEx[T](sourceValue)
4949
}
50-
}
50+
}

src/main/scala/com/ckkloverdos/convert/Converters.scala

+16-18
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,18 @@ package com.ckkloverdos.convert
1818

1919
import select.ConverterSelectionStrategy
2020
import com.ckkloverdos.maybe._
21-
import com.ckkloverdos.manifest.ManifestHelpers
22-
import org.slf4j.LoggerFactory
2321

2422
/**
2523
* An immutable registry for converters.
2624
*
2725
* @author Christos KK Loverdos <loverdos@gmail.com>.
2826
*/
2927
class Converters(selector: ConverterSelectionStrategy) extends ConverterBase {
30-
def canConvertType[S: Manifest, T: Manifest]: Boolean = selector.canConvertType[S, T]
28+
def canConvertType[S: Type, T: Type]: Boolean = selector.canConvertType[S, T]
3129

32-
def findConverter[S : Manifest, T : Manifest]: Maybe[Converter] = {
33-
val sm = manifest[S]
34-
val tm = manifest[T]
30+
def findConverter[S : Type, T : Type]: Maybe[Converter] = {
31+
val sm = typeOf[S]
32+
val tm = typeOf[T]
3533
// logger.debug("findConverter(%s, %s)".format(sm, tm))
3634
selector.find(sm, tm)
3735
}
@@ -40,9 +38,9 @@ class Converters(selector: ConverterSelectionStrategy) extends ConverterBase {
4038
* Converts a value or throws an exception if the value cannot be converted.
4139
*/
4240
@throws(classOf[ConverterException])
43-
def convertEx[T: Manifest](sourceValue: Any): T = {
44-
val sm = ManifestHelpers.manifestOfAny(sourceValue)
45-
val tm = manifest[T]
41+
def convertEx[T: Type](sourceValue: Any): T = {
42+
val sm = typeOfAny(sourceValue)
43+
val tm = typeOf[T]
4644
// logger.debug("[1] Converters::convertEx(%s: %s)(tm=%s)".format(sourceValue, if(null eq sourceValue.asInstanceOf[AnyRef]) "Null" else sourceValue.getClass, tm))
4745
// logger.debug("[2] Converters::convertEx(%s: %s): %s".format(sourceValue, sm, tm))
4846
findConverter(sm, tm) match {
@@ -67,14 +65,14 @@ class Converters(selector: ConverterSelectionStrategy) extends ConverterBase {
6765
}
6866
}
6967

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)
68+
def convertToByte[S: Type](sourceValue: S): Maybe[Byte] = convert[Byte](sourceValue)
69+
def convertToBoolean[S: Type](sourceValue: S): Maybe[Boolean] = convert[Boolean](sourceValue)
70+
def convertToShort[S: Type](sourceValue: S): Maybe[Short] = convert[Short](sourceValue)
71+
def convertToChar[S: Type](sourceValue: S): Maybe[Char] = convert[Char](sourceValue)
72+
def convertToInt[S: Type](sourceValue: S): Maybe[Int] = convert[Int](sourceValue)
73+
def convertToLong[S: Type](sourceValue: S): Maybe[Long] = convert[Long](sourceValue)
74+
def convertToFloat[S: Type](sourceValue: S): Maybe[Float] = convert[Float](sourceValue)
75+
def convertToDouble[S: Type](sourceValue: S): Maybe[Double] = convert[Double](sourceValue)
7876
}
7977

8078
object Converters {
@@ -86,6 +84,6 @@ object Converters {
8684
final def justIdentityConverter: Just[IdentityConverter.type] =
8785
Just(IdentityConverter)
8886

89-
final def newSourceTargetConverter[S, T](sm: Manifest[S], tm: Manifest[T], strictSource: Boolean)(f: S => T): SourceTargetConverter[S, T] =
87+
final def newSourceTargetConverter[S, T](sm: Type[S], tm: Type[T], strictSource: Boolean)(f: S T): SourceTargetConverter[S, T] =
9088
new SourceTargetConverter[S, T](sm, tm, strictSource, f)
9189
}

src/main/scala/com/ckkloverdos/convert/ConvertersBuilder.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class ConvertersBuilder {
4141
this
4242
}
4343

44-
def +=[S: Manifest, T: Manifest](cw: Converter): this.type = {
44+
def +=[S: Type, T: Type](cw: Converter): this.type = {
4545
this.registerConverter(cw)
4646
}
4747

@@ -56,14 +56,14 @@ class ConvertersBuilder {
5656
this
5757
}
5858

59-
def registerST[S, T](sm: Manifest[S], tm: Manifest[T], strictSource: Boolean = true)(f: (S) => T): this.type = {
59+
def registerST[S, T](sm: Type[S], tm: Type[T], strictSource: Boolean = true)(f: (S) => T): this.type = {
6060
val converter = Converters.newSourceTargetConverter(sm, tm, strictSource)(f)
6161
this += converter
6262
this
6363
}
6464

65-
def register[S: Manifest, T: Manifest](strictSource: Boolean)(f: (S) => T): this.type = {
66-
registerST(manifest[S], manifest[T], strictSource)(f)
65+
def register[S: Type, T: Type](strictSource: Boolean)(f: (S) => T): this.type = {
66+
registerST(typeOf[S], typeOf[T], strictSource)(f)
6767
}
6868

6969
def build: Converters =

src/main/scala/com/ckkloverdos/convert/IdentityConverter.scala

+5-7
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,17 @@ package com.ckkloverdos.convert
2525
object IdentityConverter extends Converter {
2626
def isStrictSource = false
2727

28-
def canConvertType[S: Manifest, T: Manifest]: Boolean = {
29-
val sm = manifest[S]
30-
val tm = manifest[T]
31-
sm == tm
28+
def canConvertType[S: Type, T: Type]: Boolean = {
29+
typeOf[S] == typeOf[T]
3230
}
3331

3432
@throws(classOf[ConverterException])
35-
def convertEx[T: Manifest](sourceValue: Any): T = {
36-
val tm = manifest[T]
33+
def convertEx[T: Type](sourceValue: Any): T = {
34+
val tm = typeOf[T]
3735
try sourceValue.asInstanceOf[T]
3836
catch {
3937
case e: ClassCastException
40-
ConverterException(e, "Unexpected failure for identity conversion %s -> %s for value %s".format(sourceValue.getClass, tm.erasure, sourceValue))
38+
ConverterException(e, "Unexpected failure for identity conversion %s -> %s for value %s".format(sourceValue.getClass, erasureOf[T], sourceValue))
4139
}
4240
}
4341

src/main/scala/com/ckkloverdos/convert/SourceTargetConverter.scala

+27-27
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ package com.ckkloverdos.convert
2121
* @author Christos KK Loverdos <loverdos@gmail.com>.
2222
*/
2323
class SourceTargetConverter[SS, TT](
24-
val sourceType: Manifest[SS],
25-
val targetType: Manifest[TT],
24+
val sourceType: Type[SS],
25+
val targetType: Type[TT],
2626
val isStrictSource: Boolean,
2727
val function: SS => TT)
2828
extends Converter {
2929

30-
def canConvertType[S: Manifest, T: Manifest]: Boolean = {
31-
val sm = manifest[S]
32-
val tm = manifest[T]
30+
def canConvertType[S: Type, T: Type]: Boolean = {
31+
val sm = typeOf[S]
32+
val tm = typeOf[T]
3333

3434
// logger.debug("canConvertType(%s, %s), sourceType=%s, targetType=%s".format(sm, tm, sourceType, targetType))
3535

@@ -39,7 +39,7 @@ class SourceTargetConverter[SS, TT](
3939
canConvertNonStrictSource(sm, tm)
4040
}
4141

42-
private[this] def canConvertStrictSource(sm: Manifest[_], tm: Manifest[_]) = {
42+
private[this] def canConvertStrictSource(sm: Type[_], tm: Type[_]) = {
4343
SourceTargetConverter.canConvertWithStrictSource(
4444
sourceType,
4545
targetType,
@@ -48,7 +48,7 @@ class SourceTargetConverter[SS, TT](
4848
)
4949
}
5050

51-
private[this] def canConvertNonStrictSource(sm: Manifest[_], tm: Manifest[_]) = {
51+
private[this] def canConvertNonStrictSource(sm: Type[_], tm: Type[_]) = {
5252
SourceTargetConverter.canConvertWithNonStrictSource(
5353
sourceType,
5454
targetType,
@@ -59,8 +59,8 @@ class SourceTargetConverter[SS, TT](
5959

6060

6161
@throws(classOf[ConverterException])
62-
def convertEx[T: Manifest](sourceValue: Any): T = {
63-
val tm = manifest[T]
62+
def convertEx[T: Type](sourceValue: Any): T = {
63+
val tm = typeOf[T]
6464
if(targetType != tm) {
6565
ConverterException("Unexpeced target type %s. It should have been %s".format(tm, targetType))
6666
}
@@ -79,50 +79,50 @@ class SourceTargetConverter[SS, TT](
7979
}
8080

8181
object SourceTargetConverter {
82-
def canConvertWithStrictSource[SS, TT, S, T](sourceType: Manifest[SS],
83-
targetType: Manifest[TT],
84-
givenSourceType: Manifest[S],
85-
givenTargetType: Manifest[T]): Boolean = {
82+
def canConvertWithStrictSource[SS, TT, S, T](sourceType: Type[SS],
83+
targetType: Type[TT],
84+
givenSourceType: Type[S],
85+
givenTargetType: Type[T]): Boolean = {
8686

8787
canConvertWithStrictSource(sourceType.erasure, targetType.erasure, givenSourceType, givenTargetType)
8888
}
8989

9090
def canConvertWithStrictSource[SS, TT, S, T](sourceType: Class[SS],
9191
targetType: Class[TT],
92-
givenSourceType: Manifest[S],
93-
givenTargetType: Manifest[T]): Boolean = {
92+
givenSourceType: Type[S],
93+
givenTargetType: Type[T]): Boolean = {
9494

9595
sourceType.equals(givenSourceType.erasure) && targetType.equals(givenTargetType.erasure)
9696
}
9797

98-
def canConvertWithNonStrictSource[SS, TT, S, T](sourceType: Manifest[SS],
99-
targetType: Manifest[TT],
100-
givenSourceType: Manifest[S],
101-
givenTargetType: Manifest[T]): Boolean = {
98+
def canConvertWithNonStrictSource[SS, TT, S, T](sourceType: Type[SS],
99+
targetType: Type[TT],
100+
givenSourceType: Type[S],
101+
givenTargetType: Type[T]): Boolean = {
102102

103103
canConvertWithNonStrictSource(sourceType.erasure, targetType.erasure, givenSourceType, givenTargetType)
104104
}
105105

106106
def canConvertWithNonStrictSource[SS, TT, S, T](sourceType: Class[SS],
107107
targetType: Class[TT],
108-
givenSourceType: Manifest[S],
109-
givenTargetType: Manifest[T]): Boolean = {
108+
givenSourceType: Type[S],
109+
givenTargetType: Type[T]): Boolean = {
110110

111111
sourceType.isAssignableFrom(givenSourceType.erasure) && targetType.equals(givenTargetType.erasure)
112112
}
113113
}
114114

115-
abstract class StrictSourceConverterSkeleton[SS: Manifest, TT: Manifest]extends Converter {
116-
final def canConvertType[S: Manifest, T: Manifest]: Boolean = {
117-
SourceTargetConverter.canConvertWithStrictSource(manifest[SS], manifest[TT], manifest[S], manifest[T])
115+
abstract class StrictSourceConverterSkeleton[SS: Type, TT: Type]extends Converter {
116+
final def canConvertType[S: Type, T: Type]: Boolean = {
117+
SourceTargetConverter.canConvertWithStrictSource(typeOf[SS], typeOf[TT], typeOf[S], typeOf[T])
118118
}
119119

120120
final def isStrictSource = true
121121
}
122122

123-
abstract class NonStrictSourceConverterSkeleton[SS: Manifest, TT: Manifest]extends Converter {
124-
final def canConvertType[S: Manifest, T: Manifest]: Boolean = {
125-
SourceTargetConverter.canConvertWithNonStrictSource(manifest[SS], manifest[TT], manifest[S], manifest[T])
123+
abstract class NonStrictSourceConverterSkeleton[SS: Type, TT: Type]extends Converter {
124+
final def canConvertType[S: Type, T: Type]: Boolean = {
125+
SourceTargetConverter.canConvertWithNonStrictSource(typeOf[SS], typeOf[TT], typeOf[S], typeOf[T])
126126
}
127127

128128
final def isStrictSource = false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright 2011 Christos KK Loverdos
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.ckkloverdos
18+
19+
20+
package object convert {
21+
type Type[A] = Manifest[A]
22+
23+
@inline
24+
def typeOf[T: Type]: Type[T] = Predef.manifest[T]
25+
26+
@inline
27+
def erasureOf[T: Type]: Class[T] = typeOf[T].erasure.asInstanceOf[Class[T]]
28+
29+
private[convert]
30+
final class TypeWithErasure[T](tpe: Type[T]) {
31+
def erasure: Class[T] = erasureOf(tpe)
32+
}
33+
34+
implicit def enrichType[T: Type] = new TypeWithErasure(typeOf[T])
35+
36+
private[convert]
37+
def typeOfClass[T](clazz: Class[T]): Type[_ <: AnyRef] = {
38+
if(clazz.isArray) {
39+
Manifest.arrayType(typeOfClass(clazz.getComponentType))
40+
} else {
41+
Manifest.classType(clazz)
42+
}
43+
}
44+
45+
private[convert]
46+
def typeOfAny[A](any: A): Type[_ <: Any] = {
47+
any match {
48+
case byte: Byte =>
49+
Manifest.Byte
50+
case byte: Boolean =>
51+
Manifest.Boolean
52+
case short: Short =>
53+
Manifest.Short
54+
case byte: Char =>
55+
Manifest.Char
56+
case any: Int =>
57+
Manifest.Int
58+
case long: Long =>
59+
Manifest.Long
60+
case float: Float =>
61+
Manifest.Float
62+
case double: Double =>
63+
Manifest.Double
64+
case unit: Unit =>
65+
Manifest.Unit
66+
67+
// case byte: java.lang.Byte =>
68+
// Manifest.Byte
69+
// case byte: java.lang.Boolean =>
70+
// Manifest.Boolean
71+
// case short: java.lang.Short =>
72+
// Manifest.Short
73+
// case byte: java.lang.Character =>
74+
// Manifest.Char
75+
// case any: java.lang.Integer =>
76+
// Manifest.Int
77+
// case long: java.lang.Long =>
78+
// Manifest.Long
79+
// case float: java.lang.Float =>
80+
// Manifest.Float
81+
// case double: java.lang.Double =>
82+
// Manifest.Double
83+
84+
case null =>
85+
Manifest.Null
86+
87+
case anyRef: AnyRef =>
88+
typeOfClass(anyRef.getClass)
89+
}
90+
}
91+
}

src/main/scala/com/ckkloverdos/convert/select/CachedMostSpecificTypeFirstSelection.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ final class CachedMostSpecificTypeFirstSelection(converters: Traversable[Convert
3232

3333
def isCaching = true
3434

35-
def addToCache(sm: Manifest[_], tm: Manifest[_], cv: Converter) = {
35+
def addToCache(sm: Type[_], tm: Type[_], cv: Converter) = {
3636
lock(_lock) {
3737
_cache += ((sm, tm) -> Just(cv))
3838
}
3939
}
4040

41-
override def findCached[S, T](sm: Manifest[S], tm: Manifest[T]) = {
41+
override def findCached[S, T](sm: Type[S], tm: Type[T]) = {
4242
_cache.get((sm, tm)) match {
4343
case Some(jcv) =>
4444
// logger.debug("findCached(%s, %s) => %s".format(sm, tm, jcv))
@@ -49,7 +49,7 @@ final class CachedMostSpecificTypeFirstSelection(converters: Traversable[Convert
4949
}
5050
}
5151

52-
def findNonCached[S, T](sm: Manifest[S], tm: Manifest[T]): Maybe[Converter] = {
52+
def findNonCached[S, T](sm: Type[S], tm: Type[T]): Maybe[Converter] = {
5353
_strictSourceConverters.find(_.get.canConvertType(sm, tm)) match {
5454
case Some(jcv) =>
5555
// logger.debug("findNonCached(%s, %s) => STRICT: %s".format(sm, tm, jcv))

0 commit comments

Comments
 (0)