Skip to content

Commit

Permalink
Adding processIOItems()
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-brandizi committed Dec 12, 2024
1 parent 1f52b65 commit 389f0be
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/main/java/uk/ac/ebi/utils/opt/io/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.stream.Stream;

import org.apache.commons.io.input.ReaderInputStream;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -89,6 +92,7 @@
* Miscellanea of small IO utilities.
*
* TODO: complete the migration to unchecked exceptions.
* TODO: does it need to be an *.opt.* package?
*
* <dl><dt>date</dt><dd>July 29, 2007, 1:03 PM</dd></dl>
* @author brandizi
Expand Down Expand Up @@ -219,6 +223,39 @@ public static String[] readFiles ( String dirPath ) throws UncheckedIOException
}


/**
* Returns a new stream assuming that the input comes from some I/O describing one
* item per line. Currently,
*
* <ul>
* <li>if doTrim, {@link StringUtils#trimToNull(String) trims to null} and then passes the result
* downstream</li>
* <li>it removes blank lines (empty and space-like only lines)</li>
* <li>it removes lines starting with '#', ie, comments (spaces before are allowed)</li>
* </ul>
*
* TODO: write unit tests (it's being used in KnetMiner)
*/
public static Stream<String> processIOItems ( Stream<String> ioItemsStrm, boolean doTrim )
{
Validate.notNull ( ioItemsStrm, "processIOItems(), can't process a null stream" );

if ( doTrim ) ioItemsStrm = ioItemsStrm.map ( StringUtils::trimToNull );

return ioItemsStrm.filter ( p -> p != null )
.filter ( p -> !StringUtils.isWhitespace ( p ) )
.filter ( p -> !p.trim ().startsWith ( "#" ) );
}

/**
* Defaults to ioItemsStrm = true
*/
public static Stream<String> processIOItems ( Stream<String> ioItemsStrm )
{
return processIOItems ( ioItemsStrm, true );
}


/**
* Facility to get a reader from a resource, uses {@link Resources#getResource(Class, String)} and
* {@link URL#openStream()}.
Expand Down Expand Up @@ -294,7 +331,7 @@ public static String readResource ( String path ) throws IOException

/**
* Facility to read a resource from a class loader.
* @see ClassLoader#getResource(String).
* @see ClassLoader#getResource(String)
*
*/
public static String readResource ( ClassLoader classLoader, String path, Charset charset ) throws IOException
Expand Down Expand Up @@ -377,8 +414,12 @@ public static String getHash ( File f, String algorithm ) throws IOException, No
public static String getHash ( String string, String algorithm ) throws NoSuchAlgorithmException
{
try {
return getHash ( new ReaderInputStream ( new StringReader ( string ), "UTF-8" ), algorithm
);
var in = ReaderInputStream.builder ()
.setReader ( new StringReader ( string ) )
.setCharset ( "UTF-8" )
.get ();

return getHash ( in, algorithm );
}
catch ( IOException ex ) {
throw new IllegalArgumentException ( "Internal error: " + ex.getMessage (), ex );
Expand Down

0 comments on commit 389f0be

Please sign in to comment.