Skip to content

refactor: Replace private usages of Guava Collections API with Java Collections API #2136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.appium.java_client;

import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.remote.Response;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -44,7 +43,7 @@ default Map<String, Object> executeCdpCommand(String command, @Nullable Map<Stri
data.put("params", params == null ? Collections.emptyMap() : params);
Response response = execute(EXECUTE_GOOGLE_CDP_COMMAND, data);
//noinspection unchecked
return ImmutableMap.copyOf((Map<String, Object>) response.getValue());
return Collections.unmodifiableMap((Map<String, Object>) response.getValue());
}

/**
Expand Down
85 changes: 32 additions & 53 deletions src/main/java/io/appium/java_client/InteractsWithApps.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.appium.java_client;

import com.google.common.collect.ImmutableMap;
import io.appium.java_client.appmanagement.ApplicationState;
import io.appium.java_client.appmanagement.BaseActivateApplicationOptions;
import io.appium.java_client.appmanagement.BaseInstallApplicationOptions;
Expand All @@ -28,9 +27,8 @@

import javax.annotation.Nullable;
import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static io.appium.java_client.MobileCommand.ACTIVATE_APP;
import static io.appium.java_client.MobileCommand.INSTALL_APP;
Expand All @@ -40,6 +38,7 @@
import static io.appium.java_client.MobileCommand.RUN_APP_IN_BACKGROUND;
import static io.appium.java_client.MobileCommand.TERMINATE_APP;
import static java.util.Objects.requireNonNull;
import static java.util.Optional.ofNullable;

@SuppressWarnings({"rawtypes", "unchecked"})
public interface InteractsWithApps extends ExecutesMethod, CanRememberExtensionPresence {
Expand All @@ -63,23 +62,17 @@ default void installApp(String appPath) {
default void installApp(String appPath, @Nullable BaseInstallApplicationOptions options) {
final String extName = "mobile: installApp";
try {
Map<String, Object> args = ImmutableMap.<String, Object>builder()
.put("app", appPath)
.put("appPath", appPath)
.putAll(Optional.ofNullable(options).map(BaseOptions::build).orElseGet(Collections::emptyMap))
.build();
var args = new HashMap<String, Object>();
args.put("app", appPath);
args.put("appPath", appPath);
ofNullable(options).map(BaseOptions::build).ifPresent(args::putAll);
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, args);
} catch (UnsupportedCommandException | InvalidArgumentException e) {
// TODO: Remove the fallback
Map args = ImmutableMap.builder()
.put("appPath", appPath)
.putAll(Optional.ofNullable(options).map(
opts -> Map.of("options", opts.build())
).orElseGet(Map::of))
.build();
CommandExecutionHelper.execute(
markExtensionAbsence(extName), Map.entry(INSTALL_APP, args)
);
var args = new HashMap<String, Object>();
args.put("appPath", appPath);
ofNullable(options).map(BaseOptions::build).ifPresent(opts -> args.put("options", opts));
CommandExecutionHelper.execute(markExtensionAbsence(extName), Map.entry(INSTALL_APP, args));
}
}

Expand Down Expand Up @@ -153,22 +146,18 @@ default boolean removeApp(String bundleId) {
default boolean removeApp(String bundleId, @Nullable BaseRemoveApplicationOptions options) {
final String extName = "mobile: removeApp";
try {
Map<String, Object> args = ImmutableMap.<String, Object>builder()
.put("bundleId", bundleId)
.put("appId", bundleId)
.putAll(Optional.ofNullable(options).map(BaseOptions::build).orElseGet(Collections::emptyMap))
.build();
var args = new HashMap<String, Object>();
args.put("bundleId", bundleId);
args.put("appId", bundleId);
ofNullable(options).map(BaseOptions::build).ifPresent(args::putAll);
return requireNonNull(
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, args)
);
} catch (UnsupportedCommandException | InvalidArgumentException e) {
// TODO: Remove the fallback
Map args = ImmutableMap.builder()
.put("bundleId", bundleId)
.putAll(Optional.ofNullable(options).map(
opts -> Map.of("options", opts.build())
).orElseGet(Map::of))
.build();
var args = new HashMap<String, Object>();
args.put("bundleId", bundleId);
ofNullable(options).map(BaseOptions::build).ifPresent(opts -> args.put("options", opts));
//noinspection RedundantCast
return requireNonNull(
(Boolean) CommandExecutionHelper.execute(
Expand Down Expand Up @@ -200,23 +189,17 @@ default void activateApp(String bundleId) {
default void activateApp(String bundleId, @Nullable BaseActivateApplicationOptions options) {
final String extName = "mobile: activateApp";
try {
Map<String, Object> args = ImmutableMap.<String, Object>builder()
.put("bundleId", bundleId)
.put("appId", bundleId)
.putAll(Optional.ofNullable(options).map(BaseOptions::build).orElseGet(Collections::emptyMap))
.build();
var args = new HashMap<String, Object>();
args.put("bundleId", bundleId);
args.put("appId", bundleId);
ofNullable(options).map(BaseOptions::build).ifPresent(args::putAll);
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, args);
} catch (UnsupportedCommandException | InvalidArgumentException e) {
// TODO: Remove the fallback
Map args = ImmutableMap.builder()
.put("bundleId", bundleId)
.putAll(Optional.ofNullable(options).map(
opts -> Map.of("options", opts.build())
).orElseGet(Map::of))
.build();
CommandExecutionHelper.execute(
markExtensionAbsence(extName), Map.entry(ACTIVATE_APP, args)
);
var args = new HashMap<String, Object>();
args.put("bundleId", bundleId);
ofNullable(options).map(BaseOptions::build).ifPresent(opts -> args.put("options", opts));
CommandExecutionHelper.execute(markExtensionAbsence(extName), Map.entry(ACTIVATE_APP, args));
}
}

Expand Down Expand Up @@ -274,22 +257,18 @@ default boolean terminateApp(String bundleId) {
default boolean terminateApp(String bundleId, @Nullable BaseTerminateApplicationOptions options) {
final String extName = "mobile: terminateApp";
try {
Map<String, Object> args = ImmutableMap.<String, Object>builder()
.put("bundleId", bundleId)
.put("appId", bundleId)
.putAll(Optional.ofNullable(options).map(BaseOptions::build).orElseGet(Collections::emptyMap))
.build();
var args = new HashMap<String, Object>();
args.put("bundleId", bundleId);
args.put("appId", bundleId);
ofNullable(options).map(BaseOptions::build).ifPresent(args::putAll);
return requireNonNull(
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, args)
);
} catch (UnsupportedCommandException | InvalidArgumentException e) {
// TODO: Remove the fallback
Map args = ImmutableMap.builder()
.put("bundleId", bundleId)
.putAll(Optional.ofNullable(options).map(
opts -> Map.of("options", opts.build())
).orElseGet(Map::of))
.build();
var args = new HashMap<String, Object>();
args.put("bundleId", bundleId);
ofNullable(options).map(BaseOptions::build).ifPresent(opts -> args.put("options", opts));
//noinspection RedundantCast
return requireNonNull(
(Boolean) CommandExecutionHelper.execute(
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/io/appium/java_client/MobileCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import javax.annotation.Nullable;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -610,12 +611,12 @@ public static ImmutableMap<String, Object> prepareArguments(String[] params,
public static Map.Entry<String, Map<String, ?>> compareImagesCommand(ComparisonMode mode,
byte[] img1Data, byte[] img2Data,
@Nullable BaseComparisonOptions options) {
ImmutableMap.Builder<String, Object> argsBuilder = ImmutableMap.<String, Object>builder()
.put("mode", mode.toString())
.put("firstImage", new String(img1Data, StandardCharsets.UTF_8))
.put("secondImage", new String(img2Data, StandardCharsets.UTF_8));
Optional.ofNullable(options).ifPresent(opts -> argsBuilder.put("options", options.build()));
return Map.entry(COMPARE_IMAGES, argsBuilder.build());
var args = new HashMap<String, Object>();
args.put("mode", mode.toString());
args.put("firstImage", new String(img1Data, StandardCharsets.UTF_8));
args.put("secondImage", new String(img2Data, StandardCharsets.UTF_8));
Optional.ofNullable(options).ifPresent(opts -> args.put("options", options.build()));
return Map.entry(COMPARE_IMAGES, Collections.unmodifiableMap(args));
}

/**
Expand Down
19 changes: 8 additions & 11 deletions src/main/java/io/appium/java_client/MultiTouchAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

package io.appium.java_client;

import com.google.common.collect.ImmutableList;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -53,12 +52,12 @@
@Deprecated
public class MultiTouchAction implements PerformsActions<MultiTouchAction> {

private ImmutableList.Builder<TouchAction> actions;
private List<TouchAction> actions;
private PerformsTouchActions performsTouchActions;

public MultiTouchAction(PerformsTouchActions performsTouchActions) {
this.performsTouchActions = performsTouchActions;
actions = ImmutableList.builder();
actions = new ArrayList<>();
}

/**
Expand All @@ -76,21 +75,19 @@ public MultiTouchAction add(TouchAction action) {
* Perform the multi-touch action on the mobile performsTouchActions.
*/
public MultiTouchAction perform() {
List<TouchAction> touchActions = actions.build();
checkArgument(touchActions.size() > 0,
checkArgument(!actions.isEmpty(),
"MultiTouch action must have at least one TouchAction added before it can be performed");
if (touchActions.size() > 1) {
if (actions.size() > 1) {
performsTouchActions.performMultiTouchAction(this);
return this;
} //android doesn't like having multi-touch actions with only a single TouchAction...
performsTouchActions.performTouchAction(touchActions.get(0));
performsTouchActions.performTouchAction(actions.get(0));
return this;
}

protected Map<String, List<Object>> getParameters() {
return Map.of("actions",
actions.build().stream().map(touchAction ->
touchAction.getParameters().get("actions")).collect(toList())
actions.stream().map(touchAction -> touchAction.getParameters().get("actions")).collect(toList())
);
}

Expand All @@ -100,7 +97,7 @@ protected Map<String, List<Object>> getParameters() {
* @return this MultiTouchAction, for possible segmented-touches.
*/
protected MultiTouchAction clearActions() {
actions = ImmutableList.builder();
actions = new ArrayList<>();
return this;
}
}
Loading