Add a method for checking peripheral equality

This feels a little overkill, but nice to standardise how this code
looks.

There's a bit of me which wonders if we should remove
IPeripheral.equals, and just use Object.equals, but I do also kinda like
the explicitness of the current interface? IDK.
This commit is contained in:
Jonathan Coates 2024-03-16 14:01:22 +00:00
parent 61f9b1d0c6
commit f8ef40d378
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
6 changed files with 38 additions and 8 deletions

View File

@ -6,6 +6,7 @@
import dan200.computercraft.api.network.wired.WiredNetworkChange;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.core.util.PeripheralHelpers;
import java.util.Collections;
import java.util.HashMap;
@ -52,7 +53,7 @@ static WiredNetworkChangeImpl changeOf(Map<String, IPeripheral> oldPeripherals,
var oldValue = entry.getValue();
if (newPeripherals.containsKey(oldKey)) {
var rightValue = added.get(oldKey);
if (oldValue.equals(rightValue)) {
if (PeripheralHelpers.equals(oldValue, rightValue)) {
added.remove(oldKey);
} else {
removed.put(oldKey, oldValue);

View File

@ -6,6 +6,7 @@
import dan200.computercraft.api.ComputerCraftTags;
import dan200.computercraft.api.peripheral.IPeripheral;
import dan200.computercraft.core.util.PeripheralHelpers;
import dan200.computercraft.shared.computer.core.ServerContext;
import dan200.computercraft.shared.platform.ComponentAccess;
import net.minecraft.core.BlockPos;
@ -65,7 +66,7 @@ public boolean attach(Level world, BlockPos origin, Direction direction) {
this.id = ServerContext.get(assertNonNull(world.getServer())).getNextId("peripheral." + type);
}
return oldPeripheral == null || !oldPeripheral.equals(peripheral);
return !PeripheralHelpers.equals(oldPeripheral, peripheral);
}
}

View File

@ -218,8 +218,7 @@ public void attach(IComputerAccess computer) {
ConcurrentMap<String, RemotePeripheralWrapper> wrappers;
synchronized (peripheralWrappers) {
wrappers = peripheralWrappers.get(computer);
if (wrappers == null) peripheralWrappers.put(computer, wrappers = new ConcurrentHashMap<>());
wrappers = peripheralWrappers.computeIfAbsent(computer, k -> new ConcurrentHashMap<>());
}
synchronized (modem.getRemotePeripherals()) {

View File

@ -14,6 +14,7 @@
import dan200.computercraft.api.turtle.TurtleSide;
import dan200.computercraft.api.upgrades.UpgradeData;
import dan200.computercraft.core.computer.ComputerSide;
import dan200.computercraft.core.util.PeripheralHelpers;
import dan200.computercraft.impl.TurtleUpgrades;
import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.computer.core.ServerComputer;
@ -589,7 +590,7 @@ private void updatePeripherals(ServerComputer serverComputer) {
}
var existing = peripherals.get(side);
if (existing == peripheral || (existing != null && peripheral != null && existing.equals(peripheral))) {
if (PeripheralHelpers.equals(existing, peripheral)) {
// If the peripheral is the same, just use that.
peripheral = existing;
} else {

View File

@ -11,6 +11,7 @@
import dan200.computercraft.core.filesystem.FileSystem;
import dan200.computercraft.core.metrics.MetricsObserver;
import dan200.computercraft.core.terminal.Terminal;
import dan200.computercraft.core.util.PeripheralHelpers;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
@ -268,9 +269,7 @@ public void setPeripheral(ComputerSide side, @Nullable IPeripheral peripheral) {
synchronized (peripherals) {
var index = side.ordinal();
var existing = peripherals[index];
if ((existing == null && peripheral != null) ||
(existing != null && peripheral == null) ||
(existing != null && !existing.equals(peripheral))) {
if (!PeripheralHelpers.equals(existing, peripheral)) {
peripherals[index] = peripheral;
if (peripheralListener != null) peripheralListener.onPeripheralChanged(side, peripheral);
}

View File

@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: 2023 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.core.util;
import dan200.computercraft.api.peripheral.IPeripheral;
import javax.annotation.Nullable;
/**
* Utilities for working with {@linkplain IPeripheral peripherals}.
*/
public final class PeripheralHelpers {
private PeripheralHelpers() {
}
/**
* Determine if two peripherals are equal. This is equivalent to {@link java.util.Objects#equals(Object, Object)},
* but using {@link IPeripheral#equals(IPeripheral)} instead.
*
* @param a The first peripheral.
* @param b The second peripheral.
* @return If the two peripherals are equal.
*/
public static boolean equals(@Nullable IPeripheral a, @Nullable IPeripheral b) {
return a == b || (a != null && b != null && a.equals(b));
}
}