Files
LuaScript/src/main/java/org/openautonomousconnection/luascript/hosts/HostServices.java

145 lines
3.5 KiB
Java
Raw Normal View History

2026-01-16 21:47:04 +01:00
package org.openautonomousconnection.luascript.hosts;
import java.util.Objects;
import java.util.Optional;
/**
2026-02-10 19:24:38 +01:00
* Container for host-side services exposed to Lua.
2026-01-16 21:47:04 +01:00
*
2026-02-10 19:24:38 +01:00
* <p>All services are optional to allow different embedding scenarios.</p>
2026-01-16 21:47:04 +01:00
*/
2026-02-10 19:24:38 +01:00
public interface HostServices {
2026-01-16 21:47:04 +01:00
/**
2026-02-10 19:24:38 +01:00
* Returns an optional UI host.
*
2026-02-10 19:24:38 +01:00
* @return ui host
*/
2026-02-10 19:24:38 +01:00
Optional<UiHost> ui();
2026-01-16 21:53:46 +01:00
/**
2026-02-10 19:24:38 +01:00
* Returns an optional DOM host.
*
* @return dom host
2026-01-16 21:53:46 +01:00
*/
2026-02-10 19:24:38 +01:00
Optional<DomHost> dom();
2026-01-16 21:53:46 +01:00
2026-01-16 21:47:04 +01:00
/**
2026-02-10 19:24:38 +01:00
* Returns an optional event host.
*
* @return event host
2026-01-16 21:47:04 +01:00
*/
2026-02-10 19:24:38 +01:00
Optional<EventHost> events();
2026-01-16 21:47:04 +01:00
/**
2026-02-10 19:24:38 +01:00
* Returns an optional resource host.
*
* @return resource host
2026-01-16 21:47:04 +01:00
*/
2026-02-10 19:24:38 +01:00
Optional<ResourceHost> resources();
2026-01-16 21:47:04 +01:00
/**
2026-02-10 19:24:38 +01:00
* Returns an optional console host.
*
* @return console host
2026-01-16 21:47:04 +01:00
*/
2026-02-10 19:24:38 +01:00
Optional<ConsoleHost> console();
2026-01-16 21:47:04 +01:00
2026-01-16 21:53:46 +01:00
/**
2026-02-10 19:24:38 +01:00
* Simple immutable implementation.
2026-01-16 21:53:46 +01:00
*/
2026-02-10 19:24:38 +01:00
final class Default implements HostServices {
private final UiHost ui;
private final DomHost dom;
private final EventHost events;
private final ResourceHost resources;
private final ConsoleHost console;
2026-01-16 21:47:04 +01:00
2026-02-10 19:24:38 +01:00
/**
* Creates a HostServices container.
*
* @param ui ui host
* @param dom dom host
* @param events event host
* @param resources resource host
* @param console console host
*/
public Default(UiHost ui, DomHost dom, EventHost events, ResourceHost resources, ConsoleHost console) {
this.ui = ui;
this.dom = dom;
this.events = events;
this.resources = resources;
this.console = console;
}
2026-01-16 21:47:04 +01:00
2026-02-10 19:24:38 +01:00
@Override
public Optional<UiHost> ui() {
return Optional.ofNullable(ui);
2026-01-16 21:47:04 +01:00
}
2026-02-10 19:24:38 +01:00
@Override
public Optional<DomHost> dom() {
return Optional.ofNullable(dom);
2026-01-16 21:47:04 +01:00
}
2026-02-10 19:24:38 +01:00
@Override
public Optional<EventHost> events() {
return Optional.ofNullable(events);
2026-01-16 21:47:04 +01:00
}
2026-02-10 19:24:38 +01:00
@Override
public Optional<ResourceHost> resources() {
return Optional.ofNullable(resources);
2026-01-16 21:47:04 +01:00
}
2026-02-10 19:24:38 +01:00
@Override
public Optional<ConsoleHost> console() {
return Optional.ofNullable(console);
}
}
/**
* Stdout-based console host.
*/
final class StdoutConsole implements ConsoleHost {
private final String prefix;
2026-01-16 21:47:04 +01:00
/**
2026-02-10 19:24:38 +01:00
* Creates a new stdout console with a prefix.
2026-01-16 21:47:04 +01:00
*
2026-02-10 19:24:38 +01:00
* @param prefix prefix (may be empty)
2026-01-16 21:47:04 +01:00
*/
2026-02-10 19:24:38 +01:00
public StdoutConsole(String prefix) {
this.prefix = Objects.requireNonNull(prefix, "prefix");
}
@Override
public void info(String message) {
System.out.println(prefix + "[info] " + safe(message));
}
@Override
public void log(String message) {
System.out.println(prefix + "[log] " + safe(message));
}
@Override
public void warn(String message) {
System.out.println(prefix + "[warn] " + safe(message));
}
@Override
public void error(String message) {
System.err.println(prefix + "[error] " + safe(message));
}
@Override
public void exception(String message) {
System.err.println(prefix + "[exception] " + safe(message));
2026-01-16 21:47:04 +01:00
}
2026-02-10 19:24:38 +01:00
private static String safe(String s) {
return s == null ? "" : s;
2026-01-16 21:47:04 +01:00
}
}
2026-02-10 19:24:38 +01:00
}