@@ -115,105 +115,191 @@ private static void initAuth() {
115
115
}
116
116
117
117
/**
118
- * 多厂商icon上传,多厂商上传同一个icon文件
118
+ * 多厂商icon上传,多厂商上传同一个icon文件,多线程执行
119
119
*
120
120
* @param file 本地icon文件
121
121
* @return 多厂商icon上传结果
122
122
* @throws FileNotFoundException 本地icon文件找不到
123
123
*/
124
124
public static Map <String , Result > uploadIcon (File file ) throws FileNotFoundException {
125
+ return uploadIcon (CommonConfig .mThread , file );
126
+ }
127
+
128
+ /**
129
+ * 多厂商icon上传,多厂商上传同一个icon文件
130
+ *
131
+ * @param mThread 是否使用多线程
132
+ * @param file 本地icon文件
133
+ * @return 多厂商icon上传结果
134
+ * @throws FileNotFoundException 本地icon文件找不到
135
+ */
136
+ public static Map <String , Result > uploadIcon (boolean mThread , File file ) throws FileNotFoundException {
125
137
if (!file .exists ()) {
126
138
throw new FileNotFoundException (file .getAbsolutePath ());
127
139
}
128
140
init ();
129
- Map <String , Result > result = new HashMap <>();
141
+ Map <String , Result > result = new HashMap <>(factory . size () );
130
142
if (factory .size () > 0 ) {
131
- Map <String , CompletableFuture <Result >> futures = factory .entrySet ().stream ()
132
- .collect (Collectors .toMap (
133
- Map .Entry ::getKey ,
134
- e -> CompletableFuture .supplyAsync (() -> e .getValue ().uploadIcon (file ), myExecutor )));
143
+ if (mThread ) {
144
+ Map <String , CompletableFuture <Result >> futures = factory .entrySet ().stream ()
145
+ .collect (Collectors .toMap (
146
+ Map .Entry ::getKey ,
147
+ e -> CompletableFuture .supplyAsync (() -> e .getValue ().uploadIcon (file ), myExecutor )));
135
148
136
- getFutureResult (result , futures , Thread .currentThread ().getStackTrace ()[1 ].getMethodName ());
149
+ getFutureResult (result , futures , Thread .currentThread ().getStackTrace ()[1 ].getMethodName ());
150
+ } else {
151
+ factory .forEach ((k , v ) -> {
152
+ try {
153
+ result .put (k , v .uploadIcon (file ));
154
+ } catch (AuthFailedException e ) {
155
+ result .put (k , Result .authFail ());
156
+ }
157
+ });
158
+ }
137
159
}
138
160
return result ;
139
161
}
140
162
141
163
/**
142
- * 多厂商icon上传,指定每个厂商上传的icon文件
164
+ * 多厂商icon上传,指定每个厂商上传的icon文件,多线程执行
143
165
*
144
166
* @param manufacturerFile icon文件
145
167
* @return 多厂商icon上传结果
146
168
*/
147
169
public static Map <String , Result > uploadIcon (ManufacturerFile ... manufacturerFile ) {
170
+ return uploadIcon (CommonConfig .mThread , manufacturerFile );
171
+ }
172
+
173
+ /**
174
+ * 多厂商icon上传,指定每个厂商上传的icon文件
175
+ *
176
+ * @param mThread 是否使用多线程
177
+ * @param manufacturerFile icon文件
178
+ * @return 多厂商icon上传结果
179
+ */
180
+ public static Map <String , Result > uploadIcon (boolean mThread , ManufacturerFile ... manufacturerFile ) {
148
181
init ();
149
- Map <String , Result > result = new HashMap <>();
182
+ Map <String , Result > result = new HashMap <>(manufacturerFile . length );
150
183
if (factory .size () > 0 ) {
184
+ if (mThread ) {
185
+ Function <ManufacturerFile , CompletableFuture <Result >> valueMapper = file -> {
186
+ if (!file .exists ()) {
187
+ return CompletableFuture .supplyAsync (() -> Result .fail (String .format ("file %s not found" , file .getAbsolutePath ())), myExecutor );
188
+ }
189
+ Optional <BaseManufacturer > optional = Optional .ofNullable (factory .get (file .getManufacturerName ()));
190
+ return optional .map (baseManufacturer -> CompletableFuture .supplyAsync (() -> baseManufacturer .uploadIcon (file ), myExecutor ))
191
+ .orElseGet (() -> CompletableFuture .supplyAsync (Result ::noInstance , myExecutor ));
192
+ };
151
193
152
- Function <ManufacturerFile , CompletableFuture <Result >> valueMapper = file -> {
153
- if (!file .exists ()) {
154
- return CompletableFuture .supplyAsync (() -> Result .fail (String .format ("file %s not found" , file .getAbsolutePath ())), myExecutor );
155
- }
156
- Optional <BaseManufacturer > optional = Optional .ofNullable (factory .get (file .getManufacturerName ()));
157
- return optional .map (baseManufacturer -> CompletableFuture .supplyAsync (() -> baseManufacturer .uploadIcon (file ), myExecutor ))
158
- .orElseGet (() -> CompletableFuture .supplyAsync (Result ::noInstance , myExecutor ));
159
- };
194
+ Map <String , CompletableFuture <Result >> futures = Arrays .stream (manufacturerFile )
195
+ .collect (Collectors .toMap (ManufacturerFile ::getManufacturerName , valueMapper ));
160
196
161
- Map <String , CompletableFuture <Result >> futures = Arrays .stream (manufacturerFile )
162
- .collect (Collectors .toMap (ManufacturerFile ::getManufacturerName , valueMapper ));
163
-
164
- getFutureResult (result , futures , Thread .currentThread ().getStackTrace ()[1 ].getMethodName ());
197
+ getFutureResult (result , futures , Thread .currentThread ().getStackTrace ()[1 ].getMethodName ());
198
+ } else {
199
+ Arrays .stream (manufacturerFile ).forEach (file -> {
200
+ BaseManufacturer manufacturer = factory .get (file .getManufacturerName ());
201
+ try {
202
+ result .put (file .getManufacturerName (), manufacturer .uploadIcon (file ));
203
+ } catch (AuthFailedException e ) {
204
+ result .put (file .getManufacturerName (), Result .authFail ());
205
+ }
206
+ });
207
+ }
165
208
}
166
209
return result ;
167
210
}
168
211
169
212
/**
170
- * 多厂商图片上传,多厂商上传同一个图片文件
213
+ * 多厂商图片上传,多厂商上传同一个图片文件,多线程执行
171
214
*
172
215
* @param file 本地图片文件
173
216
* @return 多厂商图片上传结果
174
- * @throws FileNotFoundException 本地icon文件找不到
217
+ * @throws FileNotFoundException 本地图片文件找不到
175
218
*/
176
219
public static Map <String , Result > uploadPic (File file ) throws FileNotFoundException {
220
+ return uploadPic (CommonConfig .mThread , file );
221
+ }
222
+
223
+ /**
224
+ * 多厂商图片上传,多厂商上传同一个图片文件
225
+ *
226
+ * @param mThread 是否使用多线程
227
+ * @param file 本地图片文件
228
+ * @return 多厂商图片上传结果
229
+ * @throws FileNotFoundException 本地图片文件找不到
230
+ */
231
+ public static Map <String , Result > uploadPic (boolean mThread , File file ) throws FileNotFoundException {
177
232
if (!file .exists ()) {
178
233
throw new FileNotFoundException (file .getAbsolutePath ());
179
234
}
180
235
init ();
181
- Map <String , Result > result = new HashMap <>();
236
+ Map <String , Result > result = new HashMap <>(factory . size () );
182
237
if (factory .size () > 0 ) {
183
- Map <String , CompletableFuture <Result >> futures = factory .entrySet ().stream ()
184
- .collect (Collectors .toMap (
185
- Map .Entry ::getKey ,
186
- e -> CompletableFuture .supplyAsync (() -> e .getValue ().uploadPic (file ), myExecutor )));
238
+ if (mThread ) {
239
+ Map <String , CompletableFuture <Result >> futures = factory .entrySet ().stream ()
240
+ .collect (Collectors .toMap (
241
+ Map .Entry ::getKey ,
242
+ e -> CompletableFuture .supplyAsync (() -> e .getValue ().uploadPic (file ), myExecutor )));
187
243
188
- getFutureResult (result , futures , Thread .currentThread ().getStackTrace ()[1 ].getMethodName ());
244
+ getFutureResult (result , futures , Thread .currentThread ().getStackTrace ()[1 ].getMethodName ());
245
+ } else {
246
+ factory .forEach ((k , v ) -> {
247
+ try {
248
+ result .put (k , v .uploadPic (file ));
249
+ } catch (AuthFailedException e ) {
250
+ result .put (k , Result .authFail ());
251
+ }
252
+ });
253
+ }
189
254
}
190
255
return result ;
191
256
}
192
257
193
258
/**
194
- * 多厂商图片上传,指定每个厂商上传的图片文件
259
+ * 多厂商图片上传,指定每个厂商上传的图片文件,多线程执行
195
260
*
196
261
* @param manufacturerFile 图片文件
197
262
* @return 多厂商图片上传结果
198
263
*/
199
264
public static Map <String , Result > uploadPic (ManufacturerFile ... manufacturerFile ) {
265
+ return uploadPic (CommonConfig .mThread , manufacturerFile );
266
+ }
267
+
268
+ /**
269
+ * 多厂商图片上传,指定每个厂商上传的图片文件
270
+ *
271
+ * @param mThread 是否使用多线程
272
+ * @param manufacturerFile 图片文件
273
+ * @return 多厂商图片上传结果
274
+ */
275
+ public static Map <String , Result > uploadPic (boolean mThread , ManufacturerFile ... manufacturerFile ) {
200
276
init ();
201
- Map <String , Result > result = new HashMap <>();
277
+ Map <String , Result > result = new HashMap <>(manufacturerFile . length );
202
278
if (factory .size () > 0 ) {
279
+ if (mThread ) {
280
+ Function <ManufacturerFile , CompletableFuture <Result >> valueMapper = file -> {
281
+ if (!file .exists ()) {
282
+ return CompletableFuture .supplyAsync (() -> Result .fail (String .format ("file %s not found" , file .getAbsolutePath ())), myExecutor );
283
+ }
284
+ Optional <BaseManufacturer > optional = Optional .ofNullable (factory .get (file .getManufacturerName ()));
285
+ return optional .map (baseManufacturer -> CompletableFuture .supplyAsync (() -> baseManufacturer .uploadPic (file ), myExecutor ))
286
+ .orElseGet (() -> CompletableFuture .supplyAsync (Result ::noInstance , myExecutor ));
287
+ };
203
288
204
- Function <ManufacturerFile , CompletableFuture <Result >> valueMapper = file -> {
205
- if (!file .exists ()) {
206
- return CompletableFuture .supplyAsync (() -> Result .fail (String .format ("file %s not found" , file .getAbsolutePath ())), myExecutor );
207
- }
208
- Optional <BaseManufacturer > optional = Optional .ofNullable (factory .get (file .getManufacturerName ()));
209
- return optional .map (baseManufacturer -> CompletableFuture .supplyAsync (() -> baseManufacturer .uploadPic (file ), myExecutor ))
210
- .orElseGet (() -> CompletableFuture .supplyAsync (Result ::noInstance , myExecutor ));
211
- };
289
+ Map <String , CompletableFuture <Result >> futures = Arrays .stream (manufacturerFile )
290
+ .collect (Collectors .toMap (ManufacturerFile ::getManufacturerName , valueMapper ));
212
291
213
- Map <String , CompletableFuture <Result >> futures = Arrays .stream (manufacturerFile )
214
- .collect (Collectors .toMap (ManufacturerFile ::getManufacturerName , valueMapper ));
215
-
216
- getFutureResult (result , futures , Thread .currentThread ().getStackTrace ()[1 ].getMethodName ());
292
+ getFutureResult (result , futures , Thread .currentThread ().getStackTrace ()[1 ].getMethodName ());
293
+ } else {
294
+ Arrays .stream (manufacturerFile ).forEach (file -> {
295
+ BaseManufacturer manufacturer = factory .get (file .getManufacturerName ());
296
+ try {
297
+ result .put (file .getManufacturerName (), manufacturer .uploadPic (file ));
298
+ } catch (AuthFailedException e ) {
299
+ result .put (file .getManufacturerName (), Result .authFail ());
300
+ }
301
+ });
302
+ }
217
303
}
218
304
return result ;
219
305
}
0 commit comments