Skip to content

Commit

Permalink
more adaptive QueryManager
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Jan 31, 2025
1 parent 57fc8fb commit b530b15
Show file tree
Hide file tree
Showing 17 changed files with 421 additions and 192 deletions.
11 changes: 11 additions & 0 deletions query/core/src/main/java/me/hsgamer/topper/query/core/Query.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package me.hsgamer.topper.query.core;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.BiFunction;

public interface Query<A> extends BiFunction<@Nullable A, @NotNull String, @NotNull QueryResult> {
@Override
@NotNull QueryResult apply(@Nullable A actor, @NotNull String query);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,63 +1,31 @@
package me.hsgamer.topper.query.core;

import me.hsgamer.topper.core.DataHolder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;

public abstract class QueryManager<K, V, H extends DataHolder<K, V>, A> {
private final Map<String, QueryAction<K, V, H, A>> actions = new HashMap<>();
public class QueryManager<A> {
private final List<BiFunction<@Nullable A, @NotNull String, @NotNull QueryResult>> queryList = new ArrayList<>();

protected abstract Optional<H> getHolder(String name);

protected boolean isSingleHolder() {
return false;
}

protected void registerAction(String name, QueryAction<K, V, H, A> action) {
actions.put(name, action);
}

protected void registerFunction(String name, BiFunction<@NotNull H, @NotNull String, @Nullable String> function) {
registerAction(name, (actor, holder, args) -> function.apply(holder, args));
public void addQuery(BiFunction<@Nullable A, @NotNull String, @NotNull QueryResult> queryFunction) {
queryList.add(queryFunction);
}

protected void registerActorFunction(String name, BiFunction<@NotNull A, @NotNull H, @Nullable String> function) {
registerAction(name, (actor, holder, args) -> {
if (actor == null) return null;
return function.apply(actor, holder);
});
public void removeQuery(BiFunction<@Nullable A, @NotNull String, @NotNull QueryResult> queryFunction) {
queryList.remove(queryFunction);
}

@Nullable
public String get(@Nullable A actor, String query) {
String holderName;
String actionName;
String args;
if (isSingleHolder()) {
String[] split = query.split(";", 2);
holderName = "";
actionName = split[0];
args = split.length > 1 ? split[1] : "";
} else {
String[] split = query.split(";", 3);
if (split.length < 2) return null;
holderName = split[0];
actionName = split[1];
args = split.length > 2 ? split[2] : "";
for (BiFunction<@Nullable A, @NotNull String, @NotNull QueryResult> queryFunction : queryList) {
QueryResult result = queryFunction.apply(actor, query);
if (result.handled) {
return result.result;
}
}

Optional<H> optionalHolder = getHolder(holderName);
if (!optionalHolder.isPresent()) return null;
H holder = optionalHolder.get();

QueryAction<K, V, H, A> action = actions.get(actionName);
if (action == null) return null;

return action.get(actor, holder, args);
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package me.hsgamer.topper.query.core;

public final class QueryResult {
public final boolean handled;
public final String result;

private QueryResult(boolean handled, String result) {
this.handled = handled;
this.result = result;
}

public static QueryResult handled(String result) {
return new QueryResult(true, result);
}

public static QueryResult notHandled() {
return new QueryResult(false, null);
}
}
26 changes: 26 additions & 0 deletions query/holder/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>me.hsgamer</groupId>
<artifactId>topper-query</artifactId>
<version>3.5.0-SNAPSHOT</version>
</parent>

<artifactId>topper-query-holder</artifactId>

<dependencies>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>topper-query-simple</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>topper-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package me.hsgamer.topper.query.holder;

import me.hsgamer.topper.core.DataEntry;
import me.hsgamer.topper.core.DataHolder;
import me.hsgamer.topper.query.simple.SimpleQuery;
import me.hsgamer.topper.query.simple.SimpleQueryContext;
import me.hsgamer.topper.query.simple.SimpleQueryDisplay;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

public abstract class HolderQuery<K, V, H extends DataHolder<K, V>, A> extends SimpleQuery<A, HolderQuery.Context<K, V, H>> {
protected HolderQuery() {
registerActorAction("key", (actor, context) -> {
K key = getKey(actor, context);
return getDisplay(context.holder).getDisplayKey(key);
});
registerActorAction("name", (actor, context) -> {
K key = getKey(actor, context);
return getDisplay(context.holder).getDisplayName(key);
});
registerActorAction("value", (actor, context) -> {
K key = getKey(actor, context);
if (key == null) return null;
V value = context.holder.getEntry(key).map(DataEntry::getValue).orElse(null);
return getDisplay(context.holder).getDisplayValue(value, context.parent.args);
});
registerActorAction("value_raw", (actor, context) -> {
K key = getKey(actor, context);
if (key == null) return null;
V value = context.holder.getEntry(key).map(DataEntry::getValue).orElse(null);
return getDisplay(context.holder).getDisplayValue(value, "raw");
});
}

protected abstract Optional<H> getHolder(@NotNull String name);

@NotNull
protected abstract SimpleQueryDisplay<K, V> getDisplay(@NotNull H holder);

protected boolean isSingleHolder() {
return false;
}

@Nullable
protected abstract K getKey(@NotNull A actor, @NotNull Context<K, V, H> context);

@Override
protected Optional<Context<K, V, H>> getContext(@NotNull String query) {
return SimpleQueryContext.fromQuery(query, isSingleHolder())
.map(context -> {
Optional<H> optionalHolder = getHolder(context.name);
if (!optionalHolder.isPresent()) return null;
H holder = optionalHolder.get();
return new Context<>(holder, context);
});
}

public static final class Context<K, V, H extends DataHolder<K, V>> implements SimpleQuery.Context {
public final @NotNull H holder;
public final @NotNull SimpleQueryContext parent;

Context(@NotNull H holder, @NotNull SimpleQueryContext parent) {
this.holder = holder;
this.parent = parent;
}

@Override
public @NotNull String getActionName() {
return parent.getActionName();
}
}
}
10 changes: 2 additions & 8 deletions query/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@
<packaging>pom</packaging>
<modules>
<module>core</module>
<module>simple</module>
<module>snapshot</module>
<module>holder</module>
</modules>

<dependencies>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>topper-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
21 changes: 21 additions & 0 deletions query/simple/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>me.hsgamer</groupId>
<artifactId>topper-query</artifactId>
<version>3.5.0-SNAPSHOT</version>
</parent>

<artifactId>topper-query-simple</artifactId>

<dependencies>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>topper-query-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package me.hsgamer.topper.query.simple;

import me.hsgamer.topper.query.core.Query;
import me.hsgamer.topper.query.core.QueryResult;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;

public abstract class SimpleQuery<A, C extends SimpleQuery.Context> implements Query<A> {
private final Map<String, BiFunction<@Nullable A, @NotNull C, @Nullable String>> actions = new HashMap<>();

protected abstract Optional<C> getContext(@NotNull String query);

protected void registerAction(String name, BiFunction<@Nullable A, @NotNull C, @Nullable String> action) {
actions.put(name, action);
}

protected void registerActorAction(String name, BiFunction<@NotNull A, @NotNull C, @Nullable String> function) {
registerAction(name, (actor, context) -> {
if (actor == null) return null;
return function.apply(actor, context);
});
}

@Override
public @NotNull QueryResult apply(@Nullable A actor, @NotNull String query) {
Optional<C> optionalContext = getContext(query);
if (!optionalContext.isPresent()) return QueryResult.notHandled();
C context = optionalContext.get();

String actionName = context.getActionName();
BiFunction<@Nullable A, @NotNull C, @Nullable String> action = actions.get(actionName);
if (action == null) return QueryResult.notHandled();

return QueryResult.handled(action.apply(actor, context));
}

public interface Context {
@NotNull String getActionName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package me.hsgamer.topper.query.simple;

import org.jetbrains.annotations.NotNull;

import java.util.Optional;

public final class SimpleQueryContext implements SimpleQuery.Context {
public final @NotNull String name;
public final @NotNull String args;
private final @NotNull String actionName;

public SimpleQueryContext(@NotNull String name, @NotNull String args, @NotNull String actionName) {
this.name = name;
this.args = args;
this.actionName = actionName;
}

public static Optional<SimpleQueryContext> fromQuery(@NotNull String query, boolean singleName) {
String name;
String actionName;
String args;
if (singleName) {
String[] split = query.split(";", 2);
name = "";
actionName = split[0];
args = split.length > 1 ? split[1] : "";
} else {
String[] split = query.split(";", 3);
if (split.length < 2) return Optional.empty();
name = split[0];
actionName = split[1];
args = split.length > 2 ? split[2] : "";
}
return Optional.of(new SimpleQueryContext(name, args, actionName));
}

@Override
public @NotNull String getActionName() {
return actionName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.hsgamer.topper.query.simple;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public interface SimpleQueryDisplay<K, V> {
@NotNull
String getDisplayName(@Nullable K key);

@NotNull
String getDisplayValue(@Nullable V value, @NotNull String args);

@NotNull
String getDisplayKey(@Nullable K key);
}
2 changes: 1 addition & 1 deletion query/snapshot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<dependencies>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>topper-query-core</artifactId>
<artifactId>topper-query-simple</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
Expand Down
Loading

0 comments on commit b530b15

Please sign in to comment.