Skip to content

Commit

Permalink
Replace or remove assert statements (openhab#3994)
Browse files Browse the repository at this point in the history
Java assertions are disabled by default so in this PR they are replaced/removed where applicable.

Signed-off-by: Wouter Born <github@maindrain.net>
Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
  • Loading branch information
wborn authored and Ciprian Pascu committed Jan 17, 2024
1 parent 85fba42 commit cad8cfb
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

Expand Down Expand Up @@ -112,17 +113,11 @@ public static Tone silenceTone(long millis) {
}

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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,7 @@ public static short extractUInt8(byte[] bytes, int registerIndex, boolean hiByte
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);
}

/**
Expand Down Expand Up @@ -313,9 +311,7 @@ public static short extractSInt16(byte[] bytes, int index) {
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;
}

/**
Expand Down Expand Up @@ -350,9 +346,7 @@ public static int extractSInt32(byte[] bytes, int index) {
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;
}

/**
Expand Down Expand Up @@ -392,9 +386,7 @@ public static int extractSInt32Swap(byte[] bytes, int index) {
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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ private void startSerialServer() {
}

public ModbusSlaveEndpoint getEndpoint() {
assert tcpModbusPort > 0;
assertTrue(tcpModbusPort > 0);
return new ModbusTCPSlaveEndpoint("127.0.0.1", tcpModbusPort, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public void testSlaveReadErrorResponse() throws Exception {
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 -> {
Expand Down Expand Up @@ -194,7 +194,7 @@ public void testSlaveConnectionError() throws Exception {
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 -> {
Expand Down Expand Up @@ -228,7 +228,7 @@ public void testIOError() throws Exception {
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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -105,8 +106,7 @@ public synchronized void lostConnection() {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,7 @@ public <T extends Quantity<T>> Unit<T> getUnit(Class<T> dimension) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,7 @@ public void updatedMetadata(Metadata oldMetadata, Metadata newMetadata) {
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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,8 @@ public static Collection<CidrAddress> getAllInterfaceAddresses() {
}

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()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import javax.measure.Quantity;
Expand All @@ -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
Expand Down

0 comments on commit cad8cfb

Please sign in to comment.