Skip to content

Commit

Permalink
Upgrade to Java 17 (#320)
Browse files Browse the repository at this point in the history
* [BOM] Bump faker version

* Code cleanup

* Upgrade to java 17 features

* Upgrade to java 17 features

* Upgrade to java 17 features

* Upgrade to java 17 features

* Code cleanup

* Code cleanup

* upgraded to Java 17

* upgraded to Java 17

* upgraded to Java 17

* Sonar fixes

* Optimise imports

* Optimise imports

* Using java 17 features

* Optimise imports

* Optimise imports

* Revert few changes

* updated test

* updated test

* updated test
  • Loading branch information
RameshBabuPrudhvi authored Jan 23, 2024
1 parent 9e57006 commit 1730708
Show file tree
Hide file tree
Showing 41 changed files with 198 additions and 305 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/.toolchains.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<toolchain>
<type>jdk</type>
<provides>
<version>11</version>
<version>17</version>
<vendor>openjdk</vendor>
</provides>
<configuration>
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Java
uses: actions/setup-java@v4
with:
distribution: corretto
java-version: 17
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<sonar.host.url>https://sonarcloud.io</sonar.host.url>

<!-- Java version -->
<java.version>11</java.version>
<java.version>17</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>

Expand Down
2 changes: 1 addition & 1 deletion selcukes-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<name>Selcukes: Bill of Materials</name>

<properties>
<faker.version>0.0.2</faker.version>
<faker.version>1.0.0</faker.version>
</properties>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

import lombok.experimental.UtilityClass;

import java.util.*;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public String createFile(final Path filePath) {
*/
public Path writeToFile(final Path filePath, final @NonNull String fileContent) {
try {
return Files.write(filePath, fileContent.getBytes(UTF_8));
return Files.writeString(filePath, fileContent);
} catch (IOException e) {
throw new DataStreamException("Failed to write content to file: " + filePath.toAbsolutePath(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,6 @@ public <T> Stream<T> of(final Iterator<? extends T> iterator) {
.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false);
}

/**
* Converts an Iterable to a Stream.
*
* @param iterable The iterable to convert to a stream.
* @param <T> The type of elements in the iterable.
* @return A stream of the iterable.
*/
public static <T> Stream<T> of(final Iterable<T> iterable) {
return StreamSupport.stream(iterable.spliterator(), false);
}

/**
* Returns an OptionalInt that contains the index of the first element in
* the list that matches the given predicate, or an empty OptionalInt if no
Expand Down Expand Up @@ -157,7 +146,7 @@ public static <T, U, R> Stream<R> zip(List<T> first, List<U> second, BiFunction<
* @return the resulting {@code DataTable<String, String>}
*/
public <T> DataTable<String, String> toTable(List<? extends List<T>> cells) {
List<String> headers = Lists.toString(cells.get(0));
var headers = Lists.toString(cells.get(0));
return cells.stream()
.skip(1)
.map(row -> Maps.of(headers, Lists.toString(row), ""))
Expand All @@ -166,7 +155,7 @@ public <T> DataTable<String, String> toTable(List<? extends List<T>> cells) {

/**
* Convert a List of Maps into a Map of Lists, where the keys of each Map in
* the input List become the keys of the output Map and the values of each
* the input List become the keys of the output Map, and the values of each
* Map in the input List are added to the corresponding List in the output
* Map.
*
Expand Down Expand Up @@ -205,7 +194,7 @@ public String[][] toArray(final List<List<String>> cells) {
public List<String> trim(final List<String> list) {
return list.stream()
.map(String::trim)
.collect(Collectors.toList());
.toList();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public String toCamelCase(final String text) {
*/
public static String toFieldName(final String text) {
final String fieldName = text.replaceAll(CAMEL_CASE_REGEX, "");
return fieldName.length() > 0 ? fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1) : null;
return !fieldName.isEmpty() ? fieldName.substring(0, 1).toLowerCase() + fieldName.substring(1) : null;
}

/**
Expand Down Expand Up @@ -165,14 +165,15 @@ public static List<List<String>> toListOfList(Stream<String> lines, String delim
}

/**
* Returns the first group of the match if the text is not null and the
* Returns the first group of the matches if the text is not null and the
* compiled pattern matches. Note: This function compiles the pattern only
* once, so subsequent calls with the same pattern are faster.
*
* @param pattern The regular expression pattern to match.
* @param text The text to search for the pattern.
* @return An Optional String containing the first group of the
* match, or empty if no match is found or the text is null.
* matches, or empty if no match is found or the text is
* null.
*/
public static Optional<String> findPattern(String pattern, String text) {
var compiledPattern = Pattern.compile(pattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.github.selcukes.collections;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -74,8 +75,8 @@ public String printCSV() {
.collect(Collectors.joining("\n"));
}

private <T> String getCSVRow(Iterable<T> values) {
return Streams.of(values)
private <T> String getCSVRow(Collection<T> values) {
return values.stream()
.map(Object::toString)
.map(this::escapeCsvValue)
.collect(Collectors.joining(","));
Expand All @@ -96,35 +97,32 @@ public String printHtmlTable() {
private static class HtmlTable<K, V> {

public String buildTable(DataTable<K, V> table) {

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

private String buildHeaderRow(Map<K, V> row) {
StringBuilder sb = new StringBuilder();
sb.append("<tr>\n");
row.keySet().forEach(key -> sb.append("<th>").append(key).append("</th>\n"));
sb.append("</tr>\n");
return sb.toString();
return buildRow(row.keySet(), "<th>");
}

private String buildDataRows(DataTable<K, V> table) {
StringBuilder sb = new StringBuilder();
table.rows()
return table.rows()
.map(this::buildDataRow)
.forEach(sb::append);
return sb.toString();
.collect(Collectors.joining());
}

private String buildDataRow(Map<K, V> row) {
StringBuilder sb = new StringBuilder();
sb.append("<tr>\n");
row.values().forEach(value -> sb.append("<td>").append(value).append("</td>\n"));
sb.append("</tr>\n");
return sb.toString();
return buildRow(row.values(), "<td>");
}

private <T> String buildRow(Collection<T> values, String tag) {
return "<tr>\n" +
values.stream()
.map(value -> String.format("%s%s%s", tag, value, tag))
.collect(Collectors.joining("\n")) +
"</tr>\n";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
Expand All @@ -53,7 +52,7 @@ public void testZipWithMultipleElementLists() {
var first = List.of("a", "b", "c");
var second = List.of(1, 2, 3);

var result = Streams.zip(first, second, (s, i) -> s + i).collect(Collectors.toList());
var result = Streams.zip(first, second, (s, i) -> s + i).toList();

assertEquals(result.size(), 3);
assertEquals(result.get(0), "a1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
Expand Down Expand Up @@ -318,13 +317,15 @@ public void testPrettyTable() {
rowC.put("name", "Bob Johnson");
rowC.put("age", "40");
var table = DataTable.of(rowA, rowB, rowC);
String expectedOutput = "+-------------+-----+\n" +
"| name | age |\n" +
"+-------------+-----+\n" +
"| John Doe | 30 |\n" +
"| Jane Smith | 25 |\n" +
"| Bob Johnson | 40 |\n" +
"+-------------+-----+\n";
String expectedOutput = """
+-------------+-----+
| name | age |
+-------------+-----+
| John Doe | 30 |
| Jane Smith | 25 |
| Bob Johnson | 40 |
+-------------+-----+
""";
assertEquals(table.prettyTable(), expectedOutput);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* A simple thread based utility that consumes {@link InputStream} and provides
Expand All @@ -48,7 +47,7 @@ public StreamGuzzler(final InputStream stream) {
public void run() {
Objects.requireNonNull(stream, "Cannot guzzle an empty/null stream.");
try (var reader = new BufferedReader(new InputStreamReader(stream))) {
content.addAll(reader.lines().collect(Collectors.toList()));
content.addAll(reader.lines().toList());
} catch (IOException exception) {
throw new CommandException(exception);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -92,7 +93,7 @@ public void setFileExecutable(final String filePath) {
* system.
*/
public String systemFilePath(final String filePath) {
String fileSeparator = System.getProperty("file.separator");
String fileSeparator = FileSystems.getDefault().getSeparator();

if (!fileSeparator.isEmpty() && "\\".equals(fileSeparator)) {
int beginIndex = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import java.util.List;
import java.util.ServiceLoader;
import java.util.stream.Collectors;

/**
* "Load the first service of the given type from the given class loader."
Expand Down Expand Up @@ -67,6 +66,6 @@ public <T> T loadFirst(final Class<T> type, final ClassLoader classLoader) {
*/
public <T> List<T> load(final Class<T> type, final ClassLoader classLoader) {
return Streams.of(ServiceLoader.load(type, classLoader).iterator())
.collect(Collectors.toList());
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import static java.net.http.HttpRequest.BodyPublisher;
Expand Down Expand Up @@ -160,10 +165,10 @@ public WebResponse put(final Object payload) {

@SneakyThrows
private BodyPublisher bodyPublisher(final Object payload) {
if (payload instanceof String) {
bodyPublisher = BodyPublishers.ofString(payload.toString());
} else if (payload instanceof Path) {
bodyPublisher = BodyPublishers.ofFile((Path) payload);
if (payload instanceof String payloadString) {
bodyPublisher = BodyPublishers.ofString(payloadString);
} else if (payload instanceof Path filePath) {
bodyPublisher = BodyPublishers.ofFile(filePath);
} else {
bodyPublisher = BodyPublishers.ofString(JsonUtils.toJson(payload));
}
Expand All @@ -179,8 +184,7 @@ private <K, V> BodyPublisher multiPartBody(final Map<K, V> data, final String bo
for (var entry : data.entrySet()) {
byteArrays.add(separator);

if (entry.getValue() instanceof Path) {
var path = (Path) entry.getValue();
if (entry.getValue() instanceof Path path) {
String mimeType = Files.probeContentType(path);
byteArrays.add(("\"" + entry.getKey() + "\"; filename=\""
+ path.getFileName() + "\"\r\nContent-Type: " + mimeType
Expand All @@ -205,7 +209,10 @@ private WebResponse execute() {
clientBuilder.followRedirects(HttpClient.Redirect.NORMAL);
}
var request = requestBuilder.uri(buildUri()).build();
return new WebResponse(clientBuilder.build().send(request, ofString()));
var httpResponse = clientBuilder.build().send(request, ofString());
var response = new WebResponse(httpResponse);
response.logIfError();
return response;
}

/**
Expand Down
Loading

0 comments on commit 1730708

Please sign in to comment.