Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Redux Java
class Action {
private String type;
private Object data;
public Action(String type, Object data) {
this.type = type;
this.data = data;
}
public String getType() {
return type;
}
public Object getData() {
return data;
}
}
abstract class ImpureAction extends Action {
public ImpureAction(String type, Object data) {
super(type, data);
}
public abstract void doImpureAction();
}
interface StateTransition<STATE> {
STATE nextState(STATE currentState, Action action);
}
class SquareCalculator implements StateTransition<Integer> {
public Integer nextState(Integer currentState, Action action) {
if(action.getType().equals("SQUARE_CALCULATOR")) {
return (Integer)action.getData() * (Integer)action.getData();
} else {
throw new IllegalStateException("Invalid action=[ " + action + "]");
}
}
}
class ReduxStateHolder<STATE> {
private STATE state;
public ReduxStateHolder(STATE initialState) {
this.state = initialState;
}
public STATE getState() {
return state;
}
public void setState(STATE state) {
this.state = state;
}
}
interface Store{
void dispatch(Action action);
}
class ReduxStore<STATE> implements Store {
private ReduxStateHolder<STATE> reduxStateHolder;
private StateTransition<STATE> stateTransition;
public ReduxStore(ReduxStateHolder<STATE> reduxStateHolder, StateTransition<STATE> stateTransition) {
this.reduxStateHolder = reduxStateHolder;
this.stateTransition = stateTransition;
}
public void dispatch(Action action) {
reduxStateHolder.setState(stateTransition.nextState(reduxStateHolder.getState(), action));
}
}
abstract class AbstractStore implements Store {
private Store nextStore;
public AbstractStore(Store nextStore) {
this.nextStore = nextStore;
}
public Store getNextStore() {
return nextStore;
}
}
class InputValidation extends ImpureAction {
public InputValidation(String type, Object data) {
super(type, data);
}
public void doImpureAction() {
try {
// Assuming you are making an IO call to call a service to do validation which is
// an impure operation
Thread.sleep(2000);
// Validation OK so dispatch
System.out.println("[InputValidation OK!!!! so doing pure dispatch.");
ReduxInformationHolder.getInstance().getGlobalStore().dispatch(new Action(this.getType(), this.getData()));
} catch(Exception e) {
// NO OP
}
}
}
class Thunk extends AbstractStore {
public Thunk(Store nextStore) {
super(nextStore);
}
public void dispatch(Action action) {
if (action instanceof ImpureAction) {
System.out.println("Impure");
((ImpureAction)action).doImpureAction();
} else {
System.out.println("Pure");
getNextStore().dispatch(action);
}
}
}
class LoggingStore extends AbstractStore {
public LoggingStore(Store nextStore) {
super(nextStore);
}
public void dispatch(Action action) {
System.out.println("[BEFORE CALLING NEXT]: state[" + ReduxInformationHolder.getInstance().getGlobalReduxStateHolder().getState() + "]");
getNextStore().dispatch(action);
System.out.println("[AFTER CALLING NEXT]: state[" + ReduxInformationHolder.getInstance().getGlobalReduxStateHolder().getState() + "]");
}
}
class TimerStore extends AbstractStore {
public TimerStore(Store nextStore) {
super(nextStore);
}
public void dispatch(Action action) {
long startTime = System.nanoTime();
getNextStore().dispatch(action);
long endTime = System.nanoTime();
System.out.println("Total Time: [" + (endTime-startTime) + "] nanoseconds");
System.out.println("");
}
}
class ReduxInformationHolder {
private static ReduxInformationHolder instance;
private Store globalStore;
private ReduxStateHolder globalReduxStateHolder;
private ReduxInformationHolder(Store globalStore, ReduxStateHolder globalReduxStateHolder) {
this.globalStore = globalStore;
this.globalReduxStateHolder = globalReduxStateHolder;
}
public static ReduxInformationHolder getInstance() {
if(instance == null) {
throw new IllegalStateException("ReduxInformationHolder::getInstance: instance is NULL!!!");
} else {
return instance;
}
}
public static ReduxInformationHolder createInstance(Store globalStore, ReduxStateHolder globalReduxStateHolder) {
if(instance == null) {
instance = new ReduxInformationHolder(globalStore, globalReduxStateHolder);
return instance;
} else {
return instance;
}
}
public Store getGlobalStore() {
return this.globalStore;
}
public ReduxStateHolder getGlobalReduxStateHolder() {
return this.globalReduxStateHolder;
}
}
public class ReduxRunner
{
public static void main(String[] args)
{
ReduxInformationHolder reduxInformationHolder;
ReduxStateHolder<Integer> globalReduxStateHolder;
globalReduxStateHolder = new ReduxStateHolder<Integer>(2);
StateTransition<Integer> stateTransition = new SquareCalculator();
Store baseStore = new ReduxStore<Integer>(globalReduxStateHolder, stateTransition);
Store globalStore;
globalStore = new Thunk(new TimerStore(new LoggingStore(baseStore)));
ReduxInformationHolder.createInstance(globalStore, globalReduxStateHolder);
globalStore.dispatch(new Action("SQUARE_CALCULATOR", Integer.parseInt("4")));
globalStore.dispatch(new Action("SQUARE_CALCULATOR", Integer.parseInt("2")));
globalStore.dispatch(new InputValidation("SQUARE_CALCULATOR", Integer.parseInt("10")));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment