Class ResourceAsLines

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class ResourceAsLines
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Wrapper for resource that may open that resource as stream of text lines.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      void forEachLine​(java.util.function.Consumer<java.lang.String> lineConsumer)
      Applies lineConsumer to each text line from resource.
      void forEachLineChecked​(ThrowingConsumer<java.lang.String> lineConsumer)
      Applies lineConsumer to each text line from resource.
      java.util.stream.Stream<java.lang.String> lines()
      Returns stream of text lines from wrapped resource.
      ResourceAsLines onFirstLine​(java.util.function.Consumer<java.lang.String> firstLineConsumer)
      Defines hook that is executed when first line of resource was read.
      ResourceAsLines onFirstLineChecked​(ThrowingConsumer<java.lang.String> firstLineConsumer)
      Defines hook that is executed when first line of resource was read.
      <T> java.util.stream.Stream<T> parseLines​(java.util.function.Function<java.lang.String,​T> parsingFunction)
      Applies parsingFunction to each text line from resource and returns the resulting stream.
      <T> java.util.stream.Stream<T> parseLinesChecked​(ThrowingFunction<java.lang.String,​T> parsingFunction)
      Applies parsingFunction to each text line from resource and returns the resulting stream.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • onFirstLine

        public ResourceAsLines onFirstLine​(java.util.function.Consumer<java.lang.String> firstLineConsumer)
        Defines hook that is executed when first line of resource was read. Useful for processing of headers.
        Parameters:
        firstLineConsumer - consumer of the first line from resource
        Returns:
        this ResourceAsLines wrapper
      • onFirstLineChecked

        public ResourceAsLines onFirstLineChecked​(ThrowingConsumer<java.lang.String> firstLineConsumer)
        Defines hook that is executed when first line of resource was read. Hook may throw an exception Useful for processing of headers.
        Parameters:
        firstLineConsumer - consumer of the first line from resource. May throw an exception
        Returns:
        this ResourceAsLines wrapper
      • lines

        public java.util.stream.Stream<java.lang.String> lines()
        Returns stream of text lines from wrapped resource.

        If firstLineConsumer is not null, the first line will be excluded from returned stream. First line will be processed by firstLineConsumer instead.

        Returns:
        steam of text lines from resource
      • parseLines

        public <T> java.util.stream.Stream<T> parseLines​(java.util.function.Function<java.lang.String,​T> parsingFunction)
        Applies parsingFunction to each text line from resource and returns the resulting stream.

        If firstLineConsumer is not null, the first line will be excluded from processed stream. First line will be processed by firstLineConsumer instead.

        Type Parameters:
        T - type of elements of result stream
        Parameters:
        parsingFunction - function that parses a single text line
        Returns:
        stream of results of parsingFunction
      • parseLinesChecked

        public <T> java.util.stream.Stream<T> parseLinesChecked​(ThrowingFunction<java.lang.String,​T> parsingFunction)
        Applies parsingFunction to each text line from resource and returns the resulting stream. parsingFunction may throw an exception.

        If firstLineConsumer is not null, the first line will be excluded from processed stream. First line will be processed by firstLineConsumer instead.

        Type Parameters:
        T - type of elements of result stream
        Parameters:
        parsingFunction - function that parses a single text line. May throw an exception
        Returns:
        stream of results of parsingFunction
      • forEachLine

        public void forEachLine​(java.util.function.Consumer<java.lang.String> lineConsumer)
        Applies lineConsumer to each text line from resource.

        If firstLineConsumer is not null, the first line will be excluded from processed stream. First line will be processed by firstLineConsumer instead.

        Parameters:
        lineConsumer - consumer of a single line
      • forEachLineChecked

        public void forEachLineChecked​(ThrowingConsumer<java.lang.String> lineConsumer)
        Applies lineConsumer to each text line from resource. lineConsumer may throw an exception.

        If firstLineConsumer is not null, the first line will be excluded from processed stream. First line will be processed by firstLineConsumer instead.

        Parameters:
        lineConsumer - consumer of a single line. May throw an exception
      • close

        public void close()
                   throws java.lang.Exception
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.lang.Exception