feat: improving readability of buildRetweetAndReplyFields(..) in BasicIndexingConverter

- as this method already has a java doc, why not have the entire logic explained on it, instead of inside the method

- since the lambda parameters of the Predicate represent an id, giving them better names than 'x'
This commit is contained in:
pedroluiznogueira 2023-04-01 00:43:06 -03:00
parent ec83d01dca
commit c276641062
1 changed files with 62 additions and 58 deletions

View File

@ -500,66 +500,70 @@ public class BasicIndexingConverter {
/**
* Build the correct ThriftIndexingEvent's fields based on retweet and reply status.
*
* <pre>
*
* We have six combinations here. A tweet can be
* 1) a reply to another tweet (then it has both in-reply-to-user-id and
* in-reply-to-status-id set),
* 2) directed-at a user (then it only has in-reply-to-user-id set),
* 3) not a reply at all.
* Additionally, it may or may not be a retweet (if it is, then it has retweet-user-id and
* retweet-status-id set).
*
* We want to set some fields unconditionally, and some fields (reference-author-id and
* shared-status-id) depending on the reply/retweet combination.
*
* 1. Normal tweet (not a reply, not a retweet). None of the fields should be set.
*
* 2. Reply to a tweet (both in-reply-to-user-id and in-reply-to-status-id set).
* IN_REPLY_TO_USER_ID_FIELD should be set to in-reply-to-user-id
* SHARED_STATUS_ID_CSF should be set to in-reply-to-status-id
* IS_REPLY_FLAG should be set
*
* 3. Directed-at a user (only in-reply-to-user-id is set).
* IN_REPLY_TO_USER_ID_FIELD should be set to in-reply-to-user-id
* IS_REPLY_FLAG should be set
*
* 4. Retweet of a normal tweet (retweet-user-id and retweet-status-id are set).
* RETWEET_SOURCE_USER_ID_FIELD should be set to retweet-user-id
* SHARED_STATUS_ID_CSF should be set to retweet-status-id
* IS_RETWEET_FLAG should be set
*
* 5. Retweet of a reply (both in-reply-to-user-id and in-reply-to-status-id set,
* retweet-user-id and retweet-status-id are set).
* RETWEET_SOURCE_USER_ID_FIELD should be set to retweet-user-id
* SHARED_STATUS_ID_CSF should be set to retweet-status-id (retweet beats reply!)
* IS_RETWEET_FLAG should be set
* IN_REPLY_TO_USER_ID_FIELD should be set to in-reply-to-user-id
* IS_REPLY_FLAG should NOT be set
*
* 6. Retweet of a directed-at tweet (only in-reply-to-user-id is set,
* retweet-user-id and retweet-status-id are set).
* RETWEET_SOURCE_USER_ID_FIELD should be set to retweet-user-id
* SHARED_STATUS_ID_CSF should be set to retweet-status-id
* IS_RETWEET_FLAG should be set
* IN_REPLY_TO_USER_ID_FIELD should be set to in-reply-to-user-id
* IS_REPLY_FLAG should NOT be set
*
* In other words:
* SHARED_STATUS_ID_CSF logic: if this is a retweet SHARED_STATUS_ID_CSF should be set to
* retweet-status-id, otherwise if it's a reply to a tweet, it should be set to
* in-reply-to-status-id.
*
* </pre>
*/
public static void buildRetweetAndReplyFields(
long retweetUserIdVal,
long sharedStatusIdVal,
long inReplyToStatusIdVal,
long inReplyToUserIdVal,
boolean strict,
EarlybirdThriftDocumentBuilder builder) {
Optional<Long> retweetUserId = Optional.of(retweetUserIdVal).filter(x -> x > 0);
Optional<Long> sharedStatusId = Optional.of(sharedStatusIdVal).filter(x -> x > 0);
Optional<Long> inReplyToUserId = Optional.of(inReplyToUserIdVal).filter(x -> x > 0);
Optional<Long> inReplyToStatusId = Optional.of(inReplyToStatusIdVal).filter(x -> x > 0);
// We have six combinations here. A tweet can be
// 1) a reply to another tweet (then it has both in-reply-to-user-id and
// in-reply-to-status-id set),
// 2) directed-at a user (then it only has in-reply-to-user-id set),
// 3) not a reply at all.
// Additionally, it may or may not be a retweet (if it is, then it has retweet-user-id and
// retweet-status-id set).
//
// We want to set some fields unconditionally, and some fields (reference-author-id and
// shared-status-id) depending on the reply/retweet combination.
//
// 1. Normal tweet (not a reply, not a retweet). None of the fields should be set.
//
// 2. Reply to a tweet (both in-reply-to-user-id and in-reply-to-status-id set).
// IN_REPLY_TO_USER_ID_FIELD should be set to in-reply-to-user-id
// SHARED_STATUS_ID_CSF should be set to in-reply-to-status-id
// IS_REPLY_FLAG should be set
//
// 3. Directed-at a user (only in-reply-to-user-id is set).
// IN_REPLY_TO_USER_ID_FIELD should be set to in-reply-to-user-id
// IS_REPLY_FLAG should be set
//
// 4. Retweet of a normal tweet (retweet-user-id and retweet-status-id are set).
// RETWEET_SOURCE_USER_ID_FIELD should be set to retweet-user-id
// SHARED_STATUS_ID_CSF should be set to retweet-status-id
// IS_RETWEET_FLAG should be set
//
// 5. Retweet of a reply (both in-reply-to-user-id and in-reply-to-status-id set,
// retweet-user-id and retweet-status-id are set).
// RETWEET_SOURCE_USER_ID_FIELD should be set to retweet-user-id
// SHARED_STATUS_ID_CSF should be set to retweet-status-id (retweet beats reply!)
// IS_RETWEET_FLAG should be set
// IN_REPLY_TO_USER_ID_FIELD should be set to in-reply-to-user-id
// IS_REPLY_FLAG should NOT be set
//
// 6. Retweet of a directed-at tweet (only in-reply-to-user-id is set,
// retweet-user-id and retweet-status-id are set).
// RETWEET_SOURCE_USER_ID_FIELD should be set to retweet-user-id
// SHARED_STATUS_ID_CSF should be set to retweet-status-id
// IS_RETWEET_FLAG should be set
// IN_REPLY_TO_USER_ID_FIELD should be set to in-reply-to-user-id
// IS_REPLY_FLAG should NOT be set
//
// In other words:
// SHARED_STATUS_ID_CSF logic: if this is a retweet SHARED_STATUS_ID_CSF should be set to
// retweet-status-id, otherwise if it's a reply to a tweet, it should be set to
// in-reply-to-status-id.
long retweetUserIdVal,
long sharedStatusIdVal,
long inReplyToStatusIdVal,
long inReplyToUserIdVal,
boolean strict,
EarlybirdThriftDocumentBuilder builder) {
Optional<Long> retweetUserId = Optional.of(retweetUserIdVal).filter(id -> id > 0);
Optional<Long> sharedStatusId = Optional.of(sharedStatusIdVal).filter(id -> id > 0);
Optional<Long> inReplyToUserId = Optional.of(inReplyToUserIdVal).filter(id -> id > 0);
Optional<Long> inReplyToStatusId = Optional.of(inReplyToStatusIdVal).filter(id -> id > 0);
Preconditions.checkState(retweetUserId.isPresent() == sharedStatusId.isPresent());