Last active
December 17, 2015 15:19
-
-
Save vshank77/5630720 to your computer and use it in GitHub Desktop.
SemVersion.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.regex.Matcher; | |
| import java.util.regex.Pattern; | |
| public class SemVersion implements java.io.Serializable { | |
| private static final long serialVersionUID = 1L; | |
| private final static SemVersion UNKNOWN_VERSION = new SemVersion(0, 0, 0, null); | |
| private static final Pattern SEMVER_PATTERN = Pattern.compile("(\\d)\\.(\\d)\\.(\\d)([A-Za-z0-9\\.\\+-]*)?$"); | |
| private final int majorVersion; | |
| private final int minorVersion; | |
| private final int patchLevel; | |
| private final String snapshotInfo; | |
| public static SemVersion parse(String versionStr) { | |
| Matcher m = SEMVER_PATTERN.matcher(versionStr); | |
| if (!m.matches()) | |
| throw new IllegalArgumentException("unknown version format"); | |
| return new SemVersion(versionInt(m, 1), versionInt(m, 2), versionInt(m, 3), snapshotInfo(m)); | |
| } | |
| private static String snapshotInfo(Matcher matcher) { | |
| String _snapshotInfo = matcher.group(4); | |
| return _snapshotInfo.isEmpty() ? null : _snapshotInfo; | |
| } | |
| private static int versionInt(Matcher matcher, int pos) { | |
| return Integer.parseInt(matcher.group(pos)); | |
| } | |
| protected SemVersion(int major, int minor, int patch, String snapshot) { | |
| this.majorVersion = major; | |
| this.minorVersion = minor; | |
| this.patchLevel = patch; | |
| this.snapshotInfo = snapshot; | |
| } | |
| public static SemVersion unknownVersion() { | |
| return UNKNOWN_VERSION; | |
| } | |
| public boolean isUknownVersion() { | |
| return (this == UNKNOWN_VERSION); | |
| } | |
| public boolean isSnapshotInfo() { | |
| return (snapshotInfo != null && snapshotInfo.length() > 0); | |
| } | |
| public boolean isSnapshot() { | |
| return isSnapshotInfo() && snapshotInfo.endsWith("SNAPSHOT"); | |
| } | |
| public int getMajorVersion() { | |
| return majorVersion; | |
| } | |
| public int getMinorVersion() { | |
| return minorVersion; | |
| } | |
| public int getPatchLevel() { | |
| return patchLevel; | |
| } | |
| public String getSnapshotInfo() { | |
| return snapshotInfo; | |
| } | |
| @Override | |
| public String toString() { | |
| StringBuilder sb = new StringBuilder(); | |
| sb.append(majorVersion).append('.'); | |
| sb.append(minorVersion).append('.'); | |
| sb.append(patchLevel); | |
| if (snapshotInfo != null) { | |
| sb.append(snapshotInfo); | |
| } | |
| return sb.toString(); | |
| } | |
| @Override | |
| public int hashCode() { | |
| return majorVersion ^ minorVersion + patchLevel; | |
| } | |
| @Override | |
| public boolean equals(Object o) { | |
| if (o == this) | |
| return true; | |
| if (o == null) | |
| return false; | |
| if (o.getClass() != getClass()) | |
| return false; | |
| SemVersion other = (SemVersion) o; | |
| return (other.majorVersion == majorVersion) && (other.minorVersion == minorVersion) | |
| && (other.patchLevel == patchLevel) | |
| && (other.snapshotInfo == null ? snapshotInfo == null : other.snapshotInfo.equals(snapshotInfo)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.polyglotted.attribrepo.core; | |
| import static org.junit.Assert.assertNotNull; | |
| import static org.junit.Assert.assertNull; | |
| import org.junit.Test; | |
| public class VersionTest { | |
| @Test(expected = IllegalArgumentException.class) | |
| public void testUnknownFormat() { | |
| assertNull(SemVersion.parse("1.2-rc1")); | |
| } | |
| @Test | |
| public void testPattern() { | |
| assertNotNull(SemVersion.parse("1.2.0")); | |
| assertNotNull(SemVersion.parse("1.2.0-SNAPSHOT")); | |
| assertNotNull(SemVersion.parse("1.2.0-alpha-a.b-c+build.1-ref.1-its-okay")); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment