r2679 - in trunk/src: main/java/org/nuiton/util/version test/java/org/nuiton/util/version
Author: echatellier Date: 2014-08-26 11:24:42 +0200 (Tue, 26 Aug 2014) New Revision: 2679 Url: http://forge.nuiton.org/projects/nuiton-utils/repository/revisions/2679 Log: fixes #3477: Add Versions#extractVersion(Version, int, int) Added: trunk/src/test/java/org/nuiton/util/version/VersionsTest.java Modified: trunk/src/main/java/org/nuiton/util/version/Versions.java Modified: trunk/src/main/java/org/nuiton/util/version/Versions.java =================================================================== --- trunk/src/main/java/org/nuiton/util/version/Versions.java 2014-08-25 12:25:59 UTC (rev 2678) +++ trunk/src/main/java/org/nuiton/util/version/Versions.java 2014-08-26 09:24:42 UTC (rev 2679) @@ -1,5 +1,8 @@ package org.nuiton.util.version; +import java.util.ArrayList; +import java.util.List; + /* * #%L * Nuiton Utils @@ -120,4 +123,40 @@ Version result = VersionBuilder.create(version).setSnapshot(false).build(); return result; } + + /** + * Create a new version containing a single component from a given version. + * + * @param version original version + * @param component component index to extract + * @return new {@link Version} with a single component + */ + public static Version extractVersion(Version version, int component) { + Version result = extractVersion(version, component, component); + return result; + } + + /** + * Create a new version containing a sub set of component from a given version. + * + * @param version original version + * @param firstComponent first 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) { + if (lastComponent < firstComponent) { + throw new IllegalArgumentException("lastComponent must be greater or equals to firstComponent"); + } + + // extract components + List<Comparable> componants = new ArrayList<Comparable>(); + for (int index = firstComponent; index <= lastComponent; index++) { + Comparable component = version.getComponant(index).getValue(); + componants.add(component); + } + + Version result = VersionBuilder.create().setComponants(componants).build(); + return result; + } } Added: trunk/src/test/java/org/nuiton/util/version/VersionsTest.java =================================================================== --- trunk/src/test/java/org/nuiton/util/version/VersionsTest.java (rev 0) +++ trunk/src/test/java/org/nuiton/util/version/VersionsTest.java 2014-08-26 09:24:42 UTC (rev 2679) @@ -0,0 +1,63 @@ +package org.nuiton.util.version; + +import org.junit.Assert; +import org.junit.Test; + +/* + * #%L + * Nuiton Utils + * %% + * Copyright (C) 2014 CodeLutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +/** + * Test about {@link Versions} utility class. + * + * @author Eric Chatellier + */ +public class VersionsTest { + + /** + * Test to create a new version by extracting a single component. + */ + @Test + public void testExtractSingleComponent() { + Version initVersion = Versions.valueOf("10.20.30.40"); + Version subVersion = Versions.extractVersion(initVersion, 2); + Assert.assertEquals(Versions.valueOf("30"), subVersion); + } + + /** + * Test to create a new version by extracting a components sub set. + */ + @Test + public void testExtractTwoComponents() { + Version initVersion = Versions.valueOf("10.20.30.40"); + Version subVersion = Versions.extractVersion(initVersion, 2, 3); + Assert.assertEquals(Versions.valueOf("30.40"), subVersion); + } + + /** + * Test to create a new version by extracting with illegal parameters. + */ + @Test(expected=IllegalArgumentException.class) + public void testExtractIllegalParams() { + Version initVersion = Versions.valueOf("10.20.30.40"); + Version subVersion = Versions.extractVersion(initVersion, 3, 2); + } +} Property changes on: trunk/src/test/java/org/nuiton/util/version/VersionsTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL Added: svn:eol-style + native
participants (1)
-
echatellier@users.nuiton.org