org.scalactic.anyvals.NegZLong.scala Maven / Gradle / Ivy
/* * Copyright 2001-2016 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 import scala.language.implicitConversions import scala.util.{Try, Success, Failure} import org.scalactic.{Validation, Pass, Fail} import org.scalactic.{Or, Good, Bad} /** * An
Double in * Scala can lose precision.) This makes it convenient to use a *AnyVal
for non-positiveLong
s. * * * ** Because
* *NegZLong
is anAnyVal
it * will usually be as efficient as anLong
, being * boxed only when anLong
would have been boxed. ** The
* *NegZLong.apply
factory method is implemented * in terms of a macro that checks literals for validity at * compile time. CallingNegZLong.apply
with a * literalLong
value will either produce a valid *NegZLong
instance at run time or an error at * compile time. Here's an example: ** scala> import anyvals._ * import anyvals._ * * scala> NegZLong(-42L) * res0: org.scalactic.anyvals.NegZLong = NegZLong(-42L) * * scala> NegZLong(-1L) * <console>:14: error: NegZLong.apply can only be invoked on a non-positive (i <= 0L) integer literal, like NegZLong(-42L). * NegZLong(-1L) * ^ ** **
* *NegZLong.apply
cannot 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 * toNegZLong.apply
, you'll get a compiler error * that suggests you use a different factor method, *NegZLong.from
, instead: ** scala> val x = -42L * x: Long = -42 * * scala> NegZLong(x) * <console>:15: error: NegZLong.apply can only be invoked on an long literal, like NegZLong(-42L). Please use NegZLong.from instead. * NegZLong(x) * ^ ** ** The
* *NegZLong.from
factory method will inspect the * value at runtime and return an *Option[NegZLong]
. If the value is valid, *NegZLong.from
will return a *Some[NegZLong]
, else it will return a *None
. Here's an example: ** scala> NegZLong.from(x) * res3: Option[org.scalactic.anyvals.NegZLong] = Some(NegZLong(-42)) * * scala> val y = 1L * y: Long = 1 * * scala> NegZLong.from(y) * res4: Option[org.scalactic.anyvals.NegZLong] = None ** ** The
* *NegZLong.apply
factory method is marked * implicit, so that you can pass literalLong
s * into methods that requireNegZLong
, and get the * same compile-time checking you get when calling *NegZLong.apply
explicitly. Here's an example: ** scala> def invert(pos: NegZLong): Long = Long.MaxValue - pos * invert: (pos: org.scalactic.anyvals.NegZLong)Long * * scala> invert(1L) * res5: Long = 9223372036854775806 * * scala> invert(Long.MaxValue) * res6: Long = 0 * * scala> invert(1L) * <console>:15: error: NegZLong.apply can only be invoked on a non-positive (i <= 0L) integer literal, like NegZLong(-42L). * invert(1L) * ^ * ** ** This example also demonstrates that the
NegZLong
* 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 fromLong
toNegZLong
where aLong
or wider type * is needed. An example is the subtraction in the body of the *invert
method defined above,Long.MaxValue * - pos
. AlthoughLong.MaxValue
is a *Long
, which has no-
method that * takes aNegZLong
(the type ofpos
), * you can still subtractpos
, because the *NegZLong
will be implicitly widened to *Long
. * * * @param value TheLong
value underlying thisNegZLong
. */ final class NegZLong private (val value: Long) extends AnyVal { /** * A string representation of thisNegZLong
. */ override def toString: String = s"NegZLong(${value}L)" /** * Converts thisNegZLong
to aByte
. */ def toByte: Byte = value.toByte /** * Converts thisNegZLong
to aShort
. */ def toShort: Short = value.toShort /** * Converts thisNegZLong
to aChar
. */ def toChar: Char = value.toChar /** * Converts thisNegZLong
to anInt
. */ def toInt: Int = value.toInt /** * Converts thisNegZLong
to aLong
. */ def toLong: Long = value.toLong /** * Converts thisNegZLong
to aFloat
. */ def toFloat: Float = value.toFloat /** * Converts thisNegZLong
to 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_+ : NegZLong = this /** Returns the negation of this value. */ def unary_- : Long = -value /** * Converts thisNegZLong
's value to a string then concatenates the given string. */ def +(x: String): String = s"${value.toString()}$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 thisNegZLong
's underlyingLong
* as an unsigned integer in base 2. * ** The unsigned
* * @return the string representation of the unsignedlong
value is thisNegZLong
's underlyingLong
plus * 264 if the underlyingLong
is 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 *0
s. 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 thisNegZLong
's underlyingLong
in binary (base 2). */ def toBinaryString: String = java.lang.Long.toBinaryString(value) /** * Returns a string representation of thisNegZLong
's underlyingLong
* as an unsigned integer in base 16. * ** The unsigned
* *long
value is thisNegZLong
's underlyingLong
plus * 264 if the underlyingLong
is 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 * leading0
s. 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, * thetoUpperCase
method may be called * on the result. *long
* value represented by thisNegZLong
's underlyingLong
in hexadecimal * (base 16). */ def toHexString: String = java.lang.Long.toHexString(value) /** * Returns a string representation of thisNegZLong
's underlyingLong
* as an unsigned integer in base 8. * ** The unsigned
* *long
value is thisNegZLong
's underlyingLong
plus * 264 if the underlyingLong
is negative; otherwise, it is * equal to the underlyingLong
. This value is converted to a string of * ASCII digits in octal (base 8) with no extra leading *0
s. ** 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 octal * digits: *** *01234567
** These are the characters
* * @return the string representation of the unsigned'\u0030'
through *'\u0037'
. *long
* value represented by thisNegZLong
's underlyingLong
in octal (base 8). */ def toOctalString: String = java.lang.Long.toOctalString(value) /** * Returnsthis
ifthis > that
orthat
otherwise. */ def max(that: NegZLong): NegZLong = if (math.max(value, that.value) == value) this else that /** * Returnsthis
ifthis < that
orthat
otherwise. */ def min(that: NegZLong): NegZLong = if (math.min(value, that.value) == value) this else that // adapted from RichInt: /** * Create aRange
from thisNegZLong
value * 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 aRange
from thisNegZLong
value * until the specifiedend
(exclusive) with the specifiedstep
value. * * @param end The final bound of the range to make. * @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 inclusiveRange
from thisNegZLong
value * to the specifiedend
with 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 inclusiveRange
from thisNegZLong
value * to the specifiedend
with the specifiedstep
value. * * @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) /** * Applies the passedLong => Long
function to the underlyingLong
* value, and if the result is positive, returns the result wrapped in aNegZLong
, * else throwsAssertionError
. * ** This method will inspect the result of applying the given function to this *
* *NegZLong
's underlyingLong
value and if the result * is non-positive, it will return aNegZLong
representing that value. * Otherwise, theLong
value returned by the given function is * not non-positive, this method will throwAssertionError
. ** This method differs from a vanilla
* * @param f theassert
orensuring
* call in that you get something you didn't already have if the assertion * succeeds: a type that promises anLong
is non-positive. * With this method, you are asserting that you are convinced the result of * the computation represented by applying the given function to thisNegZLong
's * value will not overflow. Instead of overflowing silently likeLong
, this * method will signal an overflow with a loudAssertionError
. *Long => Long
function to apply to thisNegZLong
's * underlyingLong
value. * @return the result of applying thisNegZLong
's underlyingLong
value to * to the passed function, wrapped in aNegZLong
if it is non-positive (else throwsAssertionError
). * @throws AssertionError if the result of applying thisNegZLong
's underlyingLong
value to * to the passed function is not positive. */ def ensuringValid(f: Long => Long): NegZLong = { val candidateResult: Long = f(value) if (NegZLongMacro.isValid(candidateResult)) new NegZLong(candidateResult) else throw new AssertionError(s"${candidateResult.toString()}, the result of applying the passed function to ${value.toString()}, was not a valid NegZLong") } } /** * The companion object forNegZLong
that offers * factory methods that produceNegZLong
s, implicit * widening conversions fromNegZLong
to other * numeric types, and maximum and minimum constant values for *NegZLong
. */ object NegZLong { /** * The largest value representable as a non-positive *Long
, which isNegZLong(0L)
. */ final val MaxValue: NegZLong = NegZLong.ensuringValid(0L) /** * The smallest value representable as a positive *Long
, which isNegZLong(-9223372036854775808)
. */ final val MinValue: NegZLong = NegZLong.ensuringValid(Long.MinValue) // Can't use the macro here /** * A factory method that produces anOption[NegZLong]
given a *Long
value. * ** This method will inspect the passed
* *Long
value and if * it is a non-positiveLong
, it will return aNegZLong
representing that value, * wrapped in aSome
. Otherwise, the passedLong
* value is not non-positive, so this method will returnNone
. ** This factory method differs from the
* * @param value theapply
* factory method in thatapply
is implemented * via a macro that inspectsLong
literals at * compile time, whereasfrom
inspects *Long
values at run time. *Long
to inspect, and if non-positive, return * wrapped in aSome[NegZLong]
. * @return the specifiedLong
value wrapped in a *Some[NegZLong]
, if it is non-positive, else *None
. */ def from(value: Long): Option[NegZLong] = if (NegZLongMacro.isValid(value)) Some(new NegZLong(value)) else None /** * A factory/assertion method that produces anNegZLong
given a * validLong
value, or throwsAssertionError
, * if given an invalidLong
value. * * Note: you should use this method only when you are convinced that it will * always succeed, i.e., never throw an exception. It is good practice to * add a comment near the invocation of this method indicating ''why'' you think * it will always succeed to document your reasoning. If you are not sure an * `ensuringValid` call will always succeed, you should use one of the other * factory or validation methods provided on this object instead: `isValid`, * `tryingValid`, `passOrElse`, `goodOrElse`, or `rightOrElse`. * ** This method will inspect the passed
* *Long
value and if * it is a non-positiveLong
, it will return aNegZLong
representing that value. * Otherwise, the passedLong
value is not non-positive, so * this method will throwAssertionError
. ** This factory method differs from the
* * @param value theapply
* factory method in thatapply
is implemented * via a macro that inspectsLong
literals at * compile time, whereasfrom
inspects *Long
values at run time. * It differs from a vanillaassert
orensuring
* call in that you get something you didn't already have if the assertion * succeeds: a type that promises aLong
is positive. *Long
to inspect, and if non-positive, return * wrapped in aNegZLong
. * @return the specifiedLong
value wrapped in a *NegZLong
, if it is non-positive, else * throwsAssertionError
. * @throws AssertionError if the passed value is not non-positive */ def ensuringValid(value: Long): NegZLong = if (NegZLongMacro.isValid(value)) new NegZLong(value) else { throw new AssertionError(s"${value.toString()} was not a valid NegZLong") } /** * A factory/validation method that produces aNegZLong
, wrapped * in aSuccess
, given a validLong
value, or if the * givenLong
is invalid, anAssertionError
, wrapped * in aFailure
. * ** This method will inspect the passed
* *Long
value and if * it is a non-positiveLong
, it will return aNegZLong
* representing that value, wrapped in aSuccess
. * Otherwise, the passedLong
value is not non-positive, so this * method will return anAssertionError
, wrapped in aFailure
. ** This factory method differs from the
* * @param value theapply
factory method * in thatapply
is implemented via a macro that inspects *Long
literals at compile time, whereas this method inspects *Long
values at run time. *Long
to inspect, and if non-positive, return * wrapped in aSuccess(NegZLong)
. * @return the specifiedLong
value wrapped * in aSuccess(NegZLong)
, if it is non-positive, else aFailure(AssertionError)
. */ def tryingValid(value: Long): Try[NegZLong] = if (NegZLongMacro.isValid(value)) Success(new NegZLong(value)) else Failure(new AssertionError(s"${value.toString()} was not a valid NegZLong")) /** * A validation method that produces aPass
* given a validLong
value, or * an error value of typeE
produced by passing the * given invalidInt
value * to the given functionf
, wrapped in aFail
. * ** This method will inspect the passed
* *Long
value and if * it is a non-positiveLong
, it will return aPass
. * Otherwise, the passedLong
value is non-positive, so this * method will return a result of typeE
obtained by passing * the invalidLong
value to the given functionf
, * wrapped in a `Fail`. ** This factory method differs from the
* * @param value the `Long` to validate that it is non-positive. * @return a `Pass` if the specified `Long` value is non-positive, * else a `Fail` containing an error value produced by passing the * specified `Long` to the given function `f`. */ def passOrElse[E](value: Long)(f: Long => E): Validation[E] = if (NegZLongMacro.isValid(value)) Pass else Fail(f(value)) /** * A factory/validation method that produces aapply
factory method * in thatapply
is implemented via a macro that inspects *Long
literals at compile time, whereas this method inspects *Long
values at run time. *NegZLong
, wrapped * in aGood
, given a validLong
value, or if the * givenLong
is invalid, an error value of typeB
* produced by passing the given invalidLong
value * to the given functionf
, wrapped in aBad
. * ** This method will inspect the passed
* *Long
value and if * it is a non-positiveLong
, it will return aNegZLong
* representing that value, wrapped in aGood
. * Otherwise, the passedLong
value is not non-positive, so this * method will return a result of typeB
obtained by passing * the invalidLong
value to the given functionf
, * wrapped in a `Bad`. ** This factory method differs from the
* * @param value theapply
factory method * in thatapply
is implemented via a macro that inspects *Long
literals at compile time, whereas this method inspects *Long
values at run time. *Long
to inspect, and if non-positive, return * wrapped in aGood(NegZLong)
. * @return the specifiedLong
value wrapped * in aGood(NegZLong)
, if it is non-positive, else aBad(f(value))
. */ def goodOrElse[B](value: Long)(f: Long => B): NegZLong Or B = if (NegZLongMacro.isValid(value)) Good(NegZLong.ensuringValid(value)) else Bad(f(value)) /** * A factory/validation method that produces aNegZLong
, wrapped * in aRight
, given a validInt
value, or if the * givenInt
is invalid, an error value of typeL
* produced by passing the given invalidInt
value * to the given functionf
, wrapped in aLeft
. * ** This method will inspect the passed
* *Int
value and if * it is a non-positiveInt
, it will return aNegZLong
* representing that value, wrapped in aRight
. * Otherwise, the passedInt
value is not non-positive, so this * method will return a result of typeL
obtained by passing * the invalidInt
value to the given functionf
, * wrapped in a `Left`. ** This factory method differs from the
* * @param value theapply
factory method * in thatapply
is implemented via a macro that inspects *Int
literals at compile time, whereas this method inspects *Int
values at run time. *Int
to inspect, and if non-positive, return * wrapped in aRight(NegZLong)
. * @return the specifiedInt
value wrapped * in aRight(NegZLong)
, if it is non-positive, else aLeft(f(value))
. */ def rightOrElse[L](value: Long)(f: Long => L): Either[L, NegZLong] = if (NegZLongMacro.isValid(value)) Right(NegZLong.ensuringValid(value)) else Left(f(value)) /** * A predicate method that returns true if a given *Long
value is non-positive. * * @param value theLong
to inspect, and if non-positive, return true. * @return true if the specifiedLong
is non-positive, else false. */ def isValid(value: Long): Boolean = NegZLongMacro.isValid(value) /** * A factory method that produces aNegZLong
given a *Long
value and a defaultNegZLong
. * ** This method will inspect the passed
* *Long
value and if * it is a non-positiveLong
, it will return aNegZLong
representing that value. * Otherwise, the passedLong
value is not non-positive, so this * method will return the passeddefault
value. ** This factory method differs from the
* * @param value theapply
* factory method in thatapply
is implemented * via a macro that inspectsLong
literals at * compile time, whereasfrom
inspects *Long
values at run time. *Long
to inspect, and if non-positive, return. * @param default theNegZLong
to return if the passed *Long
value is not non-positive. * @return the specifiedLong
value wrapped in a *NegZLong
, if it is non-positive, else the *default
NegZLong
value. */ def fromOrElse(value: Long, default: => NegZLong): NegZLong = if (NegZLongMacro.isValid(value)) new NegZLong(value) else default import language.experimental.macros /** * A factory method, implemented via a macro, that produces a *NegZLong
if passed a validLong
* literal, otherwise a compile time error. * ** The macro that implements this method will inspect the * specified
* *Long
expression at compile time. If * the expression is a non-positiveLong
literal, * it will return aNegZLong
representing that value. * Otherwise, the passedLong
expression is either a literal * that is not non-positive, 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 inspectsLong
literals at compile * time, whereasfrom
inspectsLong
* values at run time. *Long
literal expression to * inspect at compile time, and if non-positive, to return * wrapped in aNegZLong
at run time. * @return the specified, validLong
literal * value wrapped in aNegZLong
. (If the * specified expression is not a validLong
* literal, the invocation of this method will not * compile.) */ inline implicit def apply(value: => Long): NegZLong = ${ NegZLongMacro('{value}) } /** * Implicit widening conversion fromNegZLong
to *Long
. * * @param pos theNegZLong
to widen * @return theLong
value underlying the specified *NegZLong
. */ implicit def widenToLong(pos: NegZLong): Long = pos.value /** * Implicit widening conversion fromNegZLong
to *Float
. * * @param pos theNegZLong
to widen * @return theLong
value underlying the specified *NegZLong
, widened toFloat
. */ implicit def widenToFloat(pos: NegZLong): Float = pos.value /** * Implicit widening conversion fromNegZLong
to *Double
. * * @param pos theNegZLong
to widen * @return theLong
value underlying the specified *NegZLong
, widened toDouble
. */ implicit def widenToDouble(pos: NegZLong): Double = pos.value /** * Implicit widening conversion fromNegZLong
toNegZFloat
. * * @param pos theNegZLong
to widen * @return theLong
value underlying the specifiedNegZLong
, * widened toFloat
and wrapped in aNegZFloat
. */ implicit def widenToNegZFloat(pos: NegZLong): NegZFloat = NegZFloat.ensuringValid(pos.value) /** * Implicit widening conversion fromNegZLong
toNegZDouble
. * * @param pos theNegZLong
to widen * @return theLong
value underlying the specifiedNegZLong
, * widened toDouble
and wrapped in aNegZDouble
. */ implicit def widenToNegZDouble(pos: NegZLong): NegZDouble = NegZDouble.ensuringValid(pos.value) /** * Implicit Ordering instance. */ implicit val ordering: Ordering[NegZLong] = new Ordering[NegZLong] { def compare(x: NegZLong, y: NegZLong): Int = x.toLong.compare(y) } }