2 * Copyright (c) 2016 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.concepts;
10 import static com.google.common.base.Preconditions.checkArgument;
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;
19 * A single version according to <a href="http://semver.org/">Semantic Versioning</a>.
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;
28 private SemVer(final int major, final int minor, final int patch) {
29 checkArgument(major >= 0);
31 checkArgument(minor >= 0);
33 checkArgument(patch >= 0);
37 public static @NonNull SemVer create(final @NonNegative int major) {
38 return create(major, 0);
41 public static @NonNull SemVer create(final @NonNegative int major, final @NonNegative int minor) {
42 return create(major, minor, 0);
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);
50 public static @NonNull SemVer valueOf(final @NonNull String str) {
51 final int minorIdx = str.indexOf('.');
53 return create(Integer.parseInt(str));
56 final String minorStr;
57 final int patchIdx = str.indexOf('.', minorIdx + 1);
59 minorStr = str.substring(minorIdx + 1);
60 return create(Integer.parseInt(str.substring(0, minorIdx), 10), Integer.parseInt(minorStr, 10));
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));
69 * Return the major version number.
71 * @return major version number
73 public int getMajor() {
78 * Return the minor version number.
80 * @return minor version number
82 public int getMinor() {
87 * Return the patch version number.
89 * @return patch version number
91 public int getPatch() {
96 public int compareTo(final SemVer other) {
97 int cmp = Integer.compare(major, other.major);
99 cmp = Integer.compare(minor, other.minor);
101 return Integer.compare(patch, other.patch);
109 public int hashCode() {
110 return Objects.hash(major, minor, patch);
114 public boolean equals(final Object obj) {
118 if (!(obj instanceof SemVer)) {
122 final SemVer o = (SemVer) obj;
123 return major == o.major && minor == o.minor && patch == o.patch;
127 public String toString() {
128 return major + "." + minor + "." + patch;