Skip to content

Commit

Permalink
OpenRewrite: Java 17
Browse files Browse the repository at this point in the history
  • Loading branch information
centic9 committed Nov 19, 2024
1 parent 1d8346d commit fac94f5
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ public int compare(Map.Entry<T, Long> o1, Map.Entry<T, Long> o2) {
return 1;
}

if (key1 instanceof Comparable && key2 instanceof Comparable &&
if (key1 instanceof Comparable comparable && key2 instanceof Comparable &&
// e.g. String.compareTo() expects a String as parameter!
key1.getClass().isAssignableFrom(key2.getClass())) {
//noinspection unchecked,rawtypes
return ((Comparable) key1).compareTo(key2);
return comparable.compareTo(key2);
} else {
return key1.toString().compareTo(key2.toString());
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/dstadler/commons/date/DayCalendar.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.dstadler.commons.date;

import java.io.Serial;
import java.util.Calendar;
import java.util.GregorianCalendar;

Expand All @@ -11,6 +12,7 @@
* copyright 2004 Paul Hill
*/
public class DayCalendar extends GregorianCalendar {
@Serial
private static final long serialVersionUID = -9054523308590467657L;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/dstadler/commons/gpx/TrackPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public String formatTime(long start) {
int minutes = (int) ((getTime() - start) / 60 / 1000);
int seconds = (int) ((getTime() - start) / 1000) % 60;

return String.format("%02d:%02d", minutes, seconds);
return "%02d:%02d".formatted(minutes, seconds);
}

public int getHr() {
Expand Down
55 changes: 29 additions & 26 deletions src/main/java/org/dstadler/commons/http/NanoHTTPD.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,10 @@ public void stop()
@SuppressForbidden(reason = "Allow to exit the application with a return code here")
public static void main( String[] args ) throws IOException
{
System.out.println( "NanoHTTPD 1.14 (C) 2001,2005-2010 Jarno Elonen\n" +
"(Command line options: [port] [--licence])\n" );
System.out.println( """
NanoHTTPD 1.14 (C) 2001,2005-2010 Jarno Elonen
(Command line options: [port] [--licence])
""" );

// Show licence if requested
int lopt = -1;
Expand Down Expand Up @@ -919,28 +921,29 @@ private String createDirListing(String uri, File f) {
* The distribution licence
*/
private static final String LICENCE =
"Copyright (C) 2001,2005-2010 by Jarno Elonen <[email protected]>\n"+
"\n"+
"Redistribution and use in source and binary forms, with or without\n"+
"modification, are permitted provided that the following conditions\n"+
"are met:\n"+
"\n"+
"Redistributions of source code must retain the above copyright notice,\n"+
"this list of conditions and the following disclaimer. Redistributions in\n"+
"binary form must reproduce the above copyright notice, this list of\n"+
"conditions and the following disclaimer in the documentation and/or other\n"+
"materials provided with the distribution. The name of the author may not\n"+
"be used to endorse or promote products derived from this software without\n"+
"specific prior written permission. \n"+
" \n"+
"THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n"+
"IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n"+
"OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n"+
"IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\n"+
"INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n"+
"NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"+
"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"+
"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"+
"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"+
"OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.";
"""
Copyright (C) 2001,2005-2010 by Jarno Elonen <[email protected]>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer. Redistributions in
binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution. The name of the author may not
be used to endorse or promote products derived from this software without
specific prior written permission.\s
\s
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.""";
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public long[] getWindow() {
// copy of Guava to avoid including Guava in this core library
private void checkArgument(boolean argument, String msg, Object... args) {
if (!argument) {
throw new IllegalArgumentException(String.format(msg, args));
throw new IllegalArgumentException(msg.formatted(args));
}
}
}
16 changes: 8 additions & 8 deletions src/main/java/org/dstadler/commons/net/UrlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public static byte[] retrieveRawData(String sUrl, int timeout, SSLSocketFactory
conn.setDoOutput(false);
conn.setDoInput(true);

if(conn instanceof HttpsURLConnection && sslFactory != null) {
((HttpsURLConnection)conn).setSSLSocketFactory(sslFactory);
if(conn instanceof HttpsURLConnection connection && sslFactory != null) {
connection.setSSLSocketFactory(sslFactory);
}

conn.connect();
Expand Down Expand Up @@ -289,8 +289,8 @@ private static void prepareConnection(URLConnection connection, int timeout, SSL
connection.setDoInput(true); // whether we want to read from the connection
connection.setDoOutput(false); // whether we want to write to the connection

if(connection instanceof HttpsURLConnection && sslFactory != null) {
((HttpsURLConnection)connection).setSSLSocketFactory(sslFactory);
if(connection instanceof HttpsURLConnection lConnection && sslFactory != null) {
lConnection.setSSLSocketFactory(sslFactory);
}
}

Expand Down Expand Up @@ -403,11 +403,11 @@ public static String getAccessError(String destinationUrl, boolean fireRequest,
conn.setReadTimeout(timeout);
}

if(ignoreHTTPSHostCheck && conn instanceof HttpsURLConnection) {
((HttpsURLConnection)conn).setHostnameVerifier(new AllowingHostnameVerifier());
if(ignoreHTTPSHostCheck && conn instanceof HttpsURLConnection connection) {
connection.setHostnameVerifier(new AllowingHostnameVerifier());
}
if(conn instanceof HttpsURLConnection && sslFactory != null) {
((HttpsURLConnection)conn).setSSLSocketFactory(sslFactory);
if(conn instanceof HttpsURLConnection connection && sslFactory != null) {
connection.setSSLSocketFactory(sslFactory);
}

conn.setDoOutput(false);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/dstadler/commons/util/SortedProperties.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.dstadler.commons.util;

import java.io.Serial;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Properties;
Expand All @@ -11,6 +12,7 @@
* See http://www.rgagnon.com/javadetails/java-0614.html
*/
public class SortedProperties extends Properties {
@Serial
private static final long serialVersionUID = 1L;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/dstadler/commons/util/ThreadDump.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,6 @@ public static String threadInfoToString(ThreadInfo info) {
* the radix indicator <code>0x</code> and is left padded with <code>0</code>
*/
public static String toHexString(final long value) {
return String.format("%#018x", value);
return "%#018x".formatted(value);
}
}
79 changes: 48 additions & 31 deletions src/test/java/org/dstadler/commons/graphviz/DotUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@


public class DotUtilsTest {
private static final String DOT_FILE = "digraph G {" +
"dpi=300;\n" +
"rankdir=LR;\n" +
"node [shape=box];\n" +
"\n" +
"\"agent15\"->\"c.d.d.agent\";\n" +
"\"com.dynatrace.adk\"->\"c.d.d.agent\";\n" +
"\"c.d.d.adm\"->\"test.util.nonprod\";\n" +
"\"c.d.d.adm\"->\"c.d.d.sdk\";\n" +
"\"c.d.d.adm\"->\"c.d.d.portlets\";\n" +
"\"c.d.d.adm\"->\"c.d.d.server.shared\";\n" +
"\"c.d.d.adm\"->\"c.d.d.util\";\n" +
"}\n";
private static final String DOT_FILE = """
digraph G {\
dpi=300;
rankdir=LR;
node [shape=box];
"agent15"->"c.d.d.agent";
"com.dynatrace.adk"->"c.d.d.agent";
"c.d.d.adm"->"test.util.nonprod";
"c.d.d.adm"->"c.d.d.sdk";
"c.d.d.adm"->"c.d.d.portlets";
"c.d.d.adm"->"c.d.d.server.shared";
"c.d.d.adm"->"c.d.d.util";
}
""";

@Before
public void setUp() {
Expand Down Expand Up @@ -136,30 +138,39 @@ public void testWriteHeaderContent() throws Exception {
DotUtils.writeHeader(fileWriter, 0, null, "G", null);
fileWriter.flush();
assertEquals(FileUtils.readFileToString(temp, "UTF-8"),
"digraph G {\n" +
"rankdir=LR;\n" +
"node [shape=box];\n\n");
"""
digraph G {
rankdir=LR;
node [shape=box];
""");
}

// DPI set
try (FileWriter fileWriter = new FileWriter(temp, false)) {
DotUtils.writeHeader(fileWriter, 200, null, "G", null);
fileWriter.flush();
assertEquals(FileUtils.readFileToString(temp, "UTF-8"),
"digraph G {\n" +
"dpi=200;\n" +
"rankdir=LR;\n" +
"node [shape=box];\n\n");
"""
digraph G {
dpi=200;
rankdir=LR;
node [shape=box];
""");
}

// Another rankdir
try (FileWriter fileWriter = new FileWriter(temp, false)) {
DotUtils.writeHeader(fileWriter, 0, "AB", "G", null);
fileWriter.flush();
assertEquals(FileUtils.readFileToString(temp, "UTF-8"),
"digraph G {\n" +
"rankdir=AB;\n" +
"node [shape=box];\n\n");
"""
digraph G {
rankdir=AB;
node [shape=box];
""");
}

// Extra lines
Expand All @@ -171,21 +182,27 @@ public void testWriteHeaderContent() throws Exception {
DotUtils.writeHeader(fileWriter, 0, null, "G", lines);
fileWriter.flush();
assertEquals(FileUtils.readFileToString(temp, "UTF-8"),
"digraph G {\n" +
"rankdir=LR;\n" +
"someline;\n" +
"someotherline;\n" +
"node [shape=box];\n\n");
"""
digraph G {
rankdir=LR;
someline;
someotherline;
node [shape=box];
""");
}

// different title
try (FileWriter fileWriter = new FileWriter(temp, false)) {
DotUtils.writeHeader(fileWriter, 0, null, "mygraph", null);
fileWriter.flush();
assertEquals(FileUtils.readFileToString(temp, "UTF-8"),
"digraph mygraph {\n" +
"rankdir=LR;\n" +
"node [shape=box];\n\n");
"""
digraph mygraph {
rankdir=LR;
node [shape=box];
""");
}
} finally {
assertTrue(temp.delete());
Expand Down
11 changes: 6 additions & 5 deletions src/test/java/org/dstadler/commons/http/NanoHTTPDTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ void testServeWithInvalidHeader() throws Exception {
PrintWriter out =
new PrintWriter(socket.getOutputStream(), true);

out.println("GET /example/file.html?query=123 prop1:prop2\n"
+ "Content-Type: blabla\n"
+ "InvalidHeader\n"
+ "\n"
+ "Content");
out.println("""
GET /example/file.html?query=123 prop1:prop2
Content-Type: blabla
InvalidHeader
Content""");

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
IOUtils.copy(socket.getInputStream(), bytes);
Expand Down
15 changes: 8 additions & 7 deletions src/test/java/org/dstadler/commons/svn/SVNLogFileParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,14 @@ public void testLogEntryPathsSorting() {
assertEquals(LogEntry.MORE, it.next().getLeft());
}

private static final String XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<log>\n" +
"<logentry revision=\"431200\">\n" +
"<date>2014-09-18T10:40:52.345853Z</date>\n" +
"<msg>APM-23041 System.exit</msg>\n" +
"</logentry>\n" +
"</log>";
private static final String XML = """
<?xml version="1.0" encoding="UTF-8"?>
<log>
<logentry revision="431200">
<date>2014-09-18T10:40:52.345853Z</date>
<msg>APM-23041 System.exit</msg>
</logentry>
</log>""";

@Test
public void testParserProblem() throws SAXException, IOException {
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/dstadler/commons/zip/ZipFileWalkerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
public class ZipFileWalkerTest {
private final static Logger log = LoggerFactory.make();

private static final String LOGLINE = "2014-04-17 13:52:12 [000000c4] info [native] Native watchdog feature turned off\n" +
// add an unparsable line as well for other tests
"2014-2j 2lkjl23 q4l;j3;4l56kj q3l46j q3;l6jq";
private static final String LOGLINE = """
2014-04-17 13:52:12 [000000c4] info [native] Native watchdog feature turned off
2014-2j 2lkjl23 q4l;j3;4l56kj q3l46j q3;l6jq""";

@Test
public void testWalk() throws Exception {
Expand Down

0 comments on commit fac94f5

Please sign in to comment.