You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Version.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.updater;
  23. import javax.annotation.Nonnull;
  24. /**
  25. * Describes a version of a component, either as an integer or as a String which corresponds to the
  26. * output of `git-describe --tags`.
  27. *
  28. * @since 0.6.3m1
  29. */
  30. public class Version implements Comparable<Version> {
  31. protected final int intVersion;
  32. protected final String strVersion;
  33. /**
  34. * Creates an invalid version.
  35. */
  36. public Version() {
  37. this.intVersion = Integer.MIN_VALUE;
  38. this.strVersion = null;
  39. }
  40. /**
  41. * Creates a new integer version.
  42. *
  43. * @param version Version to create
  44. */
  45. public Version(final int version) {
  46. this.intVersion = version;
  47. this.strVersion = null;
  48. }
  49. /**
  50. * Creates a new git version.
  51. *
  52. * @param version Git version
  53. */
  54. public Version(final String version) {
  55. if (version.matches("^[0-9]+$")) {
  56. this.intVersion = Integer.parseInt(version);
  57. this.strVersion = null;
  58. } else if (version.matches(
  59. "^[0-9]+(\\.[0-9]+)*((a|b|rc|m)[0-9]+)*(\\-[0-9]+\\-g[a-z0-9]{7})?(-SNAPSHOT)?$")) {
  60. this.intVersion = Integer.MIN_VALUE;
  61. this.strVersion = version.replaceAll("-SNAPSHOT$", "");
  62. } else {
  63. this.intVersion = Integer.MIN_VALUE;
  64. this.strVersion = null;
  65. }
  66. }
  67. @Override
  68. public int compareTo(@Nonnull final Version o) {
  69. if (o.intVersion > Integer.MIN_VALUE && intVersion > Integer.MIN_VALUE) {
  70. return intVersion - o.intVersion;
  71. } else if (o.strVersion == null && strVersion == null) {
  72. return 0;
  73. } else if (o.strVersion == null && strVersion != null) {
  74. return 1;
  75. } else if (o.strVersion != null && strVersion == null) {
  76. return -1;
  77. } else {
  78. final String[] myParts = strVersion.split("-");
  79. final String[] thParts = o.strVersion.split("-");
  80. final String[] myFirstParts = myParts[0].split("\\.|(?=a|b|rc|m)");
  81. final String[] thFirstParts = thParts[0].split("\\.|(?=a|b|rc|m)");
  82. for (int i = 0; i < Math.max(myFirstParts.length, thFirstParts.length); i++) {
  83. final boolean myExists = myFirstParts.length > i;
  84. final boolean thExists = thFirstParts.length > i;
  85. final boolean myIsInt = myExists && myFirstParts[i].matches("[0-9]+");
  86. final boolean thIsInt = thExists && thFirstParts[i].matches("[0-9]+");
  87. final int myInt = myIsInt ? Integer.parseInt(myFirstParts[i]) : 0;
  88. final int thInt = thIsInt ? Integer.parseInt(thFirstParts[i]) : 0;
  89. // Please consult handwritten truth table in the care of
  90. // Chris for an explanation of what the hell is going on here.
  91. // If there's a bug in this code it should probably be
  92. // rewritten.
  93. if (!myExists && !thExists
  94. || myIsInt && thIsInt && myInt == thInt
  95. || myExists && thExists && !myIsInt && !thIsInt
  96. && myFirstParts[i].equals(thFirstParts[i])) {
  97. continue;
  98. } else if (!thExists && myIsInt
  99. || thExists && !thIsInt && (!myExists || myIsInt)) {
  100. return 1;
  101. } else if (thIsInt && !myExists
  102. || myExists && !myIsInt && (!thExists || thIsInt)) {
  103. return -1;
  104. } else if (thIsInt && myIsInt) {
  105. return myInt - thInt;
  106. } else {
  107. final String[] myLetterParts = myFirstParts[i].split("(?=[0-9])", 2);
  108. final String[] thLetterParts = thFirstParts[i].split("(?=[0-9])", 2);
  109. if (myLetterParts[0].equals(thLetterParts[0])) {
  110. return Integer.parseInt(myLetterParts[1])
  111. - Integer.parseInt(thLetterParts[1]);
  112. } else if ("m".equals(myLetterParts[0])
  113. || "rc".equals(thLetterParts[0])
  114. || "a".equals(myLetterParts[0]) && "b".equals(thLetterParts[0])) {
  115. return -1;
  116. } else {
  117. return 1;
  118. }
  119. }
  120. }
  121. final int myInt = myParts.length > 1 ? Integer.parseInt(myParts[1]) : 0;
  122. final int thInt = thParts.length > 1 ? Integer.parseInt(thParts[1]) : 0;
  123. return myInt - thInt;
  124. }
  125. }
  126. @Override
  127. public boolean equals(final Object obj) {
  128. return obj instanceof Version && compareTo((Version) obj) == 0;
  129. }
  130. @Override
  131. public int hashCode() {
  132. int hash = 3;
  133. hash = 17 * hash + intVersion;
  134. hash = 17 * hash + (strVersion == null ? 0 : strVersion.hashCode());
  135. return hash;
  136. }
  137. /**
  138. * Determines whether or not this represents a valid version.
  139. *
  140. * @return True if the version is valid, false otherwise
  141. */
  142. public boolean isValid() {
  143. return intVersion > Integer.MIN_VALUE || strVersion != null;
  144. }
  145. @Override
  146. public String toString() {
  147. return strVersion == null ? String.valueOf(intVersion) : strVersion;
  148. }
  149. }