All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.scalautils.Tolerance.scala Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2001-2013 Artima, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.scalautils

import TripleEqualsSupport.Spread

/**
 * Trait containing an implicit conversion that adds a +- method to Numeric types, which enables
 * spreads to be expressed in terms of a pivot and tolerance.
 *
 * 

* For example, the TripleEquals trait (and its type-checking siblings TypeCheckedTripleEquals and * ConversionCheckedTripleEquals) enable you to write: *

* *
 * a === (1.0 +- 0.1)
 * 
* * @author Bill Venners */ trait Tolerance { /** * Wrapper class with a +- method that, given a Numeric argument, returns a Spread. * * @param tolerance the tolerance with which to create (and return) a Spread * * @author Bill Venners */ final class PlusOrMinusWrapper[T: Numeric](pivot: T) { /** * Creates and returns a Spread from the pivot passed to the constructor and * the tolerance passed to this method. * * @param tolerance the tolerance with which to create (and return) the Spread */ def +-(tolerance: T): Spread[T] = { val numeric = implicitly[Numeric[T]] if (numeric.lteq(tolerance, numeric.zero)) throw new IllegalArgumentException(tolerance.toString + " passed to +- was zero or negative. Must be a positive non-zero number.") // throw newTestFailedException(Resources("negativeOrZeroRange", tolerance.toString)) Spread(pivot, tolerance) } /** * The plusOrMinus method has been deprecated and will be removed in a future version of ScalaUtils and ScalaTest. Please use +- instead. */ @deprecated("The plusOrMinus method has been deprecated and will be removed in a future version of ScalaTest. Please use +- instead.") def plusOrMinus(tolerance: T): Spread[T] = { val numeric = implicitly[Numeric[T]] if (numeric.lteq(tolerance, numeric.zero)) throw new IllegalArgumentException(tolerance.toString + " passed to +- was zero or negative. Must be a positive non-zero number.") // throw newTestFailedException(Resources("negativeOrZeroRange", tolerance.toString)) Spread(pivot, tolerance) } } import scala.language.implicitConversions /** * Implicitly converts an object of a Numeric type to a PlusOrMinusWrapper, * to enable a +- method to be invoked on that object. */ implicit def convertNumericToPlusOrMinusWrapper[T : Numeric](pivot: T): PlusOrMinusWrapper[T] = new PlusOrMinusWrapper(pivot) } /** * Companion object to trait Tolerance that facilitates the importing of Tolerance members as * an alternative to mixing it in. One use case is to import Tolerance members so you can use * them in the Scala interpreter: * *
 * $ scala -classpath scalautils.jar
 * Welcome to Scala version 2.10.0
 * Type in expressions to have them evaluated.
 * Type :help for more information.
 *
 * scala> import org.scalautils._
 * import org.scalautils._
 *
 * scala> import Tolerance._
 * import Tolerance._
 *
 * scala> 1.0 +- 0.1
 * res0: org.scalautils.TripleEqualsSupport.Spread[Double] = Spread(1.0,0.1)
 * 
*/ object Tolerance extends Tolerance