<commit_before>package com.jvm_bloggers.core.social.twitter;

import com.jvm_bloggers.core.blogpost_redirect.LinkGenerator;
import com.jvm_bloggers.entities.newsletter_issue.NewsletterIssue;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Random;

import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static lombok.AccessLevel.PACKAGE;

@Component
@Slf4j
@RequiredArgsConstructor(access = PACKAGE)
class TweetContentGenerator {

    private static final int TWEET_MAX_LENGTH = 140;
    private static final String MESSAGE_TEMPLATE =
        "Nowy numer #%s już online - %s z postami m.in. %s, %s i %s #java #jvm";
    private static final String SHORT_MESSAGE_TEMPLATE =
        "Nowy numer #%s już online - %s z postami m.in. %s i %s #java #jvm";

    private final LinkGenerator linkGenerator;
    private final Random random = new Random();

    public String generateTweetContent(NewsletterIssue issue) {
        final List<String> personalTTs =
            issue.getBlogPosts().stream().map(b -> b.getBlog()).filter(b -> b.isPersonal())
                .map(b -> b.getTwitter()).collect(toList());
        final List<String> companyTTs =
            issue.getBlogPosts().stream().map(b -> b.getBlog()).filter(b -> !b.isPersonal())
                .map(b -> b.getTwitter()).collect(toList());

        final String personalHandle1 = personalTTs.remove(random.nextInt(personalTTs.size()));
        final String personalHandle2 = personalTTs.remove(random.nextInt(personalTTs.size()));
        final String companyHandle = companyTTs.remove(random.nextInt(companyTTs.size()));

        final String issueLink = linkGenerator.generateIssueLink(issue.getIssueNumber());
        final String tweetContent =
            format(
                MESSAGE_TEMPLATE, issue.getIssueNumber(), issueLink,
                personalHandle1, companyHandle, personalHandle2
            );

        if (tweetIsTooLong(tweetContent, issueLink.length())) {
            return format(
                SHORT_MESSAGE_TEMPLATE, issue.getIssueNumber(), issueLink,
                personalHandle1, companyHandle
            );
        } else {
            return tweetContent;
        }
        // check for length - link 23 chars, number 3 chars
    }

    private boolean tweetIsTooLong(String tweetContent, int originalIssuesLinkLength) {
        return (tweetContent.length() - originalIssuesLinkLength + 23) > TWEET_MAX_LENGTH;
    }

}
<commit_msg>Vavr, fluent API and shuffling<commit_after>package com.jvm_bloggers.core.social.twitter;

import com.jvm_bloggers.core.blogpost_redirect.LinkGenerator;
import com.jvm_bloggers.entities.blog.Blog;
import com.jvm_bloggers.entities.blog_post.BlogPost;
import com.jvm_bloggers.entities.newsletter_issue.NewsletterIssue;

import io.vavr.collection.List;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;

import static java.lang.String.format;
import static lombok.AccessLevel.PACKAGE;

@Component
@Slf4j
@RequiredArgsConstructor(access = PACKAGE)
class TweetContentGenerator {

    private static final int TWEET_MAX_LENGTH = 140;
    private static final String MESSAGE_TEMPLATE =
        "Nowy numer #%s już online - %s z postami m.in. %s, %s i %s #java #jvm";
    private static final String SHORT_MESSAGE_TEMPLATE =
        "Nowy numer #%s już online - %s z postami m.in. %s i %s #java #jvm";

    private final LinkGenerator linkGenerator;

    public String generateTweetContent(NewsletterIssue issue) {
        final List<String> personalTTs =
            List.ofAll(issue.getBlogPosts())
                .map(BlogPost::getBlog)
                .filter(Blog::isPersonal)
                .map(Blog::getTwitter)
                .shuffle()
                .take(2);

        final String companyTT =
            List.ofAll(issue.getBlogPosts())
                .map(BlogPost::getBlog)
                .filter(b -> !b.isPersonal())
                .map(Blog::getTwitter)
                .shuffle()
                .head();

        final String issueLink = linkGenerator.generateIssueLink(issue.getIssueNumber());
        final String tweetContent =
            format(
                MESSAGE_TEMPLATE, issue.getIssueNumber(), issueLink,
                personalTTs.get(0), companyTT, personalTTs.get(1)
            );

        if (tweetIsTooLong(tweetContent, issueLink.length())) {
            return format(
                SHORT_MESSAGE_TEMPLATE, issue.getIssueNumber(), issueLink,
                personalTTs.get(0), companyTT
            );
        } else {
            return tweetContent;
        }
    }

    private boolean tweetIsTooLong(String tweetContent, int originalIssuesLinkLength) {
        return (tweetContent.length() - originalIssuesLinkLength + 23) > TWEET_MAX_LENGTH;
    }

}

{'fraction_non_alphanumeric': 0.0992550835514395, 'fraction_numerical': 0.005637205556674049, 'mean_word_length': 2.830377794911334, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4047568', 'n_tokens_mistral': 1532, 'n_tokens_neox': 1430, 'n_words': 309}