Skip to content

Commit f7e5217

Browse files
committed
Apply google-java-format
1 parent 27aa926 commit f7e5217

File tree

120 files changed

+2425
-3515
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+2425
-3515
lines changed

config/checkstyle/checkstyle.xml

Lines changed: 125 additions & 70 deletions
Large diffs are not rendered by default.

config/ide/intellij-code-style.xml

Lines changed: 0 additions & 587 deletions
This file was deleted.

modbus-serial/src/main/java/com/digitalpetri/modbus/serial/SerialPortTransportConfig.java

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ public record SerialPortTransportConfig(
2525
int stopBits,
2626
int parity,
2727
boolean rs485Mode,
28-
ExecutorService executor
29-
) {
28+
ExecutorService executor) {
3029

3130
/**
3231
* Create a new {@link SerialPortTransportConfig}, using the callback to configure the builder as
@@ -51,14 +50,10 @@ public static class Builder {
5150
*/
5251
public String serialPort;
5352

54-
/**
55-
* The desired baud rate to operate at.
56-
*/
53+
/** The desired baud rate to operate at. */
5754
public int baudRate = 9600;
5855

59-
/**
60-
* The number of data bits per word to use.
61-
*/
56+
/** The number of data bits per word to use. */
6257
public int dataBits = 8;
6358

6459
/**
@@ -84,8 +79,7 @@ public static class Builder {
8479
/**
8580
* Enable RS-485 mode, i.e. transmit/receive mode signaling using the RTS pin.
8681
*
87-
* <p>This requires support from the underlying driver and may not work with all RS-485
88-
* devices.
82+
* <p>This requires support from the underlying driver and may not work with all RS-485 devices.
8983
*/
9084
public boolean rs485Mode = false;
9185

@@ -110,16 +104,7 @@ public SerialPortTransportConfig build() {
110104
}
111105

112106
return new SerialPortTransportConfig(
113-
serialPort,
114-
baudRate,
115-
dataBits,
116-
stopBits,
117-
parity,
118-
rs485Mode,
119-
executor
120-
);
107+
serialPort, baudRate, dataBits, stopBits, parity, rs485Mode, executor);
121108
}
122-
123109
}
124-
125110
}

modbus-serial/src/main/java/com/digitalpetri/modbus/serial/client/SerialPortClientTransport.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public SerialPortClientTransport(SerialPortTransportConfig config) {
4141
config.dataBits(),
4242
config.stopBits(),
4343
config.parity(),
44-
config.rs485Mode()
45-
);
44+
config.rs485Mode());
4645

4746
executionQueue = new ExecutionQueue(config.executor());
4847
}
@@ -63,8 +62,7 @@ public synchronized CompletableFuture<Void> connect() {
6362
return CompletableFuture.failedFuture(
6463
new Exception(
6564
"failed to open port '%s', lastErrorCode=%d"
66-
.formatted(config.serialPort(), serialPort.getLastErrorCode()))
67-
);
65+
.formatted(config.serialPort(), serialPort.getLastErrorCode())));
6866
}
6967
}
7068
}
@@ -80,8 +78,7 @@ public synchronized CompletableFuture<Void> disconnect() {
8078
return CompletableFuture.failedFuture(
8179
new Exception(
8280
"failed to close port '%s', lastErrorCode=%d"
83-
.formatted(config.serialPort(), serialPort.getLastErrorCode()))
84-
);
81+
.formatted(config.serialPort(), serialPort.getLastErrorCode())));
8582
}
8683
} else {
8784
return CompletableFuture.completedFuture(null);
@@ -136,9 +133,7 @@ public void resetFrameParser() {
136133

137134
private class ModbusRtuDataListener implements SerialPortDataListener {
138135

139-
/**
140-
* Bit mask indicating what events we're interested in.
141-
*/
136+
/** Bit mask indicating what events we're interested in. */
142137
private static final int LISTENING_EVENTS = SerialPort.LISTENING_EVENT_DATA_RECEIVED;
143138

144139
@Override
@@ -173,24 +168,21 @@ private void onFrameReceived(ModbusRtuFrame frame) {
173168
executionQueue.submit(() -> frameReceiver.accept(frame));
174169
}
175170
}
176-
177171
}
178172

179173
/**
180174
* Create a new {@link SerialPortClientTransport} with a callback that allows customizing the
181175
* configuration.
182176
*
183-
* @param configure a {@link Consumer} that accepts a
184-
* {@link SerialPortTransportConfig.Builder} instance to configure.
177+
* @param configure a {@link Consumer} that accepts a {@link SerialPortTransportConfig.Builder}
178+
* instance to configure.
185179
* @return a new {@link SerialPortClientTransport}.
186180
*/
187181
public static SerialPortClientTransport create(
188-
Consumer<SerialPortTransportConfig.Builder> configure
189-
) {
182+
Consumer<SerialPortTransportConfig.Builder> configure) {
190183

191184
var builder = new SerialPortTransportConfig.Builder();
192185
configure.accept(builder);
193186
return new SerialPortClientTransport(builder.build());
194187
}
195-
196188
}

modbus-serial/src/main/java/com/digitalpetri/modbus/serial/server/SerialPortServerTransport.java

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ public SerialPortServerTransport(SerialPortTransportConfig config) {
4949
config.dataBits(),
5050
config.stopBits(),
5151
config.parity(),
52-
config.rs485Mode()
53-
);
52+
config.rs485Mode());
5453

5554
executionQueue = new ExecutionQueue(config.executor());
5655
}
@@ -70,8 +69,7 @@ public CompletionStage<Void> bind() {
7069
return CompletableFuture.failedFuture(
7170
new Exception(
7271
"failed to open port '%s', lastErrorCode=%d"
73-
.formatted(config.serialPort(), serialPort.getLastErrorCode()))
74-
);
72+
.formatted(config.serialPort(), serialPort.getLastErrorCode())));
7573
}
7674
}
7775
}
@@ -87,8 +85,7 @@ public CompletionStage<Void> unbind() {
8785
return CompletableFuture.failedFuture(
8886
new Exception(
8987
"failed to close port '%s', lastErrorCode=%d"
90-
.formatted(config.serialPort(), serialPort.getLastErrorCode()))
91-
);
88+
.formatted(config.serialPort(), serialPort.getLastErrorCode())));
9289
}
9390
} else {
9491
return CompletableFuture.completedFuture(null);
@@ -102,9 +99,7 @@ public void receive(FrameReceiver<ModbusRtuRequestContext, ModbusRtuFrame> frame
10299

103100
private class ModbusRtuDataListener implements SerialPortDataListener {
104101

105-
/**
106-
* Bit mask indicating what events we're interested in.
107-
*/
102+
/** Bit mask indicating what events we're interested in. */
108103
private static final int LISTENING_EVENTS = SerialPort.LISTENING_EVENT_DATA_RECEIVED;
109104

110105
@Override
@@ -138,56 +133,54 @@ private void onFrameReceived(ModbusRtuFrame requestFrame) {
138133
SerialPortServerTransport.this.frameReceiver.get();
139134

140135
if (frameReceiver != null) {
141-
executionQueue.submit(() -> {
142-
try {
143-
ModbusRtuFrame responseFrame =
144-
frameReceiver.receive(new ModbusRtuRequestContext() {}, requestFrame);
145-
146-
int unitId = responseFrame.unitId();
147-
ByteBuffer pdu = responseFrame.pdu();
148-
ByteBuffer crc = responseFrame.crc();
149-
150-
byte[] data = new byte[1 + pdu.remaining() + crc.remaining()];
151-
data[0] = (byte) unitId;
152-
pdu.get(data, 1, pdu.remaining());
153-
crc.get(data, data.length - 2, crc.remaining());
154-
155-
int totalWritten = 0;
156-
while (totalWritten < data.length) {
157-
int written = serialPort.writeBytes(data, data.length - totalWritten, totalWritten);
158-
if (written == -1) {
159-
logger.error("Error writing frame to serial port");
160-
161-
return;
136+
executionQueue.submit(
137+
() -> {
138+
try {
139+
ModbusRtuFrame responseFrame =
140+
frameReceiver.receive(new ModbusRtuRequestContext() {}, requestFrame);
141+
142+
int unitId = responseFrame.unitId();
143+
ByteBuffer pdu = responseFrame.pdu();
144+
ByteBuffer crc = responseFrame.crc();
145+
146+
byte[] data = new byte[1 + pdu.remaining() + crc.remaining()];
147+
data[0] = (byte) unitId;
148+
pdu.get(data, 1, pdu.remaining());
149+
crc.get(data, data.length - 2, crc.remaining());
150+
151+
int totalWritten = 0;
152+
while (totalWritten < data.length) {
153+
int written =
154+
serialPort.writeBytes(data, data.length - totalWritten, totalWritten);
155+
if (written == -1) {
156+
logger.error("Error writing frame to serial port");
157+
158+
return;
159+
}
160+
totalWritten += written;
161+
}
162+
} catch (UnknownUnitIdException e) {
163+
logger.debug("Ignoring request for unknown unit id: {}", requestFrame.unitId());
164+
} catch (Exception e) {
165+
logger.error("Error handling frame: {}", e.getMessage(), e);
162166
}
163-
totalWritten += written;
164-
}
165-
} catch (UnknownUnitIdException e) {
166-
logger.debug("Ignoring request for unknown unit id: {}", requestFrame.unitId());
167-
} catch (Exception e) {
168-
logger.error("Error handling frame: {}", e.getMessage(), e);
169-
}
170-
});
167+
});
171168
}
172169
}
173-
174170
}
175171

176172
/**
177173
* Create a new {@link SerialPortServerTransport} with a callback that allows customizing the
178174
* configuration.
179175
*
180-
* @param configure a {@link Consumer} that accepts a
181-
* {@link SerialPortTransportConfig.Builder} instance to configure.
176+
* @param configure a {@link Consumer} that accepts a {@link SerialPortTransportConfig.Builder}
177+
* instance to configure.
182178
* @return a new {@link SerialPortServerTransport}.
183179
*/
184-
public static SerialPortServerTransport create(
185-
Consumer<Builder> configure
186-
) {
180+
public static SerialPortServerTransport create(Consumer<Builder> configure) {
187181

188182
var builder = new SerialPortTransportConfig.Builder();
189183
configure.accept(builder);
190184
return new SerialPortServerTransport(builder.build());
191185
}
192-
193186
}

modbus-tcp/src/main/java/com/digitalpetri/modbus/tcp/ModbusTcpCodec.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,4 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
4040
}
4141
}
4242
}
43-
4443
}

modbus-tcp/src/main/java/com/digitalpetri/modbus/tcp/Netty.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,36 @@ public final class Netty {
1919
*/
2020
public static synchronized NioEventLoopGroup sharedEventLoop() {
2121
if (EVENT_LOOP == null) {
22-
ThreadFactory threadFactory = new ThreadFactory() {
23-
private final AtomicLong threadNumber = new AtomicLong(0L);
24-
25-
@Override
26-
public Thread newThread(Runnable r) {
27-
Thread thread = new Thread(r,
28-
"modbus-netty-event-loop-" + threadNumber.getAndIncrement());
29-
thread.setDaemon(true);
30-
return thread;
31-
}
32-
};
22+
ThreadFactory threadFactory =
23+
new ThreadFactory() {
24+
private final AtomicLong threadNumber = new AtomicLong(0L);
25+
26+
@Override
27+
public Thread newThread(Runnable r) {
28+
Thread thread =
29+
new Thread(r, "modbus-netty-event-loop-" + threadNumber.getAndIncrement());
30+
thread.setDaemon(true);
31+
return thread;
32+
}
33+
};
3334

3435
EVENT_LOOP = new NioEventLoopGroup(1, threadFactory);
3536
}
3637

3738
return EVENT_LOOP;
3839
}
3940

40-
4141
/**
4242
* @return a shared {@link HashedWheelTimer}.
4343
*/
4444
public static synchronized HashedWheelTimer sharedWheelTimer() {
4545
if (WHEEL_TIMER == null) {
46-
ThreadFactory threadFactory = r -> {
47-
Thread thread = new Thread(r, "modbus-netty-wheel-timer");
48-
thread.setDaemon(true);
49-
return thread;
50-
};
46+
ThreadFactory threadFactory =
47+
r -> {
48+
Thread thread = new Thread(r, "modbus-netty-wheel-timer");
49+
thread.setDaemon(true);
50+
return thread;
51+
};
5152

5253
WHEEL_TIMER = new HashedWheelTimer(threadFactory);
5354
}
@@ -81,8 +82,7 @@ public static synchronized void releaseSharedResources(long timeout, TimeUnit un
8182
}
8283
} catch (InterruptedException e) {
8384
Thread.currentThread().interrupt();
84-
LoggerFactory.getLogger(Netty.class)
85-
.warn("Interrupted awaiting event loop shutdown", e);
85+
LoggerFactory.getLogger(Netty.class).warn("Interrupted awaiting event loop shutdown", e);
8686
}
8787
EVENT_LOOP = null;
8888
}
@@ -92,5 +92,4 @@ public static synchronized void releaseSharedResources(long timeout, TimeUnit un
9292
WHEEL_TIMER = null;
9393
}
9494
}
95-
9695
}

0 commit comments

Comments
 (0)