FINERACT-2081: Replace switch statements with if statements for better readability

This commit is contained in:
Hossam Hatem
2025-02-28 16:20:21 +01:00
committed by Adam Saghy
parent 4c66151b47
commit 843b27926e
@@ -37,11 +37,11 @@ import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Objects;
import lombok.Getter;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import org.apache.fineract.client.models.ExternalId;
import org.apache.fineract.client.util.adapter.ExternalIdAdapter;
import org.jetbrains.annotations.NotNull;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
@@ -51,6 +51,7 @@ import retrofit2.converter.gson.GsonConverterFactory;
// The original JSON class is deleted during the build (see FINERACT-1231).
public class JSON {
@Getter
private final Gson gson;
private final DateTypeAdapter dateTypeAdapter = new DateTypeAdapter();
private final SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter();
@@ -66,10 +67,6 @@ public class JSON {
.create();
}
public Gson getGson() {
return gson;
}
/**
* GSON TypeAdapter for JSR-310 OffsetDateTime type.
*/
@@ -88,11 +85,11 @@ public class JSON {
@Override
public OffsetDateTime read(JsonReader in) throws IOException {
switch (in.peek()) {
case NULL:
JsonToken token = in.peek();
if (token == JsonToken.NULL) {
in.nextNull();
return null;
default:
}
String date = in.nextString();
if (date.endsWith("+0000")) {
date = date.substring(0, date.length() - 5) + "Z";
@@ -100,7 +97,6 @@ public class JSON {
return OffsetDateTime.parse(date, formatter);
}
}
}
/**
* GSON TypeAdapter for JSR-310 LocalDate type, which Fineract API's RETURNS as JSON array in the
@@ -123,29 +119,34 @@ public class JSON {
@Override
public LocalDate read(JsonReader in) throws IOException {
switch (in.peek()) {
case NULL:
JsonToken token = in.peek();
if (token == JsonToken.NULL) {
in.nextNull();
return null;
case STRING:
}
if (token == JsonToken.STRING) {
String dateString = in.nextString();
if (dateString != null && dateString.length() != 0) {
if (dateString != null && !dateString.isEmpty()) {
return LocalDate.parse(dateString);
}
return null;
case BEGIN_ARRAY:
}
if (token == JsonToken.BEGIN_ARRAY) {
in.beginArray();
int year = in.nextInt();
int month = in.nextInt();
int day = in.nextInt();
in.endArray();
return LocalDate.of(year, month, day);
default:
}
throw new JsonParseException(
"Fineract's API normally always sends LocalDate as e.g. [2009,1,1].. or does it not?! (FINERACT-1220)");
}
}
}
/**
* GSON TypeAdapter for java.sql.Date type. If the dateFormat is null, a simple "yyyy-MM-dd" format will be used
@@ -172,11 +173,11 @@ public class JSON {
@Override
public java.sql.Date read(JsonReader in) throws IOException {
switch (in.peek()) {
case NULL:
JsonToken token = in.peek();
if (token == JsonToken.NULL) {
in.nextNull();
return null;
default:
}
String date = in.nextString();
try {
if (dateFormat != null) {
@@ -188,7 +189,6 @@ public class JSON {
}
}
}
}
/**
* GSON TypeAdapter for java.util.Date type. If the dateFormat is null, ISO8601Utils will be used.
@@ -226,7 +226,6 @@ public class JSON {
}
}
@NotNull
private Date parseDate(String date) {
try {
if (null != dateFormat) {