Skip to content

Commit 67c5059

Browse files
author
Javen
committed
Refactor code to reduce duplicated code in notification builder.
1 parent d9fc617 commit 67c5059

File tree

4 files changed

+40
-230
lines changed

4 files changed

+40
-230
lines changed

src/main/java/cn/jpush/api/push/model/notification/AndroidNotification.java

Lines changed: 5 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package cn.jpush.api.push.model.notification;
22

3-
import java.util.HashMap;
43
import java.util.Map;
54

6-
import cn.jpush.api.utils.Preconditions;
7-
85
import com.google.gson.JsonElement;
96
import com.google.gson.JsonObject;
107
import com.google.gson.JsonPrimitive;
@@ -58,10 +55,14 @@ public JsonElement toJSON() {
5855
}
5956

6057

61-
public static class Builder extends PlatformNotification.Builder<AndroidNotification> {
58+
public static class Builder extends PlatformNotification.Builder<AndroidNotification, Builder> {
6259
private String title;
6360
private int builderId;
6461

62+
protected Builder getThis() {
63+
return this;
64+
}
65+
6566
public Builder setTitle(String title) {
6667
this.title = title;
6768
return this;
@@ -77,73 +78,6 @@ public Builder setAlert(String alert) {
7778
return this;
7879
}
7980

80-
public Builder addExtra(String key, String value) {
81-
Preconditions.checkArgument(! (null == key), "Key should not be null.");
82-
if (null == value) {
83-
LOG.debug("Extra value is null, throw away it.");
84-
return this;
85-
}
86-
if (null == extrasBuilder) {
87-
extrasBuilder = new HashMap<String, String>();
88-
}
89-
extrasBuilder.put(key, value);
90-
return this;
91-
}
92-
93-
public Builder addExtras(Map<String, String> extras) {
94-
if (null == extras) {
95-
LOG.warn("Null extras param. Throw away it.");
96-
return this;
97-
}
98-
99-
if (null == extrasBuilder) {
100-
extrasBuilder = new HashMap<String, String>();
101-
}
102-
for (String key : extras.keySet()) {
103-
extrasBuilder.put(key, extras.get(key));
104-
}
105-
return this;
106-
}
107-
108-
public Builder addExtra(String key, Number value) {
109-
Preconditions.checkArgument(! (null == key), "Key should not be null.");
110-
if (null == value) {
111-
LOG.debug("Extra value is null, throw away it.");
112-
return this;
113-
}
114-
if (null == numberExtrasBuilder) {
115-
numberExtrasBuilder = new HashMap<String, Number>();
116-
}
117-
numberExtrasBuilder.put(key, value);
118-
return this;
119-
}
120-
121-
public Builder addExtra(String key, Boolean value) {
122-
Preconditions.checkArgument(! (null == key), "Key should not be null.");
123-
if (null == value) {
124-
LOG.debug("Extra value is null, throw away it.");
125-
return this;
126-
}
127-
if (null == booleanExtrasBuilder) {
128-
booleanExtrasBuilder = new HashMap<String, Boolean>();
129-
}
130-
booleanExtrasBuilder.put(key, value);
131-
return this;
132-
}
133-
134-
public Builder addExtra(String key, JsonObject value) {
135-
Preconditions.checkArgument(! (null == key), "Key should not be null.");
136-
if (null == value) {
137-
LOG.debug("Extra value is null, throw away it.");
138-
return this;
139-
}
140-
if (null == jsonExtrasBuilder) {
141-
jsonExtrasBuilder = new HashMap<String, JsonObject>();
142-
}
143-
jsonExtrasBuilder.put(key, value);
144-
return this;
145-
}
146-
14781

14882
public AndroidNotification build() {
14983
return new AndroidNotification(alert, title, builderId,

src/main/java/cn/jpush/api/push/model/notification/IosNotification.java

Lines changed: 5 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package cn.jpush.api.push.model.notification;
22

3-
import java.util.HashMap;
43
import java.util.Map;
54

65
import cn.jpush.api.common.ServiceHelper;
7-
import cn.jpush.api.utils.Preconditions;
86

97
import com.google.gson.JsonElement;
108
import com.google.gson.JsonObject;
@@ -108,14 +106,18 @@ public JsonElement toJSON() {
108106
}
109107

110108

111-
public static class Builder extends PlatformNotification.Builder<IosNotification> {
109+
public static class Builder extends PlatformNotification.Builder<IosNotification, Builder> {
112110
private String sound;
113111
private String badge;
114112
private boolean contentAvailable = false;
115113
private boolean soundDisabled = false;
116114
private boolean badgeDisabled = false;
117115
private String category;
118116

117+
protected Builder getThis() {
118+
return this;
119+
}
120+
119121
public Builder setSound(String sound) {
120122
this.sound = sound;
121123
return this;
@@ -176,73 +178,6 @@ public Builder setAlert(String alert) {
176178
return this;
177179
}
178180

179-
public Builder addExtra(String key, String value) {
180-
Preconditions.checkArgument(! (null == key), "Key should not be null.");
181-
if (null == value) {
182-
LOG.debug("Extra value is null, throw away it.");
183-
return this;
184-
}
185-
if (null == extrasBuilder) {
186-
extrasBuilder = new HashMap<String, String>();
187-
}
188-
extrasBuilder.put(key, value);
189-
return this;
190-
}
191-
192-
public Builder addExtras(Map<String, String> extras) {
193-
if (null == extras) {
194-
LOG.warn("Null extras param. Throw away it.");
195-
return this;
196-
}
197-
198-
if (null == extrasBuilder) {
199-
extrasBuilder = new HashMap<String, String>();
200-
}
201-
for (String key : extras.keySet()) {
202-
extrasBuilder.put(key, extras.get(key));
203-
}
204-
return this;
205-
}
206-
207-
public Builder addExtra(String key, Number value) {
208-
Preconditions.checkArgument(! (null == key), "Key should not be null.");
209-
if (null == value) {
210-
LOG.debug("Extra value is null, throw away it.");
211-
return this;
212-
}
213-
if (null == numberExtrasBuilder) {
214-
numberExtrasBuilder = new HashMap<String, Number>();
215-
}
216-
numberExtrasBuilder.put(key, value);
217-
return this;
218-
}
219-
220-
public Builder addExtra(String key, Boolean value) {
221-
Preconditions.checkArgument(! (null == key), "Key should not be null.");
222-
if (null == value) {
223-
LOG.debug("Extra value is null, throw away it.");
224-
return this;
225-
}
226-
if (null == booleanExtrasBuilder) {
227-
booleanExtrasBuilder = new HashMap<String, Boolean>();
228-
}
229-
booleanExtrasBuilder.put(key, value);
230-
return this;
231-
}
232-
233-
public Builder addExtra(String key, JsonObject value) {
234-
Preconditions.checkArgument(! (null == key), "Key should not be null.");
235-
if (null == value) {
236-
LOG.debug("Extra value is null, throw away it.");
237-
return this;
238-
}
239-
if (null == jsonExtrasBuilder) {
240-
jsonExtrasBuilder = new HashMap<String, JsonObject>();
241-
}
242-
jsonExtrasBuilder.put(key, value);
243-
return this;
244-
}
245-
246181

247182
public IosNotification build() {
248183
return new IosNotification(alert, sound, badge, contentAvailable,

src/main/java/cn/jpush/api/push/model/notification/PlatformNotification.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,32 +103,40 @@ protected void setAlert(String alert) {
103103

104104
protected abstract String getPlatform();
105105

106-
protected abstract static class Builder<T> {
106+
protected abstract static class Builder<T extends PlatformNotification, B extends Builder<T, B>> {
107+
private B theBuilder;
108+
107109
protected String alert;
108110
protected Map<String, String> extrasBuilder;
109111
protected Map<String, Number> numberExtrasBuilder;
110112
protected Map<String, Boolean> booleanExtrasBuilder;
111113
protected Map<String, JsonObject> jsonExtrasBuilder;
112114

113-
public abstract Builder<T> setAlert(String alert);
115+
public Builder () {
116+
theBuilder = getThis();
117+
}
118+
119+
protected abstract B getThis();
120+
121+
public abstract B setAlert(String alert);
114122

115-
public Builder<T> addExtra(String key, String value) {
123+
public B addExtra(String key, String value) {
116124
Preconditions.checkArgument(! (null == key), "Key should not be null.");
117125
if (null == value) {
118126
LOG.debug("Extra value is null, throw away it.");
119-
return this;
127+
return theBuilder;
120128
}
121129
if (null == extrasBuilder) {
122130
extrasBuilder = new HashMap<String, String>();
123131
}
124132
extrasBuilder.put(key, value);
125-
return this;
133+
return theBuilder;
126134
}
127135

128-
public Builder<T> addExtras(Map<String, String> extras) {
136+
public B addExtras(Map<String, String> extras) {
129137
if (null == extras) {
130138
LOG.warn("Null extras param. Throw away it.");
131-
return this;
139+
return theBuilder;
132140
}
133141

134142
if (null == extrasBuilder) {
@@ -137,46 +145,46 @@ public Builder<T> addExtras(Map<String, String> extras) {
137145
for (String key : extras.keySet()) {
138146
extrasBuilder.put(key, extras.get(key));
139147
}
140-
return this;
148+
return theBuilder;
141149
}
142150

143-
public Builder<T> addExtra(String key, Number value) {
151+
public B addExtra(String key, Number value) {
144152
Preconditions.checkArgument(! (null == key), "Key should not be null.");
145153
if (null == value) {
146154
LOG.debug("Extra value is null, throw away it.");
147-
return this;
155+
return theBuilder;
148156
}
149157
if (null == numberExtrasBuilder) {
150158
numberExtrasBuilder = new HashMap<String, Number>();
151159
}
152160
numberExtrasBuilder.put(key, value);
153-
return this;
161+
return theBuilder;
154162
}
155163

156-
public Builder<T> addExtra(String key, Boolean value) {
164+
public B addExtra(String key, Boolean value) {
157165
Preconditions.checkArgument(! (null == key), "Key should not be null.");
158166
if (null == value) {
159167
LOG.debug("Extra value is null, throw away it.");
160-
return this;
168+
return theBuilder;
161169
}
162170
if (null == booleanExtrasBuilder) {
163171
booleanExtrasBuilder = new HashMap<String, Boolean>();
164172
}
165173
booleanExtrasBuilder.put(key, value);
166-
return this;
174+
return theBuilder;
167175
}
168176

169-
public Builder<T> addExtra(String key, JsonObject value) {
177+
public B addExtra(String key, JsonObject value) {
170178
Preconditions.checkArgument(! (null == key), "Key should not be null.");
171179
if (null == value) {
172180
LOG.debug("Extra value is null, throw away it.");
173-
return this;
181+
return theBuilder;
174182
}
175183
if (null == jsonExtrasBuilder) {
176184
jsonExtrasBuilder = new HashMap<String, JsonObject>();
177185
}
178186
jsonExtrasBuilder.put(key, value);
179-
return this;
187+
return theBuilder;
180188
}
181189

182190
public abstract T build();

0 commit comments

Comments
 (0)