@SuppressWarnings("checkstyle:cyclomaticComplexity")
    public LocalReplicaChanges localChanges(int brokerId) {
        Set<TopicPartition> deletes = new HashSet<>();
        Map<TopicPartition, LocalReplicaChanges.PartitionInfo> electedLeaders = new HashMap<>();
        Map<TopicPartition, LocalReplicaChanges.PartitionInfo> leaders = new HashMap<>();
        Map<TopicPartition, LocalReplicaChanges.PartitionInfo> followers = new HashMap<>();
        Map<String, Uuid> topicIds = new HashMap<>();
        Map<TopicIdPartition, Uuid> directoryIds = new HashMap<>();

        for (Entry<Integer, PartitionRegistration> entry : partitionChanges.entrySet()) {
            if (!Replicas.contains(entry.getValue().replicas, brokerId)) {
                PartitionRegistration prevPartition = image.partitions().get(entry.getKey());
                if (prevPartition != null && Replicas.contains(prevPartition.replicas, brokerId)) {
                    deletes.add(new TopicPartition(name(), entry.getKey()));
                }
            } else if (entry.getValue().leader == brokerId) {
                PartitionRegistration prevPartition = image.partitions().get(entry.getKey());
                if (prevPartition == null || prevPartition.partitionEpoch != entry.getValue().partitionEpoch) {
                    TopicPartition tp = new TopicPartition(name(), entry.getKey());
                    LocalReplicaChanges.PartitionInfo partitionInfo = new LocalReplicaChanges.PartitionInfo(id(), entry.getValue());
                    leaders.put(tp, partitionInfo);
                    if (prevPartition == null || prevPartition.leaderEpoch != entry.getValue().leaderEpoch) {
                        electedLeaders.put(tp, partitionInfo);
                    }
                    topicIds.putIfAbsent(name(), id());
                }
            } else {
                PartitionRegistration prevPartition = image.partitions().get(entry.getKey());
                if (prevPartition == null || prevPartition.partitionEpoch != entry.getValue().partitionEpoch) {
                    followers.put(
                        new TopicPartition(name(), entry.getKey()),
                        new LocalReplicaChanges.PartitionInfo(id(), entry.getValue())
                    );
                    topicIds.putIfAbsent(name(), id());
                }
            }

            try {
                PartitionRegistration prevPartition = image.partitions().get(entry.getKey());
                if (
                        prevPartition == null ||
                        prevPartition.directories == null ||
                        prevPartition.directory(brokerId) != entry.getValue().directory(brokerId)
                ) {
                    directoryIds.put(
                        new TopicIdPartition(id(), new TopicPartition(name(), entry.getKey())),
                        entry.getValue().directory(brokerId)
                    );
                }
            } catch (IllegalArgumentException e) {
                // Do nothing if broker isn't part of the replica set.
            }
        }

        return new LocalReplicaChanges(deletes, electedLeaders, leaders, followers, topicIds, directoryIds);
    }
Find the partitions that have change based on the replica given.
<p>
The changes identified are:
<ul>
  <li>deletes: partitions for which the broker is not a replica anymore</li>
  <li>electedLeaders: partitions for which the broker is now a leader (leader epoch bump on the leader)</li>
  <li>leaders: partitions for which the isr or replicas change if the broker is a leader (partition epoch bump on the leader)</li>
  <li>followers: partitions for which the broker is now a follower or follower with isr or replica updates (partition epoch bump on follower)</li>
  <li>topicIds: a map of topic names to topic IDs in leaders and followers changes</li>
  <li>directoryIds: partitions for which directory id changes or newly added to the broker</li>
</ul>
<p>
Leader epoch bumps are a strict subset of all partition epoch bumps, so all partitions in electedLeaders will be in leaders.

@param brokerId the broker id
@return the LocalReplicaChanges that cover changes in the broker
metadata/src/main/java/org/apache/kafka/image/TopicDelta.java