Text Line Iterable

An easy way to iterate over all lines in a source of text while using memory proportional to the length of the longest line.

License

License

GroupId

GroupId

us.bpsm
ArtifactId

ArtifactId

text-line-iterable
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Text Line Iterable
An easy way to iterate over all lines in a source of text while using memory proportional to the length of the longest line.
Project URL

Project URL

https://github.com/bpsm/text-line-iterable
Source Code Management

Source Code Management

https://github.com/bpsm/text-line-iterable

Download text-line-iterable

How to add to project

<!-- https://jarcasting.com/artifacts/us.bpsm/text-line-iterable/ -->
<dependency>
    <groupId>us.bpsm</groupId>
    <artifactId>text-line-iterable</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/us.bpsm/text-line-iterable/
implementation 'us.bpsm:text-line-iterable:1.0.0'
// https://jarcasting.com/artifacts/us.bpsm/text-line-iterable/
implementation ("us.bpsm:text-line-iterable:1.0.0")
'us.bpsm:text-line-iterable:jar:1.0.0'
<dependency org="us.bpsm" name="text-line-iterable" rev="1.0.0">
  <artifact name="text-line-iterable" type="jar" />
</dependency>
@Grapes(
@Grab(group='us.bpsm', module='text-line-iterable', version='1.0.0')
)
libraryDependencies += "us.bpsm" % "text-line-iterable" % "1.0.0"
[us.bpsm/text-line-iterable "1.0.0"]

Dependencies

compile (1)

Group / Artifact Type Version
com.google.guava : guava jar 17.0

test (1)

Group / Artifact Type Version
junit : junit jar 4.11

Project Modules

There are no modules declared in this project.

Text Line Iterable

An easy way to iterate over all lines in a source of text while using memory proportional to the length of the longest line.

Installation

This is a Maven project with the following coordinates:

<dependency>
    <groupId>us.bpsm</groupId>
    <artifactId>text-line-iterable</artifactId>
    <version>1.0.0</version>
</dependency>

It is available through the repositories Maven Central and OSS Sonatype Releases.

License

Text Line Iterator is relased under the Eclipse Public License - v 1.0.

Usage

    @Test
    public void usageExample() throws IOException {
        final Charset utf8 = Charset.forName("UTF-8");
        final Closer closer = Closer.create();
        try {
            // The file contains ten lines of the form:
            //   1,one
            //   2,two
            //   ...
            //   10,ten
            final TextLineIterable tlit =
                    closer.register(new TextLineIterable(someFile(), utf8));

            // tlit is an Iterable: we can process lines using a for(:) loop
            int numberOfCharacters = 0;
            for (String line: tlit) {
                numberOfCharacters += line.length();
            }
            assertEquals(60, numberOfCharacters);

            // We can make use of FluentIterable's methods to do complex
            // processing on our lines without ever needing to have them all
            // in memory at once.

            // We're going to gather a list of the names of even numbers
            // whose names contain an odd number of characters.
            final List<String> names;
            names = tlit.transform(new Function<String,List<String>>() {
                // split each line into value and name (list of two strings)
                public List<String> apply(String s) {
                    return Arrays.asList(s.split(","));
                }
            }).filter(new Predicate<List<String>>() {
                // select only those with even value and odd name length
                public boolean apply(List<String> strings) {
                    int value = Integer.parseInt(strings.get(0));
                    String name = strings.get(1);
                    return value % 2 == 0 && name.length() % 2 != 0;
                }
            }).transform(new Function<List<String>,String>() {
                // extract only the name from each (name, value) pair.
                public String apply(List<String> strings) {
                    return strings.get(1);
                }
            }).toList();
            assertEquals(Arrays.asList("two", "six", "eight", "ten"), names);
        } catch (Throwable e) {
            throw closer.rethrow(e);
        } finally {
            closer.close();
        }
    }

Dependencies

This code requires a recent version of Google Guava. The included pom.xml specifies version 17.0 though it may work with older versions as well.

Versions

Version
1.0.0