Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ComponentRegistry: return list-type #1382

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions java/se/sics/mspsim/cli/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,8 @@ public void init(String name, ComponentRegistry registry) {

@Override
public void start() {
Object[] commandBundles = registry.getAllComponents(CommandBundle.class);
if (commandBundles != null) {
for (Object commandBundle : commandBundles) {
((CommandBundle) commandBundle).setupCommands(registry, this);
}
for (var commandBundle: registry.getAllComponents(CommandBundle.class)) {
commandBundle.setupCommands(registry, this);
}
}

Expand Down
4 changes: 2 additions & 2 deletions java/se/sics/mspsim/cli/MiscCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ public int executeCommand(CommandContext context) {
verbose = false;
}
if (context.getArgumentCount() == index) {
ServiceComponent[] sc = registry.getAllComponents(ServiceComponent.class);
if (sc.length == 0) {
var sc = registry.getAllComponents(ServiceComponent.class);
if (sc.isEmpty()) {
context.out.println("No services found.");
} else {
for (ServiceComponent service : sc) {
Expand Down
11 changes: 5 additions & 6 deletions java/se/sics/mspsim/util/ComponentRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
package se.sics.mspsim.util;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

public class ComponentRegistry {

Expand Down Expand Up @@ -111,15 +112,14 @@ public synchronized <T> T getComponent(Class<T> type, String name) {
return null;
}

@SuppressWarnings("unchecked")
public synchronized <T> T[] getAllComponents(Class<T> type, String name) {
public synchronized <T> List<T> getAllComponents(Class<T> type, String name) {
ArrayList<T> list = new ArrayList<>();
for (ComponentEntry entry : components) {
if (type.isInstance(entry.component) && name.equals(entry.name)) {
list.add(type.cast(entry.component));
}
}
return list.toArray((T[]) java.lang.reflect.Array.newInstance(type, list.size()));
return list;
}

public synchronized <T> T getComponent(Class<T> type) {
Expand All @@ -131,15 +131,14 @@ public synchronized <T> T getComponent(Class<T> type) {
return null;
}

@SuppressWarnings("unchecked")
public synchronized <T> T[] getAllComponents(Class<T> type) {
public synchronized <T> List<T> getAllComponents(Class<T> type) {
ArrayList<T> list = new ArrayList<>();
for (ComponentEntry entry : components) {
if (type.isInstance(entry.component)) {
list.add(type.cast(entry.component));
}
}
return list.toArray((T[]) java.lang.reflect.Array.newInstance(type, list.size()));
return list;
}

public void start() {
Expand Down