trait Uniformity[A] extends Normalization[A]
Defines a custom way to normalize instances of a type that can also handle normalization of that type when passed as Any
.
For example, to normalize Double
s by truncating off any decimal part,
you might write:
import org.scalactic._ val truncated = new Uniformity[Double] { def normalized(d: Double) = d.floor def normalizedCanHandle(o: Any) = o.isInstanceOf[Double] def normalizedOrSame(o: Any): Any = o match { case d: Double => normalized(d) case _ => o } }
Given this definition you could use it with the Explicitly
DSL like this:
import org.scalatest._ import Matchers._ 2.1 should equal (2.0) (after being truncated)
If you make the truncated
val
implicit and import or mix in the members of NormMethods
,
you can access the behavior by invoking .norm
on Double
s.
implicit val doubleUniformity = truncated import NormMethods._ val d = 2.1 d.norm // returns 2.0
Note that by creating a Uniformity
rather than just an instance of its supertype, Normalization
,
it can be used more generally. For example, Uniformity
s allow you to the Explicitly
DSL with
TripleEquals
, whereas Normalization
s require
TypeCheckedTripleEquals
.
Uniformity
s also enable you to use the Explicitly
DSL with ScalaTest's should
===
, equal
,
and contain
matcher syntax, whereas a plain Normalization
can only be used with should
===
, and only
under TypeCheckedTripleEquals
.
- A
the type whose uniformity is being defined
- Self Type
- Uniformity[A]
- Source
- Uniformity.scala
- Alphabetic
- By Inheritance
- Uniformity
- Normalization
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Abstract Value Members
-
abstract
def
normalized(a: A): A
Returns a normalized form of the passed object.
Returns a normalized form of the passed object.
If the passed object is already in normal form, this method may return the same instance passed.
- a
the object to normalize
- returns
the normalized form of the passed object
- Definition Classes
- Normalization
-
abstract
def
normalizedCanHandle(b: Any): Boolean
Indicates whether this
Uniformity
'snormalized
method can “handle” the passed object, if cast to the appropriate typeA
.Indicates whether this
Uniformity
'snormalized
method can “handle” the passed object, if cast to the appropriate typeA
.If this method returns true for a particular passed object, it means that if the object is passed to
normalizedOrSame
, that method will return the result of passing it tonormalized
. It does not mean that the object will necessarily be modified when passed tonormalizedOrSame
ornormalized
. For example, thelowerCased
field ofStringNormalizations
is aUniformity[String]
that normalizes strings by forcing all characters to lower case:scala> import org.scalactic._ import org.scalactic._ scala> import StringNormalizations._ import StringNormalizations._ scala> lowerCased res0: org.scalactic.Uniformity[String] = lowerCased scala> lowerCased.normalized("HALLOOO!") res1: String = hallooo!
Now consider two strings held from variables of type
AnyRef
:scala> val yell: AnyRef = "HOWDY" yell: AnyRef = HOWDY scala> val whisper: AnyRef = "howdy" whisper: AnyRef = howdy
As you would expect, when
yell
is passed tonormalizedCanHandle
, it returns true, and whenyell
is passed tonormalizedOrSame
, it returns a lower-cased (normal) form:scala> lowerCased.normalizedCanHandle(yell) res2: Boolean = true scala> lowerCased.normalizedOrSame(yell) res3: Any = howdy
A similar thing happens, however, when
whisper
is passed tonormalizedCanHandle
andnormalizedOrSame
, even though in this case the string is already in normal form according to thelowerCased
Uniformity
:scala> lowerCased.normalizedCanHandle(whisper) res4: Boolean = true scala> lowerCased.normalizedOrSame(whisper) res5: Any = howdy
This illustrates that
normalizedCanHandle
does not indicate that the passed object is not in normalized form already, just that it can be be handled by thenormalized
method. This further means that thenormalized
method itself simply ensures that objects are returned in normal form. It need not necessarily change them: if a passed object is already in normal form,normalized
can (and usually should) return the exact same object. That is in fact what happened when we normalizedwhisper
. Sincewhisper
's value of"hello"
was already in normal form (all lower-cased),normalized
( invoked by thenormalizedOrSame
method) returned the exact same object passed:scala> val whisperNormed = res5.asInstanceOf[AnyRef] whisperNormed: AnyRef = howdy scala> whisperNormed eq whisper res8: Boolean = true
-
abstract
def
normalizedOrSame(b: Any): Any
Returns either the result of passing this object to
normalized
, if appropriate, or the same object.Returns either the result of passing this object to
normalized
, if appropriate, or the same object.Implementations can decide what “appropriate” means, but the intent is that it will usually mean the value passed is of the type
A
. For example, if this is aUniformity[String]
, appropriate means that the value (of typeAny
) passed is actually an instance ofString
. Because of erasure, however, aUniformity[List[String]]
will only be able to tell whether a value is aList[_]
, so it might declare anyList[_]
that contains onlyString
s (determined by invokingisInstanceOf[String]
on each element) to be appropriate. This means aUniformity[List[String]]
might normalize aList[AnyRef]
that happens to contain onlyStrings
.- b
the object to normalize, if appropriate
- returns
a normalized form of the passed object, if this
Uniformity
was able to normalize it, else the same object passed
Concrete Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
and(other: Uniformity[A]): Uniformity[A]
Returns a new
Uniformity
that combines this and the passedUniformity
.Returns a new
Uniformity
that combines this and the passedUniformity
.The
normalized
andnormalizedOrSame
methods of theUniformity
returned by this method return a result obtained by forwarding the passed value first to thisUniformity
's implementation of the method, then passing that result to the otherUniformity
's implementation of the method, respectively. Essentially, the body of the composednormalized
method is:uniformityPassedToAnd.normalized(uniformityOnWhichAndWasInvoked.normalized(a))
And the body of the composed
normalizedOrSame
method is:uniformityPassedToAnd.normalizedOrSame(uniformityOnWhichAndWasInvoked.normalizedOrSame(a))
The
normalizeCanHandle
method of theUniformity
returned by this method returns a result obtained by anding the result of forwarding the passed value to thisUniformity
's implementation of the method with the result of forwarding it to the passedUniformity
's implementation. Essentially, the body of the composednormalizeCanHandle
method is:uniformityOnWhichAndWasInvoked.normalizeCanHandle(a) && uniformityPassedToAnd.normalizeCanHandle(a)
- other
a
Uniformity
to 'and' with this one- returns
a
Uniformity
representing the composition of this and the passedUniformity
-
final
def
and(other: Normalization[A]): Normalization[A]
Returns a new
Normalization
that composes this and the passedNormalization
.Returns a new
Normalization
that composes this and the passedNormalization
.The
normalized
method of theNormalization
returned by this method returns a normalized form of the passed object obtained by forwarding the passed value first to thisNormalization
'snormalized
method, then passing that result to the otherNormalization
'snormalized
method. Essentially, the body of the composednormalized
method is:normalizationPassedToAnd.normalized(normalizationOnWhichAndWasInvoked.normalized(a))
- other
a
Normalization
to 'and' with this one- returns
a
Normalization
representing the composition of this and the passedNormalization
- Definition Classes
- Normalization
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
final
def
toEquality(implicit equality: Equality[A]): NormalizingEquality[A]
Converts this
Uniformity
to aNormalizingEquality[A]
whosenormalized
,normalizedCanHandle
, andnormalizedOrSame
methods delegate to thisUniformity[A]
and whoseafterNormalizationEquality
field returns the implicitly passedEquality[A]
.Converts this
Uniformity
to aNormalizingEquality[A]
whosenormalized
,normalizedCanHandle
, andnormalizedOrSame
methods delegate to thisUniformity[A]
and whoseafterNormalizationEquality
field returns the implicitly passedEquality[A]
.- equality
the
Equality
that the returnedNormalizingEquality
will delegate to determine equality after normalizing both left and right (if appropriate) sides.
-
final
def
toEquivalence(implicit equivalence: Equivalence[A]): NormalizingEquivalence[A]
Converts this
Normalization
to aNormalizingEquivalence[A]
whosenormalized
method delegates to thisNormalization[A]
and whoseafterNormalizationEquivalence
field returns the implicitly passedEquivalence[A]
.Converts this
Normalization
to aNormalizingEquivalence[A]
whosenormalized
method delegates to thisNormalization[A]
and whoseafterNormalizationEquivalence
field returns the implicitly passedEquivalence[A]
.- equivalence
the
Equivalence
that the returnedNormalizingEquivalence
will delegate to determine equality after normalizing both left and right (if appropriate) sides.
- Definition Classes
- Normalization
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )