Interface FormatManager
A FormatManager allows you to dynamically register new parsing/formatting rules
at runtime, keyed by a unique identifier. Once loaded, each format can be retrieved,
edited, or removed by its ID. This interface abstracts the lifecycle of
Format instances, which encapsulate:
- A regular expression pattern to locate format markers in strings.
- Logic to strip the markers from the text.
- Transformation logic to produce a value of type
Tfrom the marked text.
// Create and register a custom format that parses "invalid input: '<'uppercase:...>" blocks
FormatManager manager = new MyFormatManagerImplementation();
manager.load("upper", new Format<String>() {
public @Regex String getRegex() {
return "<uppercase:(.+?)>";
}
public String apply(Collection<? extends Player> players, String input) {
Matcher m = matcher(input);
if (m.find()) return m.group(1).toUpperCase();
return input;
}
});
// Apply the format
String result = manager.get("upper").apply("Hello invalid input: '<'uppercase:world>!");
// "Hello WORLD!"
- See Also:
-
Method Summary
Modifier and TypeMethodDescription<T> booleaneditFormat(String id, Format<T> newFormat) Replace the existing format under the specifiedidwith a new implementation.booleanChange the key under which a format is stored.<T,F extends Format<T>>
FRetrieve the format instance tied to the given identifier.<T> booleanRegister a new format under the specified identifier.booleanRemove the format associated with the given identifier.
-
Method Details
-
load
Register a new format under the specified identifier.If the given
idis already in use, the existing format is not replaced and this method returnsfalse. Otherwise, the format is stored and will be used for all subsequent retrievals viaget(String).- Type Parameters:
T- the output type produced by the format when applied- Parameters:
id- a case-sensitive key under which this format will be storedformat- theFormatimplementation encapsulating regex, removal, and transformation logic- Returns:
trueif the format was successfully registered;falseif the id was already taken
-
remove
Remove the format associated with the given identifier.All future calls to
get(String)with this id will returnnull.- Parameters:
id- the identifier of the format to remove- Returns:
trueif a format existed and was removed;falseif no format was mapped to the id
-
editFormat
Replace the existing format under the specifiedidwith a new implementation.Allows you to update parsing/transformation logic without changing the format key. Calling this with an
idthat does not exist returnsfalse.- Type Parameters:
T- the output type of the new format- Parameters:
id- the identifier of the existing formatnewFormat- the newFormatinstance to use- Returns:
trueif the format was successfully replaced;falseotherwise
-
editId
Change the key under which a format is stored.Useful for renaming a format without losing its existing logic. If
oldIdis not found ornewIdis already in use, no change is made.- Parameters:
oldId- the current identifier of the formatnewId- the desired new identifier- Returns:
trueif the key was successfully updated;falseotherwise
-
get
Retrieve the format instance tied to the given identifier.The returned
Formatcan be used to detect, strip, or transform strings according to its regex and logic. If no format exists foridentifier, this method returnsnull.
-