Skip to content

Commit

Permalink
feat: Improve State kotlin interop
Browse files Browse the repository at this point in the history
  • Loading branch information
phinner committed May 13, 2024
1 parent c4b231f commit f85960f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static Action none() {
}

static <T> Action with(final State.Key<T> key, final T value) {
return window -> window.getState().put(key, value);
return window -> window.getState().set(key, value);
}

static Action without(final State.Key<?> key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ static State create() {
return new StateImpl();
}

<T> Optional<T> get(final Key<T> key);
<T> Optional<T> getOptional(final Key<T> key);

<T> @Nullable T put(final Key<T> key, final T value);
default <T> @Nullable T get(final Key<T> key) {
return getOptional(key).orElse(null);
}

<T> @Nullable T set(final Key<T> key, final T value);

<T> @Nullable T remove(final Key<T> key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ final class StateImpl implements State {
private final Map<Key<?>, Object> data = new HashMap<>();

@Override
public <T> Optional<T> get(final Key<T> key) {
public <T> Optional<T> getOptional(final Key<T> key) {
return Optional.ofNullable(key.getValueType().cast(data.get(key)));
}

@Override
public <T> @Nullable T put(final Key<T> key, final T value) {
public <T> @Nullable T set(final Key<T> key, final T value) {
return key.getValueType().cast(data.put(key, value));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public ListTransformer<E> setPageKey(final State.Key<Integer> pageKey) {
@Override
public void transform(final MenuPane pane, final Window window) {
final var elements = provider.apply(window);
final int page = Math.floorDiv(window.getState().get(pageKey).orElse(0), getPageSize());
window.getState().put(pageKey, page);
final int page = Math.floorDiv(window.getState().getOptional(pageKey).orElse(0), getPageSize());
window.getState().set(pageKey, page);

int cursor = 0;
for (int i = 0; i < height; i++) {
Expand Down

0 comments on commit f85960f

Please sign in to comment.