Improve SemVer.equals()
[yangtools.git] / common / concepts / src / main / java / org / opendaylight / yangtools / concepts / SemVer.java
1 /*
2  * Copyright (c) 2016 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.concepts;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.annotations.Beta;
13 import java.io.Serializable;
14 import java.util.Objects;
15 import org.checkerframework.checker.index.qual.NonNegative;
16 import org.eclipse.jdt.annotation.NonNull;
17
18 /**
19  * A single version according to <a href="http://semver.org/">Semantic Versioning</a>.
20  */
21 @Beta
22 public final class SemVer implements Comparable<SemVer>, Serializable {
23     private static final long serialVersionUID = 1L;
24     private final int major;
25     private final int minor;
26     private final int patch;
27
28     private SemVer(final int major, final int minor, final int patch) {
29         checkArgument(major >= 0);
30         this.major = major;
31         checkArgument(minor >= 0);
32         this.minor = minor;
33         checkArgument(patch >= 0);
34         this.patch = patch;
35     }
36
37     public static @NonNull SemVer create(final @NonNegative int major) {
38         return create(major, 0);
39     }
40
41     public static @NonNull SemVer create(final @NonNegative int major, final @NonNegative int minor) {
42         return create(major, minor, 0);
43     }
44
45     public static @NonNull  SemVer create(final @NonNegative int major, final @NonNegative int minor,
46             final @NonNegative int patch) {
47         return new SemVer(major, minor, patch);
48     }
49
50     public static @NonNull SemVer valueOf(final @NonNull String str) {
51         final int minorIdx = str.indexOf('.');
52         if (minorIdx == -1) {
53             return create(Integer.parseInt(str));
54         }
55
56         final String minorStr;
57         final int patchIdx = str.indexOf('.', minorIdx + 1);
58         if (patchIdx == -1) {
59             minorStr = str.substring(minorIdx + 1);
60             return create(Integer.parseInt(str.substring(0, minorIdx), 10), Integer.parseInt(minorStr, 10));
61         }
62
63         minorStr = str.substring(minorIdx + 1, patchIdx);
64         return create(Integer.parseInt(str.substring(0, minorIdx), 10), Integer.parseInt(minorStr, 10),
65             Integer.parseInt(str.substring(patchIdx + 1), 10));
66     }
67
68     /**
69      * Return the major version number.
70      *
71      * @return major version number
72      */
73     public int getMajor() {
74         return major;
75     }
76
77     /**
78      * Return the minor version number.
79      *
80      * @return minor version number
81      */
82     public int getMinor() {
83         return minor;
84     }
85
86     /**
87      * Return the patch version number.
88      *
89      * @return patch version number
90      */
91     public int getPatch() {
92         return patch;
93     }
94
95     @Override
96     public int compareTo(final SemVer other) {
97         int cmp = Integer.compare(major, other.major);
98         if (cmp == 0) {
99             cmp = Integer.compare(minor, other.minor);
100             if (cmp == 0) {
101                 return Integer.compare(patch, other.patch);
102             }
103         }
104
105         return cmp;
106     }
107
108     @Override
109     public int hashCode() {
110         return Objects.hash(major, minor, patch);
111     }
112
113     @Override
114     public boolean equals(final Object obj) {
115         return this == obj || obj instanceof SemVer other && major == other.major && minor == other.minor
116             && patch == other.patch;
117     }
118
119     @Override
120     public String toString() {
121         return major + "." + minor + "." + patch;
122     }
123 }