/
PosLong.scala
1165 lines (1100 loc) · 45.4 KB
/
PosLong.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 <code>AnyVal</code> for positive <code>Long</code>s.
*
* Note: a <code>PosLong</code> may not equal 0. If you want positive number or 0, use [[PosZLong]].
*
* <p>
* Because <code>PosLong</code> is an <code>AnyVal</code> it
* will usually be as efficient as an <code>Long</code>, being
* boxed only when an <code>Long</code> would have been boxed.
* </p>
*
* <p>
* The <code>PosLong.apply</code> factory method is implemented
* in terms of a macro that checks literals for validity at
* compile time. Calling <code>PosLong.apply</code> with a
* literal <code>Long</code> value will either produce a valid
* <code>PosLong</code> instance at run time or an error at
* compile time. Here's an example:
* </p>
*
* <pre class="stREPL">
* scala> import anyvals._
* import anyvals._
*
* scala> PosLong(42L)
* res0: org.scalactic.anyvals.PosLong = PosLong(42L)
*
* scala> PosLong(0L)
* <console>:14: error: PosLong.apply can only be invoked on a positive (i > 0L) integer literal, like PosLong(42L).
* PosLong(0L)
* ^
* </pre>
*
* <p>
* <code>PosLong.apply</code> cannot be used if the value being
* passed is a variable (<em>i.e.</em>, not a literal), because
* the macro cannot determine the validity of variables at
* compile time (just literals). If you try to pass a variable
* to <code>PosLong.apply</code>, you'll get a compiler error
* that suggests you use a different factor method,
* <code>PosLong.from</code>, instead:
* </p>
*
* <pre class="stREPL">
* scala> val x = 42LL
* x: Long = 42L
*
* scala> PosLong(x)
* <console>:15: error: PosLong.apply can only be invoked on an long literal, like PosLong(42L). Please use PosLong.from instead.
* PosLong(x)
* ^
* </pre>
*
* <p>
* The <code>PosLong.from</code> factory method will inspect the
* value at runtime and return an
* <code>Option[PosLong]</code>. If the value is valid,
* <code>PosLong.from</code> will return a
* <code>Some[PosLong]</code>, else it will return a
* <code>None</code>. Here's an example:
* </p>
*
* <pre class="stREPL">
* scala> PosLong.from(x)
* res3: Option[org.scalactic.anyvals.PosLong] = Some(PosLong(42L))
*
* scala> val y = 0LL
* y: Long = 0L
*
* scala> PosLong.from(y)
* res4: Option[org.scalactic.anyvals.PosLong] = None
* </pre>
*
* <p>
* The <code>PosLong.apply</code> factory method is marked
* implicit, so that you can pass literal <code>Long</code>s
* into methods that require <code>PosLong</code>, and get the
* same compile-time checking you get when calling
* <code>PosLong.apply</code> explicitly. Here's an example:
* </p>
*
* <pre class="stREPL">
* scala> def invert(pos: PosLong): Long = Long.MaxValue - pos
* invert: (pos: org.scalactic.anyvals.PosLong)Long
*
* scala> invert(1L)
* res5: Long = 9223372036854775806
*
* scala> invert(Long.MaxValue)
* res6: Long = 0
*
* scala> invert(0LL)
* <console>:15: error: PosLong.apply can only be invoked on a positive (i > 0L) integer literal, like PosLong(42LL).
* invert(0LL)
* ^
*
* </pre>
*
* <p>
* This example also demonstrates that the <code>PosLong</code>
* 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 from <code>Long</code> to </code>Double</code> in
* Scala can lose precision.) This makes it convenient to use a
* <code>PosLong</code> where a <code>Long</code> or wider type
* is needed. An example is the subtraction in the body of the
* <code>invert</code> method defined above, <code>Long.MaxValue
* - pos</code>. Although <code>Long.MaxValue</code> is a
* <code>Long</code>, which has no <code>-</code> method that
* takes a <code>PosLong</code> (the type of <code>pos</code>),
* you can still subtract <code>pos</code>, because the
* <code>PosLong</code> will be implicitly widened to
* <code>Long</code>.
* </p>
*
* @param value The <code>Long</code> value underlying this <code>PosLong</code>.
*/
final class PosLong private (val value: Long) extends AnyVal {
/**
* A string representation of this <code>PosLong</code>.
*/
override def toString: String = s"PosLong(${value}L)"
/**
* Converts this <code>PosLong</code> to a <code>Byte</code>.
*/
def toByte: Byte = value.toByte
/**
* Converts this <code>PosLong</code> to a <code>Short</code>.
*/
def toShort: Short = value.toShort
/**
* Converts this <code>PosLong</code> to a <code>Char</code>.
*/
def toChar: Char = value.toChar
/**
* Converts this <code>PosLong</code> to an <code>Int</code>.
*/
def toInt: Int = value.toInt
/**
* Converts this <code>PosLong</code> to a <code>Long</code>.
*/
def toLong: Long = value.toLong
/**
* Converts this <code>PosLong</code> to a <code>Float</code>.
*/
def toFloat: Float = value.toFloat
/**
* Converts this <code>PosLong</code> to a <code>Double</code>.
*/
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_+ : PosLong = this
/** Returns the negation of this value. */
def unary_- : NegLong = NegLong.ensuringValid(-value)
/**
* Converts this <code>PosLong</code>'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 this <code>PosLong</code>'s underlying <code>Long</code>
* as an unsigned integer in base 2.
*
* <p>
* The unsigned <code>long</code> value is this <code>PosLong</code>'s underlying <code>Long</code> plus
* 2<sup>64</sup> if the underlying <code>Long</code> is negative; otherwise, it is
* equal to the underlying <code>Long</code>. This value is converted to a string of
* ASCII digits in binary (base 2) with no extra leading
* <code>0</code>s. If the unsigned magnitude is zero, it is
* represented by a single zero character <code>'0'</code>
* (<code>'\u0030'</code>); otherwise, the first character of
* the representation of the unsigned magnitude will not be the
* zero character. The characters <code>'0'</code>
* (<code>'\u0030'</code>) and <code>'1'</code>
* (<code>'\u0031'</code>) are used as binary digits.
* </p>
*
* @return the string representation of the unsigned <code>long</code>
* value represented by this <code>PosLong</code>'s underlying <code>Long</code> in binary (base 2).
*/
def toBinaryString: String = java.lang.Long.toBinaryString(value)
/**
* Returns a string representation of this <code>PosLong</code>'s underlying <code>Long</code>
* as an unsigned integer in base 16.
*
* <p>
* The unsigned <code>long</code> value is this <code>PosLong</code>'s underlying <code>Long</code> plus
* 2<sup>64</sup> if the underlying <code>Long</code> is negative; otherwise, it is
* equal to the underlying <code>Long</code>. This value is converted to a string of
* ASCII digits in hexadecimal (base 16) with no extra
* leading <code>0</code>s. If the unsigned magnitude is zero, it
* is represented by a single zero character <code>'0'</code>
* (<code>'\u0030'</code>); 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:
* </p>
*
* <blockquote>
* <code>0123456789abcdef</code>
* </blockquote>
*
* <p>
* These are the characters <code>'\u0030'</code> through
* <code>'\u0039'</code> and <code>'\u0061'</code> through
* <code>'\u0066'</code>. If uppercase letters are desired,
* the <code>toUpperCase</code> method may be called
* on the result.
* </p>
*
* @return the string representation of the unsigned <code>long</code>
* value represented by this <code>PosLong</code>'s underlying <code>Long</code> in hexadecimal
* (base 16).
*/
def toHexString: String = java.lang.Long.toHexString(value)
/**
* Returns a string representation of this <code>PosLong</code>'s underlying <code>Long</code>
* as an unsigned integer in base 8.
*
* <p>
* The unsigned <code>long</code> value is this <code>PosLong</code>'s underlying <code>Long</code> plus
* 2<sup>64</sup> if the underlying <code>Long</code> is negative; otherwise, it is
* equal to the underlying <code>Long</code>. This value is converted to a string of
* ASCII digits in octal (base 8) with no extra leading
* <code>0</code>s.
* </p>
*
* <p>
* If the unsigned magnitude is zero, it is represented by a
* single zero character <code>'0'</code>
* (<code>'\u0030'</code>); 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:
* </p>
*
* <blockquote>
* <code>01234567</code>
* </blockquote>
*
* <p>
* These are the characters <code>'\u0030'</code> through
* <code>'\u0037'</code>.
* </p>
*
* @return the string representation of the unsigned <code>long</code>
* value represented by this <code>PosLong</code>'s underlying <code>Long</code> in octal (base 8).
*/
def toOctalString: String = java.lang.Long.toOctalString(value)
/**
* Returns <code>this</code> if <code>this > that</code> or <code>that</code> otherwise.
*/
def max(that: PosLong): PosLong = if (math.max(value, that.value) == value) this else that
/**
* Returns <code>this</code> if <code>this < that</code> or <code>that</code> otherwise.
*/
def min(that: PosLong): PosLong = if (math.min(value, that.value) == value) this else that
// adapted from RichInt:
/**
* Create a <code>Range</code> from this <code>PosLong</code> value
* until the specified <code>end</code> (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 a <code>Range</code> from this <code>PosLong</code> value
* until the specified <code>end</code> (exclusive) with the specified <code>step</code> 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 inclusive <code>Range</code> from this <code>PosLong</code> value
* to the specified <code>end</code> 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 inclusive <code>Range</code> from this <code>PosLong</code> value
* to the specified <code>end</code> with the specified <code>step</code> 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 passed <code>Long => Long</code> function to the underlying <code>Long</code>
* value, and if the result is positive, returns the result wrapped in a <code>PosLong</code>,
* else throws <code>AssertionError</code>.
*
* <p>
* This method will inspect the result of applying the given function to this
* <code>PosLong</code>'s underlying <code>Long</code> value and if the result
* is positive, it will return a <code>PosLong</code> representing that value.
* Otherwise, the <code>Long</code> value returned by the given function is
* not positive, this method will throw <code>AssertionError</code>.
* </p>
*
* <p>
* This method differs from a vanilla <code>assert</code> or <code>ensuring</code>
* call in that you get something you didn't already have if the assertion
* succeeds: a <em>type</em> that promises an <code>Long</code> is positive.
* With this method, you are asserting that you are convinced the result of
* the computation represented by applying the given function to this <code>PosLong</code>'s
* value will not overflow. Instead of overflowing silently like <code>Long</code>, this
* method will signal an overflow with a loud <code>AssertionError</code>.
* </p>
*
* @param f the <code>Long => Long</code> function to apply to this <code>PosLong</code>'s
* underlying <code>Long</code> value.
* @return the result of applying this <code>PosLong</code>'s underlying <code>Long</code> value to
* to the passed function, wrapped in a <code>PosLong</code> if it is positive (else throws <code>AssertionError</code>).
* @throws AssertionError if the result of applying this <code>PosLong</code>'s underlying <code>Long</code> value to
* to the passed function is not positive.
*/
def ensuringValid(f: Long => Long): PosLong = {
val candidateResult: Long = f(value)
if (PosLongMacro.isValid(candidateResult)) new PosLong(candidateResult)
else throw new AssertionError(s"${candidateResult.toString()}, the result of applying the passed function to ${value.toString()}, was not a valid PosLong")
}
}
/**
* The companion object for <code>PosLong</code> that offers
* factory methods that produce <code>PosLong</code>s, implicit
* widening conversions from <code>PosLong</code> to other
* numeric types, and maximum and minimum constant values for
* <code>PosLong</code>.
*/
object PosLong {
/**
* The largest value representable as a positive
* <code>Long</code>, which is <code>PosLong(9223372036854775807)</code>.
*/
final val MaxValue: PosLong = PosLong.ensuringValid(Long.MaxValue)
/**
* The smallest value representable as a positive
* <code>Long</code>, which is <code>PosLong(1L)</code>.
*/
final val MinValue: PosLong = PosLong.ensuringValid(1L) // Can't use the macro here
/**
* A factory method that produces an <code>Option[PosLong]</code> given a
* <code>Long</code> value.
*
* <p>
* This method will inspect the passed <code>Long</code> value and if
* it is a positive <code>Long</code>, it will return a <code>PosLong</code> representing that value,
* wrapped in a <code>Some</code>. Otherwise, the passed <code>Long</code>
* value is not positive, so this method will return <code>None</code>.
* </p>
*
* <p>
* This factory method differs from the <code>apply</code>
* factory method in that <code>apply</code> is implemented
* via a macro that inspects <code>Long</code> literals at
* compile time, whereas <code>from</code> inspects
* <code>Long</code> values at run time.
* </p>
*
* @param value the <code>Long</code> to inspect, and if positive, return
* wrapped in a <code>Some[PosLong]</code>.
* @return the specified <code>Long</code> value wrapped in a
* <code>Some[PosLong]</code>, if it is positive, else
* <code>None</code>.
*/
def from(value: Long): Option[PosLong] =
if (PosLongMacro.isValid(value)) Some(new PosLong(value)) else None
/**
* A factory/assertion method that produces an <code>PosLong</code> given a
* valid <code>Long</code> value, or throws <code>AssertionError</code>,
* if given an invalid <code>Long</code> 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`.
*
* <p>
* This method will inspect the passed <code>Long</code> value and if
* it is a positive <code>Long</code>, it will return a <code>PosLong</code> representing that value.
* Otherwise, the passed <code>Long</code> value is not positive, so
* this method will throw <code>AssertionError</code>.
* </p>
*
* <p>
* This factory method differs from the <code>apply</code>
* factory method in that <code>apply</code> is implemented
* via a macro that inspects <code>Long</code> literals at
* compile time, whereas <code>from</code> inspects
* <code>Long</code> values at run time.
* It differs from a vanilla <code>assert</code> or <code>ensuring</code>
* call in that you get something you didn't already have if the assertion
* succeeds: a <em>type</em> that promises a <code>Long</code> is positive.
* </p>
*
* @param value the <code>Long</code> to inspect, and if positive, return
* wrapped in a <code>PosLong</code>.
* @return the specified <code>Long</code> value wrapped in a
* <code>PosLong</code>, if it is positive, else
* throws <code>AssertionError</code>.
* @throws AssertionError if the passed value is not positive
*/
def ensuringValid(value: Long): PosLong =
if (PosLongMacro.isValid(value)) new PosLong(value) else {
throw new AssertionError(s"${value.toString()} was not a valid PosLong")
}
/**
* A factory/validation method that produces a <code>PosLong</code>, wrapped
* in a <code>Success</code>, given a valid <code>Long</code> value, or if the
* given <code>Long</code> is invalid, an <code>AssertionError</code>, wrapped
* in a <code>Failure</code>.
*
* <p>
* This method will inspect the passed <code>Long</code> value and if
* it is a positive <code>Long</code>, it will return a <code>PosLong</code>
* representing that value, wrapped in a <code>Success</code>.
* Otherwise, the passed <code>Long</code> value is not positive, so this
* method will return an <code>AssertionError</code>, wrapped in a <code>Failure</code>.
* </p>
*
* <p>
* This factory method differs from the <code>apply</code> factory method
* in that <code>apply</code> is implemented via a macro that inspects
* <code>Long</code> literals at compile time, whereas this method inspects
* <code>Long</code> values at run time.
* </p>
*
* @param value the <code>Long</code> to inspect, and if positive, return
* wrapped in a <code>Success(PosLong)</code>.
* @return the specified <code>Long</code> value wrapped
* in a <code>Success(PosLong)</code>, if it is positive, else a <code>Failure(AssertionError)</code>.
*/
def tryingValid(value: Long): Try[PosLong] =
if (PosLongMacro.isValid(value))
Success(new PosLong(value))
else
Failure(new AssertionError(s"${value.toString()} was not a valid PosLong"))
/**
* A validation method that produces a <code>Pass</code>
* given a valid <code>Long</code> value, or
* an error value of type <code>E</code> produced by passing the
* given <em>invalid</em> <code>Int</code> value
* to the given function <code>f</code>, wrapped in a <code>Fail</code>.
*
* <p>
* This method will inspect the passed <code>Long</code> value and if
* it is a positive <code>Long</code>, it will return a <code>Pass</code>.
* Otherwise, the passed <code>Long</code> value is positive, so this
* method will return a result of type <code>E</code> obtained by passing
* the invalid <code>Long</code> value to the given function <code>f</code>,
* wrapped in a `Fail`.
* </p>
*
* <p>
* This factory method differs from the <code>apply</code> factory method
* in that <code>apply</code> is implemented via a macro that inspects
* <code>Long</code> literals at compile time, whereas this method inspects
* <code>Long</code> values at run time.
* </p>
*
* @param value the `Long` to validate that it is positive.
* @return a `Pass` if the specified `Long` value is 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 (PosLongMacro.isValid(value)) Pass else Fail(f(value))
/**
* A factory/validation method that produces a <code>PosLong</code>, wrapped
* in a <code>Good</code>, given a valid <code>Long</code> value, or if the
* given <code>Long</code> is invalid, an error value of type <code>B</code>
* produced by passing the given <em>invalid</em> <code>Long</code> value
* to the given function <code>f</code>, wrapped in a <code>Bad</code>.
*
* <p>
* This method will inspect the passed <code>Long</code> value and if
* it is a positive <code>Long</code>, it will return a <code>PosLong</code>
* representing that value, wrapped in a <code>Good</code>.
* Otherwise, the passed <code>Long</code> value is not positive, so this
* method will return a result of type <code>B</code> obtained by passing
* the invalid <code>Long</code> value to the given function <code>f</code>,
* wrapped in a `Bad`.
* </p>
*
* <p>
* This factory method differs from the <code>apply</code> factory method
* in that <code>apply</code> is implemented via a macro that inspects
* <code>Long</code> literals at compile time, whereas this method inspects
* <code>Long</code> values at run time.
* </p>
*
* @param value the <code>Long</code> to inspect, and if positive, return
* wrapped in a <code>Good(PosLong)</code>.
* @return the specified <code>Long</code> value wrapped
* in a <code>Good(PosLong)</code>, if it is positive, else a <code>Bad(f(value))</code>.
*/
def goodOrElse[B](value: Long)(f: Long => B): PosLong Or B =
if (PosLongMacro.isValid(value)) Good(PosLong.ensuringValid(value)) else Bad(f(value))
/**
* A factory/validation method that produces a <code>PosLong</code>, wrapped
* in a <code>Right</code>, given a valid <code>Int</code> value, or if the
* given <code>Int</code> is invalid, an error value of type <code>L</code>
* produced by passing the given <em>invalid</em> <code>Int</code> value
* to the given function <code>f</code>, wrapped in a <code>Left</code>.
*
* <p>
* This method will inspect the passed <code>Int</code> value and if
* it is a positive <code>Int</code>, it will return a <code>PosLong</code>
* representing that value, wrapped in a <code>Right</code>.
* Otherwise, the passed <code>Int</code> value is not positive, so this
* method will return a result of type <code>L</code> obtained by passing
* the invalid <code>Int</code> value to the given function <code>f</code>,
* wrapped in a `Left`.
* </p>
*
* <p>
* This factory method differs from the <code>apply</code> factory method
* in that <code>apply</code> is implemented via a macro that inspects
* <code>Int</code> literals at compile time, whereas this method inspects
* <code>Int</code> values at run time.
* </p>
*
* @param value the <code>Int</code> to inspect, and if positive, return
* wrapped in a <code>Right(PosLong)</code>.
* @return the specified <code>Int</code> value wrapped
* in a <code>Right(PosLong)</code>, if it is positive, else a <code>Left(f(value))</code>.
*/
def rightOrElse[L](value: Long)(f: Long => L): Either[L, PosLong] =
if (PosLongMacro.isValid(value)) Right(PosLong.ensuringValid(value)) else Left(f(value))
/**
* A predicate method that returns true if a given
* <code>Long</code> value is positive.
*
* @param value the <code>Long</code> to inspect, and if positive, return true.
* @return true if the specified <code>Long</code> is positive, else false.
*/
def isValid(value: Long): Boolean = PosLongMacro.isValid(value)
/**
* A factory method that produces a <code>PosLong</code> given a
* <code>Long</code> value and a default <code>PosLong</code>.
*
* <p>
* This method will inspect the passed <code>Long</code> value and if
* it is a positive <code>Long</code>, it will return a <code>PosLong</code> representing that value.
* Otherwise, the passed <code>Long</code> value is not positive, so this
* method will return the passed <code>default</code> value.
* </p>
*
* <p>
* This factory method differs from the <code>apply</code>
* factory method in that <code>apply</code> is implemented
* via a macro that inspects <code>Long</code> literals at
* compile time, whereas <code>from</code> inspects
* <code>Long</code> values at run time.
* </p>
*
* @param value the <code>Long</code> to inspect, and if positive, return.
* @param default the <code>PosLong</code> to return if the passed
* <code>Long</code> value is not positive.
* @return the specified <code>Long</code> value wrapped in a