Skip to content

A store may be used to save entities each with a primary key

License

Notifications You must be signed in to change notification settings

mP1/walkingkooka-store

Repository files navigation

Build Status Coverage Status License Language grade: Java Total alerts J2CL compatible

A store may be used to persist entities. A TreeMap memory store is provided for testing purposes.

public static void main(final String[] args) {
    final Store<String, TestUser> store = Stores.treeMap(String.CASE_INSENSITIVE_ORDER, Sample::idSetter);

    final String id1 = "user1";
    final String email1 = "email1@example.com";
    store.save(new TestUser(Optional.of(id1), email1));

    final String id2 = "user2";
    final String email2 = "email2@example.com";
    store.save(new TestUser(Optional.of(id2), email2));

    assertEquals(2, store.count());
    assertEquals(email1, store.loadOrFail(id1).email);
}

private static TestUser idSetter(final String id, final TestUser user) {
    return new TestUser(Optional.of(id), user.email);
}

static class TestUser implements HasId<Optional<String>> {

    private TestUser(final Optional<String> id, final String email) {
        super();
        this.id = id;
        this.email = email;
    }

    @Override
    public Optional<String> id() {
        return this.id;
    }

    final Optional<String> id;
    final String email;

    @Override
    public int hashCode() {
        return this.id.hashCode();
    }

    public boolean equals(final Object other) {
        return this == other || other instanceof TestUser && this.equals0(Cast.to(other));
    }

    private boolean equals0(final TestUser other) {
        return this.id.equals(other.id) && this.email.equals(other.email);
    }

    @Override
    public String toString() {
        return ToStringBuilder.empty()
                .value(this.id)
                .value(this.email)
                .build();
    }
}

About

A store may be used to save entities each with a primary key

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages