com.github.lontime.extredisson.configuration.ProducerOption Maven / Gradle / Ivy
The newest version!
package com.github.lontime.extredisson.configuration;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import com.github.lontime.base.commonj.utils.CollectionHelper;
import com.github.lontime.base.serial.model.Header;
import com.github.lontime.shaded.com.google.common.base.Preconditions;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
* ProducerOption.
* @author lontime
* @since 1.0
*/
@Getter
@Setter
@ToString
public class ProducerOption {
private static final ProducerOption DEFAULT_TOPIC = ProducerOption.topic("default");
private static final ProducerOption REF_TOPIC = ProducerOption.refTopic("ref");
private String name;
private String topic;
private Duration timeout = Duration.ofSeconds(10);
private List headers;
private List tags;
private Boolean enableReply = Boolean.FALSE;
private Boolean ref = Boolean.FALSE;
private Integer maxRecheck = 3;
private Integer page = 10;
public boolean shouldRef() {
return (ref || CollectionHelper.isNotEmpty(tags));
}
public static ProducerOption topic(String name) {
return topic(name, false);
}
public static ProducerOption topic(String name, boolean enableReply) {
if (name == null) {
return topic();
}
final ProducerOption option = new ProducerOption();
option.setName(name);
option.setTopic(name);
option.setEnableReply(enableReply);
return option;
}
public static ProducerOption topic(String name, Duration timeout) {
if (timeout == null || timeout.isZero()) {
return topic(name);
}
final ProducerOption option = topic(name, true);
option.setTimeout(timeout);
return option;
}
public static ProducerOption refTopic(String name) {
if (name == null) {
return refTopic();
}
return refTopic(name, false);
}
public static ProducerOption refTopic(String name, boolean enableReply) {
final ProducerOption option = new ProducerOption();
option.setName(name);
option.setTopic(name);
option.setRef(true);
option.setEnableReply(enableReply);
return option;
}
public static ProducerOption refTopic(String name, Duration timeout) {
if (timeout == null || timeout.isZero()) {
return refTopic(name);
}
final ProducerOption option = refTopic(name, true);
option.setTimeout(timeout);
return option;
}
public static ProducerOption topic() {
return DEFAULT_TOPIC;
}
public static ProducerOption refTopic() {
return REF_TOPIC;
}
public static Builder newBuilder() {
return new Builder();
}
public static class Builder {
private String topic;
private Duration timeout;
private List headers;
private List tags;
private boolean enableReply;
private boolean ref;
private Integer maxRecheck;
private Integer page;
public ProducerOption build() {
Preconditions.checkNotNull(topic, "topic is not null");
final ProducerOption option = ProducerOption.topic(topic);
option.setName(topic);
if (headers != null && !headers.isEmpty()) {
option.setHeaders(headers);
}
if (tags != null && !tags.isEmpty()) {
option.setTags(tags);
}
if (enableReply) {
option.setEnableReply(true);
}
if (timeout != null && !timeout.isZero()) {
option.setEnableReply(true);
option.setTimeout(timeout);
}
if (ref) {
option.setRef(true);
}
if (maxRecheck != null) {
option.setMaxRecheck(maxRecheck);
}
if (page != null) {
option.setPage(page);
}
return option;
}
// public Builder name(String nameIn) {
// this.name = nameIn;
// return this;
// }
public Builder topic(String topicIn) {
this.topic = topicIn;
return this;
}
public Builder timeout(long timeoutMs) {
this.timeout = Duration.ofMillis(timeoutMs);
return this;
}
public Builder timeout(Duration timeoutIn) {
this.timeout = timeoutIn;
return this;
}
public Builder header(Header header) {
if (this.headers == null) {
this.headers = new ArrayList<>(16);
}
this.headers.add(header);
return this;
}
public Builder headers(List headers) {
if (this.headers == null) {
this.headers = new ArrayList<>(16);
}
this.headers.addAll(headers);
return this;
}
public Builder tag(String tagIn) {
if (this.tags == null) {
this.tags = new ArrayList<>(16);
}
this.tags.add(tagIn);
return this;
}
public Builder tags(List tags) {
if (this.tags == null) {
this.tags = new ArrayList<>(16);
}
this.tags.addAll(tags);
return this;
}
public Builder enableReply() {
this.enableReply = true;
return this;
}
public Builder ref() {
this.ref = true;
return this;
}
public Builder maxRecheck(Integer maxRecheck) {
this.maxRecheck = maxRecheck;
return this;
}
public Builder page(Integer page) {
this.page = page;
return this;
}
}
}