Skip to content

README quickstart Filtered<> snippet compares String.equals(long) and ignores Wallet.id() throws IOException #138

@edmoffo

Description

@edmoffo

The "All you need is this" snippet at README.md lines 38-44 builds a Filtered<> predicate that calls w.id() and asks it to equal the literal string "9999888877776666". Wallet.id() is declared in src/main/java/io/zold/api/Wallet.java line 45 as long id() throws IOException, so the example has two defects.

First, the predicate body never matches. "9999888877776666".equals(w.id()) autoboxes the primitive long to a Long, and String.equals(Long) returns false for every wallet in the directory. The iterator's next() call therefore throws NoSuchElementException instead of returning a wallet.

Second, the snippet does not compile as written. Wallet.id() declares throws IOException, but org.cactoos.iterable.Filtered takes a plain Func<X, Boolean>. A lambda that calls a method throwing a checked exception needs IoCheckedFunc or a wrapper, neither of which the example uses.

Smallest fix: drop the quotes, parse the id as hex, and either use IoCheckedFunc or replace Filtered with the cactoos iterable that tolerates IOException. For example:

final long target = Long.parseUnsignedLong("9999888877776666", 16);
final Wallet wallet = new Filtered<>(
    new IoCheckedFunc<>(w -> w.id() == target),
    wallets
).iterator().next();

That keeps the README example honest about both the id type and the checked exception surface.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions