Selaa lähdekoodia

IntHashMapTest: warnings
ExceptionInspector: refined API

Ranides Atterwim 10 vuotta sitten
vanhempi
commit
c5834b1672

+ 34 - 41
assira/src/main/java/net/ranides/assira/trace/ExceptionInspector.java

@@ -10,10 +10,13 @@ package net.ranides.assira.trace;
 import java.io.PrintStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
-import java.util.ArrayList;
 import java.util.List;
-import java.util.function.Function;
-import net.ranides.assira.collection.CollectionUtils;
+import static java.util.Spliterator.IMMUTABLE;
+import java.util.Spliterators;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
 
 /**
  *
@@ -23,35 +26,15 @@ import net.ranides.assira.collection.CollectionUtils;
 public final class ExceptionInspector {
 
     private ExceptionInspector() { }
-
-    public static List<Throwable> getCauseList(Throwable cause) {
-        List<Throwable> result = new ArrayList<>();
-        for(Throwable i=cause; null!=i; i=i.getCause() ) {
-            result.add(i);
-        }
-        return result;
-    }
-
-    public static List<String> getMessageList(Throwable cause) {
-        return map(cause, (item) -> item.getMessage());
-    }
-
-    public static List<String> getLocalizedMessageList(Throwable cause) {
-        return map(cause, (item) -> item.getLocalizedMessage());
-    }
-
-    public static List<Class<?>> getClassList(Throwable cause) {
-        return map(cause, (item) -> item.getClass());
-    }
-
-    public static List<String> getClassNameList(Throwable cause) {
-        return map(cause, (item) -> item.getClass().getSimpleName());
+	
+	public static Stream<Throwable> getCauseStream(Throwable cause) {
+		return StreamSupport.stream(new CauseSpliterator(cause), false);
+	}
+	
+	public static List<Throwable> getCauseList(Throwable cause) {
+		return getCauseStream(cause).collect(Collectors.toList());
     }
 
-    public static List<String> getFilesList(Throwable cause) {
-        return CollectionUtils.map(cause.getStackTrace(), (item) -> String.format("%4d : %s", item.getLineNumber(), item.getFileName()));
-    }
-    
     public static String getStackTrace(Throwable cause) {
         StringWriter result = new StringWriter();
         PrintWriter writer = new PrintWriter(result);
@@ -75,13 +58,7 @@ public final class ExceptionInspector {
         }
 
     }
-    
-    public static AssertionError assertError(Throwable cause, String message, Object args) {
-        AssertionError error = new AssertionError(String.format(message, args));
-        error.initCause(cause);
-        return error;
-    }
-	
+
 	public static void print(PrintStream stream, List<? extends Throwable> list) {
 		if(list.isEmpty()) {
 			return;
@@ -113,9 +90,25 @@ public final class ExceptionInspector {
 			list.get(i).printStackTrace(writer);
 		}
     }
-    
-    private static <T> List<T> map(Throwable cause, Function<Throwable, T> function) {
-        return CollectionUtils.map(getCauseList(cause), function);
-    }
+	
+	private static final class CauseSpliterator extends Spliterators.AbstractSpliterator<Throwable> {
+		
+		private Throwable current;
+		
+		public CauseSpliterator(Throwable current) {
+			super(Long.MAX_VALUE, IMMUTABLE);
+			this.current = current;
+		}
+
+		@Override
+		public boolean tryAdvance(Consumer<? super Throwable> action) {
+			if(null == (current = current.getCause())) {
+				return false;
+			}
+			action.accept(current);
+			return true;	
+		}
+
+	}
     
 }

+ 79 - 79
assira/src/test/java/net/ranides/assira/collection/maps/IntHashMapTest.java

@@ -1,79 +1,79 @@
-/*
- * @author Ranides Atterwim <ranides@gmail.com>
- * @copyright Ranides Atterwim
- * @license WTFPL
- * @url http://ranides.net/projects/???
- */
-package net.ranides.assira.collection.maps;
-
-import java.util.Map;
-import net.ranides.assira.collection.mockup.TMaps;
-import net.ranides.assira.collection.mockup.CollectionSuite;
-import net.ranides.assira.junit.QAssert;
-import net.ranides.assira.test.TMap;
-import net.ranides.assira.test.TMap.TItems;
-import org.junit.Test;
-import static org.junit.Assert.*;
-
-/**
- *
- * @author Ranides Atterwim <ranides@gmail.com>
- */
-public class IntHashMapTest {
-    
-    private final TMap<Integer, String> $map = TMaps.MAP_IS;
-    
-    @Test
-    public void testSuite() {
-        CollectionSuite.SUITE
-            .param("map!", $map)
-            .run(() -> new IntHashMap<>(32, 0.75f));
-    }
-    
-    @Test
-    public void testConstruct() {
-        assertNotNull( new IntHashMap<>(32, 0.75f) );
-        assertNotNull( new IntHashMap<>(32) );
-        assertNotNull( new IntHashMap<>() );
-
-        QAssert.assertThrows(IllegalArgumentException.class, () -> 
-            new IntHashMap<>(32, 0.0f)
-        );
-        QAssert.assertThrows(IllegalArgumentException.class, () -> 
-            new IntHashMap<>(32, -1.0f)
-        );
-        QAssert.assertThrows(IllegalArgumentException.class, () -> 
-            new IntHashMap<>(32, 1.1f)
-        );
-        QAssert.assertThrows(IllegalArgumentException.class, () -> 
-            new IntHashMap<>(32, 2.0f)
-        );
-        QAssert.assertThrows(IllegalArgumentException.class, () -> 
-            new IntHashMap<>(-1, 0.5f)
-        );
-    }
-    
-    @Test
-    public void testConstructCopy() {
-        TItems<Integer, String> items = $map.range(60).item(2).item(3);
-        
-        Map<Integer, String> imap = items.into(new java.util.HashMap<>());
-        HashMap<Integer, String> smap = new HashMap<>(imap);
-        
-        assertEquals(60, new IntHashMap<>(imap, 0.75f).size());
-        assertEquals(60, new IntHashMap<>(imap).size());
-        
-        assertEquals(60, new IntHashMap<>(smap, 0.75f).size());
-        assertEquals(60, new IntHashMap<>(smap).size());
-        
-        assertEquals(60, new IntHashMap<>(new IntHashMap(smap), 0.75f).size());
-        assertEquals(60, new IntHashMap<>(new IntHashMap(smap)).size());
-
-        assertEquals(60, new IntHashMap<>(items.keysInt(), items.values()).size());
-        
-        QAssert.assertThrows(IllegalArgumentException.class, () -> 
-            new IntHashMap<>($map.range(60).keysInt(), $map.range(40).values())
-        );
-    }
-    
-}
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/???
+ */
+package net.ranides.assira.collection.maps;
+
+import java.util.Map;
+import net.ranides.assira.collection.mockup.TMaps;
+import net.ranides.assira.collection.mockup.CollectionSuite;
+import net.ranides.assira.junit.QAssert;
+import net.ranides.assira.test.TMap;
+import net.ranides.assira.test.TMap.TItems;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class IntHashMapTest {
+    
+    private final TMap<Integer, String> $map = TMaps.MAP_IS;
+    
+    @Test
+    public void testSuite() {
+        CollectionSuite.SUITE
+            .param("map!", $map)
+            .run(() -> new IntHashMap<>(32, 0.75f));
+    }
+    
+    @Test
+    public void testConstruct() {
+        assertNotNull( new IntHashMap<>(32, 0.75f) );
+        assertNotNull( new IntHashMap<>(32) );
+        assertNotNull( new IntHashMap<>() );
+
+        QAssert.assertThrows(IllegalArgumentException.class, () -> 
+            new IntHashMap<>(32, 0.0f)
+        );
+        QAssert.assertThrows(IllegalArgumentException.class, () -> 
+            new IntHashMap<>(32, -1.0f)
+        );
+        QAssert.assertThrows(IllegalArgumentException.class, () -> 
+            new IntHashMap<>(32, 1.1f)
+        );
+        QAssert.assertThrows(IllegalArgumentException.class, () -> 
+            new IntHashMap<>(32, 2.0f)
+        );
+        QAssert.assertThrows(IllegalArgumentException.class, () -> 
+            new IntHashMap<>(-1, 0.5f)
+        );
+    }
+    
+    @Test
+    public void testConstructCopy() {
+        TItems<Integer, String> items = $map.range(60).item(2).item(3);
+        
+        Map<Integer, String> imap = items.into(new java.util.HashMap<>());
+        HashMap<Integer, String> smap = new HashMap<>(imap);
+        
+        assertEquals(60, new IntHashMap<>(imap, 0.75f).size());
+        assertEquals(60, new IntHashMap<>(imap).size());
+        
+        assertEquals(60, new IntHashMap<>(smap, 0.75f).size());
+        assertEquals(60, new IntHashMap<>(smap).size());
+        
+        assertEquals(60, new IntHashMap<>(new IntHashMap<>(smap), 0.75f).size());
+        assertEquals(60, new IntHashMap<>(new IntHashMap<>(smap)).size());
+
+        assertEquals(60, new IntHashMap<>(items.keysInt(), items.values()).size());
+        
+        QAssert.assertThrows(IllegalArgumentException.class, () -> 
+            new IntHashMap<>($map.range(60).keysInt(), $map.range(40).values())
+        );
+    }
+    
+}