TweetTextLinkifier { static List<FormattedUrlEntity> mergeAndSortEntities(final List<FormattedUrlEntity> urls, final List<FormattedMediaEntity> media, final List<FormattedUrlEntity> hashtags, final List<FormattedUrlEntity> mentions, final List<FormattedUrlEntity> symbols) { final ArrayList<FormattedUrlEntity> combined = new ArrayList<>(urls); combined.addAll(media); combined.addAll(hashtags); combined.addAll(mentions); combined.addAll(symbols); Collections.sort(combined, (lhs, rhs) -> { if (lhs == null && rhs != null) return -1; if (lhs != null && rhs == null) return 1; if (lhs == null && rhs == null) return 0; if (lhs.start < rhs.start) return -1; if (lhs.start > rhs.start) return 1; return 0; }); return combined; } private  TweetTextLinkifier();   }
@Test public void testMergeAndSortEntities_emptyEntities() { final List<FormattedUrlEntity> urls = new ArrayList<>(); final List<FormattedMediaEntity> media = new ArrayList<>(); final List<FormattedUrlEntity> hashtags = new ArrayList<>(); final List<FormattedUrlEntity> mentions = new ArrayList<>(); final List<FormattedUrlEntity> symbols = new ArrayList<>(); assertEquals(urls, TweetTextLinkifier.mergeAndSortEntities(urls, media, hashtags, mentions, symbols)); }
@Test public void testMergeAndSortEntities_sortUrlsAndMediaAndHashtags() { final List<FormattedUrlEntity> urls = new ArrayList<>(); final UrlEntity urlEntity = TestFixtures.newUrlEntity(2, 5); final FormattedUrlEntity adjustedUrl = FormattedUrlEntity.createFormattedUrlEntity( urlEntity); urls.add(adjustedUrl); final List<FormattedMediaEntity> media = new ArrayList<>(); final MediaEntity photo = TestFixtures.newMediaEntity(1, 5, "photo"); final FormattedMediaEntity adjustedPhoto = new FormattedMediaEntity(photo); media.add(adjustedPhoto); final List<FormattedUrlEntity> hashtags = new ArrayList<>(); final HashtagEntity hashtag = TestFixtures.newHashtagEntity("TwitterForGood", 0, 13); final FormattedUrlEntity adjustedHashtag = FormattedUrlEntity.createFormattedUrlEntity(hashtag); hashtags.add(adjustedHashtag); final List<FormattedUrlEntity> mentions = new ArrayList<>(); final MentionEntity mention = TestFixtures.newMentionEntity("twitterdev", 0, 9); final FormattedUrlEntity adjustedMention = FormattedUrlEntity.createFormattedUrlEntity(mention); mentions.add(adjustedMention); final List<FormattedUrlEntity> symbols = new ArrayList<>(); final SymbolEntity symbol = TestFixtures.newSymbolEntity("TWTR", 0, 3); final FormattedUrlEntity adjustedSymbol = FormattedUrlEntity.createFormattedUrlEntity(symbol); symbols.add(adjustedSymbol); final List<? extends FormattedUrlEntity> combined = TweetTextLinkifier.mergeAndSortEntities(urls, media, hashtags, mentions, symbols); assertEquals(adjustedPhoto, combined.get(3)); assertEquals(adjustedUrl, combined.get(4)); assertEquals(adjustedHashtag, combined.get(0)); assertEquals(adjustedMention, combined.get(1)); assertEquals(adjustedSymbol, combined.get(2)); }