Skip to content

Commit

Permalink
[java] More conversion of guava-style code to java 8
Browse files Browse the repository at this point in the history
  • Loading branch information
barancev committed Apr 20, 2020
1 parent bf734f8 commit 66d554a
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 55 deletions.
1 change: 0 additions & 1 deletion java/client/src/org/openqa/selenium/edge/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@ java_export(
"//java/client/src/org/openqa/selenium:core",
"//java/client/src/org/openqa/selenium/chromium",
"//java/client/src/org/openqa/selenium/remote",
artifact("com.google.guava:guava"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
// under the License.
package org.openqa.selenium.edge;

import static java.util.Collections.unmodifiableList;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableList;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriverException;
Expand All @@ -27,6 +28,7 @@
import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -161,22 +163,22 @@ protected List<String> createArgs() {
}
}

ImmutableList.Builder<String> argsBuilder = ImmutableList.builder();
argsBuilder.add(String.format("--port=%d", getPort()));
List<String> args = new ArrayList<>();
args.add(String.format("--port=%d", getPort()));
if (getLogFile() != null) {
argsBuilder.add(String.format("--log-path=%s", getLogFile().getAbsolutePath()));
args.add(String.format("--log-path=%s", getLogFile().getAbsolutePath()));
}
if (verbose) {
argsBuilder.add("--verbose");
args.add("--verbose");
}
if (silent) {
argsBuilder.add("--silent");
args.add("--silent");
}
if (allowedListIps != null) {
argsBuilder.add(String.format("--whitelisted-ips=%s", allowedListIps));
args.add(String.format("--whitelisted-ips=%s", allowedListIps));
}

return argsBuilder.build();
return unmodifiableList(args);
}

@Override
Expand Down
11 changes: 8 additions & 3 deletions java/client/src/org/openqa/selenium/edge/EdgeDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
// under the License.
package org.openqa.selenium.edge;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableMap;

import org.openqa.selenium.remote.service.DriverService;

import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -62,7 +65,9 @@ public EdgeDriverService(
Duration timeout,
List<String> args,
Map<String, String> environment) throws IOException {
super(executable, port, timeout, ImmutableList.copyOf(args), ImmutableMap.copyOf(environment));
super(executable, port, timeout,
unmodifiableList(new ArrayList<>(args)),
unmodifiableMap(new HashMap<>(environment)));
}

public static abstract class Builder<DS extends EdgeDriverService, B extends EdgeDriverService.Builder<?, ?>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ java_export(
"//java:auto-service",
"//java/client/src/org/openqa/selenium/edge:edgeium",
"//java/client/src/org/openqa/selenium/remote",
artifact("com.google.guava:guava"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
// under the License.
package org.openqa.selenium.edge.edgehtml;

import static java.util.Collections.unmodifiableList;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableList;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriverException;
Expand All @@ -30,6 +31,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -107,14 +109,14 @@ protected File findDefaultExecutable() {

@Override
protected List<String> createArgs() {
ImmutableList.Builder<String> argsBuilder = ImmutableList.builder();
argsBuilder.add(String.format("--port=%d", getPort()));
List<String> args = new ArrayList<>();
args.add(String.format("--port=%d", getPort()));

if (verbose) {
argsBuilder.add("--verbose");
args.add("--verbose");
}

return argsBuilder.build();
return unmodifiableList(args);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
package org.openqa.selenium.firefox;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Collections.unmodifiableList;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableList;
import com.google.common.io.ByteStreams;

import org.openqa.selenium.Capabilities;
Expand All @@ -34,6 +34,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -189,13 +190,13 @@ protected File findDefaultExecutable() {

@Override
protected List<String> createArgs() {
ImmutableList.Builder<String> argsBuilder = ImmutableList.builder();
argsBuilder.add(String.format("--port=%d", getPort()));
List<String> args = new ArrayList<>();
args.add(String.format("--port=%d", getPort()));
if (firefoxBinary != null) {
argsBuilder.add("-b");
argsBuilder.add(firefoxBinary.getPath());
args.add("-b");
args.add(firefoxBinary.getPath());
} // else GeckoDriver will be responsible for finding Firefox on the PATH or via a capability.
return argsBuilder.build();
return unmodifiableList(args);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

package org.openqa.selenium.ie;

import static java.util.Collections.unmodifiableList;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableList;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriverException;
Expand All @@ -28,6 +29,7 @@
import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -204,25 +206,25 @@ protected List<String> createArgs() {
}
}

ImmutableList.Builder<String> argsBuilder = ImmutableList.builder();
argsBuilder.add(String.format("--port=%d", getPort()));
List<String> args = new ArrayList<>();
args.add(String.format("--port=%d", getPort()));
if (getLogFile() != null) {
argsBuilder.add(String.format("--log-file=\"%s\"", getLogFile().getAbsolutePath()));
args.add(String.format("--log-file=\"%s\"", getLogFile().getAbsolutePath()));
}
if (logLevel != null) {
argsBuilder.add(String.format("--log-level=%s", logLevel.toString()));
args.add(String.format("--log-level=%s", logLevel.toString()));
}
if (host != null) {
argsBuilder.add(String.format("--host=%s", host));
args.add(String.format("--host=%s", host));
}
if (extractPath != null) {
argsBuilder.add(String.format("--extract-path=\"%s\"", extractPath.getAbsolutePath()));
args.add(String.format("--extract-path=\"%s\"", extractPath.getAbsolutePath()));
}
if (silent != null && silent.equals(Boolean.TRUE)) {
argsBuilder.add("--silent");
args.add("--silent");
}

return argsBuilder.build();
return unmodifiableList(args);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.openqa.selenium.ie;

import static java.util.stream.Collectors.toUnmodifiableList;
import static org.openqa.selenium.ie.InternetExplorerDriver.BROWSER_ATTACH_TIMEOUT;
import static org.openqa.selenium.ie.InternetExplorerDriver.ELEMENT_SCROLL_BEHAVIOR;
import static org.openqa.selenium.ie.InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING;
Expand All @@ -32,7 +33,6 @@
import static org.openqa.selenium.remote.CapabilityType.BROWSER_NAME;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Streams;

Expand Down Expand Up @@ -163,7 +163,7 @@ public InternetExplorerOptions addCommandSwitches(String... switches) {
Streams.concat((Stream<?>) List.class.cast(raw).stream(), Stream.of(switches))
.filter(i -> i instanceof String)
.map(String.class::cast)
.collect(ImmutableList.toImmutableList()));
.collect(toUnmodifiableList()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

package org.openqa.selenium.opera;

import static java.util.Collections.unmodifiableList;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableList;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriverException;
Expand All @@ -28,6 +29,7 @@
import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -168,19 +170,19 @@ protected List<String> createArgs() {
}
}

ImmutableList.Builder<String> argsBuilder = ImmutableList.builder();
argsBuilder.add(String.format("--port=%d", getPort()));
List<String> args = new ArrayList<>();
args.add(String.format("--port=%d", getPort()));
if (getLogFile() != null) {
argsBuilder.add(String.format("--log-path=%s", getLogFile().getAbsolutePath()));
args.add(String.format("--log-path=%s", getLogFile().getAbsolutePath()));
}
if (verbose) {
argsBuilder.add("--verbose");
args.add("--verbose");
}
if (silent) {
argsBuilder.add("--silent");
args.add("--silent");
}

return argsBuilder.build();
return unmodifiableList(args);
}

@Override
Expand Down
17 changes: 9 additions & 8 deletions java/client/src/org/openqa/selenium/opera/OperaOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableMap;
import static org.openqa.selenium.remote.BrowserType.OPERA_BLINK;
import static org.openqa.selenium.remote.CapabilityType.BROWSER_NAME;

import com.google.common.collect.ImmutableList;
import com.google.common.io.Files;

import org.openqa.selenium.remote.AbstractDriverOptions;
Expand All @@ -32,8 +33,8 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -110,7 +111,7 @@ public OperaOptions setBinary(String path) {
* @see #addArguments(java.util.List)
*/
public OperaOptions addArguments(String... arguments) {
addArguments(ImmutableList.copyOf(arguments));
addArguments(Arrays.asList(arguments));
return this;
}

Expand Down Expand Up @@ -139,7 +140,7 @@ public OperaOptions addArguments(List<String> arguments) {
* @see #addExtensions(java.util.List)
*/
public OperaOptions addExtensions(File... paths) {
addExtensions(ImmutableList.copyOf(paths));
addExtensions(Arrays.asList(paths));
return this;
}

Expand All @@ -165,7 +166,7 @@ public OperaOptions addExtensions(List<File> paths) {
* @see #addEncodedExtensions(java.util.List)
*/
public OperaOptions addEncodedExtensions(String... encoded) {
addEncodedExtensions(ImmutableList.copyOf(encoded));
addEncodedExtensions(Arrays.asList(encoded));
return this;
}

Expand Down Expand Up @@ -220,7 +221,7 @@ public Map<String, Object> asMap() {
options.put("binary", binary);
}

options.put("args", ImmutableList.copyOf(args));
options.put("args", unmodifiableList(new ArrayList<>(args)));

List<String> encoded_extensions = new ArrayList<>(
extensionFiles.size() + extensions.size());
Expand All @@ -234,10 +235,10 @@ public Map<String, Object> asMap() {
}
}
encoded_extensions.addAll(extensions);
options.put("extensions", encoded_extensions);
options.put("extensions", unmodifiableList(encoded_extensions));

toReturn.put(CAPABILITY, options);

return Collections.unmodifiableMap(toReturn);
return unmodifiableMap(toReturn);
}
}
Loading

0 comments on commit 66d554a

Please sign in to comment.