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

Commit 3d9a863

Browse files
committed
All tests pass again
1 parent af95640 commit 3d9a863

File tree

7 files changed

+36
-23
lines changed

7 files changed

+36
-23
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ trait ConverterBase {
4444
def convertEx[T: Type](sourceValue: Any, hint: AnyRef = EmptyHint): T
4545

4646
def convert[T: Type](sourceValue: Any, hint: AnyRef = EmptyHint): MaybeEither[T] = MaybeEither {
47-
// logger.debug("ConverterBase::convert(%s: %s): %s".format(sourceValue, sourceValue.getClass, manifest[T]))
47+
// logger.debug("ConverterBase::convert(%s: %s): %s".format(sourceValue, typeOfAny(sourceValue), manifest[T]))
4848
convertEx[T](sourceValue, hint)
4949
}
5050

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ import com.ckkloverdos.maybe._
2525
* @author Christos KK Loverdos <loverdos@gmail.com>.
2626
*/
2727
class Converters(selector: ConverterSelectionStrategy) extends ConverterBase {
28-
def canConvertType[S: Type, T: Type](hint: AnyRef = EmptyHint): Boolean = selector.canConvertType[S, T](hint)
29-
30-
def findConverter[S : Type, T : Type](hint: AnyRef = EmptyHint): Maybe[Converter] = {
28+
def canConvertType[S: Type, T: Type](hint: AnyRef = EmptyHint): Boolean = {
3129
val sm = typeOf[S]
3230
val tm = typeOf[T]
33-
// logger.debug("findConverter(%s, %s)".format(sm, tm))
31+
selector.canConvertType[S, T](sm, tm, hint)
32+
}
33+
34+
def findConverter[S, T](sm: Type[S], tm: Type[T], hint: AnyRef = EmptyHint): Maybe[Converter] = {
35+
// logger.debug("findConverter(%s, %s, %s)".format(sm, tm, hint))
3436
selector.find(sm, tm, hint)
3537
}
3638

@@ -43,6 +45,7 @@ class Converters(selector: ConverterSelectionStrategy) extends ConverterBase {
4345
val tm = typeOf[T]
4446
// logger.debug("[1] Converters::convertEx(%s: %s)(tm=%s)".format(sourceValue, if(null eq sourceValue.asInstanceOf[AnyRef]) "Null" else sourceValue.getClass, tm))
4547
// logger.debug("[2] Converters::convertEx(%s: %s): %s".format(sourceValue, sm, tm))
48+
// logger.debug("[3] Calling findConverter(%s, %s, %s)".format(sm, tm, hint))
4649
findConverter(sm, tm, hint) match {
4750
case Just(cv)
4851
try {

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ class ConvertersBuilder {
3535

3636
def registerConverter(converter: Converter): this.type = {
3737
lock(_lock) {
38-
logger.trace("Adding converter %s".format(converter))
38+
// logger.debug("Adding converter %s".format(converter))
3939
_converters = converter +: _converters
4040
}
4141
this
4242
}
4343

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

@@ -63,7 +63,10 @@ class ConvertersBuilder {
6363
}
6464

6565
def register[S: Type, T: Type](strictSource: Boolean)(f: (S) => T): this.type = {
66-
registerST(typeOf[S], typeOf[T], strictSource)(f)
66+
val sm = typeOf[S]
67+
val tm = typeOf[T]
68+
// logger.debug("register(%s, %s, %s)".format(sm, tm, strictSource))
69+
registerST(sm, tm, strictSource)(f)
6770
}
6871

6972
def build: Converters =

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ class SourceTargetConverter[SS, TT](
2929
) extends Converter {
3030

3131
def canConvertType[S: Type, T: Type](hint: AnyRef = EmptyHint): Boolean = {
32+
// logger.debug("canConvertType(): theHint = %s, hint = %s".format(theHint, hint))
3233
(theHint == hint) && {
3334
val sm = typeOf[S]
3435
val tm = typeOf[T]
3536

36-
// logger.debug("canConvertType(%s, %s), sourceType=%s, targetType=%s".format(sm, tm, sourceType, targetType))
37+
// logger.debug("canConvertType(%s, %s), sourceType=%s, targetType=%s".format(sm, tm, sourceType, targetType))
3738

3839
if(isStrictSource)
3940
canConvertStrictSource(sm, tm)

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

+13-10
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,39 @@ final class CachedMostSpecificTypeFirstSelection(converters: Traversable[Convert
3434

3535
def addToCache(sm: Type[_], tm: Type[_], hint: AnyRef, cv: Converter) = {
3636
lock(_lock) {
37+
// logger.debug("addToCache(%s, %s, %s, %s)".format(sm, tm, hint, cv))
3738
_cache += ((sm, tm, hint) -> Just(cv))
3839
}
3940
}
4041

4142
override def findCached[S, T](sm: Type[S], tm: Type[T], hint: AnyRef) = {
4243
_cache.get((sm, tm, hint)) match {
4344
case Some(jcv) =>
44-
logger.debug("findCached(%s, %s) => %s".format(sm, tm, jcv))
45+
// logger.debug("findCached(%s, %s, %s) => %s".format(sm, tm, hint, jcv))
4546
jcv.asInstanceOf[Maybe[Converter]]
4647
case None =>
47-
logger.debug("findCached(%s, %s) => %s".format(sm, tm, None))
48+
// logger.debug("findCached(%s, %s, %s) => %s".format(sm, tm, hint, None))
4849
NoVal
4950
}
5051
}
5152

5253
def findNonCached[S, T](sm: Type[S], tm: Type[T], hint: AnyRef): Maybe[Converter] = {
53-
_strictSourceConverters.find(_.get.canConvertType(sm, tm, hint)) match {
54+
_strictSourceConverters.find(_.get.canConvertType(hint)(sm, tm)) match {
5455
case Some(jcv) =>
55-
logger.debug("findNonCached(%s, %s, %s) => STRICT: %s".format(sm, tm, hint, jcv))
56+
// logger.debug("findNonCached(%s, %s, %s) => STRICT: %s".format(sm, tm, hint, jcv))
5657
jcv.asInstanceOf[Maybe[Converter]]
5758
case None =>
58-
_nonStrictSourceConverters foreach { case convJ =>
59-
val conv = convJ.get
60-
}
61-
_nonStrictSourceConverters.find(_.get.canConvertType(sm, tm, hint)) match {
59+
_nonStrictSourceConverters.find { converterJust
60+
val converter = converterJust.get
61+
val canConvert = converter.canConvertType(hint)(sm, tm)
62+
// logger.debug("%s: canConvertType(%s, %s, %s) by %s".format(canConvert, sm, tm, hint, converter))
63+
canConvert
64+
} match {
6265
case Some(jcv) =>
63-
logger.debug("findNonCached(%s, %s, %s) => NON-STRICT: %s".format(sm, tm, hint, jcv))
66+
// logger.debug("findNonCached(%s, %s, %s) => NON-STRICT: %s".format(sm, tm, hint, jcv))
6467
jcv.asInstanceOf[Maybe[Converter]]
6568
case None =>
66-
logger.debug("findNonCached(%s, %s, %s) => %s".format(sm, tm, hint, None))
69+
// logger.debug("findNonCached(%s, %s, %s) => %s".format(sm, tm, hint, None))
6770
NoVal
6871
}
6972
}

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ trait ConverterSelectionStrategy {
3232

3333
def isCaching: Boolean
3434

35-
def canConvertType[S: Type, T: Type](hint: AnyRef): Boolean = {
36-
this.find(typeOf[S], typeOf[T], hint).isJust
35+
def canConvertType[S, T](sm: Type[S], tm: Type[T], hint: AnyRef): Boolean = {
36+
this.find(sm, tm, hint).isJust
3737
}
3838

3939
def shouldCache(sm: Type[_], tm: Type[_], hint: AnyRef, cv: Converter): Boolean = isCaching
@@ -43,12 +43,13 @@ trait ConverterSelectionStrategy {
4343
def findNonCached[S, T](sm: Type[S], tm: Type[T], hint: AnyRef): Maybe[Converter]
4444

4545
def find[S, T](sm: Type[S], tm: Type[T], hint: AnyRef): Maybe[Converter] = {
46-
logger.debug("find(%s, %s)".format(sm, tm))
46+
// logger.debug("find(%s, %s, %s)".format(sm, tm, hint))
4747
if(sm == tm) {
4848
val justIdentityConverter = Converters.justIdentityConverter
4949
// logger.debug("Found %s".format(justIdentityConverter))
5050
justIdentityConverter
5151
} else if(isCaching) {
52+
// logger.debug("isCaching = true, first findCached() and if that fails then findNonCached()")
5253
findCached[S, T](sm, tm, hint) match {
5354
case j@Just(cv) =>
5455
j
@@ -64,6 +65,7 @@ trait ConverterSelectionStrategy {
6465
failed
6566
}
6667
} else {
68+
// logger.debug("isCaching = false, findNonCached()")
6769
findNonCached[S, T](sm, tm, hint)
6870
}
6971
}

src/test/scala/com/ckkloverdos/convert/ConvertersTest.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package com.ckkloverdos.convert
1919
import org.junit.Assert
2020
import org.junit.Test
2121
import Assert.{assertEquals, assertTrue, fail}
22-
import com.ckkloverdos.maybe.{Maybe, Just, NoVal}
22+
import com.ckkloverdos.maybe.{Failed, Maybe, Just, NoVal}
2323
import java.nio.CharBuffer
2424

2525
/**
@@ -74,6 +74,7 @@ class ConvertersTest {
7474
def testNullToNoVal: Unit = {
7575
val registry = new StdConvertersBuilder().register[AnyRef, Maybe[AnyRef]](true)((x) => Maybe(x)).build
7676
val value = registry.convert[Maybe[AnyRef]](null)
77+
value.throwMe()
7778
assertEquals(Just(NoVal), value)
7879
}
7980

0 commit comments

Comments
 (0)