org.scalactic.anyvals.PosZLong.scala Maven / Gradle / Ivy
/* * Copyright 2001-2014 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.scalactic.anyvals import scala.collection.immutable.NumericRange /** * AnDouble in * Scala can lose precision.) This makes it convenient to use a *AnyValfor non-negativeLongs. * ** Because
* *PosZLongis anAnyValit will usually be * as efficient as anLong, being boxed only when an *Longwould have been boxed. ** The
* *PosZLong.applyfactory method is implemented in terms of a * macro that checks literals for validity at compile time. Calling *PosZLong.applywith a literalLongvalue will * either produce a validPosZLonginstance at run time or an * error at compile time. Here's an example: ** scala> import anyvals._ * import anyvals._ * * scala> PosZLong(1L) * res0: org.scalactic.anyvals.PosZLong = PosZLong(1) * * scala> PosZLong(0L) * res1: org.scalactic.anyvals.PosZLong = PosZLong(0) * * scala> PosZLong(-1L) * <console>:14: error: PosZLong.apply can only be invoked on a non-negative (i >= 0L) integer literal, like PosZLong(42L). * PosZLong(-1L) * ^ ** **
* *PosZLong.applycannot be used if the value being passed is a * variable (i.e., not a literal), because the macro cannot determine * the validity of variables at compile time (just literals). If you try to * pass a variable toPosZLong.apply, you'll get a compiler error * that suggests you use a different factor method,PosZLong.from, * instead: ** scala> val x = 1L * x: Long = 1 * * scala> PosZLong(x) * <console>:15: error: PosZLong.apply can only be invoked on an integer literal, like PosZLong(42L). Please use PosZLong.from instead. * PosZLong(x) * ^ ** ** The
* *PosZLong.fromfactory method will inspect the value at runtime and return anOption[PosZLong]. If * the value is valid,PosZLong.fromwill return aSome[PosZLong], else it will return aNone. * Here's an example: ** scala> PosZLong.from(x) * res4: Option[org.scalactic.anyvals.PosZLong] = Some(PosZLong(1)) * * scala> val y = -1L * y: Long = -1 * * scala> PosZLong.from(y) * res5: Option[org.scalactic.anyvals.PosZLong] = None ** ** The
* *PosZLong.applyfactory method is marked implicit, so that * you can pass literalLongs into methods that require *PosZLong, and get the same compile-time checking you get when * callingPosZLong.applyexplicitly. Here's an example: ** scala> def invert(pos: PosZLong): Long = Long.MaxValue - pos * invert: (pos: org.scalactic.anyvals.PosZLong)Long * * scala> invert(0L) * res6: Long = 9223372036854775807 * * scala> invert(Long.MaxValue) * res7: Long = 0 * * scala> invert(-1L) * <console>:15: error: PosZLong.apply can only be invoked on a non-negative (i >= 0L) integer literal, like PosZLong(42L). * invert(-1L) * ^ ** ** This example also demonstrates that the
PosZLong* companion object also defines implicit widening conversions * when either no loss of precision will occur or a similar * conversion is provided in Scala. (For example, the implicit * conversion fromLongtoPosZLongwhere aLongor wider type * is needed. An example is the subtraction in the body of the *invertmethod defined above,Long.MaxValue * - pos. AlthoughLong.MaxValueis a *Long, which has no-method that * takes aPosZLong(the type ofpos), * you can still subtractpos, because the *PosZLongwill be implicitly widened to *Long. * * * @param value TheLongvalue underlying thisPosZLong. */ final class PosZLong private (val value: Long) extends AnyVal { /** * A string representation of thisPosZLong. */ override def toString: String = s"PosZLong($value)" /** * Converts thisPosZLongto aByte. */ def toByte: Byte = value.toByte /** * Converts thisPosZLongto aShort. */ def toShort: Short = value.toShort /** * Converts thisPosZLongto aChar. */ def toChar: Char = value.toChar /** * Converts thisPosZLongto anInt. */ def toInt: Int = value.toInt /** * Converts thisPosZLongto aLong. */ def toLong: Long = value.toLong /** * Converts thisPosZLongto aFloat. */ def toFloat: Float = value.toFloat /** * Converts thisPosZLongto aDouble. */ def toDouble: Double = value.toDouble /** * Returns the bitwise negation of this value. * @example {{{ * ~5 == -6 * // in binary: ~00000101 == * // 11111010 * }}} */ def unary_~ : Long = ~value /** Returns this value, unmodified. */ def unary_+ : PosZLong = this /** Returns the negation of this value. */ def unary_- : Long = -value /** * Converts thisPosZLong's value to a string then concatenates the given string. */ def +(x: String): String = value + x /** * Returns this value bit-shifted left by the specified number of bits, * filling in the new right bits with zeroes. * @example {{{ 6 << 3 == 48 // in binary: 0110 << 3 == 0110000 }}} */ def <<(x: Int): Long = value << x /** * Returns this value bit-shifted left by the specified number of bits, * filling in the new right bits with zeroes. * @example {{{ 6 << 3 == 48 // in binary: 0110 << 3 == 0110000 }}} */ def <<(x: Long): Long = value << x /** * Returns this value bit-shifted right by the specified number of bits, * filling the new left bits with zeroes. * @example {{{ 21 >>> 3 == 2 // in binary: 010101 >>> 3 == 010 }}} * @example {{{ * -21 >>> 3 == 536870909 * // in binary: 11111111 11111111 11111111 11101011 >>> 3 == * // 00011111 11111111 11111111 11111101 * }}} */ def >>>(x: Int): Long = value >>> x /** * Returns this value bit-shifted right by the specified number of bits, * filling the new left bits with zeroes. * @example {{{ 21 >>> 3 == 2 // in binary: 010101 >>> 3 == 010 }}} * @example {{{ * -21 >>> 3 == 536870909 * // in binary: 11111111 11111111 11111111 11101011 >>> 3 == * // 00011111 11111111 11111111 11111101 * }}} */ def >>>(x: Long): Long = value >>> x /** * Returns this value bit-shifted left by the specified number of bits, * filling in the right bits with the same value as the left-most bit of this. * The effect of this is to retain the sign of the value. * @example {{{ * -21 >> 3 == -3 * // in binary: 11111111 11111111 11111111 11101011 >> 3 == * // 11111111 11111111 11111111 11111101 * }}} */ def >>(x: Int): Long = value >> x /** * Returns this value bit-shifted left by the specified number of bits, * filling in the right bits with the same value as the left-most bit of this. * The effect of this is to retain the sign of the value. * @example {{{ * -21 >> 3 == -3 * // in binary: 11111111 11111111 11111111 11101011 >> 3 == * // 11111111 11111111 11111111 11111101 * }}} */ def >>(x: Long): Long = value >> x /** Returns `true` if this value is less than x, `false` otherwise. */ def <(x: Byte): Boolean = value < x /** Returns `true` if this value is less than x, `false` otherwise. */ def <(x: Short): Boolean = value < x /** Returns `true` if this value is less than x, `false` otherwise. */ def <(x: Char): Boolean = value < x /** Returns `true` if this value is less than x, `false` otherwise. */ def <(x: Int): Boolean = value < x /** Returns `true` if this value is less than x, `false` otherwise. */ def <(x: Long): Boolean = value < x /** Returns `true` if this value is less than x, `false` otherwise. */ def <(x: Float): Boolean = value < x /** Returns `true` if this value is less than x, `false` otherwise. */ def <(x: Double): Boolean = value < x /** Returns `true` if this value is less than or equal to x, `false` otherwise. */ def <=(x: Byte): Boolean = value <= x /** Returns `true` if this value is less than or equal to x, `false` otherwise. */ def <=(x: Short): Boolean = value <= x /** Returns `true` if this value is less than or equal to x, `false` otherwise. */ def <=(x: Char): Boolean = value <= x /** Returns `true` if this value is less than or equal to x, `false` otherwise. */ def <=(x: Int): Boolean = value <= x /** Returns `true` if this value is less than or equal to x, `false` otherwise. */ def <=(x: Long): Boolean = value <= x /** Returns `true` if this value is less than or equal to x, `false` otherwise. */ def <=(x: Float): Boolean = value <= x /** Returns `true` if this value is less than or equal to x, `false` otherwise. */ def <=(x: Double): Boolean = value <= x /** Returns `true` if this value is greater than x, `false` otherwise. */ def >(x: Byte): Boolean = value > x /** Returns `true` if this value is greater than x, `false` otherwise. */ def >(x: Short): Boolean = value > x /** Returns `true` if this value is greater than x, `false` otherwise. */ def >(x: Char): Boolean = value > x /** Returns `true` if this value is greater than x, `false` otherwise. */ def >(x: Int): Boolean = value > x /** Returns `true` if this value is greater than x, `false` otherwise. */ def >(x: Long): Boolean = value > x /** Returns `true` if this value is greater than x, `false` otherwise. */ def >(x: Float): Boolean = value > x /** Returns `true` if this value is greater than x, `false` otherwise. */ def >(x: Double): Boolean = value > x /** Returns `true` if this value is greater than or equal to x, `false` otherwise. */ def >=(x: Byte): Boolean = value >= x /** Returns `true` if this value is greater than or equal to x, `false` otherwise. */ def >=(x: Short): Boolean = value >= x /** Returns `true` if this value is greater than or equal to x, `false` otherwise. */ def >=(x: Char): Boolean = value >= x /** Returns `true` if this value is greater than or equal to x, `false` otherwise. */ def >=(x: Int): Boolean = value >= x /** Returns `true` if this value is greater than or equal to x, `false` otherwise. */ def >=(x: Long): Boolean = value >= x /** Returns `true` if this value is greater than or equal to x, `false` otherwise. */ def >=(x: Float): Boolean = value >= x /** Returns `true` if this value is greater than or equal to x, `false` otherwise. */ def >=(x: Double): Boolean = value >= x /** * Returns the bitwise OR of this value and `x`. * @example {{{ * (0xf0 | 0xaa) == 0xfa * // in binary: 11110000 * // | 10101010 * // -------- * // 11111010 * }}} */ def |(x: Byte): Long = value | x /** * Returns the bitwise OR of this value and `x`. * @example {{{ * (0xf0 | 0xaa) == 0xfa * // in binary: 11110000 * // | 10101010 * // -------- * // 11111010 * }}} */ def |(x: Short): Long = value | x /** * Returns the bitwise OR of this value and `x`. * @example {{{ * (0xf0 | 0xaa) == 0xfa * // in binary: 11110000 * // | 10101010 * // -------- * // 11111010 * }}} */ def |(x: Char): Long = value | x /** * Returns the bitwise OR of this value and `x`. * @example {{{ * (0xf0 | 0xaa) == 0xfa * // in binary: 11110000 * // | 10101010 * // -------- * // 11111010 * }}} */ def |(x: Int): Long = value | x /** * Returns the bitwise OR of this value and `x`. * @example {{{ * (0xf0 | 0xaa) == 0xfa * // in binary: 11110000 * // | 10101010 * // -------- * // 11111010 * }}} */ def |(x: Long): Long = value | x /** * Returns the bitwise AND of this value and `x`. * @example {{{ * (0xf0 & 0xaa) == 0xa0 * // in binary: 11110000 * // & 10101010 * // -------- * // 10100000 * }}} */ def &(x: Byte): Long = value & x /** * Returns the bitwise AND of this value and `x`. * @example {{{ * (0xf0 & 0xaa) == 0xa0 * // in binary: 11110000 * // & 10101010 * // -------- * // 10100000 * }}} */ def &(x: Short): Long = value & x /** * Returns the bitwise AND of this value and `x`. * @example {{{ * (0xf0 & 0xaa) == 0xa0 * // in binary: 11110000 * // & 10101010 * // -------- * // 10100000 * }}} */ def &(x: Char): Long = value & x /** * Returns the bitwise AND of this value and `x`. * @example {{{ * (0xf0 & 0xaa) == 0xa0 * // in binary: 11110000 * // & 10101010 * // -------- * // 10100000 * }}} */ def &(x: Int): Long = value & x /** * Returns the bitwise AND of this value and `x`. * @example {{{ * (0xf0 & 0xaa) == 0xa0 * // in binary: 11110000 * // & 10101010 * // -------- * // 10100000 * }}} */ def &(x: Long): Long = value & x /** * Returns the bitwise XOR of this value and `x`. * @example {{{ * (0xf0 ^ 0xaa) == 0x5a * // in binary: 11110000 * // ^ 10101010 * // -------- * // 01011010 * }}} */ def ^(x: Byte): Long = value ^ x /** * Returns the bitwise XOR of this value and `x`. * @example {{{ * (0xf0 ^ 0xaa) == 0x5a * // in binary: 11110000 * // ^ 10101010 * // -------- * // 01011010 * }}} */ def ^(x: Short): Long = value ^ x /** * Returns the bitwise XOR of this value and `x`. * @example {{{ * (0xf0 ^ 0xaa) == 0x5a * // in binary: 11110000 * // ^ 10101010 * // -------- * // 01011010 * }}} */ def ^(x: Char): Long = value ^ x /** * Returns the bitwise XOR of this value and `x`. * @example {{{ * (0xf0 ^ 0xaa) == 0x5a * // in binary: 11110000 * // ^ 10101010 * // -------- * // 01011010 * }}} */ def ^(x: Int): Long = value ^ x /** * Returns the bitwise XOR of this value and `x`. * @example {{{ * (0xf0 ^ 0xaa) == 0x5a * // in binary: 11110000 * // ^ 10101010 * // -------- * // 01011010 * }}} */ def ^(x: Long): Long = value ^ x /** Returns the sum of this value and `x`. */ def +(x: Byte): Long = value + x /** Returns the sum of this value and `x`. */ def +(x: Short): Long = value + x /** Returns the sum of this value and `x`. */ def +(x: Char): Long = value + x /** Returns the sum of this value and `x`. */ def +(x: Int): Long = value + x /** Returns the sum of this value and `x`. */ def +(x: Long): Long = value + x /** Returns the sum of this value and `x`. */ def +(x: Float): Float = value + x /** Returns the sum of this value and `x`. */ def +(x: Double): Double = value + x /** Returns the difference of this value and `x`. */ def -(x: Byte): Long = value - x /** Returns the difference of this value and `x`. */ def -(x: Short): Long = value - x /** Returns the difference of this value and `x`. */ def -(x: Char): Long = value - x /** Returns the difference of this value and `x`. */ def -(x: Int): Long = value - x /** Returns the difference of this value and `x`. */ def -(x: Long): Long = value - x /** Returns the difference of this value and `x`. */ def -(x: Float): Float = value - x /** Returns the difference of this value and `x`. */ def -(x: Double): Double = value - x /** Returns the product of this value and `x`. */ def *(x: Byte): Long = value * x /** Returns the product of this value and `x`. */ def *(x: Short): Long = value * x /** Returns the product of this value and `x`. */ def *(x: Char): Long = value * x /** Returns the product of this value and `x`. */ def *(x: Int): Long = value * x /** Returns the product of this value and `x`. */ def *(x: Long): Long = value * x /** Returns the product of this value and `x`. */ def *(x: Float): Float = value * x /** Returns the product of this value and `x`. */ def *(x: Double): Double = value * x /** Returns the quotient of this value and `x`. */ def /(x: Byte): Long = value / x /** Returns the quotient of this value and `x`. */ def /(x: Short): Long = value / x /** Returns the quotient of this value and `x`. */ def /(x: Char): Long = value / x /** Returns the quotient of this value and `x`. */ def /(x: Int): Long = value / x /** Returns the quotient of this value and `x`. */ def /(x: Long): Long = value / x /** Returns the quotient of this value and `x`. */ def /(x: Float): Float = value / x /** Returns the quotient of this value and `x`. */ def /(x: Double): Double = value / x /** Returns the remainder of the division of this value by `x`. */ def %(x: Byte): Long = value % x /** Returns the remainder of the division of this value by `x`. */ def %(x: Short): Long = value % x /** Returns the remainder of the division of this value by `x`. */ def %(x: Char): Long = value % x /** Returns the remainder of the division of this value by `x`. */ def %(x: Int): Long = value % x /** Returns the remainder of the division of this value by `x`. */ def %(x: Long): Long = value % x /** Returns the remainder of the division of this value by `x`. */ def %(x: Float): Float = value % x /** Returns the remainder of the division of this value by `x`. */ def %(x: Double): Double = value % x // Stuff from RichLong: /** * Returns a string representation of thisPosLong's underlyingLong* as an unsigned integer in base 2. * ** The unsigned
* * @return the string representation of the unsignedlongvalue is thisPosLong's underlyingLongplus * 264 if the underlyingLongis negative; otherwise, it is * equal to the underlyingLong. This value is converted to a string of * ASCII digits in binary (base 2) with no extra leading *0s. If the unsigned magnitude is zero, it is * represented by a single zero character'0'* ('\u0030'); otherwise, the first character of * the representation of the unsigned magnitude will not be the * zero character. The characters'0'* ('\u0030') and'1'* ('\u0031') are used as binary digits. *long* value represented by thisPosLong's underlyingLongin binary (base 2). */ def toBinaryString: String = java.lang.Long.toBinaryString(value) /** * Returns a string representation of thisPosLong's underlyingLong* as an unsigned integer in base 16. * ** The unsigned
* *longvalue is thisPosLong's underlyingLongplus * 264 if the underlyingLongis negative; otherwise, it is * equal to the underlyingLong. This value is converted to a string of * ASCII digits in hexadecimal (base 16) with no extra * leading0s. If the unsigned magnitude is zero, it * is represented by a single zero character'0'* ('\u0030'); otherwise, the first character of * the representation of the unsigned magnitude will not be the * zero character. The following characters are used as * hexadecimal digits: *** *0123456789abcdef** These are the characters
* * @return the string representation of the unsigned'\u0030'through *'\u0039'and'\u0061'through *'\u0066'. If uppercase letters are desired, * thetoUpperCasemethod may be called * on the result. *long* value represented by thisPosLong's underlyingLongin hexadecimal * (base 16). */ def toHexString: String = java.lang.Long.toHexString(value) def toOctalString: String = java.lang.Long.toOctalString(value) // No point to call abs on a PosZLong. /** * Returnsthisifthis > thatorthatotherwise. */ def max(that: PosZLong): PosZLong = if (math.max(value, that.value) == value) this else that /** * Returnsthisifthis < thatorthatotherwise. */ def min(that: PosZLong): PosZLong = if (math.min(value, that.value) == value) this else that // adapted from RichInt: /** * Create aRangefrom thisPosZLongvalue * until the specifiedend(exclusive) with step value 1. * * @param end The final bound of the range to make. * @return A [[scala.collection.immutable.NumericRange.Exclusive[Long]]] from `this` up to but * not including `end`. */ def until(end: Long): NumericRange.Exclusive[Long] = value.until(end) /** * Create aRangefrom thisPosZLongvalue * until the specifiedend(exclusive) with the specifiedstepvalue. * * @param end The final bound of the range to make. * @param step The number to increase by for each step of the range. * @return A [[scala.collection.immutable.NumericRange.Exclusive[Long]]] from `this` up to but * not including `end`. */ def until(end: Long, step: Long): NumericRange.Exclusive[Long] = value.until(end, step) /** * Create an inclusiveRangefrom thisPosZLongvalue * to the specifiedendwith step value 1. * * @param end The final bound of the range to make. * @return A [[scala.collection.immutable.NumericRange.Inclusive[Long]]] from `'''this'''` up to * and including `end`. */ def to(end: Long): NumericRange.Inclusive[Long] = value.to(end) /** * Create an inclusiveRangefrom thisPosZLongvalue * to the specifiedendwith the specifiedstepvalue. * * @param end The final bound of the range to make. * @param step The number to increase by for each step of the range. * @return A [[scala.collection.immutable.NumericRange.Inclusive[Long]]] from `'''this'''` up to * and including `end`. */ def to(end: Long, step: Long): NumericRange.Inclusive[Long] = value.to(end, step) } /** * The companion object forPosZLongthat offers * factory methods that producePosZLongs, implicit * widening conversions fromPosZLongto other * numeric types, and maximum and minimum constant values for *PosZLong. */ object PosZLong { /** * The largest value representable as a non-negativeLong, * which isPosZLong(9223372036854775807). */ final val MaxValue: PosZLong = PosZLong.from(Long.MaxValue).get /** * The smallest value representable as a non-negativeLong, * which isPosZLong(0). */ final val MinValue: PosZLong = PosZLong.from(0L).get // Can't use the macro here /** * A factory method that produces anOption[PosZLong]given a *Longvalue. * ** This method will inspect the passed
* *Longvalue * and if it is a non-negativeLong, * i.e., a value greater than or equal to 0, it will * return aPosZLongrepresenting that value, * wrapped in aSome. Otherwise, the passed *Longvalue is negative, so this method * will returnNone. ** This factory method differs from the
* * @param value theapply* factory method in thatapplyis implemented * via a macro that inspectsLongliterals at * compile time, whereasfrominspects *Longvalues at run time. *Longto inspect, and if non-negative, return * wrapped in aSome[PosZLong]. * @return the specifiedLongvalue wrapped * in aSome[PosZLong], if it is positive, else *None. */ def from(value: Long): Option[PosZLong] = if (value >= 0L) Some(new PosZLong(value)) else None import language.experimental.macros import scala.language.implicitConversions /** * A factory method, implemented via a macro, that produces a *PosZLongif passed a validLong* literal, otherwise a compile time error. * ** The macro that implements this method will inspect the * specified
* *Longexpression at compile time. If * the expression is a non-negativeLongliteral, * i.e., with a value greater than or equal to 0, it will return * aPosZLongrepresenting that value. Otherwise, * the passedLongexpression is either a literal * that is negative, or is not a literal, so this method * will give a compiler error. ** This factory method differs from the
* * @param value thefrom* factory method in that this method is implemented via a * macro that inspectsLongliterals at compile * time, whereasfrominspectsLong* values at run time. *Longliteral expression to inspect at * compile time, and if non-negative, to return wrapped in a *PosZLongat run time. * @return the specified, validLongliteral value wrapped * in aPosZLong. (If the specified expression is not a valid *Longliteral, the invocation of this method will not * compile.) */ implicit def apply(value: Long): PosZLong = macro PosZLongMacro.apply /** * Implicit widening conversion fromPosZLongto *Long. * * @param pos thePosZLongto widen * @return theLongvalue underlying the specified *PosZLong. */ implicit def widenToLong(poz: PosZLong): Long = poz.value /** * Implicit widening conversion fromPosZLongto *Float. * * @param pos thePosZLongto widen * @return theLongvalue underlying the specified *PosZLong, widened toFloat. */ implicit def widenToFloat(poz: PosZLong): Float = poz.value /** * Implicit widening conversion fromPosZLongto *Double. * * @param pos thePosZLongto widen * @return theLongvalue underlying the specified *PosZLong, widened toDouble. */ implicit def widenToDouble(poz: PosZLong): Double = poz.value /** * Implicit widening conversion fromPosZLongto *PosZFloat. * * @param pos thePosZLongto widen * @return theLongvalue underlying the specified *PosZLong, widened toFloatand * wrapped in aPosZFloat. */ implicit def widenToPosZFloat(poz: PosZLong): PosZFloat = PosZFloat.from(poz.value).get /** * Implicit widening conversion fromPosZLongto *PosZDouble. * * @param pos thePosZLongto widen * @return theLongvalue underlying the specified *PosZLong, widened toDouble* and wrapped in aPosZDouble. */ implicit def widenToPosZDouble(poz: PosZLong): PosZDouble = PosZDouble.from(poz.value).get /** * Implicit Ordering instance. */ implicit val posZLongOrd: Ordering[PosZLong] = new Ordering[PosZLong] { def compare(x: PosZLong, y: PosZLong): Int = x.toLong.compare(y) } }