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

Commit 610860d

Browse files
committed
API overhaul
1 parent e1fdf59 commit 610860d

15 files changed

+402
-189
lines changed

Diff for: project/build.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
project.organization=com.ckkloverdos
44
project.name=converter
55
sbt.version=0.7.7
6-
project.version=0.3.0
6+
project.version=0.4.0-SNAPSHOT
77
publish.remote=false
88
build.scala.versions=2.9.1
99
project.initialize=false

Diff for: project/build/Project.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Project(info: ProjectInfo) extends DefaultProject(info) {
6262
val lib_slf4j = "org.slf4j" % "slf4j-api" % "1.6.1" % "compile" withSources()
6363
val lib_logback_simple = "ch.qos.logback" % "logback-classic" % "0.9.28" % "test" withSources()
6464
val lib_junit_interface = "com.novocode" % "junit-interface" % "0.7" % "test"
65-
val lib_maybe = "com.ckkloverdos" %% "maybe" % "0.3.0" % "compile" withSources()
65+
val lib_maybe = "com.ckkloverdos" %% "maybe" % "0.4.0-SNAPSHOT" % "compile" withSources()
6666

6767
override def repositories =
6868
if (version.toString.endsWith("-SNAPSHOT")) super.repositories + ScalaToolsSnapshots

Diff for: src/main/scala/com/ckkloverdos/convert/CanConvert.scala

-32
This file was deleted.

Diff for: src/main/scala/com/ckkloverdos/convert/Converter.scala

+1-29
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,6 @@ import com.ckkloverdos.maybe.Maybe
2222
*
2323
* @author Christos KK Loverdos <loverdos@gmail.com>.
2424
*/
25-
trait Converter[S, T] extends CanConvert {
26-
def sourceType: Manifest[S]
27-
def targetType: Manifest[T]
25+
trait Converter extends ConverterBase {
2826
def isStrictSource: Boolean
29-
30-
/**
31-
* Convert or throw an exception.
32-
*
33-
* This is a low-level function.
34-
*/
35-
@throws(classOf[ConverterException])
36-
def convertEx(sourceValue: S): T
37-
38-
def convert(sourceValue: S): Maybe[T] = Maybe(convertEx(sourceValue))
39-
40-
def apply(sourceValue: S): Maybe[T] = convert(sourceValue)
41-
def toFunction: S => Maybe[T] = (s) => convert(s)
42-
}
43-
44-
/**
45-
* @author Christos KK Loverdos <loverdos@gmail.com>.
46-
*/
47-
object Converter {
48-
type AnyConverter = Converter[_, _]
49-
type AnyManifest = Manifest[_]
50-
51-
def newConverter[S: Manifest, T: Manifest](strictSource: Boolean)(f: S => T): Converter[S, T] = {
52-
new ConverterImpl(manifest[S], manifest[T], strictSource, f)
53-
}
5427
}
55-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.convert
18+
19+
import com.ckkloverdos.maybe.Maybe
20+
import org.slf4j.LoggerFactory
21+
22+
/**
23+
*
24+
* @author Christos KK Loverdos <loverdos@gmail.com>
25+
*/
26+
27+
trait ConverterBase {
28+
protected val logger = LoggerFactory.getLogger(getClass)
29+
30+
def canConvertValueToType[S: Manifest, T: Manifest](sourceValue: S): Boolean =
31+
canConvertType[S, T]
32+
33+
def canConvertValueToValue[S: Manifest, T: Manifest](sourceValue: S, targetValue: T): Boolean =
34+
canConvertType[S, T]
35+
36+
def canConvertType[S: Manifest, T: Manifest]: Boolean
37+
38+
/**
39+
* Convert or throw an exception.
40+
*
41+
* This is a low-level function.
42+
*/
43+
@throws(classOf[ConverterException])
44+
def convertEx[T: Manifest](sourceValue: Any): T
45+
46+
def convert[T: Manifest](sourceValue: Any): Maybe[T] = Maybe {
47+
// logger.debug("ConverterBase::convert(%s: %s): %s".format(sourceValue, sourceValue.getClass, manifest[T]))
48+
convertEx[T](sourceValue)
49+
}
50+
}

Diff for: src/main/scala/com/ckkloverdos/convert/ConverterImpl.scala

-56
This file was deleted.

Diff for: src/main/scala/com/ckkloverdos/convert/Converters.scala

+49-25
Original file line numberDiff line numberDiff line change
@@ -18,50 +18,74 @@ 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
2123

2224
/**
2325
* An immutable registry for converters.
2426
*
2527
* @author Christos KK Loverdos <loverdos@gmail.com>.
2628
*/
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]
3131

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)
3437
}
3538

3639
/**
3740
* Converts a value or throws an exception if the value cannot be converted.
3841
*/
3942
@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))
4248
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)
4763
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)
4967
}
5068
}
5169

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)
6378
}
6479

6580
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)
6791
}

Diff for: src/main/scala/com/ckkloverdos/convert/ConvertersBuilder.scala

+11-14
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.ckkloverdos.convert
1818

1919
import java.util.concurrent.locks.ReentrantLock
20-
import Converter.{AnyConverter}
2120
import ConverterHelpers.{lock}
2221
import org.slf4j.LoggerFactory
2322
import select.{ConverterSelectionStrategy, CachedMostSpecificTypeFirstSelection}
@@ -32,46 +31,44 @@ import select.{ConverterSelectionStrategy, CachedMostSpecificTypeFirstSelection}
3231
class ConvertersBuilder {
3332
protected val logger = LoggerFactory.getLogger(getClass)
3433
private[this] val _lock = new ReentrantLock()
35-
private[this] var _converters: Vector[AnyConverter] = Vector()
34+
private[this] var _converters: Vector[Converter] = Vector()
3635

37-
def registerConverter(converter: AnyConverter): this.type = {
36+
def registerConverter(converter: Converter): this.type = {
3837
lock(_lock) {
3938
logger.trace("Adding converter %s".format(converter))
4039
_converters = converter +: _converters
4140
}
4241
this
4342
}
4443

45-
def +=[S: Manifest, T: Manifest](cw: Converter[S, T]): this.type = {
44+
def +=[S: Manifest, T: Manifest](cw: Converter): this.type = {
4645
this.registerConverter(cw)
4746
}
4847

49-
def ++=(cw1: AnyConverter, cw2: AnyConverter, cwSeq: AnyConverter*): this.type = {
48+
def ++=(cw1: Converter, cw2: Converter, cwSeq: Converter*): this.type = {
5049
this += cw1 += cw2
5150
for(cw <- cwSeq) { this += cw }
5251
this
5352
}
5453

55-
def ++=(cwTrav: Traversable[AnyConverter]): this.type = {
54+
def ++=(cwTrav: Traversable[Converter]): this.type = {
5655
for(cw <- cwTrav) { this += cw }
5756
this
5857
}
5958

60-
def register[S, T](sm: Manifest[S], tm: Manifest[T], ss: Boolean = true)(f: (S) => T): this.type = {
61-
implicit val ism = sm
62-
implicit val itm = tm
63-
this += Converter.newConverter[S, T](ss)(f)
59+
def registerST[S, T](sm: Manifest[S], tm: Manifest[T], strictSource: Boolean = true)(f: (S) => T): this.type = {
60+
val converter = Converters.newSourceTargetConverter(sm, tm, strictSource)(f)
61+
this += converter
6462
this
6563
}
6664

67-
def register[S: Manifest, T: Manifest](ss: Boolean)(f: (S) => T): this.type = {
68-
this += Converter.newConverter[S, T](ss)(f)
69-
this
65+
def register[S: Manifest, T: Manifest](strictSource: Boolean)(f: (S) => T): this.type = {
66+
registerST(manifest[S], manifest[T], strictSource)(f)
7067
}
7168

7269
def build: Converters =
7370
buildWithStrategy(new CachedMostSpecificTypeFirstSelection(_))
7471

75-
def buildWithStrategy(f: (Traversable[AnyConverter]) => ConverterSelectionStrategy): Converters =
72+
def buildWithStrategy(f: (Traversable[Converter]) => ConverterSelectionStrategy): Converters =
7673
new Converters(f(_converters))
7774
}

0 commit comments

Comments
 (0)