Ver Fonte

new: FetchIterator
new: StringUtils: new line transformation

Mariusz Sieroń há 6 anos atrás
pai
commit
9e49340540

+ 39 - 0
assira/src/main/java/net/ranides/assira/collection/iterators/FetchIterator.java

@@ -0,0 +1,39 @@
+package net.ranides.assira.collection.iterators;
+
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+public abstract class FetchIterator<T> implements Iterator<T> {
+
+    private Iterator<T> iterator = Collections.emptyIterator();
+
+    private boolean stop;
+
+    public abstract List<T> fetch();
+
+    @Override
+    public boolean hasNext() {
+        if(stop) {
+            return false;
+        }
+        if(!iterator.hasNext()) {
+            iterator = fetch().iterator();
+        }
+        if(!iterator.hasNext()) {
+            stop = true;
+            return false;
+        }
+        return true;
+    }
+
+    @Override
+    public T next() {
+        if(!hasNext()) {
+            throw new NoSuchElementException();
+        }
+        return iterator.next();
+    }
+
+}

+ 23 - 0
assira/src/main/java/net/ranides/assira/text/StringUtils.java

@@ -8,6 +8,8 @@
 package net.ranides.assira.text;
 
 import java.io.IOException;
+import java.io.StringReader;
+import java.io.UncheckedIOException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -943,6 +945,27 @@ public final class StringUtils {
     public static String escapeRE(String text) {
         return REGEX_SPECIAL.matcher(text).replaceAll("\\\\$1");
     }
+
+    public static List<String> lines(String text) {
+        try {
+            return IOStrings.readLines(new StringReader(text));
+        } catch (IOException e) {
+            // it shouldn't happpen
+            throw new UncheckedIOException(e);
+        }
+    }
+
+    public static String asUnix(String text) {
+        return replace(replaceEndl(text), '\037', '\n');
+    }
+
+    public static String asWindows(String text) {
+        return replace(replaceEndl(text), '\037', "\r\n");
+    }
+
+    private static String replaceEndl(String text) {
+        return text.replaceAll("\r\n", "\037").replace('\n', '\037').replace('\r', '\037');
+    }
     
     static void checkIndex(int size, int index) {
         if( index<0 || index >= size ) {

+ 8 - 0
assira/src/test/java/net/ranides/assira/text/StringUtilsTest.java

@@ -613,6 +613,14 @@ c*/
         assertLimitEquals("Hello", StringUtils.limit("Hello", 10));
     }
 
+    @Test
+    public void asUnix() {
+        assertEquals("hello\nworld\nhere\n", StringUtils.asUnix("hello\nworld\nhere\n"));
+        assertEquals("hello\nworld\nhere\n", StringUtils.asUnix("hello\r\nworld\nhere\r\n"));
+        assertEquals("hello\nworld\n\nhere\n", StringUtils.asUnix("hello\r\nworld\n\rhere\r\n"));
+        assertEquals("hello\nworld\nhere\n", StringUtils.asUnix("hello\r\nworld\rhere\n"));
+    }
+
     private static void assertLimitEquals(String expected, CharSequence value) {
 	    assertEquals(expected, value.toString());
 	    assertEquals(expected.length(), value.length());