16
16
17
17
package io .appium .java_client .android .options .server ;
18
18
19
- import com .google .gson .JsonArray ;
20
- import com .google .gson .JsonObject ;
21
- import com .google .gson .JsonParser ;
19
+ import io .appium .java_client .internal .CapabilityHelpers ;
20
+ import io .appium .java_client .remote .options .BaseMapOptionData ;
22
21
23
- import java .util .ArrayList ;
22
+ import java .util .HashMap ;
24
23
import java .util .List ;
24
+ import java .util .Map ;
25
25
import java .util .Optional ;
26
26
27
- public class EspressoBuildConfig {
27
+ public class EspressoBuildConfig extends BaseMapOptionData < EspressoBuildConfig > {
28
28
public static final String TOOLS_VERSION = "toolsVersions" ;
29
29
public static final String ADDITIONAL_APP_DEPENDENCIES = "additionalAppDependencies" ;
30
30
public static final String ADDITIONAL_ANDROID_TEST_DEPENDENCIES
31
31
= "additionalAndroidTestDependencies" ;
32
32
33
- private JsonObject json ;
34
-
35
33
public EspressoBuildConfig () {
36
- }
37
-
38
- public EspressoBuildConfig (JsonObject json ) {
39
- this .json = json ;
34
+ super ();
40
35
}
41
36
42
37
public EspressoBuildConfig (String json ) {
43
- this ( JsonParser . parseString ( json ). getAsJsonObject () );
38
+ super ( json );
44
39
}
45
40
46
41
private EspressoBuildConfig assignToolsVersionsField (String name , Object value ) {
47
- if (json == null ) {
48
- json = new JsonObject ();
49
- }
50
- boolean hasTools = json .has (TOOLS_VERSION );
51
- JsonObject toolsVersions = hasTools
52
- ? json .getAsJsonObject (TOOLS_VERSION )
53
- : new JsonObject ();
54
- if (value instanceof Number ) {
55
- toolsVersions .addProperty (name , (Number ) value );
56
- } else {
57
- toolsVersions .addProperty (name , String .valueOf (value ));
58
- }
59
- if (!hasTools ) {
60
- json .add (TOOLS_VERSION , toolsVersions );
42
+ Optional <Map <String , Object >> toolsVersionsOptional = getOptionValue (TOOLS_VERSION );
43
+ Map <String , Object > toolsVersions = toolsVersionsOptional .orElseGet (HashMap ::new );
44
+ toolsVersions .put (name , value );
45
+ if (!toolsVersionsOptional .isPresent ()) {
46
+ assignOptionValue (TOOLS_VERSION , toolsVersions );
61
47
}
62
48
return this ;
63
49
}
64
50
65
51
private <R > Optional <R > getToolsVersionsFieldValue (String name ) {
52
+ Optional <Map <String , Object >> toolsVersionsOptional = getOptionValue (TOOLS_VERSION );
66
53
//noinspection unchecked
67
- return json == null || !json .has (TOOLS_VERSION )
68
- ? Optional .empty ()
69
- : Optional .ofNullable ((R ) json .getAsJsonObject (TOOLS_VERSION ).get (name ));
54
+ return toolsVersionsOptional .map ((v ) -> (R ) v .getOrDefault (name , null ));
70
55
}
71
56
72
57
/**
@@ -167,7 +152,7 @@ public EspressoBuildConfig withMinSdk(int apiLevel) {
167
152
*/
168
153
public Optional <Integer > getMinSdkVersion () {
169
154
Optional <Object > result = getToolsVersionsFieldValue ("minSdk" );
170
- return result .map (( v ) -> Integer . parseInt ( String . valueOf ( v )) );
155
+ return result .map (CapabilityHelpers :: toInteger );
171
156
}
172
157
173
158
/**
@@ -188,7 +173,7 @@ public EspressoBuildConfig withTargetSdk(int apiLevel) {
188
173
*/
189
174
public Optional <Integer > getTargetSdkVersion () {
190
175
Optional <Object > result = getToolsVersionsFieldValue ("targetSdk" );
191
- return result .map (( v ) -> Integer . parseInt ( String . valueOf ( v )) );
176
+ return result .map (CapabilityHelpers :: toInteger );
192
177
}
193
178
194
179
/**
@@ -211,35 +196,6 @@ public Optional<String> getKotlinVersion() {
211
196
return getToolsVersionsFieldValue ("kotlin" );
212
197
}
213
198
214
- private EspressoBuildConfig assignDependenciesField (String name , List <String > value ) {
215
- if (json == null ) {
216
- json = new JsonObject ();
217
- }
218
- boolean hasField = json .has (name );
219
- JsonArray dependencies = hasField
220
- ? json .getAsJsonArray (name )
221
- : new JsonArray ();
222
- while (dependencies .size () > 0 ) {
223
- dependencies .remove (0 );
224
- }
225
- value .forEach (dependencies ::add );
226
- if (!hasField ) {
227
- json .add (name , dependencies );
228
- }
229
- return this ;
230
- }
231
-
232
- private Optional <List <String >> getDependenciesValue (String name ) {
233
- return json == null
234
- ? Optional .empty ()
235
- : Optional .ofNullable (json .getAsJsonArray (name ))
236
- .map ((v ) -> {
237
- List <String > result = new ArrayList <>();
238
- v .forEach ((x ) -> result .add (String .valueOf (x )));
239
- return result ;
240
- });
241
- }
242
-
243
199
/**
244
200
* Set a non-empty array of dependent module names with their versions.
245
201
* The scripts add all these items as "implementation" lines of dependencies
@@ -249,7 +205,7 @@ private Optional<List<String>> getDependenciesValue(String name) {
249
205
* @return self instance for chaining.
250
206
*/
251
207
public EspressoBuildConfig withAdditionalAppDependencies (List <String > dependencies ) {
252
- return assignDependenciesField (ADDITIONAL_APP_DEPENDENCIES , dependencies );
208
+ return assignOptionValue (ADDITIONAL_APP_DEPENDENCIES , dependencies );
253
209
}
254
210
255
211
/**
@@ -258,7 +214,7 @@ public EspressoBuildConfig withAdditionalAppDependencies(List<String> dependenci
258
214
* @return Dependent module names with their versions.
259
215
*/
260
216
public Optional <List <String >> getAdditionalAppDependencies () {
261
- return getDependenciesValue (ADDITIONAL_APP_DEPENDENCIES );
217
+ return getOptionValue (ADDITIONAL_APP_DEPENDENCIES );
262
218
}
263
219
264
220
/**
@@ -270,7 +226,7 @@ public Optional<List<String>> getAdditionalAppDependencies() {
270
226
* @return self instance for chaining.
271
227
*/
272
228
public EspressoBuildConfig withAdditionalAndroidTestDependencies (List <String > dependencies ) {
273
- return assignDependenciesField (ADDITIONAL_ANDROID_TEST_DEPENDENCIES , dependencies );
229
+ return assignOptionValue (ADDITIONAL_ANDROID_TEST_DEPENDENCIES , dependencies );
274
230
}
275
231
276
232
/**
@@ -279,15 +235,6 @@ public EspressoBuildConfig withAdditionalAndroidTestDependencies(List<String> de
279
235
* @return Dependent module names with their versions.
280
236
*/
281
237
public Optional <List <String >> getAdditionalAndroidTestDependencies () {
282
- return getDependenciesValue (ADDITIONAL_ANDROID_TEST_DEPENDENCIES );
283
- }
284
-
285
- public JsonObject toJson () {
286
- return Optional .ofNullable (json ).orElseGet (JsonObject ::new );
287
- }
288
-
289
- @ Override
290
- public String toString () {
291
- return toJson ().toString ();
238
+ return getOptionValue (ADDITIONAL_ANDROID_TEST_DEPENDENCIES );
292
239
}
293
240
}
0 commit comments