├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── build.gradle
├── examples
└── basic
│ ├── build.gradle
│ └── src
│ ├── main
│ └── java
│ │ └── com
│ │ └── glung
│ │ └── redux
│ │ ├── Action.java
│ │ ├── BasicApplication.java
│ │ ├── MyReducer.java
│ │ └── ReduxApplication.java
│ └── test
│ └── java
│ └── com
│ └── glung
│ └── redux
│ └── MyReducerTest.java
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── lib
├── build.gradle
└── src
│ ├── main
│ └── java
│ │ └── com
│ │ └── glung
│ │ └── redux
│ │ ├── Middlewares.java
│ │ └── Store.java
│ └── test
│ └── java
│ └── com
│ └── glung
│ └── redux
│ ├── MiddlewaresTest.java
│ └── StoreTest.java
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea
2 | .DS_Store
3 | *.iml
4 | target
5 | .gradle
6 | build
7 | local.properties
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | jdk:
3 | - oraclejdk7
4 |
5 | # force upgrade Java8 as per https://github.com/travis-ci/travis-ci/issues/4042 (fixes compilation issue)
6 | #addons:
7 | # apt:
8 | # packages:
9 | # - oracle-java8-installer
10 |
11 | # prevent travis running gradle assemble; let the build script do it anyway
12 | install: true
13 |
14 | sudo: false
15 | # as per http://blog.travis-ci.com/2014-12-17-faster-builds-with-container-based-infrastructure/
16 |
17 | # cache between builds
18 | cache:
19 | directories:
20 | - $HOME/.m2
21 | - $HOME/.gradle
22 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Guillaume Lung
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | // This project is unmaintained //
2 |
3 | # ▲▲▲ Redux-java ▲▲▲
4 |
5 | A java implementation of [jvm-redux-api](https://github.com/jvm-redux/jvm-redux-api)
6 |
7 | # Integration
8 |
9 | ## Gradle
10 |
11 | ```
12 | allprojects {
13 | repositories {
14 | ...
15 | maven { url 'https://jitpack.io' }
16 | }
17 | }
18 | ```
19 |
20 | ```
21 | dependencies {
22 | compile 'com.github.glung:redux-java:1.0'
23 | }
24 | ```
25 |
26 | ## Maven
27 | ```
28 |
29 |
30 | jitpack.io
31 | https://jitpack.io
32 |
33 |
34 | ```
35 |
36 | ```
37 |
38 | com.github.glung
39 | redux-java
40 | 1.0
41 |
42 | ```
43 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | group GROUP
2 | version VERSION_NAME
3 |
4 | ext {
5 | sourceCompatibilityVersion = JavaVersion.VERSION_1_7
6 | targetCompatibilityVersion = JavaVersion.VERSION_1_7
7 | }
8 |
--------------------------------------------------------------------------------
/examples/basic/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java'
2 |
3 | sourceCompatibility = rootProject.ext.sourceCompatibilityVersion
4 | targetCompatibility = rootProject.ext.targetCompatibilityVersion
5 |
6 | repositories {
7 | mavenCentral()
8 | maven { url "https://jitpack.io" }
9 | }
10 |
11 | dependencies {
12 | compile project(':lib')
13 |
14 | compile 'com.github.jvm-redux.jvm-redux-api:api:2.0.0'
15 | testCompile 'junit:junit:4.12'
16 | }
17 |
--------------------------------------------------------------------------------
/examples/basic/src/main/java/com/glung/redux/Action.java:
--------------------------------------------------------------------------------
1 | package com.glung.redux;
2 |
3 | enum Action {
4 | INCREMENT, DECREMENT
5 | }
6 |
--------------------------------------------------------------------------------
/examples/basic/src/main/java/com/glung/redux/BasicApplication.java:
--------------------------------------------------------------------------------
1 | package com.glung.redux;
2 |
3 | class BasicApplication {
4 |
5 | public static void main(String[] args) {
6 | final Store.Creator creator = new Store.Creator();
7 | final ReduxApplication application = new ReduxApplication(creator, System.out);
8 | application.runDemo();
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/examples/basic/src/main/java/com/glung/redux/MyReducer.java:
--------------------------------------------------------------------------------
1 | package com.glung.redux;
2 |
3 | class MyReducer implements redux.api.Reducer {
4 | @Override
5 | public Integer reduce(Integer state, Object action) {
6 | return action instanceof Action ? reduce(state, (Action) action) : state;
7 | }
8 |
9 | private Integer reduce(Integer state, Action action) {
10 | switch (action) {
11 | case INCREMENT:
12 | return state + 1;
13 | case DECREMENT:
14 | return state - 1;
15 | default:
16 | return state;
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/examples/basic/src/main/java/com/glung/redux/ReduxApplication.java:
--------------------------------------------------------------------------------
1 | package com.glung.redux;
2 |
3 | import redux.api.Store;
4 |
5 | import java.io.PrintStream;
6 |
7 | class ReduxApplication {
8 | private final redux.api.Store store;
9 | private final PrintStream stream;
10 |
11 | ReduxApplication(redux.api.Store.Creator storeCreator, PrintStream stream) {
12 | store = storeCreator.create(new MyReducer(), 0);
13 | this.stream = stream;
14 | }
15 |
16 | void runDemo() {
17 | store.subscribe(new MySubscriber(store, stream));
18 | store.dispatch(Action.INCREMENT); // print 1
19 | store.dispatch(Action.DECREMENT); // print 0
20 | store.dispatch("unknown action"); // print 0
21 | store.dispatch(Action.INCREMENT); // print 1
22 | }
23 |
24 | private static class MySubscriber implements redux.api.Store.Subscriber {
25 | private final redux.api.Store store;
26 | private final PrintStream stream;
27 |
28 | private MySubscriber(Store store, PrintStream stream) {
29 | this.store = store;
30 | this.stream = stream;
31 | }
32 |
33 | @Override
34 | public void onStateChanged() {
35 | stream.println(store.getState());
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/examples/basic/src/test/java/com/glung/redux/MyReducerTest.java:
--------------------------------------------------------------------------------
1 | package com.glung.redux;
2 |
3 | import static org.hamcrest.CoreMatchers.is;
4 | import static org.hamcrest.MatcherAssert.assertThat;
5 |
6 | import org.junit.Test;
7 |
8 | public class MyReducerTest {
9 |
10 | private MyReducer reducer = new MyReducer();
11 |
12 | @Test
13 | public void ignore_unknown_actions() {
14 | assertThat(reducer.reduce(0, "unknown action"), is(0));
15 | }
16 |
17 | @Test
18 | public void increment() {
19 | assertThat(reducer.reduce(0, Action.INCREMENT), is(1));
20 | }
21 |
22 | @Test
23 | public void decrement() {
24 | assertThat(reducer.reduce(0, Action.DECREMENT), is(-1));
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | GROUP=com.glung.redux
2 | VERSION_NAME=1.0-SNAPSHOT
3 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/glung/redux-java/e21978fb6611ec7d27cd13eec59c8f86c4ca2a41/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Fri Dec 23 11:07:36 CET 2016
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn ( ) {
37 | echo "$*"
38 | }
39 |
40 | die ( ) {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/lib/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java'
2 |
3 | sourceCompatibility = rootProject.ext.sourceCompatibilityVersion
4 | targetCompatibility = rootProject.ext.targetCompatibilityVersion
5 |
6 | repositories {
7 | mavenCentral()
8 | maven { url "https://jitpack.io" }
9 | }
10 |
11 | dependencies {
12 | compile 'com.github.jvm-redux.jvm-redux-api:api:2.0.0'
13 | testCompile 'com.github.jvm-redux.jvm-redux-api:specs:2.0.0'
14 | }
15 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/glung/redux/Middlewares.java:
--------------------------------------------------------------------------------
1 | package com.glung.redux;
2 |
3 | import redux.api.Dispatcher;
4 | import redux.api.Reducer;
5 | import redux.api.Store;
6 | import redux.api.enhancer.Middleware;
7 |
8 | import java.util.Arrays;
9 | import java.util.List;
10 |
11 | public class Middlewares {
12 |
13 | private static class MiddlewareStore implements redux.api.Store {
14 |
15 | private final Dispatcher dispatcher;
16 | private final Store nextStore;
17 |
18 | private MiddlewareStore(Store nextStore,
19 | Dispatcher dispatcher) {
20 | this.nextStore = nextStore;
21 | this.dispatcher = dispatcher;
22 | }
23 |
24 | @Override
25 | public S getState() {
26 | return nextStore.getState();
27 | }
28 |
29 | @Override
30 | public Subscription subscribe(Subscriber subscriber) {
31 | return nextStore.subscribe(subscriber);
32 | }
33 |
34 | @Override
35 | public void replaceReducer(Reducer reducer) {
36 | nextStore.replaceReducer(reducer);
37 | }
38 |
39 | @Override
40 | public Object dispatch(Object action) {
41 | return dispatcher.dispatch(action);
42 | }
43 |
44 | }
45 |
46 | public static Store.Enhancer applyMiddlewares(final Middleware... middlewares) {
47 | return new Store.Enhancer() {
48 | @Override
49 | public Store.Creator enhance(final Store.Creator next) {
50 | return new Store.Creator() {
51 | @Override
52 | public Store create(final Reducer reducer, final S initialState) {
53 | final Store store = next.create(reducer, initialState);
54 | // This is fqr from ideal but currently it is expected that the Middleware and the Reducer carry the same type.
55 | // This is checked at run time (cf down cast below)
56 | //
57 | // Revisit when needed
58 | return new MiddlewareStore<>(store, createMiddlewareDispatcher(Arrays.asList(middlewares), (Store) store));
59 | }
60 | };
61 | }
62 | };
63 | }
64 |
65 | private static Dispatcher createMiddlewareDispatcher(final List> middlewares, final Store nextStore) {
66 | Dispatcher currentDispatcher = nextStore;
67 | for (int i = middlewares.size() - 1; i >= 0; i--) {
68 | final Middleware nextMiddleware = middlewares.get(i);
69 | currentDispatcher = createNextDispatcher(nextStore, currentDispatcher, nextMiddleware);
70 | }
71 | return currentDispatcher;
72 | }
73 |
74 | private static Dispatcher createNextDispatcher(final Store nextStore, final Dispatcher lastDispatcher, final Middleware nextMiddleware) {
75 | return new Dispatcher() {
76 | @Override
77 | public Object dispatch(Object action) {
78 | return nextMiddleware.dispatch(nextStore, lastDispatcher, action);
79 | }
80 | };
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/lib/src/main/java/com/glung/redux/Store.java:
--------------------------------------------------------------------------------
1 | package com.glung.redux;
2 |
3 | import redux.api.Reducer;
4 |
5 | import java.util.ArrayList;
6 | import java.util.List;
7 | import java.util.concurrent.atomic.AtomicBoolean;
8 |
9 | public class Store implements redux.api.Store {
10 |
11 | private final AtomicBoolean isReducing = new AtomicBoolean(false);
12 | private final List subscribers = new ArrayList<>();
13 |
14 | private Reducer reducer;
15 | private S currentState;
16 |
17 | public static redux.api.Store createStore(Reducer reducer, S initialState, Enhancer enhancer) {
18 | final redux.api.Store.Creator creator = enhancer != null ? enhancer.enhance(new Creator()) : new Creator();
19 | return creator.create(reducer, initialState);
20 | }
21 |
22 | Store(Reducer reducer, S initialState) {
23 | this.currentState = initialState;
24 | setReducer(reducer);
25 | }
26 |
27 | @Override
28 | public S getState() {
29 | return currentState;
30 | }
31 |
32 | @Override
33 | public Subscription subscribe(final Subscriber subscriber) {
34 | subscribers.add(subscriber);
35 | return new Subscription() {
36 | @Override
37 | public void unsubscribe() {
38 | subscribers.remove(subscriber);
39 | }
40 | };
41 | }
42 |
43 | @Override
44 | public void replaceReducer(Reducer reducer) {
45 | setReducer(reducer);
46 | }
47 |
48 | private void setReducer(Reducer reducer) {
49 | this.reducer = reducer;
50 | this.currentState = this.reducer.reduce(currentState, redux.api.Store.INIT);
51 | }
52 |
53 | @Override
54 | public Object dispatch(Object action) {
55 | assertIsNotReducing();
56 |
57 | currentState = reduce(action);
58 | notifySubscribers();
59 | return action;
60 | }
61 |
62 | private void notifySubscribers() {
63 | for (Subscriber subscriber : new ArrayList<>(subscribers)) {
64 | subscriber.onStateChanged();
65 | }
66 | }
67 |
68 | private void assertIsNotReducing() {
69 | if (isReducing.get()) {
70 | throw new IllegalStateException("Already reducing");
71 | }
72 | }
73 |
74 | private S reduce(Object action) {
75 | startReducing();
76 | final S reducedSate = reducer.reduce(currentState, action);
77 | stopReducing();
78 | return reducedSate;
79 | }
80 |
81 |
82 | private void stopReducing() {
83 | isReducing.set(false);
84 | }
85 |
86 | private void startReducing() {
87 | isReducing.set(true);
88 | }
89 |
90 | public static class Creator implements redux.api.Store.Creator {
91 |
92 | @Override
93 | public redux.api.Store create(Reducer reducer, S initialState) {
94 | return new Store<>(reducer, initialState);
95 | }
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/lib/src/test/java/com/glung/redux/MiddlewaresTest.java:
--------------------------------------------------------------------------------
1 | package com.glung.redux;
2 |
3 | import static com.glung.redux.Middlewares.applyMiddlewares;
4 | import static org.assertj.core.api.Assertions.assertThat;
5 | import static org.mockito.ArgumentMatchers.any;
6 | import static org.mockito.Mockito.mock;
7 | import static org.mockito.Mockito.verify;
8 | import static org.mockito.Mockito.verifyZeroInteractions;
9 | import static org.mockito.Mockito.when;
10 | import static redux.api.helpers.ActionsCreator.addTodo;
11 |
12 | import org.junit.Rule;
13 | import org.junit.Test;
14 | import org.mockito.Mock;
15 | import org.mockito.junit.MockitoJUnit;
16 | import org.mockito.junit.MockitoRule;
17 | import redux.api.Dispatcher;
18 | import redux.api.Reducer;
19 | import redux.api.Store;
20 | import redux.api.enhancer.Middleware;
21 | import redux.api.helpers.Reducers;
22 | import redux.api.helpers.State;
23 |
24 | import java.util.ArrayList;
25 | import java.util.List;
26 |
27 | public class MiddlewaresTest {
28 |
29 | @Rule public MockitoRule mockitoRule = MockitoJUnit.rule();
30 | @Mock private redux.api.Store.Creator storeCreator;
31 | @Mock private Store storeMock;
32 |
33 | private final List callOrderResult = new ArrayList<>();
34 |
35 | private Store.Creator enhancedStoreCreator() {
36 | when(storeCreator.create(any(Reducer.class), any(State.class))).thenReturn(storeMock);
37 |
38 | return applyMiddlewares(createMiddleware("ONE"),
39 | createMiddleware("TWO"),
40 | createMiddleware("THREE"))
41 | .enhance(storeCreator);
42 | }
43 |
44 | private Middleware createMiddleware(final String identifier) {
45 | return new Middleware() {
46 | @Override
47 | public Object dispatch(Store store, Dispatcher next, Object action) {
48 | callOrderResult.add(identifier);
49 | return next.dispatch(action);
50 | }
51 | };
52 | }
53 |
54 | @Test
55 | public void dispatchInvokesMiddlewaresInCorrectOrder() {
56 | final Store store = enhancedStoreCreator().create(Reducers.TODOS, new State());
57 |
58 | store.dispatch(addTodo("Test"));
59 |
60 | assertThat(callOrderResult).containsExactly("ONE", "TWO", "THREE");
61 | }
62 |
63 | @Test
64 | public void dispatchForwardsToTheOriginalStore() {
65 | final Object action = addTodo("Test");
66 | final Object expectedDispatchResult = new Object();
67 | final Store store = enhancedStoreCreator().create(Reducers.TODOS, new State());
68 | when(storeMock.dispatch(action)).thenReturn(expectedDispatchResult);
69 |
70 | verifyZeroInteractions(storeMock);
71 |
72 | Object dispatchResult = store.dispatch(action);
73 |
74 | assertThat(dispatchResult).isEqualTo(expectedDispatchResult);
75 | }
76 |
77 | @Test
78 | public void getStateForwardsToTheOriginalStore() {
79 | final Store store = enhancedStoreCreator().create(Reducers.TODOS, new State());
80 | verifyZeroInteractions(storeMock);
81 |
82 | store.getState();
83 |
84 | verify(storeMock).getState();
85 | }
86 |
87 | @Test
88 | public void replaceReducerForwardsToTheOriginalStore() {
89 | final Store store = enhancedStoreCreator().create(Reducers.TODOS, new State());
90 | verifyZeroInteractions(storeMock);
91 |
92 | store.replaceReducer(Reducers.TODOS_REVERSE);
93 |
94 | verify(storeMock).replaceReducer(Reducers.TODOS_REVERSE);
95 | }
96 |
97 | @Test
98 | public void subscribeForwardsToTheOriginalStore() {
99 | final Store.Subscriber subscriber = mock(Store.Subscriber.class);
100 | final Store.Subscription expectedSubscription = mock(Store.Subscription.class);
101 | final Store store = enhancedStoreCreator().create(Reducers.TODOS, new State());
102 | when(storeMock.subscribe(subscriber)).thenReturn(expectedSubscription);
103 |
104 | verifyZeroInteractions(storeMock);
105 |
106 | Store.Subscription subscription = store.subscribe(subscriber);
107 |
108 | assertThat(subscription).isEqualTo(expectedSubscription);
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/lib/src/test/java/com/glung/redux/StoreTest.java:
--------------------------------------------------------------------------------
1 | package com.glung.redux;
2 |
3 | import org.jetbrains.annotations.NotNull;
4 | import redux.api.Reducer;
5 |
6 | public class StoreTest extends redux.api.StoreTest {
7 |
8 | @NotNull
9 | @Override
10 | public redux.api.Store createStore(@NotNull Reducer reducer, @NotNull S state) {
11 | return new Store.Creator().create(reducer, state);
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name = 'redux-java'
2 | include 'lib'
3 | include 'examples:basic'
4 |
5 |
--------------------------------------------------------------------------------