-
-
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
10 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
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>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> |
91 changes: 91 additions & 0 deletions
91
action/action-builder/src/main/java/me/hsgamer/hscore/action/builder/ActionBuilder.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,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); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
action/action-builder/src/main/java/me/hsgamer/hscore/action/builder/ActionInput.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,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() : "")); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
action/action-builder/src/main/java/me/hsgamer/hscore/action/builder/package-info.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,4 @@ | ||
/** | ||
* Contains the builder for {@link me.hsgamer.hscore.action.common.Action} | ||
*/ | ||
package me.hsgamer.hscore.action.builder; |
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,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> |
20 changes: 20 additions & 0 deletions
20
action/action-common/src/main/java/me/hsgamer/hscore/action/common/Action.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,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); | ||
} |
4 changes: 4 additions & 0 deletions
4
action/action-common/src/main/java/me/hsgamer/hscore/action/common/package-info.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,4 @@ | ||
/** | ||
* Contains common classes for actions | ||
*/ | ||
package me.hsgamer.hscore.action.common; |
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,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> |
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ minecraft | |
expansion | ||
logger | ||
license | ||
action | ||
" | ||
|
||
BASE_DIR="$(pwd)" | ||
|