[Arakhnę-Dev] [212] * Ensure that the iterators used by ClasSPathUtil do not reply null values .

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


Revision: 212
Author:   galland
Date:     2011-02-28 11:17:28 +0100 (Mon, 28 Feb 2011)
Log Message:
-----------
* Ensure that the iterators used by ClasSPathUtil do not reply null values.

Modified Paths:
--------------
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ClasspathUtil.java
    trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ClasspathUtilTest.java

Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ClasspathUtil.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ClasspathUtil.java	2011-02-28 08:55:08 UTC (rev 211)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ClasspathUtil.java	2011-02-28 10:17:28 UTC (rev 212)
@@ -95,13 +95,13 @@
 		if (loader instanceof DynamicURLClassLoader) {
 			DynamicURLClassLoader dLoader = (DynamicURLClassLoader)loader;
 			iterator = new IteratorIterator(
-					Arrays.asList(dLoader.getURLs()).iterator(),
+					new FilteringIterator(Arrays.asList(dLoader.getURLs()).iterator()),
 					iterator);
 		}
 		else if (loader instanceof URLClassLoader) {
 			URLClassLoader dLoader = (URLClassLoader)loader;
 			iterator = new IteratorIterator(
-					Arrays.asList(dLoader.getURLs()).iterator(),
+					new FilteringIterator(Arrays.asList(dLoader.getURLs()).iterator()),
 					iterator);
 		}
 		
@@ -165,10 +165,71 @@
 	 * @mavenartifactid $ArtifactId$
 	 * @since 6.0
 	 */
+	private static class FilteringIterator implements Iterator<URL> {
+		
+		private final Iterator<URL> iterator;
+		private URL next;
+		
+		/**
+		 * @param iterator
+		 */
+		public FilteringIterator(Iterator<URL> iterator) {
+			assert(iterator!=null);
+			this.iterator = iterator;
+			searchNext();
+		}
+		
+		private void searchNext() {
+			this.next = null;
+			URL u;
+			while (this.next==null && this.iterator.hasNext()) {
+				u = this.iterator.next();
+				if (u!=null) {
+					this.next = u;
+				}
+			}
+		}
+
+		/**
+		 * {@inheritDoc}
+		 */
+		@Override
+		public boolean hasNext() {
+			return this.next!=null;
+		}
+
+		/**
+		 * {@inheritDoc}
+		 */
+		@Override
+		public URL next() {
+			URL n = this.next;
+			if (n==null) throw new NoSuchElementException();
+			searchNext();
+			return n;
+		}
+
+		/**
+		 * {@inheritDoc}
+		 */
+		@Override
+		public void remove() {
+			throw new UnsupportedOperationException();
+		}
+		
+	}
+
+	/**
+	 * @author St&eacute;phane GALLAND &lt;galland@xxxxxxxxxxx&gt;
+	 * @version $Name$ $Revision$ $Date$
+	 * @mavengroupid $GroupId$
+	 * @mavenartifactid $ArtifactId$
+	 * @since 6.0
+	 */
 	private static class PathIterator implements Iterator<URL> {
 		
-		private final String path;
-		private String nextPath;
+		private String path;
+		private URL next;
 		private int nextIndex;
 		
 		/**
@@ -176,19 +237,53 @@
 		 */
 		public PathIterator(String path) {
 			this.path = path;
-			this.nextIndex = path.indexOf(File.pathSeparatorChar);
-			if (this.nextIndex>=0)
-				this.nextPath = path.substring(0, this.nextIndex);
-			else
-				this.nextPath = path;
+			this.nextIndex = -1;
+			searchNext();
 		}
+		
+		private void searchNext() {
+			String p;
+			int index;
+			URL url;
+			
+			this.next = null;
+			
+			while (this.next==null && this.path!=null && this.nextIndex<this.path.length()) {
+				index = this.path.indexOf(File.pathSeparatorChar, this.nextIndex + 1);
+				
+				if (index>this.nextIndex+1) {
+					p = this.path.substring(this.nextIndex+1, index);
+				}
+				else {
+					p = this.path.substring(this.nextIndex+1);
+					this.path = null; // no more element
+				}
 
+				this.nextIndex = index;
+
+				if (p!=null && !"".equals(p)) { //$NON-NLS-1$
+					try {
+						url = FileSystem.convertStringToURL(p, false, true, false);
+						if (url!=null) {
+							this.next = url;
+						}
+					}
+					catch(AssertionError e) {
+						throw e;
+					}
+					catch(Throwable e) {
+						//
+					}
+				}
+			}
+		}
+
 		/**
 		 * {@inheritDoc}
 		 */
 		@Override
 		public boolean hasNext() {
-			return this.nextPath!=null;
+			return this.next!=null;
 		}
 
 		/**
@@ -196,22 +291,10 @@
 		 */
 		@Override
 		public URL next() {
-			String p = this.nextPath;
-			if (p==null) throw new NoSuchElementException();
-			if (this.nextIndex>=0) {
-				int idx = this.path.indexOf(File.pathSeparatorChar, this.nextIndex+1);
-				if (idx<0) {
-					this.nextPath = this.path.substring(this.nextIndex+1);
-				}
-				else {
-					this.nextPath = this.path.substring(this.nextIndex+1, idx);
-				}
-				this.nextIndex = idx;
-			}
-			else {
-				this.nextPath = null;
-			}
-			return FileSystem.convertStringToURL(p, false, true, false);
+			URL n = this.next;
+			if (n==null) throw new NoSuchElementException();
+			searchNext();
+			return n;
 		}
 
 		/**

Modified: trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ClasspathUtilTest.java
===================================================================
--- trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ClasspathUtilTest.java	2011-02-28 08:55:08 UTC (rev 211)
+++ trunk/arakhneVmutils/java/src/test/java/org/arakhne/vmutil/ClasspathUtilTest.java	2011-02-28 10:17:28 UTC (rev 212)
@@ -86,8 +86,7 @@
 	}
 	
 	private void assertUrl(List<String> expected, URL actual) {
-		assertNotNull(actual);
-		assertNotNull(expected);
+		assertNotNull("An url cannot be null", actual); //$NON-NLS-1$
 		Iterator<String> iterator = expected.iterator();
 		String u;
 		if (iterator.hasNext()) {


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