Skip to content

Commit acbd699

Browse files
authored
Introduce spotless
- uses DSA code style (through eclipse code style file) - adds licence header to all Java files
1 parent 4bead62 commit acbd699

14 files changed

Lines changed: 503 additions & 26 deletions

build.gradle

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import javax.inject.Inject
33
plugins {
44
id 'java'
55
id 'maven-publish'
6+
id("com.diffplug.spotless") version "8.9.0"
67
}
78

89
interface InjectedExecOps {
@@ -47,6 +48,19 @@ tasks.named("pmdMain", Pmd) {
4748
source = fileTree("src/main/java")
4849
}
4950

51+
spotless{
52+
java {
53+
targetExclude("build/**")
54+
toggleOffOn()
55+
eclipse().configFile('config/spotless/code_style.xml')
56+
57+
licenseHeader """// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
58+
// SPDX-License-Identifier: MPL-2.0
59+
60+
"""
61+
}
62+
}
63+
5064
def pgmVersion = "1.13.109"
5165

5266
def pgmDir = layout.buildDirectory.dir("pgm").get()

config/spotless/code_style.xml

Lines changed: 380 additions & 0 deletions
Large diffs are not rendered by default.

src/main/java/org/lfenergy/pgm/example/PowerGridModelExample.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
22
// SPDX-License-Identifier: MPL-2.0
33

4-
package com.alliander.ads.dgsa.pgm;
4+
package org.lfenergy.pgm.example;
55

66
import java.lang.foreign.Arena;
77
import java.lang.foreign.MemorySegment;
@@ -18,14 +18,16 @@
1818
* <p>
1919
* NOTE: The generated PowerGridModelC_1 class hardcodes the .so path at generation time. If the library has moved, re-run jextract or update SYMBOL_LOOKUP manually.
2020
*/
21-
@SuppressWarnings({"PMD.SystemPrintln", })
21+
@SuppressWarnings("PMD.SystemPrintln")
2222
public final class PowerGridModelExample {
2323

2424
private PowerGridModelExample() {
25+
2526
// Prevent initialization
2627
}
2728

2829
static void main() {
30+
2931
final PGMLoader loader = new PGMLoader();
3032

3133
loader.autoload();
@@ -44,6 +46,7 @@ static void main() {
4446
"PMD.CognitiveComplexity",
4547
"PMD.VariableDeclarationUsageDistance"})
4648
private static void runExample() {
49+
4750
final MemorySegment handle = PowerGridModelC.PGM_create_handle();
4851

4952
MemorySegment nodeInput = MemorySegment.NULL;
@@ -274,42 +277,50 @@ private static void runExample() {
274277
}
275278

276279
private static void ensureNoError(MemorySegment handle) {
280+
277281
final long errorCode = PowerGridModelC.PGM_error_code(handle);
278282
if (errorCode != PowerGridModelC.PGM_no_error()) {
279283
throw new IllegalStateException(
280-
"PGM error " + errorCode + ": " + cString(PowerGridModelC.PGM_error_message(handle))
284+
"PGM error " + errorCode + ": " + cString(PowerGridModelC.PGM_error_message(handle))
281285
);
282286
}
283287
}
284288

285289
private static String cString(MemorySegment cStringPtr) {
290+
286291
if (cStringPtr.equals(MemorySegment.NULL)) {
287292
return "<null>";
288293
}
289294
return cStringPtr.getString(0, UTF_8);
290295
}
291296

292-
private static MemorySegment toNativeIntArray(Arena arena, int... values) {
297+
private static MemorySegment toNativeIntArray(Arena arena,
298+
int... values) {
299+
293300
final MemorySegment segment = arena.allocate(values.length * PowerGridModelC.C_INT.byteSize(), PowerGridModelC.C_INT.byteAlignment());
294301
for (int i = 0; i < values.length; i++) {
295302
segment.setAtIndex(PowerGridModelC.C_INT, i, values[i]);
296303
}
297304
return segment;
298305
}
299306

300-
private static MemorySegment toNativeDoubleArray(Arena arena, double... values) {
307+
private static MemorySegment toNativeDoubleArray(Arena arena,
308+
double... values) {
309+
301310
final MemorySegment segment = arena.allocate(values.length * PowerGridModelC.C_DOUBLE.byteSize(), PowerGridModelC.C_DOUBLE.byteAlignment());
302311
for (int i = 0; i < values.length; i++) {
303312
segment.setAtIndex(PowerGridModelC.C_DOUBLE, i, values[i]);
304313
}
305314
return segment;
306315
}
307316

308-
private static MemorySegment toNativeLongArray(Arena arena, long... values) {
317+
private static MemorySegment toNativeLongArray(Arena arena,
318+
long... values) {
319+
309320
final MemorySegment segment = arena.allocate(values.length * PowerGridModelC.C_LONG_LONG.byteSize(), PowerGridModelC.C_LONG_LONG.byteAlignment());
310321
for (int i = 0; i < values.length; i++) {
311322
segment.setAtIndex(PowerGridModelC.C_LONG_LONG, i, values[i]);
312323
}
313324
return segment;
314325
}
315-
}
326+
}

src/main/java/org/lfenergy/pgm/loader/Architecture.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
2+
// SPDX-License-Identifier: MPL-2.0
3+
14
package org.lfenergy.pgm.loader;
25

36
enum Architecture {

src/main/java/org/lfenergy/pgm/loader/OperatingSystem.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
2+
// SPDX-License-Identifier: MPL-2.0
3+
14
package org.lfenergy.pgm.loader;
25

36
enum OperatingSystem {

src/main/java/org/lfenergy/pgm/loader/PGMLoader.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
2+
// SPDX-License-Identifier: MPL-2.0
3+
14
package org.lfenergy.pgm.loader;
25

36
import static java.nio.charset.StandardCharsets.UTF_8;
@@ -24,10 +27,14 @@ public class PGMLoader {
2427
private final AtomicBoolean loaded = new AtomicBoolean(false);
2528

2629
public PGMLoader() {
30+
2731
this(new PlatformDetector(), new ResourceLibraryLoader(), PowerGridModelC::PGM_version);
2832
}
2933

30-
PGMLoader(PlatformDetector platformDetector, ResourceLibraryLoader resourceLibraryLoader, PGMInvoker pgmInvoker) {
34+
PGMLoader(PlatformDetector platformDetector,
35+
ResourceLibraryLoader resourceLibraryLoader,
36+
PGMInvoker pgmInvoker) {
37+
3138
this.platformDetector = platformDetector;
3239
this.resourceLibraryLoader = resourceLibraryLoader;
3340
this.pgmInvoker = pgmInvoker;
@@ -73,6 +80,7 @@ public void autoload() {
7380
* @throws VersionMismatchException thrown when {@code matchVersion} is true and the build and runtime versions don't match
7481
*/
7582
public void check(boolean matchVersion) {
83+
7684
final String buildVersion = getPGMBuildVersion();
7785
final String runtimeVersion = getPGMRuntimeVersion();
7886

@@ -145,6 +153,7 @@ private String determineLibraryResourcePath(Platform platform) {
145153
@SuppressWarnings({"checkstyle:MethodName", "PMD.MethodNamingConventions"})
146154
@FunctionalInterface
147155
interface PGMInvoker {
156+
148157
MemorySegment PGM_version();
149158
}
150159
}

src/main/java/org/lfenergy/pgm/loader/PGMLoaderException.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
2+
// SPDX-License-Identifier: MPL-2.0
3+
14
package org.lfenergy.pgm.loader;
25

36
import java.io.Serial;
@@ -8,10 +11,13 @@ public class PGMLoaderException extends RuntimeException {
811
private static final long serialVersionUID = -4755108178968242882L;
912

1013
PGMLoaderException(String message) {
14+
1115
super(message);
1216
}
1317

14-
PGMLoaderException(String message, Throwable cause) {
18+
PGMLoaderException(String message,
19+
Throwable cause) {
20+
1521
super(message, cause);
1622
}
1723
}

src/main/java/org/lfenergy/pgm/loader/Platform.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
2+
// SPDX-License-Identifier: MPL-2.0
3+
14
package org.lfenergy.pgm.loader;
25

36
record Platform(OperatingSystem operatingSystem, Architecture architecture) {

src/main/java/org/lfenergy/pgm/loader/PlatformDetector.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
2+
// SPDX-License-Identifier: MPL-2.0
3+
14
package org.lfenergy.pgm.loader;
25

36
class PlatformDetector {
47

58
private final SystemPropertyProvider systemPropertyProvider;
69

710
PlatformDetector() {
11+
812
this(new SystemPropertyProvider() {
13+
914
@Override
1015
public String getOsArch() {
1116

@@ -21,6 +26,7 @@ public String getOsName() {
2126
}
2227

2328
PlatformDetector(SystemPropertyProvider systemPropertyProvider) {
29+
2430
this.systemPropertyProvider = systemPropertyProvider;
2531
}
2632

@@ -44,6 +50,7 @@ private Architecture detectArchitecture(final String osArch) {
4450
}
4551

4652
private OperatingSystem detectOperatingSystem(final String osName) {
53+
4754
final String normalized = osName == null ? "" : osName.toLowerCase();
4855

4956
if (normalized.contains("mac")) {
@@ -62,7 +69,9 @@ private OperatingSystem detectOperatingSystem(final String osName) {
6269
* Interface that allows for mocking in unit tests.
6370
*/
6471
interface SystemPropertyProvider {
72+
6573
String getOsArch();
74+
6675
String getOsName();
6776
}
6877
}

src/main/java/org/lfenergy/pgm/loader/ResourceLibraryLoader.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <powergridmodel@lfenergy.org>
2+
// SPDX-License-Identifier: MPL-2.0
3+
14
package org.lfenergy.pgm.loader;
25

36
import java.io.BufferedInputStream;
@@ -13,26 +16,32 @@ final class ResourceLibraryLoader {
1316
private final Dependencies dependencies;
1417

1518
ResourceLibraryLoader() {
19+
1620
this(
1721
new Dependencies() {
1822

1923
@Override
2024
public void loadNativeLibrary(final String nativeLibraryPath) {
25+
2126
System.load(nativeLibraryPath);
2227
}
2328

2429
@Override
2530
public URL openResource(String path) {
31+
2632
return Thread.currentThread().getContextClassLoader().getResource(path);
2733
}
2834

2935
@Override
3036
public boolean resourceIsFile(URL resourceURL) {
37+
3138
return "file".equals(resourceURL.getProtocol());
3239
}
3340

3441
@Override
35-
public Path copyIntoTemporaryFile(final InputStream from, final String prefix, final String suffix) throws IOException {
42+
public Path copyIntoTemporaryFile(final InputStream from,
43+
final String prefix,
44+
final String suffix) throws IOException {
3645

3746
final Path tempFile = Files.createTempFile(prefix, suffix);
3847
tempFile.toFile().deleteOnExit();
@@ -48,6 +57,7 @@ public Path copyIntoTemporaryFile(final InputStream from, final String prefix, f
4857
ResourceLibraryLoader(
4958
final Dependencies dependencies
5059
) {
60+
5161
this.dependencies = dependencies;
5262
}
5363

@@ -94,9 +104,7 @@ private Path provideLoadableLibraryFromResources(final String resourceLibraryPat
94104

95105
try (InputStream in = new BufferedInputStream(resourceUrl.openStream())) {
96106
return dependencies.copyIntoTemporaryFile(
97-
in,
98-
"pgm-native-",
99-
resourceLibraryPath.replace('.', '-')
107+
in, "pgm-native-", resourceLibraryPath.replace('.', '-')
100108
);
101109
}
102110
} catch (Exception e) {
@@ -108,9 +116,15 @@ private Path provideLoadableLibraryFromResources(final String resourceLibraryPat
108116
* Interface that allows for mocking in unit tests.
109117
*/
110118
interface Dependencies {
119+
111120
void loadNativeLibrary(String path);
121+
112122
URL openResource(String path);
123+
113124
boolean resourceIsFile(URL resourceUrl);
114-
Path copyIntoTemporaryFile(InputStream from, String prefix, String suffix) throws IOException;
125+
126+
Path copyIntoTemporaryFile(InputStream from,
127+
String prefix,
128+
String suffix) throws IOException;
115129
}
116130
}

0 commit comments

Comments
 (0)