Skip to content

Commit

Permalink
fixed some sonar bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Aug 9, 2018
1 parent 8c87fca commit 4f38a3f
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.apache.plc4x.java.api.model.Address;
import org.apache.plc4x.java.api.model.SubscriptionType;

import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

Expand All @@ -43,4 +44,32 @@ public int getPeriod() {
return period;
}

@Override
public String toString() {
return "SubscriptionRequestCyclicItem{" +
"timeUnit=" + timeUnit +
", period=" + period +
"} " + super.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof SubscriptionRequestCyclicItem)) {
return false;
}
if (!super.equals(o)) {
return false;
}
SubscriptionRequestCyclicItem that = (SubscriptionRequestCyclicItem) o;
return period == that.period &&
timeUnit == that.timeUnit;
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), timeUnit, period);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.apache.plc4x.java.api.model.Address;
import org.apache.plc4x.java.api.model.SubscriptionType;

import java.util.Objects;
import java.util.function.Consumer;

public abstract class SubscriptionRequestItem<T> extends RequestItem<T> {
Expand All @@ -42,4 +43,32 @@ public Consumer<SubscriptionEventItem<T>> getConsumer() {
return consumer;
}

@Override
public String toString() {
return "SubscriptionRequestItem{" +
"subscriptionType=" + subscriptionType +
", consumer=" + consumer +
"} " + super.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof SubscriptionRequestItem)) {
return false;
}
if (!super.equals(o)) {
return false;
}
SubscriptionRequestItem<?> that = (SubscriptionRequestItem<?>) o;
return subscriptionType == that.subscriptionType &&
Objects.equals(consumer, that.consumer);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), subscriptionType, consumer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.apache.plc4x.java.api.model.SubscriptionHandle;
import org.apache.plc4x.java.api.types.ResponseCode;

public class SubscriptionResponseItem<T> extends ResponseItem<SubscriptionRequestItem<T>> {
import java.util.Objects;

public class SubscriptionResponseItem<T> extends ResponseItem<SubscriptionRequestItem<T>> {

private SubscriptionHandle subscriptionHandle;

Expand All @@ -34,4 +36,30 @@ public SubscriptionHandle getSubscriptionHandle() {
return subscriptionHandle;
}

@Override
public String toString() {
return "SubscriptionResponseItem{" +
"subscriptionHandle=" + subscriptionHandle +
"} " + super.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof SubscriptionResponseItem)) {
return false;
}
if (!super.equals(o)) {
return false;
}
SubscriptionResponseItem<?> that = (SubscriptionResponseItem<?>) o;
return Objects.equals(subscriptionHandle, that.subscriptionHandle);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), subscriptionHandle);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Licensed to the Apache Software Foundation (ASF) under one

import org.apache.commons.io.HexDump;
import org.apache.commons.io.IOUtils;
import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
import org.junit.rules.ExternalResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -69,15 +70,15 @@ private void init(int port) throws IOException, InterruptedException {
accept = serverSocket.accept();
logger.info("Accepted {} and starting listener", accept);
} catch (IOException e) {
throw new RuntimeException(e);
throw new PlcRuntimeException(e);
}
pool.submit(() -> {
InputStream inputStream;
try {
inputStream = accept.getInputStream();
logger.info("Starting to read now");
} catch (IOException e) {
throw new RuntimeException(e);
throw new PlcRuntimeException(e);
}
byte[] buffer = new byte[4096];
try {
Expand All @@ -87,7 +88,7 @@ private void init(int port) throws IOException, InterruptedException {
logger.info("Dump:\n{}", byteArrayOutputStream);
}
} catch (IOException e) {
throw new RuntimeException(e);
throw new PlcRuntimeException(e);
}
});
});
Expand All @@ -112,7 +113,11 @@ private void stop(boolean await) throws IOException, InterruptedException {
public void after() {
try {
stop(true);
} catch (IOException | InterruptedException ignore) {
} catch (InterruptedException e) {
logger.info("Shutdown error", e);
Thread.currentThread().interrupt();
} catch (IOException e) {
logger.info("Shutdown error", e);
}
}

Expand All @@ -136,6 +141,7 @@ public void close() throws IOException {
try {
stop();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Licensed to the Apache Software Foundation (ASF) under one
package org.apache.plc4x.java.base.util;

import org.apache.commons.io.HexDump;
import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.core.IsEqual;
Expand Down Expand Up @@ -52,7 +53,7 @@ public void describeTo(Description description) {
String dump = dump(expected);
description.appendText("\n").appendText(cleanHexDump(dump));
} catch (IOException e) {
throw new RuntimeException(e);
throw new PlcRuntimeException(e);
}
}

Expand All @@ -66,7 +67,7 @@ public void describeMismatch(Object item, Description description) {
String dump = dump((byte[]) item);
description.appendText("was ").appendText("\n").appendText(cleanHexDump(dump));
} catch (IOException e) {
throw new RuntimeException(e);
throw new PlcRuntimeException(e);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static CoilModbusAddress of(String addressString) {
if (!matcher.matches()) {
throw new PlcRuntimeException(addressString + " doesn't match " + ADDRESS_PATTERN);
}
int address = Integer.valueOf(matcher.group("address"));
int address = Integer.parseInt(matcher.group("address"));
return new CoilModbusAddress(address);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public static MaskWriteRegisterModbusAddress of(String addressString) {
if (!matcher.matches()) {
throw new PlcRuntimeException(addressString + " doesn't match " + ADDRESS_PATTERN);
}
int address = Integer.valueOf(matcher.group("address"));
int andMask = Integer.valueOf(matcher.group("andMask"));
int orMask = Integer.valueOf(matcher.group("orMask"));
int address = Integer.parseInt(matcher.group("address"));
int andMask = Integer.parseInt(matcher.group("andMask"));
int orMask = Integer.parseInt(matcher.group("orMask"));
return new MaskWriteRegisterModbusAddress(address, andMask, orMask);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static ReadDiscreteInputsModbusAddress of(String addressString) {
if (!matcher.matches()) {
throw new PlcRuntimeException(addressString + " doesn't match " + ADDRESS_PATTERN);
}
int address = Integer.valueOf(matcher.group("address"));
int address = Integer.parseInt(matcher.group("address"));
return new ReadDiscreteInputsModbusAddress(address);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static ReadHoldingRegistersModbusAddress of(String addressString) {
if (!matcher.matches()) {
throw new PlcRuntimeException(addressString + " doesn't match " + ADDRESS_PATTERN);
}
int address = Integer.valueOf(matcher.group("address"));
int address = Integer.parseInt(matcher.group("address"));
return new ReadHoldingRegistersModbusAddress(address);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static ReadInputRegistersModbusAddress of(String addressString) {
if (!matcher.matches()) {
throw new PlcRuntimeException(addressString + " doesn't match " + ADDRESS_PATTERN);
}
int address = Integer.valueOf(matcher.group("address"));
int address = Integer.parseInt(matcher.group("address"));
return new ReadInputRegistersModbusAddress(address);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static RegisterModbusAddress of(String addressString) {
if (!matcher.matches()) {
throw new PlcRuntimeException(addressString + " doesn't match " + ADDRESS_PATTERN);
}
int address = Integer.valueOf(matcher.group("address"));
int address = Integer.parseInt(matcher.group("address"));
return new RegisterModbusAddress(address);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ public class Plc4XModbusProtocol extends MessageToMessageCodec<ModbusTcpPayload,

private final ConcurrentMap<Short, PlcRequestContainer<PlcRequest, PlcResponse>> requestsMap = new ConcurrentHashMap<>();

public Plc4XModbusProtocol() {
}

@Override
protected void encode(ChannelHandlerContext ctx, PlcRequestContainer<PlcRequest, PlcResponse> msg, List<Object> out) throws Exception {
LOGGER.trace("(<--OUT): {}, {}, {}", ctx, msg, out);
Expand Down

0 comments on commit 4f38a3f

Please sign in to comment.