package com.mortals.xhx.queue;

import lombok.Builder;
import lombok.Data;

import java.util.Objects;
import java.util.Optional;

@Data
public class TopicPartitionInfo {
    /**
     * topic名称
     */
    private String topic;
    /**
     * 分区，kafka存在
     */
    private Integer partition;
    /**
     * 交换机名称，rabbmitmq存在
     */
    private String exchangeName;
    /**
     * 带分区的topic
     */
    private String fullTopicName;


    @Builder
    public TopicPartitionInfo(String topic, Integer partition, String exchangeName) {
        this.topic = topic;
        this.partition = partition;
        this.exchangeName = exchangeName;
        String tmp = topic;
        if (partition != null) {
            tmp += "." + partition;
        }
        this.fullTopicName = tmp;
    }

    public TopicPartitionInfo newByTopic(String topic) {
        return new TopicPartitionInfo(topic, this.partition, "");
    }

    public String getTopic() {
        return topic;
    }


    public Optional<Integer> getPartition() {
        return Optional.ofNullable(partition);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TopicPartitionInfo that = (TopicPartitionInfo) o;
        return topic.equals(that.topic) &&
                Objects.equals(partition, that.partition) &&
                fullTopicName.equals(that.fullTopicName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(fullTopicName);
    }
}
