Skip to content

Commit

Permalink
Merge pull request #8 from izumin5210/builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Masayuki IZUMI committed Nov 23, 2015
2 parents dfb05ac + 5e15a60 commit 54fff91
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ private MethodSpec createConstructor() {
reducer.getStoreModel().getVariableName(),
reducer.getStoreModel().getClassName(),
reducer.getStoreModel().getBuilderName(),
StoreBuilderClassElement.ADD_REDUCER_METHOD_NAME,
StoreBuilderClassElement.REDUCER_SETTER_METHOD_NAME,
reducer.getVariableName(),
StoreBuilderClassElement.ADD_INITIAL_STATE_METHOD_NAME,
StoreBuilderClassElement.INITIAL_STATE_SETTER_METHOD_NAME,
reducer.getStoreModel().getBuilderVariableName(),
reducer.getStoreModel().getStateVariableName(),
StoreBuilderClassElement.BUILD_METHOD_NAME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
public class StoreBuilderClassElement {
public static final String TAG = StoreBuilderClassElement.class.getSimpleName();

static final String ADD_REDUCER_METHOD_NAME = "addReducer";
static final String ADD_INITIAL_STATE_METHOD_NAME = "addInitialState";
static final String REDUCER_SETTER_METHOD_NAME = "setReducer";
static final String INITIAL_STATE_SETTER_METHOD_NAME = "setInitialState";
static final String ADD_MIDDLEWARE_METHOD_NAME = "addMiddleware";
static final String BUILD_METHOD_NAME = "build";
static final String ERROR_MESSAGE_NOT_INITIALIZED_EXCEPTION = "$N has not been initialized.";
Expand Down Expand Up @@ -80,9 +80,10 @@ private MethodSpec createBuilderConstructor() {
private MethodSpec createAddMiddlewareMethodSpec() {
return MethodSpec.methodBuilder(ADD_MIDDLEWARE_METHOD_NAME)
.addModifiers(Modifier.PUBLIC)
.addAnnotation(getOverrideAnnotation())
.returns(storeModel.getBuilder())
.addParameter(getParameterSpec(Middleware.class))
.addStatement("getMiddlewares().add(middleware)")
.addStatement("super.addMiddleware(middleware)")
.addStatement("return this")
.build();
}
Expand All @@ -91,7 +92,7 @@ private List<MethodSpec> createAddReducerMethodSpecs() {
List<MethodSpec> specs = new ArrayList<>();
for (ReducerModel reducerModel : reducerModels) {
specs.add(
MethodSpec.methodBuilder(ADD_REDUCER_METHOD_NAME)
MethodSpec.methodBuilder(REDUCER_SETTER_METHOD_NAME)
.addModifiers(Modifier.PUBLIC)
.returns(storeModel.getBuilder())
.addParameter(getParameterSpec(reducerModel.getReducer()))
Expand All @@ -100,7 +101,7 @@ private List<MethodSpec> createAddReducerMethodSpecs() {
.build()
);
specs.add(
MethodSpec.methodBuilder(ADD_INITIAL_STATE_METHOD_NAME)
MethodSpec.methodBuilder(INITIAL_STATE_SETTER_METHOD_NAME)
.addModifiers(Modifier.PUBLIC)
.returns(storeModel.getBuilder())
.addParameter(getParameterSpec(reducerModel.getState()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,18 @@ public static class Counter {
" super();",
" }",
"",
" @Override",
" public Builder addMiddleware(Middleware middleware) {",
" getMiddlewares().add(middleware);",
" super.addMiddleware(middleware);",
" return this;",
" }",
"",
" public Builder addReducer(CounterReducer counterReducer) {",
" public Builder setReducer(CounterReducer counterReducer) {",
" this.counterReducer = counterReducer;",
" return this;",
" }",
"",
" public Builder addInitialState(Counter counter) {",
" public Builder setInitialState(Counter counter) {",
" this.counter = counter;",
" return this;",
" }",
Expand Down Expand Up @@ -148,17 +149,18 @@ public static class CounterTakesOnlyStateArgument {
" super();",
" }",
"",
" @Override",
" public Builder addMiddleware(Middleware middleware) {",
" getMiddlewares().add(middleware);",
" super.addMiddleware(middleware);",
" return this;",
" }",
"",
" public Builder addReducer(CounterReducer counterReducer) {",
" public Builder setReducer(CounterReducer counterReducer) {",
" this.counterReducer = counterReducer;",
" return this;",
" }",
"",
" public Builder addInitialState(Counter counter) {",
" public Builder setInitialState(Counter counter) {",
" this.counter = counter;",
" return this;",
" }",
Expand Down Expand Up @@ -241,17 +243,18 @@ public static class TodoList {
" super();",
" }",
"",
" @Override",
" public Builder addMiddleware(Middleware middleware) {",
" getMiddlewares().add(middleware);",
" super.addMiddleware(middleware);",
" return this;",
" }",
"",
" public Builder addReducer(TodoListReducer todoListReducer) {",
" public Builder setReducer(TodoListReducer todoListReducer) {",
" this.todoListReducer = todoListReducer;",
" return this;",
" }",
"",
" public Builder addInitialState(TodoList todoList) {",
" public Builder setInitialState(TodoList todoList) {",
" this.todoList = todoList;",
" return this;",
" }",
Expand Down Expand Up @@ -296,10 +299,10 @@ public static class CombinedTwoReducers {
" protected DroiduxRootStore(Builder builder) {",
" super(builder);",
" this.counterReducer = builder.counterReducer;",
" this.counterStore = new DroiduxCounterStore.Builder().addReducer(counterReducer).addInitialState(builder.counter).build();",
" this.counterStore = new DroiduxCounterStore.Builder().setReducer(counterReducer).setInitialState(builder.counter).build();",
" addStore(counterStore);",
" this.todoListReducer = builder.todoListReducer;",
" this.todoListStore = new DroiduxTodoListStore.Builder().addReducer(todoListReducer).addInitialState(builder.todoList).build();",
" this.todoListStore = new DroiduxTodoListStore.Builder().setReducer(todoListReducer).setInitialState(builder.todoList).build();",
" addStore(todoListStore);",
" }",
"",
Expand All @@ -321,27 +324,28 @@ public static class CombinedTwoReducers {
" super();",
" }",
"",
" @Override",
" public Builder addMiddleware(Middleware middleware) {",
" getMiddlewares().add(middleware);",
" super.addMiddleware(middleware);",
" return this;",
" }",
"",
" public Builder addReducer(CounterReducer counterReducer) {",
" public Builder setReducer(CounterReducer counterReducer) {",
" this.counterReducer = counterReducer;",
" return this;",
" }",
"",
" public Builder addInitialState(Counter counter) {",
" public Builder setInitialState(Counter counter) {",
" this.counter = counter;",
" return this;",
" }",
"",
" public Builder addReducer(TodoListReducer todoListReducer) {",
" public Builder setReducer(TodoListReducer todoListReducer) {",
" this.todoListReducer = todoListReducer;",
" return this;",
" }",
"",
" public Builder addInitialState(TodoList todoList) {",
" public Builder setInitialState(TodoList todoList) {",
" this.todoList = todoList;",
" return this;",
" }",
Expand Down
5 changes: 3 additions & 2 deletions droidux/src/main/java/info/izumin/android/droidux/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ public Builder() {
this.middlewares = new ArrayList<>();
}

protected List<Middleware> getMiddlewares() {
return middlewares;
public Builder addMiddleware(Middleware middleware) {
middlewares.add(middleware);
return this;
}

public abstract Store build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public void onCreate() {
super.onCreate();
store = new DroiduxRootStore.Builder()
.addMiddleware(new Logger())
.addReducer(new TodoListReducer())
.addInitialState(new TodoList(new ArrayList<>()))
.setReducer(new TodoListReducer())
.setInitialState(new TodoList(new ArrayList<>()))
.build();
}

Expand Down

0 comments on commit 54fff91

Please sign in to comment.