📍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

@ -174,7 +174,7 @@ public class Commander {
if (!l.isEmpty()) {
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
LOG.debug("Order " + order.id + ": Error in connecting to payment service,"
+ " trying again..");
+ " trying again..");
} else {
LOG.debug("Order " + order.id + ": Error in creating payment request..");
}
@ -195,8 +195,8 @@ public class Commander {
if (PaymentDetailsErrorException.class.isAssignableFrom(err.getClass())) {
if (!finalSiteMsgShown) {
LOG.info("There was an error in payment. Your account/card details "
+ "may have been incorrect. "
+ "Meanwhile, your order has been converted to COD and will be shipped.");
+ "may have been incorrect. "
+ "Meanwhile, your order has been converted to COD and will be shipped.");
finalSiteMsgShown = true;
}
LOG.error("Order " + order.id + ": Payment details incorrect, failed..");
@ -206,14 +206,14 @@ public class Commander {
if (o.messageSent.equals(MessageSent.NONE_SENT)) {
if (!finalSiteMsgShown) {
LOG.info("There was an error in payment. We are on it, and will get back to you "
+ "asap. Don't worry, your order has been placed and will be shipped.");
+ "asap. Don't worry, your order has been placed and will be shipped.");
finalSiteMsgShown = true;
}
LOG.warn("Order " + order.id + ": Payment error, going to queue..");
sendPaymentPossibleErrorMsg(o);
}
if (o.paid.equals(PaymentStatus.TRYING) && System
.currentTimeMillis() - o.createdTime < paymentTime) {
.currentTimeMillis() - o.createdTime < paymentTime) {
var qt = new QueueTask(o, TaskType.PAYMENT, -1);
updateQueue(qt);
}
@ -475,7 +475,7 @@ public class Commander {
}
private void handlePaymentPossibleErrorMsgRetryOperation(Order order, List<Exception> l)
throws Exception {
throws Exception {
if (!l.isEmpty()) {
if (DatabaseUnavailableException.class.isAssignableFrom(l.get(0).getClass())) {
LOG.debug("Order " + order.id + ": Error in connecting to messaging service "
@ -539,10 +539,10 @@ public class Commander {
var qt = queue.peek(); //this should probably be cloned here
//this is why we have retry for doTasksInQueue
LOG.trace("Order " + qt.order.id + ": Started doing task of type " + qt.getType());
if (qt.firstAttemptTime == -1) {
qt.firstAttemptTime = System.currentTimeMillis();
if (qt.getFirstAttemptTime() == -1) {
qt.setFirstAttemptTime(System.currentTimeMillis());
}
if (System.currentTimeMillis() - qt.firstAttemptTime >= queueTaskTime) {
if (System.currentTimeMillis() - qt.getFirstAttemptTime() >= queueTaskTime) {
tryDequeue();
LOG.trace("Order " + qt.order.id + ": This queue task of type " + qt.getType()
+ " does not need to be done anymore (timeout), dequeue..");

View File

@ -24,7 +24,8 @@
package com.iluwatar.commander;
import java.security.SecureRandom;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.Map;
/**
* Order class holds details of the order.
@ -33,11 +34,16 @@ import java.util.Hashtable;
public class Order { //can store all transactions ids also
enum PaymentStatus {
NOT_DONE, TRYING, DONE
NOT_DONE,
TRYING,
DONE
}
enum MessageSent {
NONE_SENT, PAYMENT_FAIL, PAYMENT_TRYING, PAYMENT_SUCCESSFUL
NONE_SENT,
PAYMENT_FAIL,
PAYMENT_TRYING,
PAYMENT_SUCCESSFUL
}
final User user;
@ -47,7 +53,7 @@ public class Order { //can store all transactions ids also
final long createdTime;
private static final SecureRandom RANDOM = new SecureRandom();
private static final String ALL_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
private static final Hashtable<String, Boolean> USED_IDS = new Hashtable<String, Boolean>();
private static final Map<String, Boolean> USED_IDS = new HashMap<>();
PaymentStatus paid;
MessageSent messageSent; //to avoid sending error msg on page and text more than once
boolean addedToEmployeeHandle; //to avoid creating more to enqueue

View File

@ -23,17 +23,13 @@
package com.iluwatar.commander;
import lombok.AllArgsConstructor;
/**
* User class contains details of user who places order.
*/
@AllArgsConstructor
public class User {
String name;
String address;
User(String name, String address) {
this.name = name;
this.address = address;
}
}

View File

@ -26,18 +26,16 @@ package com.iluwatar.commander.employeehandle;
import com.iluwatar.commander.Database;
import com.iluwatar.commander.Order;
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.Map;
/**
* The Employee Database is where orders which have encountered some issue(s) are added.
*/
public class EmployeeDatabase extends Database<Order> {
private final Hashtable<String, Order> data;
public EmployeeDatabase() {
this.data = new Hashtable<>();
}
private final Map<String, Order> data = new HashMap<>();
@Override
public Order add(Order o) throws DatabaseUnavailableException {

View File

@ -24,20 +24,16 @@
package com.iluwatar.commander.messagingservice;
import com.iluwatar.commander.Database;
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
import com.iluwatar.commander.messagingservice.MessagingService.MessageRequest;
import java.util.Hashtable;
import java.util.Map;
/**
* The MessagingDatabase is where the MessageRequest is added.
*/
public class MessagingDatabase extends Database<MessageRequest> {
private final Hashtable<String, MessageRequest> data;
public MessagingDatabase() {
this.data = new Hashtable<>();
}
private final Map<String, MessageRequest> data = new Hashtable<>();
@Override
public MessageRequest add(MessageRequest r) {

View File

@ -25,8 +25,8 @@ package com.iluwatar.commander.messagingservice;
import com.iluwatar.commander.Service;
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
/**
* The MessagingService is used to send messages to user regarding their order and payment status.
@ -34,21 +34,19 @@ import org.slf4j.LoggerFactory;
* is added to the {@link com.iluwatar.commander.employeehandle.EmployeeDatabase}.
*/
@Slf4j
public class MessagingService extends Service {
private static final Logger LOGGER = LoggerFactory.getLogger(MessagingService.class);
enum MessageToSend {
PAYMENT_FAIL, PAYMENT_TRYING, PAYMENT_SUCCESSFUL
PAYMENT_FAIL,
PAYMENT_TRYING,
PAYMENT_SUCCESSFUL
}
class MessageRequest {
String reqId;
MessageToSend msg;
MessageRequest(String reqId, MessageToSend msg) {
this.reqId = reqId;
this.msg = msg;
}
@RequiredArgsConstructor
static class MessageRequest {
final String reqId;
final MessageToSend msg;
}
public MessagingService(MessagingDatabase db, Exception... exc) {

View File

@ -24,22 +24,17 @@
package com.iluwatar.commander.paymentservice;
import com.iluwatar.commander.Database;
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
import com.iluwatar.commander.paymentservice.PaymentService.PaymentRequest;
import java.util.Hashtable;
import java.util.Map;
/**
* PaymentDatabase is where the PaymentRequest is added, along with details.
*/
public class PaymentDatabase extends Database<PaymentRequest> {
private final Hashtable<String, PaymentRequest> data;
public PaymentDatabase() {
this.data = new Hashtable<>();
//0-fail, 1-error, 2-success
}
//0-fail, 1-error, 2-success
private final Map<String, PaymentRequest> data = new Hashtable<>();
@Override
public PaymentRequest add(PaymentRequest r) {

View File

@ -25,6 +25,7 @@ package com.iluwatar.commander.paymentservice;
import com.iluwatar.commander.Service;
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
import lombok.RequiredArgsConstructor;
/**
* The PaymentService class receives request from the {@link com.iluwatar.commander.Commander} and
@ -33,16 +34,11 @@ import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
public class PaymentService extends Service {
class PaymentRequest {
String transactionId;
float payment;
@RequiredArgsConstructor
static class PaymentRequest {
final String transactionId;
final float payment;
boolean paid;
PaymentRequest(String transactionId, float payment) {
this.transactionId = transactionId;
this.payment = payment;
this.paid = false;
}
}
public PaymentService(PaymentDatabase db, Exception... exc) {

View File

@ -30,7 +30,6 @@ import com.iluwatar.commander.exceptions.IsEmptyException;
*
* @param <T> is the type of object the queue will hold.
*/
public class Queue<T> {
private Node<T> front;
@ -47,15 +46,6 @@ public class Queue<T> {
}
}
/**
* Queue constructor.
*/
Queue() {
front = null;
rear = null;
size = 0;
}
boolean isEmpty() {
return size == 0;
}

View File

@ -24,43 +24,34 @@
package com.iluwatar.commander.queue;
import com.iluwatar.commander.Order;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
/**
* QueueTask object is the object enqueued in queue.
*/
@RequiredArgsConstructor
public class QueueTask {
/**
* TaskType is the type of task to be done.
*/
public enum TaskType {
MESSAGING, PAYMENT, EMPLOYEE_DB
MESSAGING,
PAYMENT,
EMPLOYEE_DB
}
public Order order;
public TaskType taskType;
public int messageType; //0-fail, 1-error, 2-success
public final Order order;
public final TaskType taskType;
public final int messageType; //0-fail, 1-error, 2-success
/*we could have varargs Object instead to pass in any parameter instead of just message type
but keeping it simple here*/
public long firstAttemptTime; //when first time attempt made to do task
/**
* QueueTask constructor.
*
* @param o is the order for which the queuetask is being created
* @param t is the type of task to be done
* @param messageType if it is a message, which type of message - this could have instead been
* object varargs, and contained all additional details related to tasktype.
*/
public QueueTask(Order o, TaskType t, int messageType) {
this.order = o;
this.taskType = t;
this.messageType = messageType;
this.firstAttemptTime = -1;
}
@Getter
@Setter
private long firstAttemptTime = -1L; //when first time attempt made to do task
/**
* getType method.

View File

@ -24,9 +24,9 @@
package com.iluwatar.commander.shippingservice;
import com.iluwatar.commander.Database;
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
import com.iluwatar.commander.shippingservice.ShippingService.ShippingRequest;
import java.util.Hashtable;
import java.util.Map;
/**
* ShippingDatabase is where the ShippingRequest objects are added.
@ -34,11 +34,7 @@ import java.util.Hashtable;
public class ShippingDatabase extends Database<ShippingRequest> {
private final Hashtable<String, ShippingRequest> data;
public ShippingDatabase() {
this.data = new Hashtable<>();
}
private final Map<String, ShippingRequest> data = new Hashtable<>();
@Override
public ShippingRequest add(ShippingRequest r) {

View File

@ -25,6 +25,7 @@ package com.iluwatar.commander.shippingservice;
import com.iluwatar.commander.Service;
import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
import lombok.AllArgsConstructor;
/**
* ShippingService class receives request from {@link com.iluwatar.commander.Commander} class and
@ -33,16 +34,11 @@ import com.iluwatar.commander.exceptions.DatabaseUnavailableException;
public class ShippingService extends Service {
@AllArgsConstructor
static class ShippingRequest {
String transactionId;
String item;
String address;
ShippingRequest(String transactionId, String item, String address) {
this.transactionId = transactionId;
this.item = item;
this.address = address;
}
}
public ShippingService(ShippingDatabase db, Exception... exc) {