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 Doubles 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 Doubles.
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, Uniformitys allow you to the Explicitly DSL with
TripleEquals, whereas Normalizations require
TypeCheckedTripleEquals.
Uniformitys 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
- Protected
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'snormalizedmethod can “handle” the passed object, if cast to the appropriate typeA.Indicates whether this
Uniformity'snormalizedmethod 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 tonormalizedOrSameornormalized. For example, thelowerCasedfield ofStringNormalizationsis 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
yellis passed tonormalizedCanHandle, it returns true, and whenyellis 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
whisperis passed tonormalizedCanHandleandnormalizedOrSame, even though in this case the string is already in normal form according to thelowerCasedUniformity:scala> lowerCased.normalizedCanHandle(whisper) res4: Boolean = true scala> lowerCased.normalizedOrSame(whisper) res5: Any = howdy
This illustrates that
normalizedCanHandledoes not indicate that the passed object is not in normalized form already, just that it can be be handled by thenormalizedmethod. This further means that thenormalizedmethod 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,normalizedcan (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 thenormalizedOrSamemethod) 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 onlyStrings (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
Uniformitywas 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
Uniformitythat combines this and the passedUniformity.Returns a new
Uniformitythat combines this and the passedUniformity.The
normalizedandnormalizedOrSamemethods of theUniformityreturned 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 composednormalizedmethod is:uniformityPassedToAnd.normalized(uniformityOnWhichAndWasInvoked.normalized(a))
And the body of the composed
normalizedOrSamemethod is:uniformityPassedToAnd.normalizedOrSame(uniformityOnWhichAndWasInvoked.normalizedOrSame(a))
The
normalizeCanHandlemethod of theUniformityreturned 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 composednormalizeCanHandlemethod is:uniformityOnWhichAndWasInvoked.normalizeCanHandle(a) && uniformityPassedToAnd.normalizeCanHandle(a)
- other
a
Uniformityto 'and' with this one- returns
a
Uniformityrepresenting the composition of this and the passedUniformity
- final def and(other: Normalization[A]): Normalization[A]
Returns a new
Normalizationthat composes this and the passedNormalization.Returns a new
Normalizationthat composes this and the passedNormalization.The
normalizedmethod of theNormalizationreturned by this method returns a normalized form of the passed object obtained by forwarding the passed value first to thisNormalization'snormalizedmethod, then passing that result to the otherNormalization'snormalizedmethod. Essentially, the body of the composednormalizedmethod is:normalizationPassedToAnd.normalized(normalizationOnWhichAndWasInvoked.normalized(a))
- other
a
Normalizationto 'and' with this one- returns
a
Normalizationrepresenting the composition of this and the passedNormalization
- Definition Classes
- Normalization
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- final def getClass(): Class[_ <: AnyRef]
- 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
Uniformityto aNormalizingEquality[A]whosenormalized,normalizedCanHandle, andnormalizedOrSamemethods delegate to thisUniformity[A]and whoseafterNormalizationEqualityfield returns the implicitly passedEquality[A].Converts this
Uniformityto aNormalizingEquality[A]whosenormalized,normalizedCanHandle, andnormalizedOrSamemethods delegate to thisUniformity[A]and whoseafterNormalizationEqualityfield returns the implicitly passedEquality[A].- equality
the
Equalitythat the returnedNormalizingEqualitywill delegate to determine equality after normalizing both left and right (if appropriate) sides.
- final def toEquivalence(implicit equivalence: Equivalence[A]): NormalizingEquivalence[A]
Converts this
Normalizationto aNormalizingEquivalence[A]whosenormalizedmethod delegates to thisNormalization[A]and whoseafterNormalizationEquivalencefield returns the implicitly passedEquivalence[A].Converts this
Normalizationto aNormalizingEquivalence[A]whosenormalizedmethod delegates to thisNormalization[A]and whoseafterNormalizationEquivalencefield returns the implicitly passedEquivalence[A].- equivalence
the
Equivalencethat the returnedNormalizingEquivalencewill 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(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()