spyglass/src/main/java/com/linkedin/android/spyglass/ui/MentionsEditText.java
private void ensureMentionSpanIntegrity(Editable text) {
        if (text == null) {
            return;
        }

        MentionSpan[] spans = text.getSpans(0, text.length(), MentionSpan.class);
        boolean spanAltered = false;
        for (MentionSpan span : spans) {
            int start = text.getSpanStart(span);
            int end = text.getSpanEnd(span);
            CharSequence spanText = text.subSequence(start, end).toString();
            Mentionable.MentionDisplayMode displayMode = span.getDisplayMode();

            switch (displayMode) {

                case PARTIAL:
                case FULL:
                    String name = span.getDisplayString();
                    if (!name.equals(spanText) && start >= 0 && start < end && end <= text.length()) {
                        // Mention display name does not match what is being shown,
                        // replace text in span with proper display name
                        int cursor = getSelectionStart();
                        int diff = cursor - end;
                        text.removeSpan(span);
                        text.replace(start, end, name);
                        if (diff > 0 && start + end + diff < text.length()) {
                            text.replace(start + end, start + end + diff, "");
                        }
                        if (name.length() > 0) {
                            text.setSpan(span, start, start + name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        }
                        // Notify for partially deleted mentions.
                        if (mMentionWatchers.size() > 0 && displayMode == Mentionable.MentionDisplayMode.PARTIAL) {
                            notifyMentionPartiallyDeletedWatchers(span.getMention(), name, start, end);
                        }
                        spanAltered = true;
                    }
                    break;

                case NONE:
                default:
                    // Mention with DisplayMode == NONE should be deleted from the text
                    boolean hasListeners = mMentionWatchers.size() > 0;
                    final String textBeforeDelete = hasListeners ? text.toString() : null;
                    text.delete(start, end);
                    setSelection(start);
                    if (hasListeners) {
                        notifyMentionDeletedWatchers(span.getMention(), textBeforeDelete, start, end);
                    }
                    spanAltered = true;
                    break;
            }
        }

        // Reset input method if spans have been changed (updates suggestions)
        if (spanAltered) {
            restartInput();
        }
    }
private void ensureMentionSpanIntegrity(Editable text) {
        if (text == null) {
            return;
        }

        MentionSpan[] spans = text.getSpans(0, text.length(), MentionSpan.class);
        boolean spanAltered = false;
        for (MentionSpan span : spans) {
            int start = text.getSpanStart(span);
            int end = text.getSpanEnd(span);
            CharSequence spanText = text.subSequence(start, end).toString();
            Mentionable.MentionDisplayMode displayMode = span.getDisplayMode();

            switch (displayMode) {

                case PARTIAL:
                case FULL:
                    String name = span.getDisplayString();
                    if (!name.equals(spanText) && start >= 0 && start < end && end <= text.length()) {
                        // Mention display name does not match what is being shown,
                        // replace text in span with proper display name
                        int cursor = getSelectionStart();
                        int diff = cursor - end;
                        text.removeSpan(span);
                        text.replace(start, end, name);
                        if (diff > 0 && start + end + diff < text.length()) {
                            text.replace(start + end, start + end + diff, "");
                        }
                        if (name.length() > 0) {
                            text.setSpan(span, start, start + name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                        }
                        // Notify for partially deleted mentions.
                        if (mMentionWatchers.size() > 0 && displayMode == Mentionable.MentionDisplayMode.PARTIAL) {
                            notifyMentionPartiallyDeletedWatchers(span.getMention(), name, start, end);
                        }
                        spanAltered = true;
                    }
                    break;

                case NONE:
                default:
                    // Mention with DisplayMode == NONE should be deleted from the text
                    boolean hasListeners = mMentionWatchers.size() > 0;
                    final String textBeforeDelete = hasListeners ? text.toString() : null;
                    text.delete(start, end);
                    setSelection(start);
                    if (hasListeners) {
                        notifyMentionDeletedWatchers(span.getMention(), textBeforeDelete, start, end);
                    }
                    spanAltered = true;
                    break;
            }
        }

        // Reset input method if spans have been changed (updates suggestions)
        if (spanAltered) {
            restartInput();
        }
    }
Ensures that the text within each {@link MentionSpan} in the {@link Editable} correctly
matches what it should be outputting. If not, replace it with the correct value.

@param text the {@link Editable} to examine