Skip to content

Commit

Permalink
Replace Collections methods with {List,Map,Set}.of
Browse files Browse the repository at this point in the history
The two implementations aren't entirely compatible - the implementation
returned by .of will throw an NPE on .contains(null), whereas the
Collections implementations just return false. However, we try to avoid
passing null to collections methods, so this should be safe.

There's no strong reason to do this, but it helps make the code a little
more consistent
  • Loading branch information
SquidDev committed Oct 21, 2023
1 parent 8eabd4f commit cab66a2
Show file tree
Hide file tree
Showing 37 changed files with 79 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -219,7 +218,7 @@ public void uploadResult(UploadResult result, @Nullable Component message) {

private void alert(Component title, Component message) {
OptionScreen.show(minecraft, title, message,
Collections.singletonList(OptionScreen.newButton(OK, b -> minecraft.setScreen(this))),
List.of(OptionScreen.newButton(OK, b -> minecraft.setScreen(this))),
() -> minecraft.setScreen(this)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public record UpgradeWrapper<R extends UpgradeSerialiser<? extends T>, T extends
private final String kind;
private final ResourceKey<Registry<R>> registry;

private Map<String, UpgradeWrapper<R, T>> current = Collections.emptyMap();
private Map<T, UpgradeWrapper<R, T>> currentWrappers = Collections.emptyMap();
private Map<String, UpgradeWrapper<R, T>> current = Map.of();
private Map<T, UpgradeWrapper<R, T>> currentWrappers = Map.of();

public UpgradeManager(String kind, String path, ResourceKey<Registry<R>> registry) {
super(GSON, path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.Map;

final class WiredNetworkChangeImpl implements WiredNetworkChange {
private static final WiredNetworkChangeImpl EMPTY = new WiredNetworkChangeImpl(Collections.emptyMap(), Collections.emptyMap());
private static final WiredNetworkChangeImpl EMPTY = new WiredNetworkChangeImpl(Map.of(), Map.of());

private final Map<String, IPeripheral> removed;
private final Map<String, IPeripheral> added;
Expand All @@ -27,21 +27,21 @@ static WiredNetworkChangeImpl changed(Map<String, IPeripheral> removed, Map<Stri
}

static WiredNetworkChangeImpl added(Map<String, IPeripheral> added) {
return added.isEmpty() ? EMPTY : new WiredNetworkChangeImpl(Collections.emptyMap(), Collections.unmodifiableMap(added));
return added.isEmpty() ? EMPTY : new WiredNetworkChangeImpl(Map.of(), Collections.unmodifiableMap(added));
}

static WiredNetworkChangeImpl removed(Map<String, IPeripheral> removed) {
return removed.isEmpty() ? EMPTY : new WiredNetworkChangeImpl(Collections.unmodifiableMap(removed), Collections.emptyMap());
return removed.isEmpty() ? EMPTY : new WiredNetworkChangeImpl(Collections.unmodifiableMap(removed), Map.of());
}

static WiredNetworkChangeImpl changeOf(Map<String, IPeripheral> oldPeripherals, Map<String, IPeripheral> newPeripherals) {
// Handle the trivial cases, where all peripherals have been added or removed.
if (oldPeripherals.isEmpty() && newPeripherals.isEmpty()) {
return EMPTY;
} else if (oldPeripherals.isEmpty()) {
return new WiredNetworkChangeImpl(Collections.emptyMap(), newPeripherals);
return new WiredNetworkChangeImpl(Map.of(), newPeripherals);
} else if (newPeripherals.isEmpty()) {
return new WiredNetworkChangeImpl(oldPeripherals, Collections.emptyMap());
return new WiredNetworkChangeImpl(oldPeripherals, Map.of());
}

Map<String, IPeripheral> added = new HashMap<>(newPeripherals);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public boolean connect(WiredNode nodeU, WiredNode nodeV) {
// Move all nodes across into this network, destroying the original nodes.
nodes.addAll(otherNodes);
for (var node : otherNodes) node.network = this;
other.nodes = Collections.emptySet();
other.nodes = Set.of();

// Move all peripherals across,
other.peripherals = Collections.emptyMap();
other.peripherals = Map.of();
peripherals.putAll(otherPeripherals);

if (!thisPeripherals.isEmpty()) {
Expand Down Expand Up @@ -216,7 +216,7 @@ public boolean remove(WiredNode node) {
try {
// We special case the original node: detaching all peripherals when needed.
wired.network = wiredNetwork;
wired.peripherals = Collections.emptyMap();
wired.peripherals = Map.of();

// Ensure every network is finalised
for (var network : maximals) {
Expand Down Expand Up @@ -332,7 +332,7 @@ private void removeSingleNode(WiredNodeImpl wired, WiredNetworkImpl wiredNetwork
// Detach the old peripherals then remove them from the old network
wired.network = wiredNetwork;
wired.neighbours.clear();
wired.peripherals = Collections.emptyMap();
wired.peripherals = Map.of();

// Broadcast the change
if (!peripherals.isEmpty()) WiredNetworkChangeImpl.removed(peripherals).broadcast(wired);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
RequiredArgumentBuilder.<CommandSourceStack, ComputersArgumentType.ComputersSupplier>argument("computer", manyComputers())
.suggests((context, builder) -> Suggestions.empty())
)
.argManyValue("args", StringArgumentType.string(), Collections.emptyList())
.argManyValue("args", StringArgumentType.string(), List.of())
.executes((c, a) -> queue(getComputersArgument(c, "computer"), a)))

.then(command("view")
Expand Down Expand Up @@ -300,7 +300,7 @@ private static int trackStop(CommandSourceStack source) throws CommandSyntaxExce
return 1;
}

private static final List<AggregatedMetric> DEFAULT_FIELDS = Arrays.asList(
private static final List<AggregatedMetric> DEFAULT_FIELDS = List.of(
new AggregatedMetric(Metrics.COMPUTER_TASKS, Aggregate.COUNT),
new AggregatedMetric(Metrics.COMPUTER_TASKS, Aggregate.NONE),
new AggregatedMetric(Metrics.COMPUTER_TASKS, Aggregate.AVG)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public final class ComputersArgumentType implements ArgumentType<ComputersArgume
private static final ComputersArgumentType MANY = new ComputersArgumentType(false);
private static final ComputersArgumentType SOME = new ComputersArgumentType(true);

private static final List<String> EXAMPLES = Arrays.asList(
private static final List<String> EXAMPLES = List.of(
"0", "#0", "@Label", "~Advanced"
);

Expand Down Expand Up @@ -75,7 +75,7 @@ public ComputersSupplier parse(StringReader reader) throws CommandSyntaxExceptio
var instance = reader.readInt();
computers = s -> {
var computer = ServerContext.get(s.getServer()).registry().get(instance);
return computer == null ? Collections.emptyList() : Collections.singletonList(computer);
return computer == null ? List.of() : List.of(computer);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.function.Supplier;
Expand Down Expand Up @@ -63,7 +62,7 @@ public <T> CommandNodeBuilder<S, ArgCommand<S, List<T>>> argManyValue(String nam
}

public <T> CommandNodeBuilder<S, ArgCommand<S, List<T>>> argManyValue(String name, ArgumentType<T> type, T defaultValue) {
return argManyValue(name, type, Collections.singletonList(defaultValue));
return argManyValue(name, type, List.of(defaultValue));
}

public <T> CommandNodeBuilder<S, ArgCommand<S, List<T>>> argMany(String name, ArgumentType<T> type, Supplier<List<T>> empty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ public final long execAsync(ILuaContext context, String command) throws LuaExcep
public final List<String> list(IArguments args) throws LuaException {
var server = computer.getLevel().getServer();

if (server == null) return Collections.emptyList();
if (server == null) return List.of();
CommandNode<CommandSourceStack> node = server.getCommands().getDispatcher().getRoot();
for (var j = 0; j < args.count(); j++) {
var name = args.getString(j);
node = node.getChild(name);
if (!(node instanceof LiteralCommandNode)) return Collections.emptyList();
if (!(node instanceof LiteralCommandNode)) return List.of();
}

List<String> result = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.level.storage.loot.predicates.LootItemConditionType;

import java.util.Collections;
import java.util.Set;

/**
Expand All @@ -33,7 +32,7 @@ public boolean test(LootContext lootContext) {

@Override
public Set<LootContextParam<?>> getReferencedContextParams() {
return Collections.singleton(LootContextParams.BLOCK_ENTITY);
return Set.of(LootContextParams.BLOCK_ENTITY);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.level.storage.loot.predicates.LootItemConditionType;

import java.util.Collections;
import java.util.Set;

/**
Expand All @@ -33,7 +32,7 @@ public boolean test(LootContext lootContext) {

@Override
public Set<LootContextParam<?>> getReferencedContextParams() {
return Collections.singleton(LootContextParams.BLOCK_ENTITY);
return Set.of(LootContextParams.BLOCK_ENTITY);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.level.storage.loot.predicates.LootItemConditionType;

import java.util.Collections;
import java.util.Set;

/**
Expand All @@ -33,7 +32,7 @@ public boolean test(LootContext lootContext) {

@Override
public Set<LootContextParam<?>> getReferencedContextParams() {
return Collections.singleton(LootContextParams.THIS_ENTITY);
return Set.of(LootContextParams.THIS_ENTITY);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,19 @@
import dan200.computercraft.impl.PocketUpgrades;
import dan200.computercraft.impl.TurtleUpgrades;
import dan200.computercraft.shared.ModRegistry;
import dan200.computercraft.shared.computer.core.ComputerFamily;
import dan200.computercraft.shared.pocket.items.PocketComputerItem;
import dan200.computercraft.shared.turtle.items.TurtleItem;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;

/**
* Utilities for recipe mod plugins (such as JEI).
*/
public final class RecipeModHelpers {
static final List<ComputerFamily> MAIN_FAMILIES = Arrays.asList(ComputerFamily.NORMAL, ComputerFamily.ADVANCED);
static final List<Supplier<TurtleItem>> TURTLES = List.of(ModRegistry.Items.TURTLE_NORMAL, ModRegistry.Items.TURTLE_ADVANCED);
static final List<Supplier<PocketComputerItem>> POCKET_COMPUTERS = List.of(ModRegistry.Items.POCKET_COMPUTER_NORMAL, ModRegistry.Items.POCKET_COMPUTER_ADVANCED);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public List<T> findRecipesWithInput(ItemStack stack) {
// Suggest possible upgrades which can be applied to this turtle
var left = item.getUpgradeWithData(stack, TurtleSide.LEFT);
var right = item.getUpgradeWithData(stack, TurtleSide.RIGHT);
if (left != null && right != null) return Collections.emptyList();
if (left != null && right != null) return List.of();

List<T> recipes = new ArrayList<>();
var ingredient = Ingredient.of(stack);
Expand All @@ -135,7 +135,7 @@ public List<T> findRecipesWithInput(ItemStack stack) {
} else if (stack.getItem() instanceof PocketComputerItem) {
// Suggest possible upgrades which can be applied to this turtle
var back = PocketComputerItem.getUpgrade(stack);
if (back != null) return Collections.emptyList();
if (back != null) return List.of();

List<T> recipes = new ArrayList<>();
var ingredient = Ingredient.of(stack);
Expand All @@ -148,7 +148,7 @@ public List<T> findRecipesWithInput(ItemStack stack) {
} else {
// If this item is usable as an upgrade, find all possible recipes.
var upgrades = upgradeItemLookup.get(stack.getItem());
if (upgrades == null) return Collections.emptyList();
if (upgrades == null) return List.of();

List<T> recipes = null;
var multiple = false;
Expand All @@ -169,7 +169,7 @@ public List<T> findRecipesWithInput(ItemStack stack) {
}
}

return recipes == null ? Collections.emptyList() : Collections.unmodifiableList(recipes);
return recipes == null ? List.of() : Collections.unmodifiableList(recipes);
}
}

Expand Down Expand Up @@ -215,7 +215,7 @@ public List<T> findRecipesWithOutput(ItemStack stack) {

return Collections.unmodifiableList(recipes);
} else {
return Collections.emptyList();
return List.of();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;

import java.util.Collections;
import java.util.List;

@JeiPlugin
public class JEIComputerCraft implements IModPlugin {
Expand Down Expand Up @@ -61,7 +61,7 @@ public void onRuntimeAvailable(IJeiRuntime runtime) {
var category = registry.createRecipeLookup(RecipeTypes.CRAFTING);
category.get().forEach(wrapper -> {
if (RecipeModHelpers.shouldRemoveRecipe(wrapper.getId())) {
registry.hideRecipes(RecipeTypes.CRAFTING, Collections.singleton(wrapper));
registry.hideRecipes(RecipeTypes.CRAFTING, List.of(wrapper));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingRecipe;

import java.util.Collections;
import java.util.List;

class RecipeResolver implements IRecipeManagerPlugin {
Expand All @@ -24,36 +23,36 @@ class RecipeResolver implements IRecipeManagerPlugin {
@Override
public <V> List<RecipeType<?>> getRecipeTypes(IFocus<V> focus) {
var value = focus.getTypedValue().getIngredient();
if (!(value instanceof ItemStack stack)) return Collections.emptyList();
if (!(value instanceof ItemStack stack)) return List.of();

return switch (focus.getRole()) {
case INPUT ->
stack.getItem() instanceof TurtleItem || stack.getItem() instanceof PocketComputerItem || resolver.isUpgrade(stack)
? Collections.singletonList(RecipeTypes.CRAFTING)
: Collections.emptyList();
? List.of(RecipeTypes.CRAFTING)
: List.of();
case OUTPUT -> stack.getItem() instanceof TurtleItem || stack.getItem() instanceof PocketComputerItem
? Collections.singletonList(RecipeTypes.CRAFTING)
: Collections.emptyList();
default -> Collections.emptyList();
? List.of(RecipeTypes.CRAFTING)
: List.of();
default -> List.of();
};
}

@Override
public <T, V> List<T> getRecipes(IRecipeCategory<T> recipeCategory, IFocus<V> focus) {
if (!(focus.getTypedValue().getIngredient() instanceof ItemStack stack) || recipeCategory.getRecipeType() != RecipeTypes.CRAFTING) {
return Collections.emptyList();
return List.of();
}

return switch (focus.getRole()) {
case INPUT -> cast(resolver.findRecipesWithInput(stack));
case OUTPUT -> cast(resolver.findRecipesWithOutput(stack));
default -> Collections.emptyList();
default -> List.of();
};
}

@Override
public <T> List<T> getRecipes(IRecipeCategory<T> recipeCategory) {
return Collections.emptyList();
return List.of();
}

@SuppressWarnings({ "unchecked", "rawtypes" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import net.minecraft.world.phys.Vec3;

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;

public class CableBlockEntity extends BlockEntity {
Expand Down Expand Up @@ -274,7 +274,7 @@ void modemChanged() {
if (!canAttachPeripheral() && peripheralAccessAllowed) {
peripheralAccessAllowed = false;
peripheral.detach();
node.updatePeripherals(Collections.emptyMap());
node.updatePeripherals(Map.of());
setChanged();
updateBlockState();
}
Expand All @@ -291,7 +291,7 @@ private void togglePeripheralAccess() {
peripheral.detach();

peripheralAccessAllowed = false;
node.updatePeripherals(Collections.emptyMap());
node.updatePeripherals(Map.of());
}

updateBlockState();
Expand Down
Loading

0 comments on commit cab66a2

Please sign in to comment.