NonPositiveInteger
Represents an Integer that is less than or equal to zero.
Motivations
Motivations
Completing the sign trichotomy
Once NonZeroInteger and NonNegativeInteger exist as standalone types, "non-positive" is the natural remaining case: together, the three types make the sign of an Integer fully and symmetrically expressible in the type system, instead of leaving "less than or equal to zero" as a runtime check that callers would otherwise perform inline on an Integer.
A closure-driven arithmetic surface
unaryMinus, plus, minus with a NonNegativeInteger operand, and both times overloads are defined because each always produces a value representable as a NonPositiveInteger or a NonNegativeInteger: negating a non-positive integer is always non-negative; adding two non-positive integers stays non-positive; subtracting a non-negative integer from a non-positive one stays non-positive (x - y == x + (-y), and -y <= 0); multiplying a non-positive integer by a non-negative one stays non-positive; and multiplying two non-positive integers becomes non-negative.
minus with a NonPositiveInteger, NonZeroInteger, or Integer operand is absent, because the set of non-positive integers isn't closed under any of them: for two non-positive integers x and y where x > y, x - y is positive (e.g. -1 - (-5) = 4), and the same unrestricted sign applies when subtracting a NonZeroInteger or an Integer. A times overload accepting a NonZeroInteger is absent for the same reason: a non-zero integer can be positive or negative, so the sign of the product is indeterminate. Use toInteger together with Integer.minus or Integer.times for these cases.
Division and remainder operations are absent, since they would only duplicate the semantics already covered by Integer.div and Integer.rem.
Key features
Key features
Creations: Create from a Long, an Integer, or a decimal string (see fromLong, fromInteger and parse).
Comparisons: Compare integers using structural equality (
x == y,x != y).Arithmetic operations: Negate (
-x), add (x + y), subtract (x - y), or multiply (x * y) non-positive integers, each producing another non-positive or non-negative integer depending on the operation. Some combinations are intentionally absent, because the set of non-positive integers isn't closed under them (e.g.,-1 - (-5) = 4).Conversions: Convert to its underlying Integer (see toInteger), or to its decimal string representation (see NonPositiveInteger.toString).
Since
5.2.0
Types
Contains class-level declarations for the NonPositiveInteger type.
Functions
Subtracts the other integer from this one.
Adds the other integer to this one.
Multiplies this integer by the other one.
Returns the decimal string representation of this integer, delegating to Integer.toString.
Returns the negative of this integer.