-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
421 additions
and
192 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
query/core/src/main/java/me/hsgamer/topper/query/core/Query.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
10 changes: 0 additions & 10 deletions
10
query/core/src/main/java/me/hsgamer/topper/query/core/QueryAction.java
This file was deleted.
Oops, something went wrong.
60 changes: 14 additions & 46 deletions
60
query/core/src/main/java/me/hsgamer/topper/query/core/QueryManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
query/core/src/main/java/me/hsgamer/topper/query/core/QueryResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
74 changes: 74 additions & 0 deletions
74
query/holder/src/main/java/me/hsgamer/topper/query/holder/HolderQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
45 changes: 45 additions & 0 deletions
45
query/simple/src/main/java/me/hsgamer/topper/query/simple/SimpleQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
query/simple/src/main/java/me/hsgamer/topper/query/simple/SimpleQueryContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
query/simple/src/main/java/me/hsgamer/topper/query/simple/SimpleQueryDisplay.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.