public interface

UserMentionScanner

com.atlassian.bitbucket.user.UserMentionScanner

Class Overview

Handles rendering and parsing user mention strings in texts.

Summary

Nested Classes
interface UserMentionScanner.UserMention Represents the location and content of a user mention in the text. 
Public Methods
@Nonnull Set<String> getMentionedUsers(CharSequence text, int maxMentions)
Returns all mentioned user names in the text.
void processMentions(CharSequence text, Consumer<UserMentionScanner.UserMention> callback)
Calls callback for each user mention found in text.
@Nonnull String renderMention(String username)
Renders the full user mention for a user name.
@Nonnull Optional<CharSequence> replaceMentions(CharSequence text, Function<UserMentionScanner.UserMention, Optional<String>> replacementFunction)
Replaces all user mentions with the string returned from the passed replacementFunction.

Public Methods

@Nonnull public Set<String> getMentionedUsers (CharSequence text, int maxMentions)

Returns all mentioned user names in 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 mentions
maxMentions maximum size of the returned set
Returns

public void processMentions (CharSequence text, Consumer<UserMentionScanner.UserMention> callback)

Calls callback for each user mention found in text.

Parameters
text text to search for user mentions
callback callback that gets called for every user mention found

@Nonnull public String renderMention (String username)

Renders the full user mention for a user name.

Parameters
username user name to render
Returns
  • rendered user mention

@Nonnull public Optional<CharSequence> replaceMentions (CharSequence text, Function<UserMentionScanner.UserMention, Optional<String>> replacementFunction)

Replaces all user mentions with the string returned from the passed replacementFunction.

Return types for both the replacementFunction as well as this method are Optional so 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 in
replacementFunction function that optionally returns a replacement for the full mention of the user
Returns
  • the new text or empty() if no replacement took place