Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null checks on fields #7

Merged
merged 5 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 48 additions & 38 deletions src/main/java/me/shivzee/JMailTM.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

import static me.shivzee.util.Utility.*;


/***
* The JMailTM Class which have the instance of the API
Expand Down Expand Up @@ -84,23 +86,23 @@ public JMailTM(String bearerToken , String id){
}

private static Account mailUtility(JSONObject json) {
String id = json.get("id").toString();
String email = json.get("address").toString();
String quota = json.get("quota").toString();
String used = json.get("used").toString();
boolean isDisabled = (boolean) json.get("isDisabled");
boolean isDeleted = (boolean) json.get("isDeleted");
String createdAt = json.get("createdAt").toString();
String updatedAt = json.get("updatedAt").toString();
return new Account(id,email,quota,used,isDisabled,isDeleted,createdAt,updatedAt);
String id = safeEval(() -> json.get("id").toString());
String email = safeEval(() -> json.get("address").toString());
String quota = safeEval(() -> json.get("quota").toString());
String used = safeEval(() -> json.get("used").toString());
Boolean isDisabled = safeEval(() -> (Boolean) json.get("isDisabled"));
Boolean isDeleted = safeEval(() -> (Boolean) json.get("isDeleted"));
String createdAt = safeEval(() -> json.get("createdAt").toString());
String updatedAt = safeEval(() -> json.get("updatedAt").toString());
return new Account(id,email,quota,used, isDisabled, isDeleted,createdAt,updatedAt);
}

private Message messageUtility(JSONObject json) throws ParseException, DateTimeParserException {
String id = json.get("id").toString();
String msgid = json.get("msgid").toString();
JSONObject from = (JSONObject) parser.parse(json.get("from").toString());
String senderAddress = from.get("address").toString();
String senderName = from.get("name").toString();
String id = safeEval(() -> json.get("id").toString());
String msgid = safeEval(() -> json.get("msgid").toString());
JSONObject from = (JSONObject) parser.parse(safeEval(() -> json.get("from").toString()));
String senderAddress = safeEval(() -> from.get("address").toString());
String senderName = safeEval(() -> from.get("name").toString());

List<Receiver> receivers = new ArrayList<>();
JSONArray receiverArray = (JSONArray) parser.parse(json.get("to").toString());
Expand All @@ -109,43 +111,51 @@ private Message messageUtility(JSONObject json) throws ParseException, DateTimeP
JSONObject object = (JSONObject) jsonObject;
receivers.add(new Receiver(object.get("address").toString(), object.get("name").toString()));
}
String subject = json.get("subject").toString();
String content = json.get("text").toString();
boolean seen = (boolean) json.get("seen");
boolean flagged = (boolean) json.get("flagged");
boolean isDeleted = (boolean) json.get("isDeleted");
boolean retention = (boolean) json.get("retention");
String retentionDate = json.get("retentionDate").toString();
String rawHTML = json.get("html").toString();
boolean hasAttachments = (boolean) json.get("hasAttachments");
String subject = safeEval(() -> json.get("subject").toString());
String content = safeEval(() -> json.get("text").toString());
Boolean seen = safeEval(() -> (Boolean) json.get("seen"));
Boolean flagged = safeEval(() -> (Boolean) json.get("flagged"));
Boolean isDeleted = safeEval(() -> (Boolean) json.get("isDeleted"));
Boolean retention = safeEval(() -> (Boolean) json.get("retention"));
String retentionDate = safeEval(() -> json.get("retentionDate").toString());
String rawHTML = safeEval(() -> json.get("html").toString());
Boolean hasAttachments = safeEval(() -> (Boolean) json.get("hasAttachments"));

Object[] aArray = new Object[0];
if (hasAttachments) {
if (Boolean.TRUE.equals(hasAttachments)) {
JSONArray attachmentArray = (JSONArray) parser.parse(json.get("attachments").toString());
aArray = attachmentArray.toArray();
}

List<Attachment> attachments = new ArrayList<>();
for (Object attachmentObject : aArray) {
JSONObject object = (JSONObject) attachmentObject;
String aId = object.get("id").toString();
String aFilename = object.get("filename").toString();
String aContentType = object.get("contentType").toString();
String aDisposition = object.get("disposition").toString();
String aTransferEncoding = object.get("transferEncoding").toString();
boolean aRelated = (boolean) object.get("related");
long aSize = Long.parseLong(object.get("size").toString());
String aDownloadUrl = object.get("downloadUrl").toString();
String aId = safeEval(() -> object.get("id")).toString();
String aFilename = safeEval(() -> object.get("filename").toString());
String aContentType = safeEval(() -> object.get("contentType").toString());
String aDisposition = safeEval(() -> object.get("disposition").toString());
String aTransferEncoding = safeEval(() -> object.get("transferEncoding").toString());
Boolean aRelated = safeEval(() -> (Boolean) object.get("related"));
String strSize = safeEval(() -> object.get("size").toString());
Long aSize = null;
if (strSize != null) {
aSize = Long.parseLong(strSize);
}
String aDownloadUrl = safeEval(() -> object.get("downloadUrl").toString());
attachments.add(new Attachment(aId, aFilename, aContentType, aDisposition, aTransferEncoding, aRelated, aSize, aDownloadUrl, bearerToken));
}

long size = Long.parseLong(json.get("size").toString());
String downloadUrl = json.get("downloadUrl").toString();
String createdAt = json.get("createdAt").toString();
String updatedAt = json.get("updatedAt").toString();
long size = 0;
String strSize = safeEval(() -> json.get("size").toString());
if (strSize != null) {
size = Long.parseLong(strSize);
}
String downloadUrl = safeEval(() -> json.get("downloadUrl").toString());
String createdAt = safeEval(() -> json.get("createdAt").toString());
String updatedAt = safeEval(() -> json.get("updatedAt").toString());

ZonedDateTime createdDateTime = Utility.parseToDefaultTimeZone(createdAt, "yyyy-MM-dd'T'HH:mm:ss'+00:00'");
ZonedDateTime updatedDateTime = Utility.parseToDefaultTimeZone(updatedAt, "yyyy-MM-dd'T'HH:mm:ss'+00:00'");
ZonedDateTime createdDateTime = parseToDefaultTimeZone(createdAt, "yyyy-MM-dd'T'HH:mm:ss'+00:00'");
ZonedDateTime updatedDateTime = parseToDefaultTimeZone(updatedAt, "yyyy-MM-dd'T'HH:mm:ss'+00:00'");

return new Message(id, msgid, senderAddress, senderName, receivers, subject, content, seen, flagged, isDeleted, retention, retentionDate, rawHTML, hasAttachments, attachments, size, downloadUrl, createdAt, createdDateTime, updatedAt, updatedDateTime, bearerToken, json.toJSONString());
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/me/shivzee/util/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class Account {
private String email;
private String quota;
private String used;
private boolean isDisabled;
private boolean isDeleted;
private Boolean isDisabled;
private Boolean isDeleted;
private String createdAt;
private String updatedAt;

Expand All @@ -23,7 +23,7 @@ public Account(){
this.createdAt = "";
this.updatedAt = "";
}
public Account(String id, String email, String quota, String used, boolean isDisabled, boolean isDeleted, String createdAt, String updatedAt) {
public Account(String id, String email, String quota, String used, Boolean isDisabled, Boolean isDeleted, String createdAt, String updatedAt) {
this.id = id;
this.email = email;
this.quota = quota;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/me/shivzee/util/Attachment.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public class Attachment {
private String contentType;
private String disposition;
private String transferEncoding;
private boolean related;
private long size;
private Boolean related;
private Long size;
private String downloadUrl;
private String bearerToken;

public Attachment(String id, String filename, String contentType, String disposition, String transferEncoding, boolean related, long size, String downloadUrl, String bearerToken) {
public Attachment(String id, String filename, String contentType, String disposition, String transferEncoding, Boolean related, Long size, String downloadUrl, String bearerToken) {
this.id = id;
this.filename = filename;
this.contentType = contentType;
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/me/shivzee/util/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public class Message {
private List<Receiver> receivers;
private String subject;
private String content;
private boolean seen;
private boolean flagged;
private boolean isDeleted;
private boolean retention;
private Boolean seen;
private Boolean flagged;
private Boolean isDeleted;
private Boolean retention;
private String retentionDate;
private String rawHTML;
private boolean hasAttachments;
private Boolean hasAttachments;
private List<Attachment> attachments;
private long size;
private Long size;
private String downloadUrl;
private String createdAt;
private ZonedDateTime createdDateTime;
Expand All @@ -41,7 +41,7 @@ public class Message {
private String bearerToken;
private String rawJson;

public Message(String id, String msgid, String senderAddress, String senderName, List<Receiver> receivers, String subject, String content, boolean seen, boolean flagged, boolean isDeleted, boolean retention, String retentionDate, String rawHTML, boolean hasAttachments, List<Attachment> attachments, long size, String downloadUrl, String createdAt,ZonedDateTime createdDateTime, String updatedAt,ZonedDateTime updatedDateTime ,String bearerToken, String rawJson) {
public Message(String id, String msgid, String senderAddress, String senderName, List<Receiver> receivers, String subject, String content, Boolean seen, Boolean flagged, Boolean isDeleted, Boolean retention, String retentionDate, String rawHTML, Boolean hasAttachments, List<Attachment> attachments, Long size, String downloadUrl, String createdAt,ZonedDateTime createdDateTime, String updatedAt,ZonedDateTime updatedDateTime ,String bearerToken, String rawJson) {
this.id = id;
this.msgid = msgid;
this.senderAddress = senderAddress;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/me/shivzee/util/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;
import java.util.function.Supplier;

/**
* The Utility Class for utility ig lmao
Expand Down Expand Up @@ -42,4 +43,12 @@ public static ZonedDateTime parseToDefaultTimeZone(String dateTime, String patte
return time;
}

public static <T> T safeEval(Supplier<T> supplier) {
try {
return supplier.get();
} catch (NullPointerException ex) {
return null;
}
}

}