Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nullability annotation for kotlin interop. #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion grox-core/src/main/java/com/groupon/grox/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.groupon.grox;

import javax.annotation.Nonnull;

/**
* Like Redux actions, actions in grox are:
*
Expand All @@ -31,5 +33,5 @@
* @param <STATE> the class of the state.
*/
public interface Action<STATE> {
STATE newState(STATE oldState);
STATE newState(@Nonnull STATE oldState);
}
30 changes: 18 additions & 12 deletions grox-core/src/main/java/com/groupon/grox/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
*/
package com.groupon.grox;

import static java.util.Arrays.asList;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.annotation.Nonnull;

import static java.util.Arrays.asList;

/**
* Like Redux Stores, stores in grox are:
*
Expand All @@ -41,7 +43,7 @@
public class Store<STATE> {

/** The current state of the store. */
private STATE state;
private @Nonnull STATE state;
/** The list of internal middle wares. */
private final List<Middleware<STATE>> middlewares = new ArrayList<>();
/** The list of all state change listeners that will get notified of state changes. */
Expand All @@ -53,7 +55,7 @@ public class Store<STATE> {
private final AtomicBoolean isDispatching = new AtomicBoolean(false);

@SafeVarargs
public Store(STATE initialState, Middleware<STATE>... middlewares) {
public Store(@Nonnull STATE initialState, Middleware<STATE>... middlewares) {
this.state = initialState;
this.middlewares.add(new NotifySubscribersMiddleware());
this.middlewares.addAll(asList(middlewares));
Expand All @@ -69,7 +71,7 @@ public Store(STATE initialState, Middleware<STATE>... middlewares) {
* @see Middleware
* @see StateChangeListener
*/
public synchronized void dispatch(Action<STATE> action) {
public synchronized void dispatch(@Nonnull Action<STATE> action) {
actionQueue.add(action);
//we still need an atomic boolean here, even though the method is
//synchronized, because we also use it to unsubscribe.
Expand All @@ -89,6 +91,7 @@ private void emitSequentially() {
}

/** @return the current state of the store. */
@Nonnull
public STATE getState() {
return state;
}
Expand All @@ -99,7 +102,7 @@ public STATE getState() {
*
* @param listener the listener to be added.
*/
public void subscribe(StateChangeListener<STATE> listener) {
public void subscribe(@Nonnull StateChangeListener<STATE> listener) {
this.stateChangeListeners.add(listener);
isDispatching.set(true);
listener.onStateChanged(getState());
Expand All @@ -113,7 +116,7 @@ public void subscribe(StateChangeListener<STATE> listener) {
* @param listener the listener to be removed.
*/
@SuppressWarnings("WeakerAccess")
public void unsubscribe(StateChangeListener<STATE> listener) {
public void unsubscribe(@Nonnull StateChangeListener<STATE> listener) {
this.stateChangeListeners.remove(listener);
}

Expand All @@ -139,7 +142,7 @@ public interface Middleware<STATE> {
*
* @param chain the chain of all middle wares for the store associated to this middleware.
*/
void intercept(Chain<STATE> chain);
void intercept(@Nonnull Chain<STATE> chain);

/**
* Represents the linked list of all middle wares int the store. The order of the middle ware
Expand All @@ -150,12 +153,15 @@ public interface Middleware<STATE> {
*/
interface Chain<STATE> {
/** @return the action being dispatched. */
@Nonnull
Action<STATE> action();

/**
* @return the current state of the store. The state before the call to {@link
* #proceed(Action)} is the state prior to the action being executed. The state after the
* call is the state after the action being executed.
*/
@Nonnull
STATE state();

/**
Expand All @@ -164,7 +170,7 @@ interface Chain<STATE> {
*
* @param action the action being dispatched in the store.
*/
void proceed(Action<STATE> action);
void proceed(@Nonnull Action<STATE> action);
}
}

Expand All @@ -176,14 +182,14 @@ interface Chain<STATE> {
* @see #unsubscribe(StateChangeListener)
*/
public interface StateChangeListener<STATE> {
void onStateChanged(STATE newState);
void onStateChanged(@Nonnull STATE newState);
}

/** Internal middle ware that actually executes the action of a middle ware chain. */
private class CallReducerMiddleware implements Middleware<STATE> {

@Override
public void intercept(Chain<STATE> chain) {
public void intercept(@Nonnull Chain<STATE> chain) {
state = chain.action().newState(state);
}
}
Expand All @@ -195,7 +201,7 @@ public void intercept(Chain<STATE> chain) {
private class NotifySubscribersMiddleware implements Middleware<STATE> {

@Override
public void intercept(Chain<STATE> chain) {
public void intercept(@Nonnull Chain<STATE> chain) {
chain.proceed(chain.action());
for (StateChangeListener<STATE> stateChangeListener : stateChangeListeners) {
stateChangeListener.onStateChanged(state);
Expand Down