Skip to content

Commit

Permalink
[All] Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramesh Babu Prudhvi authored and RameshBabuPrudhvi committed Jan 17, 2024
1 parent cefc116 commit e847fce
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,8 @@ public static OutputStream newOutputStream(Path filePath) {
* be parsed
*/
public URL toURL(String urlStr) {
try {
return new URL(urlStr);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Invalid URL string: " + urlStr, e);
}
return tryURL(urlStr)
.orElseThrow(() -> new IllegalArgumentException("Invalid URL string: " + urlStr));
}

/**
Expand All @@ -284,27 +281,9 @@ public URL toURL(String urlStr) {
*/
public Optional<URL> tryURL(String urlStr) {
try {
return Optional.of(new URL(urlStr));
} catch (MalformedURLException e) {
return Optional.of(new URI(urlStr).toURL());
} catch (MalformedURLException | URISyntaxException e) {
return Optional.empty();
}
}

/**
* Returns a new URI object by parsing the given URI string.
*
* @param uriStr the URI string to be parsed into a URI
* object
* @return the URI object representing the parsed
* URI string
* @throws IllegalArgumentException if the URI string is invalid and cannot
* be parsed
*/
public URI toURI(String uriStr) {
try {
return new URI(uriStr);
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Invalid URI string: " + uriStr, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,11 @@ public String printHtmlTable() {
private static class HtmlTable<K, V> {

public String buildTable(DataTable<K, V> table) {
StringBuilder sb = new StringBuilder();
sb.append("<table>\n");
sb.append(buildHeaderRow(table.getFirst()));
sb.append(buildDataRows(table));
sb.append("</table>");

return sb.toString();
return "<table>\n" +
buildHeaderRow(table.getFirst()) +
buildDataRows(table) +
"</table>";
}

private String buildHeaderRow(Map<K, V> row) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@
*/
public class Try<T> {
private final T result;
/**
* -- GETTER --
* Returns the cause of the failure represented by this Try object.
*
* @return the cause of the failure represented by this Try object, or null
* if this Try object represents a success
*/

@Getter
private final Exception exception;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ private ExecResults interactWithProcess(final Process process) {
}
var output = new StreamGuzzler(process.getInputStream());
var error = new StreamGuzzler(process.getErrorStream());
var executors = Executors.newFixedThreadPool(2);
executors.submit(error);
executors.submit(output);
executors.shutdown();
while (!executors.isTerminated()) {
// Wait for all the tasks to complete.
Await.until(1);
try (var executors = Executors.newFixedThreadPool(2)) {
executors.submit(error);
executors.submit(output);
executors.shutdown();
while (!executors.isTerminated()) {
// Wait for all the tasks to complete.
Await.until(1);
}
}

return new ExecResults(output.getContent(), error.getContent(), process.exitValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ public String downloadToUsersFolder(final String url) {
* @return The content of the file.
*/
public String readContent(final String filePath) {
try {
return new String(loadResourceFromJar(filePath).readAllBytes(), StandardCharsets.UTF_8);
try (var inputStream = loadResourceFromJar(filePath)) {
return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
} catch (Exception e) {
throw new ConfigurationException(String.format("Cannot load [%s] from classpath", filePath));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,6 @@ private String encode(String value) {
private URI buildUri() {
var joiner = new StringJoiner("&");
queryParams.forEach((key, value) -> joiner.add(encode(key) + "=" + encode(value)));
return Resources.toURI(baseUri + endpoint + (baseUri.contains("?") ? "&" : "?") + joiner);
return URI.create(baseUri + endpoint + (baseUri.contains("?") ? "&" : "?") + joiner);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.Optional;
import java.util.stream.Stream;

@SuppressWarnings("OptionalGetWithoutIsPresent")
public final class TestSourcesModel {

private final Map<URI, TestSourceRead> pathToReadEventMap = new HashMap<>();
Expand Down

0 comments on commit e847fce

Please sign in to comment.