Redux4j

Redux implementation with Java and Vavr

License

License

GroupId

GroupId

me.grison
ArtifactId

ArtifactId

redux4j
Last Version

Last Version

1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Redux4j
Redux implementation with Java and Vavr
Project URL

Project URL

https://github.com/agrison/redux4j
Source Code Management

Source Code Management

https://github.com/agrison/redux4j

Download redux4j

How to add to project

<!-- https://jarcasting.com/artifacts/me.grison/redux4j/ -->
<dependency>
    <groupId>me.grison</groupId>
    <artifactId>redux4j</artifactId>
    <version>1.0</version>
</dependency>
// https://jarcasting.com/artifacts/me.grison/redux4j/
implementation 'me.grison:redux4j:1.0'
// https://jarcasting.com/artifacts/me.grison/redux4j/
implementation ("me.grison:redux4j:1.0")
'me.grison:redux4j:jar:1.0'
<dependency org="me.grison" name="redux4j" rev="1.0">
  <artifact name="redux4j" type="jar" />
</dependency>
@Grapes(
@Grab(group='me.grison', module='redux4j', version='1.0')
)
libraryDependencies += "me.grison" % "redux4j" % "1.0"
[me.grison/redux4j "1.0"]

Dependencies

compile (5)

Group / Artifact Type Version
io.vavr : vavr jar 0.10.3
org.slf4j : slf4j-api jar 1.7.21
com.google.code.gson : gson jar 2.3.1
commons-beanutils : commons-beanutils jar [1.9.4,)
org.projectlombok : lombok jar 1.18.20

test (2)

Group / Artifact Type Version
org.slf4j : slf4j-log4j12 jar 1.7.21
junit : junit jar 4.13.1

Project Modules

There are no modules declared in this project.

Redux in Java

redux4j codecov

Using Java and Vavr.

Features

  • Store
  • Reducer
  • CombineReducers
  • Middlewares

Install

<dependency>
  <groupId>me.grison</groupId>
  <artifactId>redux4j</artifactId>
  <version>1.0</version>
</dependency>

Counter example

public class Counter {
    // Actions
    static final String INC = "INC";
    static final String DEC = "DEC";

    // this is our reducer which increments if INC, decrement if DEC
    // and does nothing otherwise
    final Reducer<String, Integer> reducer =
            (action, state) -> state + switch (action) {
                case INC -> 1;
                case DEC -> -1;
                default -> 0;
            };

    public void foo() {
        // This is our store with its initial state of zero and the reducer seen above
        Store<Integer, String> store = Redux.createStore(0, reducer);

        // dispatch an INC action
        store.dispatch(INC);
        store.getState(); // 1

        // dispatch an DEC action
        store.dispatch(DEC);
        store.getState(); // 0
    }
}

Middlewares

public class Counter {

    final Middleware<Integer, String> middleware = (store, action, next) -> {
        System.out.println("Before " + store.getState());
        next.accept(store, action, null);
        System.out.println("After " + store.getState());
    };

    public void foo() {
        Store<Integer, String> store = Redux.createStore(0, reducer, middleware);

        store.dispatch(INC);
    }
}

Outputs:

Before 0
After 1

Combine Reducers

public class Foo {
	Reducer<String, String> concatBar =
			(action, state) -> "CONCAT".equals(action) ? state + "bar" : state;

	Reducer<String, Integer> plus2 =
			(action, state) -> "PLUS".equals(action) ? state + 2 : state;

	Reducer<String, List<String>> addFoo = (action, state) -> {
					if ("ADD".equals(action))
						state.add("foo");
					return state;
				};

	Reducer reducers = Redux.combineReducers(Tuple.of("str", concatBar), Tuple.of("int", plus2), Tuple.of("list", addFoo));

	@Test
	public void testCombineReducersMap() {
		Map<String, Object> initialState = new HashMap<String, Object>() {{
			put("str", "foo");
			put("int", 1);
			put("list", new ArrayList<String>());
		}};
		Store<Map<String,Object>, String> store = Redux.createStore(initialState, reducers);

		assertThat(store.getState().get("str"), equalTo("foo"));
		store.dispatch("CONCAT");
		assertThat(store.getState().get("str"), equalTo("foobar"));

		assertThat(store.getState().get("int"), equalTo(1));
		store.dispatch("PLUS");
		assertThat(store.getState().get("int"), equalTo(3));
		store.dispatch("PLUS");
		assertThat(store.getState().get("int"), equalTo(5));

		store.dispatch("CONCAT");
		assertThat(store.getState().get("str"), equalTo("foobarbar"));

		assertThat(store.getState().get("list"), equalTo(Arrays.asList()));
		store.dispatch("ADD");
		assertThat(store.getState().get("list"), equalTo(Arrays.asList("foo")));
		store.dispatch("ADD");
		assertThat(store.getState().get("list"), equalTo(Arrays.asList("foo", "foo")));
	}

	// same with a javabean with getters/setters
}

Versions

Version
1.0