Skip to content

Commit

Permalink
add action module
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Mar 9, 2024
1 parent 6de5d51 commit f84bed5
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 0 deletions.
26 changes: 26 additions & 0 deletions action/action-builder/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>hscore-action</artifactId>
<version>4.4.0-SNAPSHOT</version>
</parent>

<artifactId>hscore-action-builder</artifactId>

<dependencies>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>hscore-action-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>hscore-builder</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package me.hsgamer.hscore.action.builder;

import me.hsgamer.hscore.action.common.Action;
import me.hsgamer.hscore.builder.MassBuilder;
import me.hsgamer.hscore.common.CollectionUtils;

import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* The builder for the {@link Action}
*/
public final class ActionBuilder extends MassBuilder<ActionInput, Action> {
/**
* The instance of the action builder
*/
public static final ActionBuilder INSTANCE = new ActionBuilder();
/**
* The pattern for the action.
* The format is: {@code <type>(<option>): <value>}. Note that the {@code <option>} and {@code <value>} are optional.
* Also, the allowed characters of the {@code <type>} are alphanumeric, {@code _}, {@code -} and {@code $}.
* To get the {@code <type>}, {@code <option>} and {@code <value>}, use {@link Matcher#group(int)} with the index 1, 3 and 5 respectively.
*/
public static final Pattern ACTION_PATTERN = Pattern.compile("\\s*([\\w\\-$]+)\\s*(\\((.*)\\))?\\s*(:\\s*(.*)\\s*)?");

private ActionBuilder() {
// EMPTY
}

/**
* Register a new action creator
*
* @param creator the creator
* @param type the type
*/
public void register(Function<ActionInput, Action> creator, String... type) {
register(input -> {
String action = input.type;
for (String s : type) {
if (action.equalsIgnoreCase(s)) {
return Optional.of(creator.apply(input));
}
}
return Optional.empty();
});
}

/**
* Build a list of actions
*
* @param list the list of strings
* @param defaultActionFunction the default action function
*
* @return the list of actions
*/
public List<Action> build(List<String> list, Function<ActionInput, Action> defaultActionFunction) {
return list
.stream()
.flatMap(string -> {
ActionInput input;
Matcher matcher = ACTION_PATTERN.matcher(string);
if (matcher.matches()) {
String type = matcher.group(1);
String option = Optional.ofNullable(matcher.group(3)).orElse("");
String value = Optional.ofNullable(matcher.group(5)).orElse("");
input = new ActionInput(type, value, option);
} else {
input = new ActionInput("", string, "");
}
return Stream.of(build(input).orElseGet(() -> defaultActionFunction.apply(input)));
})
.collect(Collectors.toList());
}

/**
* Build a list of actions
*
* @param object the object
* @param defaultActionFunction the default action function
*
* @return the list of actions
*/
public List<Action> build(Object object, Function<ActionInput, Action> defaultActionFunction) {
return build(CollectionUtils.createStringListFromObject(object, true), defaultActionFunction);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package me.hsgamer.hscore.action.builder;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* The input for the {@link ActionBuilder}
*/
public class ActionInput {
public final String type;
public final String value;
public final String option;

/**
* Create a new input
*
* @param type the type of the action
* @param value the value of the action
* @param option the option of the action
*/
public ActionInput(String type, String value, String option) {
this.type = type;
this.value = value;
this.option = option;
}

/**
* Get the option as a stream
*
* @param separator the separator
*
* @return the list
*/
public Stream<String> getOptionStream(String separator) {
return option.isEmpty() ? Stream.empty() : Arrays.stream(option.split(separator)).map(String::trim);
}

/**
* Get the option as a stream.
* The format is {@code value,value}
*
* @return the list
*
* @see #getOptionStream(String)
*/
public Stream<String> getOptionStream() {
return getOptionStream(",");
}

/**
* Get the option as a list
*
* @param separator the separator
*
* @return the list
*
* @see #getOptionStream(String)
*/
public List<String> getOptionAsList(String separator) {
return getOptionStream(separator).collect(Collectors.toList());
}

/**
* Get the option as a list.
* The format is {@code value,value}
*
* @return the list
*
* @see #getOptionStream()
*/
public List<String> getOptionAsList() {
return getOptionStream().collect(Collectors.toList());
}

/**
* Get the option as a map.
* The format is {@code key=value,key=value}
*
* @return the map
*/
public Map<String, String> getOptionAsMap() {
return getOptionStream()
.map(s -> s.split("="))
.collect(Collectors.toMap(strings -> strings[0].trim(), strings -> strings.length > 1 ? strings[1].trim() : ""));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Contains the builder for {@link me.hsgamer.hscore.action.common.Action}
*/
package me.hsgamer.hscore.action.builder;
31 changes: 31 additions & 0 deletions action/action-common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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>hscore-action</artifactId>
<version>4.4.0-SNAPSHOT</version>
</parent>

<artifactId>hscore-action-common</artifactId>

<dependencies>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>hscore-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>hscore-task</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>me.hsgamer</groupId>
<artifactId>hscore-builder</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package me.hsgamer.hscore.action.common;

import me.hsgamer.hscore.common.StringReplacer;
import me.hsgamer.hscore.task.element.TaskProcess;

import java.util.UUID;

/**
* The action
*/
public interface Action {
/**
* Apply the action
*
* @param uuid the unique id
* @param process the task process
* @param stringReplacer the string replacer
*/
void apply(UUID uuid, TaskProcess process, StringReplacer stringReplacer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* Contains common classes for actions
*/
package me.hsgamer.hscore.action.common;
18 changes: 18 additions & 0 deletions action/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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>hscore</artifactId>
<version>4.4.0-SNAPSHOT</version>
</parent>
<packaging>pom</packaging>
<artifactId>hscore-action</artifactId>

<modules>
<module>action-common</module>
<module>action-builder</module>
</modules>
</project>
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<module>bungeecord</module>
<module>minestom</module>
<module>license</module>
<module>action</module>
</modules>

<properties>
Expand Down
1 change: 1 addition & 0 deletions updateVersion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ minecraft
expansion
logger
license
action
"

BASE_DIR="$(pwd)"
Expand Down

0 comments on commit f84bed5

Please sign in to comment.