-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract out style methods to StyleWriter/Reader
- Loading branch information
1 parent
1a1a04b
commit 39f0a87
Showing
7 changed files
with
378 additions
and
57 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
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
74 changes: 74 additions & 0 deletions
74
api/src/main/java/net/kyori/adventure/text/format/MutableStyleWriter.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 net.kyori.adventure.text.format; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.Contract; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Writes style properties to a mutable object. Used to override some default methods from {@link net.kyori.adventure.text.format.StyleWriter} | ||
* with faster alternatives that only work for mutable objects. | ||
* | ||
* @param <T> The type implementing this interface e.g. {@link net.kyori.adventure.text.Component} | ||
* @see net.kyori.adventure.text.format.StyleWriter | ||
* @since 4.10.0 | ||
*/ | ||
@ApiStatus.NonExtendable | ||
public interface MutableStyleWriter<T extends MutableStyleWriter<?>> extends StyleWriter<T> { | ||
|
||
/** | ||
* Sets {@code decorations} to {@link TextDecoration.State#TRUE}. | ||
* | ||
* @param decorations the decorations | ||
* @return a mutable object ({@code T}) | ||
* @since 4.10.0 | ||
*/ | ||
@Override | ||
@Contract("_ -> this") | ||
@SuppressWarnings("unchecked") | ||
default @NotNull T decorate(final @NotNull TextDecoration@NotNull... decorations) { | ||
for (int i = 0, length = decorations.length; i < length; i++) { | ||
this.decorate(decorations[i]); | ||
} | ||
return (T) this; | ||
} | ||
|
||
/** | ||
* Sets decorations using the specified {@code decorations} map. | ||
* | ||
* <p>If a given decoration does not have a value explicitly set, the value of that particular decoration is not changed.</p> | ||
* | ||
* @param decorations a map containing text decorations and their respective state. | ||
* @return a mutable object ({@code T}) | ||
* @since 4.10.0 | ||
*/ | ||
@Override | ||
@Contract("_ -> this") | ||
@SuppressWarnings("unchecked") | ||
default @NotNull T decorations(final @NotNull Map<TextDecoration, TextDecoration.State> decorations) { | ||
for (final Map.Entry<TextDecoration, TextDecoration.State> entry : decorations.entrySet()) { | ||
this.decoration(entry.getKey(), entry.getValue()); | ||
} | ||
return (T) this; | ||
} | ||
|
||
/** | ||
* Sets the state of a set of decorations to {@code flag}. | ||
* | ||
* @param decorations the decorations | ||
* @param flag {@code true} if this mutable object should have the decorations, {@code false} if | ||
* this mutable object should not have the decorations | ||
* @return a mutable object ({@code T}) | ||
* @since 4.10.0 | ||
*/ | ||
@Override | ||
@Contract("_, _ -> this") | ||
@SuppressWarnings("unchecked") | ||
default @NotNull T decorations(final @NotNull Set<TextDecoration> decorations, final boolean flag) { | ||
final TextDecoration.State state = TextDecoration.State.byBoolean(flag); | ||
decorations.forEach(decoration -> this.decoration(decoration, state)); | ||
return (T) this; | ||
} | ||
} |
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
Oops, something went wrong.