Package com.atlassian.bitbucket.user
Interface UserMentionScanner
public interface UserMentionScanner
Handles rendering and parsing user mention strings in texts.
- Since:
- 5.16
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceRepresents the location and content of a user mention in the text. -
Method Summary
Modifier and TypeMethodDescriptiongetMentionedUsers(CharSequence text, int maxMentions) Returns all mentioneduser namesin the text.voidprocessMentions(CharSequence text, Consumer<UserMentionScanner.UserMention> callback) Callscallbackfor each user mention found intext.renderMention(String username) Renders the full user mention for auser name.replaceMentions(CharSequence text, Function<UserMentionScanner.UserMention, Optional<String>> replacementFunction) Replaces all user mentions with the string returned from the passedreplacementFunction.
-
Method Details
-
getMentionedUsers
Returns all mentioneduser namesin the text.Note that the case of the user name returned matches the case used in the text and might not be identical to the case of the
ApplicationUser's name.- Parameters:
text- text to search for user mentionsmaxMentions- maximum size of the returned set- Returns:
- set of
user namesmentioned
-
processMentions
void processMentions(@Nonnull CharSequence text, @Nonnull Consumer<UserMentionScanner.UserMention> callback) Callscallbackfor each user mention found intext.- Parameters:
text- text to search for user mentionscallback- callback that gets called for every user mention found
-
renderMention
Renders the full user mention for auser name.- Parameters:
username-user nameto render- Returns:
- rendered user mention
-
replaceMentions
@Nonnull Optional<CharSequence> replaceMentions(@Nonnull CharSequence text, @Nonnull Function<UserMentionScanner.UserMention, Optional<String>> replacementFunction) Replaces all user mentions with the string returned from the passedreplacementFunction.Return types for both the
replacementFunctionas well as this method areOptionalso the calling code can perform more efficient operations when no replacements have taken place.Example usage:
String replacedText = userMentionScanner.replaceMentions(text, mention -> Optional.of(userMentionScanner.renderMention(mention.getUsername().replace("_", "-")))) .orElse(text);- Parameters:
text- text to search and replace user mentions inreplacementFunction- function that optionally returns a replacement for the full mention of the user- Returns:
- the new text or
Optional.empty()if no replacement took place
-