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.time.format.DateTimeFormatter;
import java.util.Date; import java.util.Date;
import java.util.Objects; import java.util.Objects;
import lombok.Getter;
import okhttp3.RequestBody; import okhttp3.RequestBody;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
import org.apache.fineract.client.models.ExternalId; import org.apache.fineract.client.models.ExternalId;
import org.apache.fineract.client.util.adapter.ExternalIdAdapter; import org.apache.fineract.client.util.adapter.ExternalIdAdapter;
import org.jetbrains.annotations.NotNull;
import retrofit2.Converter; import retrofit2.Converter;
import retrofit2.Retrofit; import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory; 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). // The original JSON class is deleted during the build (see FINERACT-1231).
public class JSON { public class JSON {
@Getter
private final Gson gson; private final Gson gson;
private final DateTypeAdapter dateTypeAdapter = new DateTypeAdapter(); private final DateTypeAdapter dateTypeAdapter = new DateTypeAdapter();
private final SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); private final SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter();
@@ -66,10 +67,6 @@ public class JSON {
.create(); .create();
} }
public Gson getGson() {
return gson;
}
/** /**
* GSON TypeAdapter for JSR-310 OffsetDateTime type. * GSON TypeAdapter for JSR-310 OffsetDateTime type.
*/ */
@@ -88,11 +85,11 @@ public class JSON {
@Override @Override
public OffsetDateTime read(JsonReader in) throws IOException { public OffsetDateTime read(JsonReader in) throws IOException {
switch (in.peek()) { JsonToken token = in.peek();
case NULL: if (token == JsonToken.NULL) {
in.nextNull(); in.nextNull();
return null; return null;
default: }
String date = in.nextString(); String date = in.nextString();
if (date.endsWith("+0000")) { if (date.endsWith("+0000")) {
date = date.substring(0, date.length() - 5) + "Z"; date = date.substring(0, date.length() - 5) + "Z";
@@ -100,7 +97,6 @@ public class JSON {
return OffsetDateTime.parse(date, formatter); return OffsetDateTime.parse(date, formatter);
} }
} }
}
/** /**
* GSON TypeAdapter for JSR-310 LocalDate type, which Fineract API's RETURNS as JSON array in the * 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 @Override
public LocalDate read(JsonReader in) throws IOException { public LocalDate read(JsonReader in) throws IOException {
switch (in.peek()) { JsonToken token = in.peek();
case NULL:
if (token == JsonToken.NULL) {
in.nextNull(); in.nextNull();
return null; return null;
case STRING: }
if (token == JsonToken.STRING) {
String dateString = in.nextString(); String dateString = in.nextString();
if (dateString != null && dateString.length() != 0) { if (dateString != null && !dateString.isEmpty()) {
return LocalDate.parse(dateString); return LocalDate.parse(dateString);
} }
return null; return null;
case BEGIN_ARRAY: }
if (token == JsonToken.BEGIN_ARRAY) {
in.beginArray(); in.beginArray();
int year = in.nextInt(); int year = in.nextInt();
int month = in.nextInt(); int month = in.nextInt();
int day = in.nextInt(); int day = in.nextInt();
in.endArray(); in.endArray();
return LocalDate.of(year, month, day); return LocalDate.of(year, month, day);
default: }
throw new JsonParseException( throw new JsonParseException(
"Fineract's API normally always sends LocalDate as e.g. [2009,1,1].. or does it not?! (FINERACT-1220)"); "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 * 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 @Override
public java.sql.Date read(JsonReader in) throws IOException { public java.sql.Date read(JsonReader in) throws IOException {
switch (in.peek()) { JsonToken token = in.peek();
case NULL: if (token == JsonToken.NULL) {
in.nextNull(); in.nextNull();
return null; return null;
default: }
String date = in.nextString(); String date = in.nextString();
try { try {
if (dateFormat != null) { 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. * 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) { private Date parseDate(String date) {
try { try {
if (null != dateFormat) { if (null != dateFormat) {