Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public final class AppStateReducer implements Reducer<AppState> {
- private final Reducer<List<TodoItem>> todoItemsReducer;
- private final Reducer<TodoFilter> filterReducer;
- private AppStateReducer(Reducer<List<TodoItem>> todoItemsReducer, Reducer<TodoFilter> filterReducer) {
- this.todoItemsReducer = todoItemsReducer;
- this.filterReducer = filterReducer;
- }
- @Override
- public AppState reduce(AppState state, Action action) {
- List<TodoItem> todoItems = null;
- TodoFilter filter = null;
- if (state != null) {
- todoItems = state.todoItems();
- filter = state.filter();
- }
- List<TodoItem> todoItemsNext = todoItemsReducer.reduce(todoItems, action);
- TodoFilter filterNext = filterReducer.reduce(filter, action);
- //If all values are the same there is no need to create an object
- if (state != null
- && todoItems == todoItemsNext
- && filter == filterNext) {
- return state;
- } else {
- return new AppStateImpl(todoItemsNext, filterNext);
- }
- }
- public static Builder builder() {
- return new Builder();
- }
- public static class Builder {
- private Reducer<List<TodoItem>> todoItemsReducer;
- private Reducer<TodoFilter> filterReducer;
- private Builder() {
- }
- public Builder todoItemsReducer(Reducer<List<TodoItem>> todoItemsReducer) {
- this.todoItemsReducer = todoItemsReducer;
- return this;
- }
- public Builder filterReducer(Reducer<TodoFilter> filterReducer) {
- this.filterReducer = filterReducer;
- return this;
- }
- public AppStateReducer build() {
- if (todoItemsReducer == null) {
- throw new IllegalStateException("todoItemsReducer should not be null");
- }
- if (filterReducer == null) {
- throw new IllegalStateException("filterReducer should not be null");
- }
- return new AppStateReducer(todoItemsReducer, filterReducer);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement