1 package org.antmod.scm; 2 3 import java.util.StringTokenizer ; 4 5 10 public final class ScmVersion implements Comparable { 11 public final static String VERSIONSTRING_TRUNK = "trunk"; 12 13 int major = -1; 14 int minor = -1; 15 int patch = -1; 16 String moduleName; 17 18 22 public ScmVersion(String versionString) { 23 int i = versionString.indexOf("-"); 24 if (i < 0) { 25 throw new IllegalArgumentException ("Separator '-' missing between modulename and moduleversion"); 26 } 27 this.moduleName = versionString.substring(0, i); 28 parseRevision(versionString.substring(i+1)); 29 } 30 31 36 public ScmVersion(String moduleName, String revisionString) { 37 this.moduleName = moduleName; 38 parseRevision(revisionString); 39 } 40 41 46 private void parseRevision(String revision) { 47 if (revision != null && !revision.equalsIgnoreCase("trunk") && !revision.equalsIgnoreCase("head")) { 49 String versionSeparator = "."; 50 if (revision.indexOf("-") > 0) { 51 versionSeparator = "-"; 52 } 53 StringTokenizer st = new StringTokenizer (revision, versionSeparator); 54 this.major = Integer.parseInt(st.nextToken()); 55 if (st.hasMoreTokens()) { 56 this.minor = Integer.parseInt(st.nextToken()); 57 } 58 if (st.hasMoreTokens()) { 59 this.patch = Integer.parseInt(st.nextToken()); 60 } 61 } 62 } 63 64 public ScmVersion(String moduleName, int major, int minor, int patch) { 65 this.moduleName = moduleName; 66 this.major = major; 67 this.minor = minor; 68 this.patch = patch; 69 } 70 71 public ScmVersion getParentBranch() { 72 if (isTrunk() || isBranch()) { 73 return getTrunkBranch(); 74 } else { 75 return new ScmVersion(this.moduleName, major, minor, -1); 76 } 77 } 78 79 public ScmVersion getTrunkBranch() { 80 return getTrunkBranch(this.moduleName); 81 } 82 83 public static ScmVersion getTrunkBranch(String moduleName) { 84 return new ScmVersion(moduleName, -1, -1, -1); 85 } 86 87 public ScmVersion getNextIncrement() { 88 if (this.moduleName == null || this.moduleName.trim().length() == 0) { 89 throw new RuntimeException ("Modulename unknown - next increment not possible."); 90 } 91 92 if (isBranch()) { 93 return new ScmVersion(this.moduleName, major, minor + 1, -1); 94 } 95 if (isTag()) { 96 return new ScmVersion(this.moduleName, major, minor, patch + 1); 97 } 98 return new ScmVersion(this.moduleName, 1, 0, -1); 99 } 100 101 public boolean isBranch() { 102 return patch < 0 && major >= 0; 103 } 104 105 public boolean isTag() { 106 return patch >= 0; 107 } 108 109 public boolean isTrunk() { 110 return major < 0; 111 } 112 113 public String getModuleName() { 114 return moduleName; 115 } 116 117 public int getMajor() { 118 return major; 119 } 120 121 public int getMinor() { 122 return minor; 123 } 124 125 public int getPatch() { 126 return patch; 127 } 128 129 public boolean equals(Object obj) { 130 if (obj instanceof ScmVersion) { 131 ScmVersion other = (ScmVersion) obj; 132 if (moduleName != null && other.getModuleName() != null) { 133 if (!moduleName.equals(other.getModuleName())) { 134 return false; 135 } 136 } 137 } 138 return toString().equals(obj.toString()) || (isTrunk() && obj.toString().equalsIgnoreCase("trunk")); 139 } 140 141 public String toString() { 142 return toString('-'); 143 } 144 145 public String toString(char versionSeparator) { 146 if (major >= 0) { 147 String result = String.valueOf(major) + versionSeparator + String.valueOf(minor); 148 if (patch >= 0) { 149 result += versionSeparator + String.valueOf(patch); 151 } 152 return result; 153 } else { 154 return VERSIONSTRING_TRUNK; 155 } 156 } 157 158 public String toFullString() { 159 return getModuleName() + "_" + toString(); 160 } 161 162 public String toFullString(char versionSeparator) { 163 return getModuleName() + "_" + toString(versionSeparator); 164 } 165 166 public int compareTo(Object obj) { 167 if (!(obj instanceof ScmVersion)) { 168 throw new IllegalArgumentException ("Cannot compare object of instance " + obj.getClass() + " to ScmVersion object"); 169 } 170 ScmVersion other = (ScmVersion) obj; 171 if (major > other.major) return -1; 172 if (major < other.major) return 1; 173 if (minor > other.minor) return -1; 174 if (minor < other.minor) return 1; 175 if (patch > other.patch) return -1; 176 if (patch < other.patch) return 1; 177 return 0; 178 } 179 } 180 | Popular Tags |