summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2012-10-24 16:50:23 +0200
committerSven Gothel <[email protected]>2012-10-24 16:50:23 +0200
commit08a8defda8b6f49eb794cf787f688ba65bfe7b37 (patch)
treea0aed7a50d301d4351120073560d20c88234c60b
parent761b2855b9c01c421ecd4d435a828a67b3a2471b (diff)
Fix VersionNumber: Non digits cut off pattern was '\D.*' and cut off digits in case leading non digits appear.
'\D.*' matches a leading non-digit and then any character. First the string is tokenized by delim: "OpenGL ES GLSL ES 1.0.16" -> "OpenGL ES GLSL ES 1", "0", "16" Enhance pattern as follows and access collected group if matching: // group1: \D* == leading non digits, optional // group2: \d* == digits // group3: .* == any pending chars, optional final java.util.regex.Pattern nonDigitsCutOff = java.util.regex.Pattern.compile("(\\D*)(\\d*)(.*)"); Reorganized storage from atomic values to int[3] array, allowing simple pattern matching loop. Added unit test.
-rwxr-xr-xmake/scripts/runtest.sh3
-rw-r--r--src/java/com/jogamp/common/util/VersionNumber.java84
-rw-r--r--src/junit/com/jogamp/common/util/TestVersionNumber.java102
3 files changed, 145 insertions, 44 deletions
diff --git a/make/scripts/runtest.sh b/make/scripts/runtest.sh
index aebe608..8cdf352 100755
--- a/make/scripts/runtest.sh
+++ b/make/scripts/runtest.sh
@@ -70,6 +70,7 @@ function onetest() {
#onetest com.jogamp.common.GlueGenVersion 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.TestSystemPropsAndEnvs 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.TestVersionInfo 2>&1 | tee -a $LOG
+onetest com.jogamp.common.util.TestVersionNumber 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.TestIteratorIndexCORE 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.locks.TestRecursiveLock01 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.locks.TestRecursiveThreadGroupLock01 2>&1 | tee -a $LOG
@@ -91,7 +92,7 @@ function onetest() {
#onetest com.jogamp.common.util.TestIOUtil01 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.TestTempJarCache 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.TestJarUtil 2>&1 | tee -a $LOG
-onetest com.jogamp.common.util.TestValueConversion 2>&1 | tee -a $LOG
+#onetest com.jogamp.common.util.TestValueConversion 2>&1 | tee -a $LOG
#onetest com.jogamp.common.net.AssetURLConnectionUnregisteredTest 2>&1 | tee -a $LOG
#onetest com.jogamp.common.net.AssetURLConnectionRegisteredTest 2>&1 | tee -a $LOG
#onetest com.jogamp.common.net.URLCompositionTest 2>&1 | tee -a $LOG
diff --git a/src/java/com/jogamp/common/util/VersionNumber.java b/src/java/com/jogamp/common/util/VersionNumber.java
index 1225231..afed32d 100644
--- a/src/java/com/jogamp/common/util/VersionNumber.java
+++ b/src/java/com/jogamp/common/util/VersionNumber.java
@@ -29,17 +29,17 @@
package com.jogamp.common.util;
import java.util.StringTokenizer;
+import java.util.regex.Matcher;
-public class VersionNumber implements Comparable {
+public class VersionNumber implements Comparable<Object> {
- protected int major;
- protected int minor;
- protected int sub;
+ /** int[3] { major, minor, sub } */
+ protected int[] val = new int[] { 0, 0, 0 };
public VersionNumber(int majorRev, int minorRev, int subMinorRev) {
- major = majorRev;
- minor = minorRev;
- sub = subMinorRev;
+ val[0] = majorRev;
+ val[1] = minorRev;
+ val[2] = subMinorRev;
}
/**
@@ -47,27 +47,19 @@ public class VersionNumber implements Comparable {
* @param delim the delimiter, e.g. "."
*/
public VersionNumber(String versionString, String delim) {
- final java.util.regex.Pattern nonDigitsCutOff = java.util.regex.Pattern.compile("\\D.*");
- major = 0;
- minor = 0;
- sub = 0;
-
- StringTokenizer tok = new StringTokenizer(versionString, delim);
- if (tok.hasMoreTokens()) {
+ // group1: \D* == leading non digits, optional
+ // group2: \d* == digits
+ // group3: .* == any pending chars, optional
+ final java.util.regex.Pattern nonDigitsCutOff = java.util.regex.Pattern.compile("(\\D*)(\\d*)(.*)");
+ final StringTokenizer tok = new StringTokenizer(versionString, delim);
+ for(int n=0; tok.hasMoreTokens() && n<3; n++) {
try {
- major = Integer.parseInt(nonDigitsCutOff.matcher(tok.nextToken()).replaceAll(""));
- } catch (NumberFormatException e) { }
+ final Matcher matcher = nonDigitsCutOff.matcher( tok.nextToken() );
+ if(matcher.matches()) {
+ val[n] = Integer.parseInt(matcher.group(2));
+ }
+ } catch (Exception e) { e.printStackTrace(); }
}
- if (tok.hasMoreTokens()) {
- try {
- minor = Integer.parseInt(nonDigitsCutOff.matcher(tok.nextToken()).replaceAll(""));
- } catch (NumberFormatException e) { }
- }
- if (tok.hasMoreTokens()) {
- try {
- sub = Integer.parseInt(nonDigitsCutOff.matcher(tok.nextToken()).replaceAll(""));
- } catch (NumberFormatException e) { }
- }
}
protected VersionNumber() { }
@@ -75,53 +67,59 @@ public class VersionNumber implements Comparable {
@Override
public final int hashCode() {
// 31 * x == (x << 5) - x
- int hash = 31 + major;
- hash = ((hash << 5) - hash) + minor;
- return ((hash << 5) - hash) + sub;
+ int hash = 31 + val[0];
+ hash = ((hash << 5) - hash) + val[1];
+ return ((hash << 5) - hash) + val[2];
}
@Override
public final boolean equals(Object o) {
- return 0 == compareTo(o);
+ if ( o instanceof VersionNumber ) {
+ return 0 == compareTo( (VersionNumber) o );
+ }
+ return false;
}
+ @Override
public final int compareTo(Object o) {
if ( ! ( o instanceof VersionNumber ) ) {
Class<?> c = (null != o) ? o.getClass() : null ;
- throw new ClassCastException("Not a Capabilities object: " + c);
+ throw new ClassCastException("Not a VersionNumber object: " + c);
}
+ return compareTo( (VersionNumber) o );
+ }
- VersionNumber vo = (VersionNumber) o;
- if (major > vo.major) {
+ public final int compareTo(VersionNumber vo) {
+ if (val[0] > vo.val[0]) { // major
return 1;
- } else if (major < vo.major) {
+ } else if (val[0] < vo.val[0]) { // major
return -1;
- } else if (minor > vo.minor) {
+ } else if (val[1] > vo.val[1]) { // minor
return 1;
- } else if (minor < vo.minor) {
+ } else if (val[1] < vo.val[1]) { // minor
return -1;
- } else if (sub > vo.sub) {
+ } else if (val[2] > vo.val[2]) { // sub
return 1;
- } else if (sub < vo.sub) {
+ } else if (val[2] < vo.val[2]) { // sub
return -1;
}
return 0; // they are equal
}
-
+
public final int getMajor() {
- return major;
+ return val[0];
}
public final int getMinor() {
- return minor;
+ return val[1];
}
public final int getSub() {
- return sub;
+ return val[2];
}
@Override
public String toString() {
- return major + "." + minor + "." + sub ;
+ return getMajor() + "." + getMinor() + "." + getSub() ;
}
}
diff --git a/src/junit/com/jogamp/common/util/TestVersionNumber.java b/src/junit/com/jogamp/common/util/TestVersionNumber.java
new file mode 100644
index 0000000..f1ed157
--- /dev/null
+++ b/src/junit/com/jogamp/common/util/TestVersionNumber.java
@@ -0,0 +1,102 @@
+/**
+ * Copyright 2012 JogAmp Community. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list of
+ * conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ * of conditions and the following disclaimer in the documentation and/or other materials
+ * provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are those of the
+ * authors and should not be interpreted as representing official policies, either expressed
+ * or implied, of JogAmp Community.
+ */
+
+package com.jogamp.common.util;
+
+import java.io.IOException;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.jogamp.junit.util.JunitTracer;
+
+public class TestVersionNumber extends JunitTracer {
+
+ @Test
+ public void test01() {
+ final String delim = ".";
+
+ final String vs00 = "1.0.16";
+ final String vs01 = "OpenGL ES GLSL ES 1.0.16";
+ final String vs02 = "1.0.16 OpenGL ES GLSL ES";
+ final VersionNumber vn0 = new VersionNumber(1, 0, 16);
+
+ VersionNumber vn;
+
+ vn = new VersionNumber(vs00, delim);
+ Assert.assertEquals(vn0, vn);
+ vn = new VersionNumber(vs01, delim);
+ Assert.assertEquals(vn0, vn);
+ vn = new VersionNumber(vs02, delim);
+ Assert.assertEquals(vn0, vn);
+ }
+
+ @Test
+ public void test02() {
+ final String delim = ".";
+
+ final String vs00 = "4.20";
+ final String vs01 = "COMPANY via Stupid tool 4.20";
+ final String vs02 = "4.20 COMPANY via Stupid tool";
+ final VersionNumber vn0 = new VersionNumber(4, 20, 0);
+
+ VersionNumber vn;
+
+ vn = new VersionNumber(vs00, delim);
+ Assert.assertEquals(vn0, vn);
+ vn = new VersionNumber(vs01, delim);
+ Assert.assertEquals(vn0, vn);
+ vn = new VersionNumber(vs02, delim);
+ Assert.assertEquals(vn0, vn);
+ }
+
+ @Test
+ public void test03() {
+ final String delim = ".";
+
+ final String vs00 = "A10.11.12b";
+ final String vs01 = "Prelim Text 10.Funny11 .Weird12 Something is odd";
+ final String vs02 = "Prelim Text 10 .Funny11l1.Weird12 2 Something is odd";
+ final VersionNumber vn0 = new VersionNumber(10, 11, 12);
+
+ VersionNumber vn;
+
+ vn = new VersionNumber(vs00, delim);
+ Assert.assertEquals(vn0, vn);
+ vn = new VersionNumber(vs01, delim);
+ Assert.assertEquals(vn0, vn);
+ vn = new VersionNumber(vs02, delim);
+ Assert.assertEquals(vn0, vn);
+ }
+
+ public static void main(String args[]) throws IOException {
+ String tstname = TestVersionNumber.class.getName();
+ org.junit.runner.JUnitCore.main(tstname);
+ }
+
+}