[Arakhnę-Dev] [183] * Add functions addExtension() in FileSystem class.

[ Thread Index | Date Index | More arakhne.org/dev Archives ]


Revision: 183
Author:   galland
Date:     2010-09-15 19:41:23 +0200 (Wed, 15 Sep 2010)
Log Message:
-----------
* Add functions addExtension() in FileSystem class.

Modified Paths:
--------------
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java
    trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java

Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2010-09-09 18:39:30 UTC (rev 182)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/FileSystem.java	2010-09-15 17:41:23 UTC (rev 183)
@@ -945,6 +945,40 @@
 		}
 	}
 
+	/** Add the extension of to specified filename.
+	 * If the filename has already the given extension, the filename is not changed.
+	 * If the filename has no extension or an other extension, the specified one is added.
+	 * 
+	 * @param filename is the filename to parse.
+	 * @param extension is the extension to remove if it is existing.
+	 * @return the filename with the extension.
+	 * @since 6.0
+	 */
+	public static File addExtension(File filename, String extension) {
+		if (filename!=null && !hasExtension(filename, extension)) {
+			return new File(filename.getParentFile(), filename.getName()+extension);
+		}
+		return filename;
+	}
+
+	/** Add the extension of to specified filename.
+	 * If the filename has already the given extension, the filename is not changed.
+	 * If the filename has no extension or an other extension, the specified one is added.
+	 * 
+	 * @param filename is the filename to parse.
+	 * @param extension is the extension to remove if it is existing.
+	 * @return the filename with the extension.
+	 * @since 6.0
+	 */
+	public static URL addExtension(URL filename, String extension) {
+		if (filename!=null && !hasExtension(filename, extension)) {
+			String basename = largeBasename(filename);
+			URL dirname = dirname(filename);
+			return join(dirname, basename+extension);
+		}
+		return filename;
+	}
+
 	/** Copy the first file into the second file.
 	 * <p>
 	 * The content of the second file will be lost.

Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java	2010-09-09 18:39:30 UTC (rev 182)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/FileSystemTest.java	2010-09-15 17:41:23 UTC (rev 183)
@@ -539,8 +539,28 @@
 	}
 
 	/**
+	 */
+	public void testAddExtensionFileString() {
+		assertEquals(new File("/home/test.x.z.z"), FileSystem.addExtension(f1, ".z")); //$NON-NLS-1$ //$NON-NLS-2$
+		assertEquals(new File("/home/test.x.z.z.toto"), FileSystem.addExtension(f1, ".toto")); //$NON-NLS-1$ //$NON-NLS-2$
+		assertEquals(new File("/home.toto"), FileSystem.addExtension(f2, ".toto")); //$NON-NLS-1$ //$NON-NLS-2$
+	}
+
+	/**
 	 * @throws Exception
 	 */
+	public void testAddExtensionURLString() throws Exception {
+		assertEquals(new File("/home/test.x.z.z").toURI().toURL(), //$NON-NLS-1$ 
+				FileSystem.addExtension(u1, ".z")); //$NON-NLS-1$
+		assertEquals(new File("/home/test.x.z.z.toto").toURI().toURL(), //$NON-NLS-1$ 
+				FileSystem.addExtension(u1, ".toto")); //$NON-NLS-1$
+		assertEquals(new File("/home.toto").toURI().toURL(), //$NON-NLS-1$ 
+				FileSystem.addExtension(u2, ".toto")); //$NON-NLS-1$
+	}
+
+	/**
+	 * @throws Exception
+	 */
 	public void testConvertStringToUrl() throws Exception {
 		URL rr;
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/