Author: tchemit Date: 2014-08-26 19:32:49 +0200 (Tue, 26 Aug 2014) New Revision: 2681 Url: http://forge.nuiton.org/projects/nuiton-utils/repository/revisions/2681 Log: fixes #3476: Move increments/decrements methods to Versions class Modified: trunk/src/main/java/org/nuiton/util/version/Version.java trunk/src/main/java/org/nuiton/util/version/Versions.java trunk/src/test/java/org/nuiton/util/version/VersionTest.java trunk/src/test/java/org/nuiton/util/version/VersionsTest.java Modified: trunk/src/main/java/org/nuiton/util/version/Version.java =================================================================== --- trunk/src/main/java/org/nuiton/util/version/Version.java 2014-08-26 09:27:46 UTC (rev 2680) +++ trunk/src/main/java/org/nuiton/util/version/Version.java 2014-08-26 17:32:49 UTC (rev 2681) @@ -34,9 +34,8 @@ import java.util.List; /** - * A class to represent a version. - * <p/> - * Replace previously {@code org.nuiton.util.Version} class. + * <p>A class to represent a version.</p> + * <p>Replace previously {@code org.nuiton.util.Version} class.</p> * <h3>Definition</h3> * A version is defined of n {@code componants} separated by {@code componantSeparator}. * <h4>Componants</h4> @@ -46,13 +45,17 @@ * <li>String componant: a sequence of characters which can't be either number nor componant separators</li> * </ul> * <h4>Componant separators</h4> + * <p> * Componant separator are characters which can't be alphanumeric and can be {@code empty character}. - * <p/> + * </p> + * <p> * Componant separators are optional and componants will be detected as soon as a character changed from a numeric string sequence to a alpha (none numeric!) sequence. + * </p> + * <p> * For example, version {@code 1a2} is composed of three componants: {code 1}, {@code a} and {@code 3}. + * </p> * <h4>Snapshot flag</h4> * Additionnaly version can be qualifed as a {@code SNAPSHOT} (see below section about ordering). - * <p/> * <h3Examples</h3> * <pre> * 0 (one componant 0) @@ -68,13 +71,8 @@ * <h3>Ordering</h3> * A version is comparable, to have all the detail of order see {@link VersionComparator}. * <h3>Immutability</h3> - * The version is immutable, to create or modify a version, use the {@link VersionBuilder} API or the internal methods - * <ul> - * <li>{@link #increments()}</li> - * <li>{@link #increments(char)}</li> - * <li>{@link #increments(int)}</li> - * <li>{@link #decrements(int)}</li> - * </ul> + * The version is immutable, to create or modify a version, use the {@link VersionBuilder} API + * or shortcut methods in {@link Versions}. * * @author tchemit <chemit@codelutin.com> * @see VersionBuilder @@ -153,7 +151,7 @@ } - public String getClassifierComponant(int componantPosition) { + public String getTextComponant(int componantPosition) { VersionComponant comparable = getComponant(componantPosition); Preconditions.checkState(comparable instanceof StringVersionComponant, "componant at " + componantPosition + " for version " + this + " is not a string (" + comparable + ")"); @@ -161,6 +159,13 @@ } + public VersionComponant getComponant(int level) { + + Preconditions.checkArgument(level > 0 || level < getComponantCount(), "not a valid level " + level + " for the Version " + this); + return componants.get(level); + + } + /** * @return the string represention value of the version */ @@ -220,11 +225,13 @@ * </ul> * * @return the incremented version + * @deprecated use instead {@link Versions#increments(Version)} */ + @Deprecated public Version increments() { - Version version = increments(DEFAULT_JOIN_COMPONANT_SEPARATOR); - return version; + Version newVersion = Versions.increments(this); + return newVersion; } @@ -242,33 +249,12 @@ * * @param componantSeperator the componant separator to use the last componant is a classifier * @return the incremented version + * @deprecated use instead {@link Versions#increments(Version, char)} */ + @Deprecated public Version increments(char componantSeperator) { - Version newVersion; - - VersionComponant lastComponant = getLastComponant(); - - if (lastComponant instanceof StringVersionComponant) { - - // must then add new number componant with value 1 - - newVersion = VersionBuilder - .create(this) - .addComponant(1, componantSeperator) - .build(); - - } else { - - // increments it - int numberComponant = ((NumberVersionComponant) lastComponant).getValue(); - - newVersion = VersionBuilder - .create(this) - .setComponant(getComponantCount() - 1, numberComponant + 1) - .build(); - } - + Version newVersion = Versions.increments(this, componantSeperator); return newVersion; } @@ -281,16 +267,12 @@ * * @param componantPosition position of the version componant to increment * @return the incremented version + * @deprecated use instead {@link Versions#increments(Version, int)} */ + @Deprecated public Version increments(int componantPosition) { - int numberComponant = getNumberComponant(componantPosition); - - Version newVersion = VersionBuilder - .create(this) - .setComponant(componantPosition, numberComponant + 1) - .build(); - + Version newVersion = Versions.increments(this, componantPosition); return newVersion; } @@ -303,16 +285,12 @@ * * @param componantPosition position of the version componant to increment * @return the decremented version + * @deprecated use {@link Versions#decrements(Version, int)} instead */ + @Deprecated public Version decrements(int componantPosition) { - int numberComponant = getNumberComponant(componantPosition); - Preconditions.checkArgument(componantPosition > 0, "Componant at position " + componantPosition + " values 0, can't decrement it."); - Version newVersion = VersionBuilder - .create(this) - .setComponant(componantPosition, numberComponant - 1) - .build(); - + Version newVersion = Versions.decrements(this, componantPosition); return newVersion; } @@ -362,13 +340,6 @@ return result; } - protected VersionComponant getComponant(int level) { - - Preconditions.checkArgument(level > 0 || level < getComponantCount(), "not a valid level " + level + " for the Version " + this); - return componants.get(level); - - } - protected VersionComponant getLastComponant() { return componants.get(getComponantCount() - 1); @@ -426,12 +397,13 @@ protected final boolean preRelease; protected final String value; + protected final String lowerCaseValue; public StringVersionComponant(boolean preRelease, String value) { this.preRelease = preRelease; this.value = value; - this.lowerCaseValue= value.toLowerCase(); + this.lowerCaseValue = value.toLowerCase(); } @Override Modified: trunk/src/main/java/org/nuiton/util/version/Versions.java =================================================================== --- trunk/src/main/java/org/nuiton/util/version/Versions.java 2014-08-26 09:27:46 UTC (rev 2680) +++ trunk/src/main/java/org/nuiton/util/version/Versions.java 2014-08-26 17:32:49 UTC (rev 2681) @@ -1,5 +1,7 @@ package org.nuiton.util.version; +import com.google.common.base.Preconditions; + import java.util.ArrayList; import java.util.List; @@ -126,8 +128,8 @@ /** * Create a new version containing a single component from a given version. - * - * @param version original version + * + * @param version original version * @param component component index to extract * @return new {@link Version} with a single component */ @@ -138,10 +140,10 @@ /** * Create a new version containing a sub set of component from a given version. - * - * @param version original version + * + * @param version original version * @param firstComponent first component index - * @param lastComponent last component index + * @param lastComponent last component index * @return new {@link Version} with a components sub set */ public static Version extractVersion(Version version, int firstComponent, int lastComponent) { @@ -155,8 +157,118 @@ Comparable component = version.getComponant(index).getValue(); componants.add(component); } - + Version result = VersionBuilder.create().setComponants(componants).build(); return result; } + + /** + * Creates a new version from this one incremented. + * <p/> + * If the last componant is a number, then just increments this number; otherwise add a new + * number componant with value 1. + * <p/> + * Example: + * <ul> + * <li>1 -> 2</li> + * <li>1-a -> 1-a.1</li> + * </ul> + * + * @return the incremented version + */ + public static Version increments(Version version) { + + Version result = increments(version, Version.DEFAULT_JOIN_COMPONANT_SEPARATOR); + return result; + + } + + /** + * Creates a new version from this one incremented. + * <p/> + * If the last componant is a number, then just increments this number; otherwise add a new + * number componant with value 1. + * <p/> + * Example: + * <ul> + * <li>1 -> 2</li> + * <li>1-a -> 1-a.1</li> + * </ul> + * + * @param componantSeperator the componant separator to use the last componant is a classifier + * @return the incremented version + */ + public static Version increments(Version version, char componantSeperator) { + + Version newVersion; + + Version.VersionComponant lastComponant = version.getLastComponant(); + + if (lastComponant instanceof Version.StringVersionComponant) { + + // must then add new number componant with value 1 + + newVersion = VersionBuilder + .create(version) + .addComponant(1, componantSeperator) + .build(); + + } else { + + // increments it + int numberComponant = ((Version.NumberVersionComponant) lastComponant).getValue(); + + newVersion = VersionBuilder + .create(version) + .setComponant(version.getComponantCount() - 1, numberComponant + 1) + .build(); + } + + return newVersion; + + } + + /** + * Creates a new version from this one with the number componant incremented at the given position. + * <p/> + * <strong>Note:</strong> + * Will fail if the componant at the required position is not a number. + * + * @param componantPosition position of the version componant to increment + * @return the incremented version + */ + public static Version increments(Version version, int componantPosition) { + + int numberComponant = version.getNumberComponant(componantPosition); + + Version newVersion = VersionBuilder + .create(version) + .setComponant(componantPosition, numberComponant + 1) + .build(); + + return newVersion; + + } + + /** + * Creates a new version from this one with the number componant decremented at the given position. + * <p/> + * <strong>Note:</strong> + * Will fail if the componant at the required position is not a number, or his value is 0. + * + * @param componantPosition position of the version componant to increment + * @return the decremented version + */ + public static Version decrements(Version version, int componantPosition) { + + int numberComponant = version.getNumberComponant(componantPosition); + Preconditions.checkArgument(componantPosition > 0, "Componant at position " + componantPosition + " values 0, can't decrement it."); + Version newVersion = VersionBuilder + .create(version) + .setComponant(componantPosition, numberComponant - 1) + .build(); + + return newVersion; + + } } Modified: trunk/src/test/java/org/nuiton/util/version/VersionTest.java =================================================================== --- trunk/src/test/java/org/nuiton/util/version/VersionTest.java 2014-08-26 09:27:46 UTC (rev 2680) +++ trunk/src/test/java/org/nuiton/util/version/VersionTest.java 2014-08-26 17:32:49 UTC (rev 2681) @@ -38,66 +38,13 @@ public void testEquals() { // Ignore case on string componants - assertEquals(VersionBuilder.create("1.1-rc").build(), VersionBuilder.create("1.1-Rc").build()); + assertEquals(Versions.valueOf("1.1-rc"), Versions.valueOf("1.1-Rc")); // Ignore componants join separator - assertEquals(VersionBuilder.create("1.1").build(), VersionBuilder.create(Lists.<Comparable>newArrayList(1, 1)).setJoinSeparator(' ').build()); + assertEquals(Versions.valueOf("1.1"), VersionBuilder.create(Lists.<Comparable>newArrayList(1, 1)).setJoinSeparator(' ').build()); } - @Test - public void testIncrements() { - - Version[] versions = new Version[]{ - VersionBuilder.create().setSnapshot(true).build(), - VersionBuilder.create().build(), - VersionBuilder.create("1").build(), - VersionBuilder.create("1.1-alpha-1").build(), - VersionBuilder.create("1.1-alpha-2").build(), - VersionBuilder.create("1.1-beta-1").build(), - VersionBuilder.create("1.1-rc-1-SNAPSHOT").build(), - VersionBuilder.create("1.1-rc-1").build(), - VersionBuilder.create("1.1-rc2").build(), - VersionBuilder.create("1.1").build(), - VersionBuilder.create("1.1.1").build(), - VersionBuilder.create("1.1.1-blablah").build(), - VersionBuilder.create("1.1.1-blablah1-SNAPSHOT").build(), - VersionBuilder.create("1.1.1-blablah1").build(), - VersionBuilder.create("1.1.1-blablah1a").build(), - VersionBuilder.create("1.1.1-blablah1b").build() - }; - - Version[] versionsIncrements = new Version[]{ - VersionBuilder.create("1-SNAPSHOT").build(), - VersionBuilder.create("1").build(), - VersionBuilder.create("2").build(), - VersionBuilder.create("1.1-alpha-2").build(), - VersionBuilder.create("1.1-alpha-3").build(), - VersionBuilder.create("1.1-beta-2").build(), - VersionBuilder.create("1.1-rc-2-SNAPSHOT").build(), - VersionBuilder.create("1.1-rc-2").build(), - VersionBuilder.create("1.1-rc3").build(), - VersionBuilder.create("1.2").build(), - VersionBuilder.create("1.1.2").build(), - VersionBuilder.create("1.1.1-blablah.1").build(), - VersionBuilder.create("1.1.1-blablah2-SNAPSHOT").build(), - VersionBuilder.create("1.1.1-blablah2").build(), - VersionBuilder.create("1.1.1-blablah1a.1").build(), - VersionBuilder.create("1.1.1-blablah1b.1").build() - }; - - for (int i = 0, l = versions.length; i < l; i++) { - - Version v = versions[i]; - Version vIncrements = versionsIncrements[i]; - Version increments = v.increments(); - Assert.assertTrue(v + " + 1 = " + increments + " should be " + vIncrements, increments.equals(vIncrements)); - Assert.assertTrue(v + " + 1 = " + increments + " should be " + vIncrements, increments.getVersion().equals(vIncrements.getVersion())); - - } - - } - protected void buildVersion(Version version, String stringRepresentation, int nbComponants, Modified: trunk/src/test/java/org/nuiton/util/version/VersionsTest.java =================================================================== --- trunk/src/test/java/org/nuiton/util/version/VersionsTest.java 2014-08-26 09:27:46 UTC (rev 2680) +++ trunk/src/test/java/org/nuiton/util/version/VersionsTest.java 2014-08-26 17:32:49 UTC (rev 2681) @@ -60,4 +60,57 @@ Version initVersion = Versions.valueOf("10.20.30.40"); Version subVersion = Versions.extractVersion(initVersion, 3, 2); } + + @Test + public void testIncrements() { + + Version[] versions = new Version[]{ + VersionBuilder.create().setSnapshot(true).build(), + VersionBuilder.create().build(), + Versions.valueOf("1"), + Versions.valueOf("1.1-alpha-1"), + Versions.valueOf("1.1-alpha-2"), + Versions.valueOf("1.1-beta-1"), + Versions.valueOf("1.1-rc-1-SNAPSHOT"), + Versions.valueOf("1.1-rc-1"), + Versions.valueOf("1.1-rc2"), + Versions.valueOf("1.1"), + Versions.valueOf("1.1.1"), + Versions.valueOf("1.1.1-blablah"), + Versions.valueOf("1.1.1-blablah1-SNAPSHOT"), + Versions.valueOf("1.1.1-blablah1"), + Versions.valueOf("1.1.1-blablah1a"), + Versions.valueOf("1.1.1-blablah1b") + }; + + Version[] versionsIncrements = new Version[]{ + Versions.valueOf("1-SNAPSHOT"), + Versions.valueOf("1"), + Versions.valueOf("2"), + Versions.valueOf("1.1-alpha-2"), + Versions.valueOf("1.1-alpha-3"), + Versions.valueOf("1.1-beta-2"), + Versions.valueOf("1.1-rc-2-SNAPSHOT"), + Versions.valueOf("1.1-rc-2"), + Versions.valueOf("1.1-rc3"), + Versions.valueOf("1.2"), + Versions.valueOf("1.1.2"), + Versions.valueOf("1.1.1-blablah.1"), + Versions.valueOf("1.1.1-blablah2-SNAPSHOT"), + Versions.valueOf("1.1.1-blablah2"), + Versions.valueOf("1.1.1-blablah1a.1"), + Versions.valueOf("1.1.1-blablah1b.1") + }; + + for (int i = 0, l = versions.length; i < l; i++) { + + Version v = versions[i]; + Version vIncrements = versionsIncrements[i]; + Version increments = Versions.increments(v); + Assert.assertTrue(v + " + 1 = " + increments + " should be " + vIncrements, increments.equals(vIncrements)); + Assert.assertTrue(v + " + 1 = " + increments + " should be " + vIncrements, increments.getVersion().equals(vIncrements.getVersion())); + + } + + } }