-
Notifications
You must be signed in to change notification settings - Fork 80
/
Version.java
1745 lines (1597 loc) · 63.5 KB
/
Version.java
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
/*
* The MIT License
*
* Copyright 2012-2024 Zafar Khaja <zafarkhaja@gmail.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.github.zafarkhaja.semver;
import com.github.zafarkhaja.semver.expr.Expression;
import com.github.zafarkhaja.semver.expr.ExpressionParser;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;
import java.util.Optional;
import java.util.function.Predicate;
import static com.github.zafarkhaja.semver.Version.Validators.*;
import static com.github.zafarkhaja.semver.VersionParser.parseBuild;
import static com.github.zafarkhaja.semver.VersionParser.parsePreRelease;
/**
* A representation of version as defined by the SemVer Specification.
* <p>
* The {@code Version} class is immutable and thread-safe.
*
* @author Zafar Khaja {@literal <zafarkhaja@gmail.com>}
* @since 0.1.0
*/
@SuppressWarnings("serial")
public class Version implements Comparable<Version>, Serializable {
/**
* A mutable builder for the immutable {@code Version} class
*/
public static class Builder {
private long major = 0;
private long minor = 0;
private long patch = 0;
private String[] preReleaseIds = {};
private String[] buildIds = {};
/**
* Default constructor, initializes fields with default values (0.0.0)
*/
public Builder() {}
/**
* Sets the major version; the minor and patch versions are assigned 0.
*
* @param major a major version number, non-negative
* @return this {@code Builder} instance
* @throws IllegalArgumentException if {@code major} is negative
* @since 0.10.0
*/
public Builder setVersionCore(long major) {
return setVersionCore(major, 0, 0);
}
/**
* Sets the major and minor versions; the patch version is assigned 0.
*
* @param major a major version number, non-negative
* @param minor a minor version number, non-negative
* @return this {@code Builder} instance
* @throws IllegalArgumentException if any of the arguments is negative
* @since 0.10.0
*/
public Builder setVersionCore(long major, long minor) {
return setVersionCore(major, minor, 0);
}
/**
* Sets major, minor and patch versions.
*
* @param major a major version number, non-negative
* @param minor a minor version number, non-negative
* @param patch a patch version number, non-negative
* @return this {@code Builder} instance
* @throws IllegalArgumentException if any of the arguments is negative
* @since 0.10.0
*/
public Builder setVersionCore(long major, long minor, long patch) {
return
setMajorVersion(major).
setMinorVersion(minor).
setPatchVersion(patch)
;
}
/**
* Sets the major version.
*
* @param major a major version number, non-negative
* @return this {@code Builder} instance
* @throws IllegalArgumentException if {@code major} is negative
* @since 0.10.0
*/
public Builder setMajorVersion(long major) {
this.major = nonNegative(major, "major");
return this;
}
/**
* Sets the minor version.
*
* @param minor a minor version number, non-negative
* @return this {@code Builder} instance
* @throws IllegalArgumentException if {@code minor} is negative
* @since 0.10.0
*/
public Builder setMinorVersion(long minor) {
this.minor = nonNegative(minor, "minor");
return this;
}
/**
* Sets the patch version.
*
* @param patch a patch version number, non-negative
* @return this {@code Builder} instance
* @throws IllegalArgumentException if {@code patch} is negative
* @since 0.10.0
*/
public Builder setPatchVersion(long patch) {
this.patch = nonNegative(patch, "patch");
return this;
}
/**
* Sets the pre-release version.
* <p>
* Multiple identifiers can be specified in a single argument joined
* with dots, or in separate arguments, or both.
*
* @param ids one or more pre-release identifiers, non-null
* @return this {@code Builder} instance
* @throws IllegalArgumentException if {@code ids} is null/empty or contains null
*/
public Builder setPreReleaseVersion(String... ids) {
preReleaseIds = oneOrMoreNonNulls(ids, "ids").clone();
return this;
}
/**
* Appends (additional) pre-release identifier(s).
* <p>
* If no pre-release identifiers have been previously set, the method
* works as {@link #setPreReleaseVersion(String...)}.
* <p>
* Multiple identifiers can be specified in a single argument joined
* with dots, or in separate arguments, or both.
*
* @param ids one or more pre-release identifiers, non-null
* @return this {@code Builder} instance
* @throws IllegalArgumentException if {@code ids} is null/empty or contains null
* @see #setPreReleaseVersion(String...)
* @since 0.10.0
*/
public Builder addPreReleaseIdentifiers(String... ids) {
if (preReleaseIds.length == 0) {
return setPreReleaseVersion(ids);
}
preReleaseIds = concatArrays(preReleaseIds, oneOrMoreNonNulls(ids, "ids"));
return this;
}
/**
* Unsets the pre-release version.
*
* @return this {@code Builder} instance
* @since 0.10.0
*/
public Builder unsetPreReleaseVersion() {
preReleaseIds = new String[0];
return this;
}
/**
* Sets the build metadata.
* <p>
* Multiple identifiers can be specified in a single argument joined
* with dots, or in separate arguments, or both.
*
* @param ids one or more build identifiers, non-null
* @return this {@code Builder} instance
* @throws IllegalArgumentException if {@code ids} is null/empty or contains null
*/
public Builder setBuildMetadata(String... ids) {
buildIds = oneOrMoreNonNulls(ids, "ids").clone();
return this;
}
/**
* Appends (additional) build identifier(s).
* <p>
* If no build identifiers have been previously set, the method works as
* {@link #setBuildMetadata(String...)}.
* <p>
* Multiple identifiers can be specified in a single argument joined
* with dots, or in separate arguments, or both.
*
* @param ids one or more build identifiers, non-null
* @return this {@code Builder} instance
* @throws IllegalArgumentException if {@code ids} is null/empty or contains null
* @see #setBuildMetadata(String...)
* @since 0.10.0
*/
public Builder addBuildIdentifiers(String... ids) {
if (buildIds.length == 0) {
return setBuildMetadata(ids);
}
buildIds = concatArrays(buildIds, oneOrMoreNonNulls(ids, "ids"));
return this;
}
/**
* Unsets the build metadata.
*
* @return this {@code Builder} instance
* @since 0.10.0
*/
public Builder unsetBuildMetadata() {
buildIds = new String[0];
return this;
}
/**
* Obtains a {@code Version} instance with previously set values.
*
* @return a {@code Version} instance
* @throws ParseException if any of the previously set identifiers can't be parsed
* @see Version#of(long, long, long, String, String)
*/
public Version build() {
return Version.of(
major,
minor,
patch,
joinIdentifiers(preReleaseIds),
joinIdentifiers(buildIds)
);
}
private static String[] concatArrays(String[] ids1, String[] ids2) {
String[] ids = new String[ids1.length + ids2.length];
System.arraycopy(ids1, 0, ids, 0, ids1.length);
System.arraycopy(ids2, 0, ids, ids1.length, ids2.length);
return ids;
}
/**
* @deprecated forRemoval since 0.10.0
*
* @param normal a string representing a normal version, non-null
* @throws IllegalArgumentException if (@code normal) is null
*/
@Deprecated
public Builder(String normal) {
setNormalVersion(normal);
}
/**
* @deprecated forRemoval since 0.10.0
*
* @param normal a string representing a normal version, non-null
* @return this {@code Builder} instance
* @throws IllegalArgumentException if (@code normal) is null
*/
@Deprecated
@SuppressWarnings("DeprecatedIsStillUsed")
public Builder setNormalVersion(String normal) {
String[] parts = nonNull(normal, "normal").split("\\" + IDENTIFIER_SEPARATOR);
return setVersionCore(
Long.parseLong(parts[0]),
parts.length > 1 ? Long.parseLong(parts[1]) : 0,
parts.length > 2 ? Long.parseLong(parts[2]) : 0
);
}
}
/**
* A comparator that sorts versions in increment order, from lowest to highest.
* <p>
* The comparator is intended for use in comparison-based data structures.
*
* @see #compareToIgnoreBuildMetadata(Version)
* @since 0.10.0
*/
public static final Comparator<Version> INCREMENT_ORDER = Version::compareToIgnoreBuildMetadata;
/**
* A comparator that sorts versions in (highest) precedence order.
* <p>
* The ordering imposed by this comparator is reverse of the "natural"
* increment ordering, that is, versions are arranged in descending order
* from highest-precedence to lowest-precedence.
* <p>
* The comparator is intended for use in comparison-based data structures.
*
* @see #INCREMENT_ORDER
* @since 0.10.0
*/
public static final Comparator<Version> PRECEDENCE_ORDER = INCREMENT_ORDER.reversed();
private final long major;
private final long minor;
private final long patch;
private final String[] preReleaseIds;
private final String[] buildIds;
private static final String IDENTIFIER_SEPARATOR = ".";
private static final String PRE_RELEASE_PREFIX = "-";
private static final String BUILD_PREFIX = "+";
/**
* @see #Version(long, long, long, String[], String[]) for documentation
*/
Version(long major, long minor, long patch) {
this(major, minor, patch, new String[0], new String[0]);
}
/**
* @see #Version(long, long, long, String[], String[]) for documentation
*/
Version(long major, long minor, long patch, String[] preReleaseIds) {
this(major, minor, patch, preReleaseIds, new String[0]);
}
/**
* Package-private constructor, for internal use only.
*
* @param major a major version number, non-negative
* @param minor a minor version number, non-negative
* @param patch a patch version number, non-negative
* @param preReleaseIds the pre-release identifiers, non-null
* @param buildIds the build identifiers, non-null
* @throws IllegalArgumentException if any of the numeric arguments is negative,
* or if any of the reference-type arguments is null
*/
Version(long major, long minor, long patch, String[] preReleaseIds, String[] buildIds) {
this.major = nonNegative(major, "major");
this.minor = nonNegative(minor, "minor");
this.patch = nonNegative(patch, "patch");
this.preReleaseIds = nonNull(preReleaseIds, "preReleaseIds").clone();
this.buildIds = nonNull(buildIds, "buildIds").clone();
}
/**
* Obtains a {@code Version} instance by parsing the specified string in
* strict mode, which ensures full compliance with the specification.
*
* @param version a string representing a SemVer version, non-null
* @return a {@code Version} instance
* @throws IllegalArgumentException if {@code version} is null
* @throws ParseException if {@code version} can't be parsed
* @see #parse(String, boolean)
* @since 0.10.0
*/
public static Version parse(String version) {
return parse(version, true);
}
/**
* Obtains a {@code Version} instance by parsing the specified string.
* <p>
* This method provides a way to parse the specified string in lenient mode,
* which accepts shorter version cores, such as "1" or "1.2".
*
* @param version a string representing a SemVer version, non-null
* @param strictly whether to parse the specified string in strict mode
* @return a {@code Version} instance
* @throws IllegalArgumentException if {@code version} is null
* @throws ParseException if {@code version} can't be parsed
* @see #parse(String)
* @since 0.10.0
*/
public static Version parse(String version, boolean strictly) {
return VersionParser.parseValidSemVer(nonNull(version, "version"), strictly);
}
/**
* Tries to obtain a {@code Version} instance by parsing the specified string
* in strict mode, which ensures full compliance with the specification.
*
* @param version a string representing a SemVer version, nullable
* @return an {@code Optional} with a {@code Version} instance, if the
* specified string can be parsed; empty {@code Optional} otherwise
* @see #tryParse(String, boolean)
* @since 0.10.0
*/
public static Optional<Version> tryParse(String version) {
return tryParse(version, true);
}
/**
* Tries to obtain a {@code Version} instance by parsing the specified string.
* <p>
* This method provides a way to parse the specified string in lenient mode,
* which accepts shorter version cores, such as "1" or "1.2".
*
* @param version a string representing a SemVer version, nullable
* @param strictly whether to parse the specified string in strict mode
* @return an {@code Optional} with a {@code Version} instance, if the
* specified string can be parsed; empty {@code Optional} otherwise
* @see #tryParse(String)
* @since 0.10.0
*/
public static Optional<Version> tryParse(String version, boolean strictly) {
try {
return Optional.of(Version.parse(version, strictly));
} catch (RuntimeException e) {
return Optional.empty();
}
}
/**
* Checks validity of the specified SemVer version string in strict mode,
* which ensures full compliance with the specification.
* <p>
* Note that internally this method makes use of {@link #parse(String)} and
* suppresses any exceptions, so using it to avoid dealing with exceptions
* like so:
*
* <pre>{@code
* String version = "1.2.3";
* if (Version.isValid(version)) {
* Version v = Version.parse(version);
* }
* }</pre>
*
* would mean parsing the same version string twice. In this case, as an
* alternative, consider using {@link #tryParse(String)}.
*
* @param version a string representing a SemVer version, nullable
* @return {@code true}, if the specified string is a valid SemVer version;
* {@code false} otherwise
* @see #isValid(String, boolean)
* @since 0.10.0
*/
public static boolean isValid(String version) {
return isValid(version, true);
}
/**
* Checks validity of the specified SemVer version string.
* <p>
* This method provides a way to parse the specified string in lenient mode,
* which accepts shorter version cores, such as "1" or "1.2".
*
* @param version a string representing a SemVer version, nullable
* @param strictly whether to parse the specified string in strict mode
* @return {@code true}, if the specified string is a valid SemVer version;
* {@code false} otherwise
* @see #isValid(String)
* @since 0.10.0
*/
public static boolean isValid(String version, boolean strictly) {
return tryParse(version, strictly).isPresent();
}
/**
* Obtains a {@code Version} instance of the specified major version.
*
* @param major a major version number, non-negative
* @return a {@code Version} instance
* @throws IllegalArgumentException if {@code major} is negative
* @since 0.10.0
*/
public static Version of(long major) {
return Version.of(major, 0, 0, null, null);
}
/**
* Obtains a {@code Version} instance of the specified major and pre-release
* versions.
*
* @param major a major version number, non-negative
* @param preRelease a pre-release version label, nullable
* @return a {@code Version} instance
* @throws IllegalArgumentException if {@code major} is negative
* @throws ParseException if {@code preRelease} can't be parsed
* @since 0.10.0
*/
public static Version of(long major, String preRelease) {
return Version.of(major, 0, 0, preRelease, null);
}
/**
* Obtains a {@code Version} instance of the specified major and pre-release
* versions, as well as build metadata.
*
* @param major a major version number, non-negative
* @param preRelease a pre-release version label, nullable
* @param build a build metadata label, nullable
* @return a {@code Version} instance
* @throws IllegalArgumentException if {@code major} is negative
* @throws ParseException if {@code preRelease} or {@code build} can't be parsed
* @since 0.10.0
*/
public static Version of(long major, String preRelease, String build) {
return Version.of(major, 0, 0, preRelease, build);
}
/**
* Obtains a {@code Version} instance of the specified major and minor versions.
*
* @param major a major version number, non-negative
* @param minor a minor version number, non-negative
* @return a {@code Version} instance
* @throws IllegalArgumentException if {@code major} or {@code minor} is negative
* @since 0.10.0
*/
public static Version of(long major, long minor) {
return Version.of(major, minor, 0, null, null);
}
/**
* Obtains a {@code Version} instance of the specified major, minor and
* pre-release versions.
*
* @param major a major version number, non-negative
* @param minor a minor version number, non-negative
* @param preRelease a pre-release version label, nullable
* @return a {@code Version} instance
* @throws IllegalArgumentException if {@code major} or {@code minor} is negative
* @throws ParseException if {@code preRelease} can't be parsed
* @since 0.10.0
*/
public static Version of(long major, long minor, String preRelease) {
return Version.of(major, minor, 0, preRelease, null);
}
/**
* Obtains a {@code Version} instance of the specified major, minor and
* pre-release versions, as well as build metadata.
*
* @param major a major version number, non-negative
* @param minor a minor version number, non-negative
* @param preRelease a pre-release version label, nullable
* @param build a build metadata label, nullable
* @return a {@code Version} instance
* @throws IllegalArgumentException if {@code major} or {@code minor} is negative
* @throws ParseException if {@code preRelease} or {@code build} can't be parsed
* @since 0.10.0
*/
public static Version of(long major, long minor, String preRelease, String build) {
return Version.of(major, minor, 0, preRelease, build);
}
/**
* Obtains a {@code Version} instance of the specified major, minor and
* patch versions.
*
* @param major a major version number, non-negative
* @param minor a minor version number, non-negative
* @param patch a patch version number, non-negative
* @return a {@code Version} instance
* @throws IllegalArgumentException if any of the arguments is negative
* @since 0.10.0
*/
public static Version of(long major, long minor, long patch) {
return Version.of(major, minor, patch, null, null);
}
/**
* Obtains a {@code Version} instance of the specified major, minor, patch
* and pre-release versions.
*
* @param major a major version number, non-negative
* @param minor a minor version number, non-negative
* @param patch a patch version number, non-negative
* @param preRelease a pre-release version label, nullable
* @return a {@code Version} instance
* @throws IllegalArgumentException if any of the numeric arguments is negative
* @throws ParseException if {@code preRelease} can't be parsed
* @since 0.10.0
*/
public static Version of(long major, long minor, long patch, String preRelease) {
return Version.of(major, minor, patch, preRelease, null);
}
/**
* Obtains a {@code Version} instance of the specified major, minor, patch
* and pre-release versions, as well as build metadata.
*
* @param major a major version number, non-negative
* @param minor a minor version number, non-negative
* @param patch a patch version number, non-negative
* @param preRelease a pre-release version label, nullable
* @param build a build metadata label, nullable
* @return a {@code Version} instance
* @throws IllegalArgumentException if any of the numeric arguments is negative
* @throws ParseException if {@code preRelease} or {@code build} can't be parsed
* @since 0.10.0
*/
public static Version of(long major, long minor, long patch, String preRelease, String build) {
return new Version(
major,
minor,
patch,
preRelease == null ? new String[0] : parsePreRelease(preRelease),
build == null ? new String[0] : parseBuild(build)
);
}
/**
* Returns this {@code Version}'s major version.
*
* @return the major version number
* @since 0.10.0
*/
public long majorVersion() {
return major;
}
/**
* Returns this {@code Version}'s minor version.
*
* @return the minor version number
* @since 0.10.0
*/
public long minorVersion() {
return minor;
}
/**
* Returns this {@code Version}'s patch version.
*
* @return the patch version number
* @since 0.10.0
*/
public long patchVersion() {
return patch;
}
/**
* Returns this {@code Version}'s pre-release version in the form of
* dot-separated identifiers.
*
* @return the pre-release version label, if present
* @since 0.10.0
*/
public Optional<String> preReleaseVersion() {
return Optional.ofNullable(joinIdentifiers(preReleaseIds));
}
/**
* Returns this {@code Version}'s build metadata in the form of
* dot-separated identifiers.
*
* @return the build metadata label, if present
* @since 0.10.0
*/
public Optional<String> buildMetadata() {
return Optional.ofNullable(joinIdentifiers(buildIds));
}
/**
* Obtains the next {@code Version} by incrementing the major version number
* by one, with an optional pre-release version label.
* <p>
* Multiple identifiers can be specified in a single argument joined with
* dots, or in separate arguments, or both.
* <p>
* This method drops the build metadata, if present.
*
* @param preReleaseIds zero or more pre-release identifiers, non-null
* @return a {@code Version} instance
* @throws ArithmeticException if the major version number overflows
* @throws IllegalArgumentException if {@code preReleaseIds} is null or contains null
* @throws ParseException if any of the specified identifiers can't be parsed
* @since 0.10.0
*/
public Version nextMajorVersion(String... preReleaseIds) {
return nextMajorVersion(safeIncrement(major), preReleaseIds);
}
/**
* Obtains the next {@code Version} of the specified major version number,
* with an optional pre-release version label.
* <p>
* The specified major version number must be higher than this {@code Version}'s
* major version.
* <p>
* Multiple identifiers can be specified in a single argument joined with
* dots, or in separate arguments, or both.
* <p>
* This method drops the build metadata, if present.
*
* @param major the next major version number, non-negative
* @param preReleaseIds zero or more pre-release identifiers, non-null
* @return a {@code Version} instance
* @throws IllegalArgumentException if {@code major} is negative, or if
* {@code preReleaseIds} is null or contains null
* @throws IllegalStateException if {@code major} is lower than or equivalent
* to this {@code Version}'s major version
* @throws ParseException if any of the specified identifiers can't be parsed
* @since 0.10.0
*/
public Version nextMajorVersion(long major, String... preReleaseIds) {
if (this.major >= nonNegative(major, "major")) {
throw new IllegalStateException("This major version is higher or equivalent");
}
String preRelease = joinIdentifiers(zeroOrMoreNonNulls(preReleaseIds, "preReleaseIds"));
return Version.of(major, 0, 0, preRelease);
}
/**
* Obtains the next {@code Version} by incrementing the minor version number
* by one, with an optional pre-release version label.
* <p>
* Multiple identifiers can be specified in a single argument joined with
* dots, or in separate arguments, or both.
* <p>
* This method drops the build metadata, if present.
*
* @param preReleaseIds zero or more pre-release identifiers, non-null
* @return a {@code Version} instance
* @throws ArithmeticException if the minor version number overflows
* @throws IllegalArgumentException if {@code preReleaseIds} is null or contains null
* @throws ParseException if any of the specified identifiers can't be parsed
* @since 0.10.0
*/
public Version nextMinorVersion(String... preReleaseIds) {
return nextMinorVersion(safeIncrement(minor), preReleaseIds);
}
/**
* Obtains the next {@code Version} of the specified minor version number,
* with an optional pre-release version label.
* <p>
* The specified minor version number must be higher than this {@code Version}'s
* minor version.
* <p>
* Multiple identifiers can be specified in a single argument joined with
* dots, or in separate arguments, or both.
* <p>
* This method drops the build metadata, if present.
*
* @param minor the next minor version number, non-negative
* @param preReleaseIds zero or more pre-release identifiers, non-null
* @return a {@code Version} instance
* @throws IllegalArgumentException if {@code minor} is negative, or if
* {@code preReleaseIds} is null or contains null
* @throws IllegalStateException if {@code minor} is lower than or equivalent
* to this {@code Version}'s minor version
* @throws ParseException if any of the specified identifiers can't be parsed
* @since 0.10.0
*/
public Version nextMinorVersion(long minor, String... preReleaseIds) {
if (this.minor >= nonNegative(minor, "minor")) {
throw new IllegalStateException("This minor version is higher or equivalent");
}
String preRelease = joinIdentifiers(zeroOrMoreNonNulls(preReleaseIds, "preReleaseIds"));
return Version.of(major, minor, 0, preRelease);
}
/**
* Obtains the next {@code Version} by incrementing the patch version number
* by one, with an optional pre-release version label.
* <p>
* Multiple identifiers can be specified in a single argument joined with
* dots, or in separate arguments, or both.
* <p>
* This method drops the build metadata, if present.
*
* @param preReleaseIds zero or more pre-release identifiers, non-null
* @return a {@code Version} instance
* @throws ArithmeticException if the patch version number overflows
* @throws IllegalArgumentException if {@code preReleaseIds} is null or contains null
* @throws ParseException if any of the specified identifiers can't be parsed
* @since 0.10.0
*/
public Version nextPatchVersion(String... preReleaseIds) {
return nextPatchVersion(safeIncrement(patch), preReleaseIds);
}
/**
* Obtains the next {@code Version} of the specified patch version number,
* with an optional pre-release version label.
* <p>
* The specified patch version number must be higher than this {@code Version}'s
* patch version.
* <p>
* Multiple identifiers can be specified in a single argument joined with
* dots, or in separate arguments, or both.
* <p>
* This method drops the build metadata, if present.
*
* @param patch the next patch version number, non-negative
* @param preReleaseIds zero or more pre-release identifiers, non-null
* @return a {@code Version} instance
* @throws IllegalArgumentException if {@code patch} is negative, or if
* {@code preReleaseIds} is null or contains null
* @throws IllegalStateException if {@code patch} is lower than or equivalent
* to this {@code Version}'s patch version
* @throws ParseException if any of the specified identifiers can't be parsed
* @since 0.10.0
*/
public Version nextPatchVersion(long patch, String... preReleaseIds) {
if (this.patch >= nonNegative(patch, "patch")) {
throw new IllegalStateException("This patch version is higher or equivalent");
}
String preRelease = joinIdentifiers(zeroOrMoreNonNulls(preReleaseIds, "preReleaseIds"));
return Version.of(major, minor, patch, preRelease);
}
/**
* Obtains the next {@code Version} by incrementing or replacing the
* pre-release version.
* <p>
* If no pre-release identifiers are specified, the current pre-release
* version's last numeric identifier is incremented. If the current
* pre-release version's last identifier is not numeric, a new numeric
* identifier of value "0" is appended for this operation. If specified,
* however, the pre-release identifiers replace the current pre-release
* version. The new pre-release version must be higher than this
* {@code Version}'s pre-release version.
* <p>
* Multiple identifiers can be specified in a single argument joined with
* dots, or in separate arguments, or both.
* <p>
* This method drops the build metadata, if present.
*
* @param ids zero or more pre-release identifiers, non-null
* @return a {@code Version} instance
* @throws ArithmeticException if the incremented numeric identifier overflows
* @throws IllegalArgumentException if {@code ids} is null or contains null
* @throws IllegalStateException if invoked on a stable {@code Version}, or
* if the specified pre-release version is lower than or equivalent
* to this {@code Version}'s pre-release version
* @throws ParseException if any of the specified identifiers can't be parsed
* @since 0.10.0
*/
public Version nextPreReleaseVersion(String... ids) {
if (!isPreRelease()) {
throw new IllegalStateException("Not a pre-release version");
}
zeroOrMoreNonNulls(ids, "ids");
String[] newPreReleaseIds;
if (ids.length > 0) {
newPreReleaseIds = parsePreRelease(joinIdentifiers(ids));
if (compareIdentifierArrays(preReleaseIds, newPreReleaseIds) >= 0) {
throw new IllegalStateException("This pre-release version is higher or equivalent");
}
} else {
newPreReleaseIds = incrementIdentifiers(preReleaseIds);
}
return new Version(major, minor, patch, newPreReleaseIds);
}
/**
* Obtains the next {@code Version} by dropping the pre-release version.
* <p>
* This method drops the build metadata, if present.
*
* @return a {@code Version} instance
* @since 0.10.0
*/
public Version toStableVersion() {
return isStable() ? this : new Version(major, minor, patch);
}
/**
* Obtains a new {@code Version} with the specified build identifiers.
* <p>
* Multiple identifiers can be specified in a single argument joined with
* dots, or in separate arguments, or both.
*
* @param ids one or more build identifiers, non-null
* @return a {@code Version} instance
* @throws IllegalArgumentException if {@code ids} is null/empty or contains null
* @throws ParseException if any of the specified identifiers can't be parsed
* @since 0.10.0
*/
public Version withBuildMetadata(String... ids) {
String[] newBuildIds = parseBuild(joinIdentifiers(oneOrMoreNonNulls(ids, "ids")));
return new Version(major, minor, patch, preReleaseIds, newBuildIds);
}
/**
* Obtains a (new) {@code Version} without build metadata.
*
* @return a {@code Version} instance
* @since 0.10.0
*/
public Version withoutBuildMetadata() {
return !buildMetadata().isPresent() ? this : new Version(major, minor, patch, preReleaseIds);
}
/**
* Checks if this {@code Version} satisfies the specified predicate.
*
* @param predicate a predicate to test, non-null
* @return {@code true}, if this {@code Version} satisfies the predicate;
* {@code false} otherwise
* @throws IllegalArgumentException if {@code predicate} is null
* @since 0.10.0
*/
public boolean satisfies(Predicate<Version> predicate) {
return nonNull(predicate, "predicate").test(this);
}
/**
* Checks if this {@code Version} satisfies the specified range expression.
*
* @param expr a SemVer Expression string, non-null
* @return {@code true}, if this {@code Version} satisfies the specified
* expression; {@code false} otherwise
* @throws IllegalArgumentException if {@code expr} is null
* @throws ParseException if {@code expr} can't be parsed
* @since 0.7.0
*/
public boolean satisfies(String expr) {
Parser<Expression> parser = ExpressionParser.newInstance();
return satisfies(parser.parse(nonNull(expr, "expr")));
}
/**
* Checks if this {@code Version} represents a pre-release version.
* <p>
* This method is opposite of {@link #isStable()}.
*
* @return {@code true}, if this {@code Version} represents a pre-release
* version; {@code false} otherwise
* @see #isStable()
* @since 0.10.0
*/
public boolean isPreRelease() {
return preReleaseVersion().isPresent();
}
/**
* Checks if this {@code Version} represents a stable version.
* <p>
* Pre-release versions are considered unstable. (SemVer p.9)
*
* @return {@code true}, if this {@code Version} represents a stable
* version; {@code false} otherwise
* @see #isPreRelease()
* @since 0.10.0
*/
public boolean isStable() {
return !isPreRelease();
}
/**
* Checks if this {@code Version} represents a stable public API.
* <p>
* Versions lower than 1.0.0 are for initial development, therefore the
* public API should not be considered stable. (SemVer p.4)
*
* @return {@code true}, if this {@code Version} represents a stable public
* API; {@code false} otherwise
* @since 0.10.0
*/
public boolean isPublicApiStable() {
return isHigherThanOrEquivalentTo(Version.of(1));
}
/**
* Checks if this {@code Version} is compatible with the specified {@code Version}