Replace or remove assert statements (#3994)

Java assertions are disabled by default so in this PR they are replaced/removed where applicable.

Signed-off-by: Wouter Born <github@maindrain.net>
pull/4004/head
Wouter Born 2024-01-02 22:21:42 +01:00 committed by GitHub
parent dbc3b19a4f
commit 896b05e177
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 23 additions and 39 deletions

View File

@ -21,6 +21,7 @@ import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
@ -112,17 +113,11 @@ public class ToneSynthesizer {
}
public ToneSynthesizer(AudioFormat audioFormat) {
assert audioFormat.getFrequency() != null;
this.sampleRate = audioFormat.getFrequency();
assert audioFormat.getBitDepth() != null;
this.bitDepth = audioFormat.getBitDepth();
assert audioFormat.getBitRate() != null;
this.bitRate = audioFormat.getBitRate();
assert audioFormat.getChannels() != null;
this.channels = audioFormat.getChannels();
var bigEndian = audioFormat.isBigEndian();
assert bigEndian != null;
this.bigEndian = bigEndian;
this.sampleRate = Objects.requireNonNull(audioFormat.getFrequency());
this.bitDepth = Objects.requireNonNull(audioFormat.getBitDepth());
this.bitRate = Objects.requireNonNull(audioFormat.getBitRate());
this.channels = Objects.requireNonNull(audioFormat.getChannels());
this.bigEndian = Objects.requireNonNull(audioFormat.isBigEndian());
}
/**

View File

@ -278,9 +278,7 @@ public class ModbusBitUtilities {
public static short extractUInt8(byte[] bytes, int index) {
assertIndexAndType(bytes, index, ValueType.UINT8);
int signed = extractSInt8(bytes, index);
short unsigned = (short) (signed & 0xff);
assert unsigned >= 0;
return unsigned;
return (short) (signed & 0xff);
}
/**
@ -313,9 +311,7 @@ public class ModbusBitUtilities {
public static int extractUInt16(byte[] bytes, int index) {
assertIndexAndType(bytes, index, ValueType.UINT16);
int signed = extractSInt16(bytes, index);
int unsigned = signed & 0xffff;
assert unsigned >= 0;
return unsigned;
return signed & 0xffff;
}
/**
@ -350,9 +346,7 @@ public class ModbusBitUtilities {
public static long extractUInt32(byte[] bytes, int index) {
assertIndexAndType(bytes, index, ValueType.UINT32);
long signed = extractSInt32(bytes, index);
long unsigned = signed & 0xffff_ffffL;
assert unsigned >= 0;
return unsigned;
return signed & 0xffff_ffffL;
}
/**
@ -392,9 +386,7 @@ public class ModbusBitUtilities {
public static long extractUInt32Swap(byte[] bytes, int index) {
assertIndexAndType(bytes, index, ValueType.UINT32_SWAP);
long signed = extractSInt32Swap(bytes, index);
long unsigned = signed & 0xffff_ffffL;
assert unsigned >= 0;
return unsigned;
return signed & 0xffff_ffffL;
}
/**

View File

@ -278,7 +278,7 @@ public class IntegrationTestSupport extends JavaTest {
}
public ModbusSlaveEndpoint getEndpoint() {
assert tcpModbusPort > 0;
assertTrue(tcpModbusPort > 0);
return new ModbusTCPSlaveEndpoint("127.0.0.1", tcpModbusPort, false);
}

View File

@ -157,7 +157,7 @@ public class SmokeTest extends IntegrationTestSupport {
try (ModbusCommunicationInterface comms = modbusManager.newModbusCommunicationInterface(endpoint, null)) {
comms.submitOneTimePoll(new ModbusReadRequestBlueprint(SLAVE_UNIT_ID,
ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS, 0, 5, 1), result -> {
assert result.getRegisters().isPresent();
assertTrue(result.getRegisters().isPresent());
okCount.incrementAndGet();
callbackCalled.countDown();
}, failure -> {
@ -194,7 +194,7 @@ public class SmokeTest extends IntegrationTestSupport {
configuration)) {
comms.submitOneTimePoll(new ModbusReadRequestBlueprint(SLAVE_UNIT_ID,
ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS, 0, 5, 1), result -> {
assert result.getRegisters().isPresent();
assertTrue(result.getRegisters().isPresent());
okCount.incrementAndGet();
callbackCalled.countDown();
}, failure -> {
@ -228,7 +228,7 @@ public class SmokeTest extends IntegrationTestSupport {
try (ModbusCommunicationInterface comms = modbusManager.newModbusCommunicationInterface(endpoint, null)) {
comms.submitOneTimePoll(new ModbusReadRequestBlueprint(SLAVE_UNIT_ID,
ModbusReadFunctionCode.READ_MULTIPLE_REGISTERS, 0, 5, 1), result -> {
assert result.getRegisters().isPresent();
assertTrue(result.getRegisters().isPresent());
okCount.incrementAndGet();
callbackCalled.countDown();
}, failure -> {

View File

@ -12,6 +12,7 @@
*/
package org.openhab.core.io.transport.mqtt.reconnect;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
@ -105,8 +106,7 @@ public class PeriodicReconnectStrategy extends AbstractReconnectStrategy {
return;
}
assert scheduler != null;
scheduledTask = scheduler.scheduleWithFixedDelay(() -> {
scheduledTask = Objects.requireNonNull(scheduler).scheduleWithFixedDelay(() -> {
MqttBrokerConnection brokerConnection = this.brokerConnection;
// If the broker connections is not available anymore, stop the timed reconnect.
if (brokerConnection == null) {

View File

@ -367,8 +367,7 @@ public class I18nProviderImpl
throw new IllegalArgumentException("Dimension " + dimension.getName() + " is unknown. This is a bug.");
}
Unit<T> unit = (Unit<T>) map.get(getMeasurementSystem());
assert unit != null;
return unit;
return Objects.requireNonNull(unit);
}
@Override

View File

@ -244,8 +244,7 @@ public class NumberItem extends GenericItem implements MetadataAwareItem {
public void removedMetadata(Metadata metadata) {
Class<? extends Quantity<?>> dimension = this.dimension;
if (dimension != null && UNIT_METADATA_NAMESPACE.equals(metadata.getUID().getNamespace())) {
assert unitProvider != null;
unit = unitProvider.getUnit((Class<? extends Quantity>) dimension);
unit = Objects.requireNonNull(unitProvider).getUnit((Class<? extends Quantity>) dimension);
logger.trace("Item '{}' now has unit '{}'", name, unit);
}
}

View File

@ -346,9 +346,8 @@ public class NetUtil implements NetworkAddressService {
}
for (InterfaceAddress cidr : networkInterface.getInterfaceAddresses()) {
final InetAddress address = cidr.getAddress();
assert address != null; // NetworkInterface.getInterfaceAddresses() should return only non-null
// addresses
// NetworkInterface.getInterfaceAddresses() should return only non-null addresses
final InetAddress address = Objects.requireNonNull(cidr.getAddress());
interfaceIPs.add(new CidrAddress(address, cidr.getNetworkPrefixLength()));
}
}

View File

@ -14,6 +14,7 @@ package org.openhab.core.internal.i18n;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import javax.measure.Quantity;
@ -38,9 +39,8 @@ public class TestUnitProvider implements UnitProvider {
@Override
@SuppressWarnings("unchecked")
public <T extends Quantity<T>> Unit<T> getUnit(Class<T> dimension) {
Unit<T> unit = (Unit<T>) dimensionMap.getOrDefault(dimension, Map.of()).get(SIUnits.getInstance());
assert unit != null;
return unit;
return Objects
.requireNonNull((Unit<T>) dimensionMap.getOrDefault(dimension, Map.of()).get(SIUnits.getInstance()));
}
@Override