From: Robert Varga Date: Wed, 13 Jan 2016 00:12:15 +0000 (+0100) Subject: BUG-4662: introduce the SemVer concept X-Git-Tag: release/boron~232 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=285d22ac3fa671abe9eb453e3f42dc8ebfbbb3ca;p=yangtools.git BUG-4662: introduce the SemVer concept This is a very simple DTO concept for semantic versions. Change-Id: I752f36198b03c37ef4116088baf1938248a01fd2 Signed-off-by: Robert Varga --- diff --git a/common/concepts/pom.xml b/common/concepts/pom.xml index d97e13c35e..b559673bec 100644 --- a/common/concepts/pom.xml +++ b/common/concepts/pom.xml @@ -28,6 +28,10 @@ jsr305 provided + + com.google.guava + guava + diff --git a/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/SemVer.java b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/SemVer.java new file mode 100644 index 0000000000..65d55f2776 --- /dev/null +++ b/common/concepts/src/main/java/org/opendaylight/yangtools/concepts/SemVer.java @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2016 Pantheon Technologies s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.concepts; + +import com.google.common.annotations.Beta; +import com.google.common.base.Preconditions; +import java.io.Serializable; +import java.util.Objects; +import javax.annotation.Nonnull; + +/** + * A single version according to Semantic Versioning. + */ +@Beta +public final class SemVer implements Comparable, Serializable { + private static final long serialVersionUID = 1L; + private final int major; + private final int minor; + private final int patch; + + private SemVer(final int major, final int minor, final int patch) { + Preconditions.checkArgument(major >= 0); + this.major = major; + Preconditions.checkArgument(minor >= 0); + this.minor = minor; + Preconditions.checkArgument(patch >= 0); + this.patch = patch; + } + + public static SemVer create(final int major) { + return create(major, 0); + } + + public static SemVer create(final int major, final int minor) { + return create(major, minor, 0); + } + + public static SemVer create(final int major, final int minor, final int patch) { + return new SemVer(major, minor, patch); + } + + public static SemVer valueOf(@Nonnull final String s) { + final int minorIdx = s.indexOf('.'); + if (minorIdx == -1) { + return create(Integer.parseInt(s)); + } + + final String minorStr; + final int patchIdx = s.indexOf('.', minorIdx + 1); + if (patchIdx == -1) { + minorStr = s.substring(minorIdx + 1); + } else { + minorStr = s.substring(minorIdx + 1, patchIdx); + } + + return create(Integer.parseInt(s.substring(0, minorIdx), 10), Integer.parseInt(minorStr, 10), + Integer.parseInt(s.substring(patchIdx + 1), 10)); + } + + /** + * @return the major + */ + public int getMajor() { + return major; + } + + /** + * @return the minor + */ + public int getMinor() { + return minor; + } + + /** + * @return the patch + */ + public int getPatch() { + return patch; + } + + @Override + public int compareTo(final SemVer o) { + int i = Integer.compare(major, o.major); + if (i == 0) { + i = Integer.compare(minor, o.minor); + if (i == 0) { + return Integer.compare(patch, patch); + } + } + + return i; + } + + @Override + public int hashCode() { + return Objects.hash(major, minor, patch); + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof SemVer)) { + return false; + } + + final SemVer o = (SemVer) obj; + return major == o.major && minor == o.minor && patch == o.patch; + } + + @Override + public String toString() { + return major + "." + minor + "." + patch; + } +}