📍Use lombok, reformat, and optimize the code (#1560)

* Use lombok, reformat, and optimize the code

* Fix merge conflicts and some sonar issues

Co-authored-by: va1m <va1m@email.com>
This commit is contained in:
va1m
2021-03-13 13:19:21 +01:00
committed by GitHub
parent 0e26a6adb5
commit 5cf2fe009b
681 changed files with 2472 additions and 4966 deletions

View File

@ -25,16 +25,14 @@ package com.iluwatar.leaderelection;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
/**
* Abstract class of all the instance implementation classes.
*/
@Slf4j
public abstract class AbstractInstance implements Instance, Runnable {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractInstance.class);
protected static final int HEARTBEAT_INTERVAL = 5000;
private static final String INSTANCE = "Instance ";

View File

@ -23,55 +23,23 @@
package com.iluwatar.leaderelection;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
/**
* Message used to transport data between instances.
*/
@Setter
@Getter
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
public class Message {
private MessageType type;
private String content;
public Message() {
}
public Message(MessageType type, String content) {
this.type = type;
this.content = content;
}
public MessageType getType() {
return type;
}
public void setType(MessageType type) {
this.type = type;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
var message = (Message) o;
return type == message.type && Objects.equals(content, message.content);
}
@Override
public int hashCode() {
return Objects.hash(type, content);
}
}

View File

@ -26,8 +26,7 @@ package com.iluwatar.leaderelection.bully;
import com.iluwatar.leaderelection.AbstractInstance;
import com.iluwatar.leaderelection.Message;
import com.iluwatar.leaderelection.MessageManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
/**
* Impelemetation with bully algorithm. Each instance should have a sequential id and is able to
@ -38,9 +37,8 @@ import org.slf4j.LoggerFactory;
* it will return an alive message (in this sample return true) and then send election message with
* its ID. If not, the original instance will send leader message to all the other instances.
*/
@Slf4j
public class BullyInstance extends AbstractInstance {
private static final Logger LOGGER = LoggerFactory.getLogger(BullyInstance.class);
private static final String INSTANCE = "Instance ";
/**

View File

@ -27,10 +27,8 @@ import com.iluwatar.leaderelection.AbstractInstance;
import com.iluwatar.leaderelection.Message;
import com.iluwatar.leaderelection.MessageManager;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
/**
* Implementation with token ring algorithm. The instances in the system are organized as a ring.
@ -43,9 +41,8 @@ import org.slf4j.LoggerFactory;
* smallest ID to be the new leader, and then send a leader message to other instances to inform the
* result.
*/
@Slf4j
public class RingInstance extends AbstractInstance {
private static final Logger LOGGER = LoggerFactory.getLogger(RingInstance.class);
private static final String INSTANCE = "Instance ";
/**