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,17 +85,16 @@ 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();
if (date.endsWith("+0000")) {
date = date.substring(0, date.length() - 5) + "Z";
}
return OffsetDateTime.parse(date, formatter);
} }
String date = in.nextString();
if (date.endsWith("+0000")) {
date = date.substring(0, date.length() - 5) + "Z";
}
return OffsetDateTime.parse(date, formatter);
} }
} }
@@ -123,27 +119,32 @@ 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:
in.nextNull(); if (token == JsonToken.NULL) {
return null; in.nextNull();
case STRING: return null;
String dateString = in.nextString();
if (dateString != null && dateString.length() != 0) {
return LocalDate.parse(dateString);
}
return null;
case 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)");
} }
if (token == JsonToken.STRING) {
String dateString = in.nextString();
if (dateString != null && !dateString.isEmpty()) {
return LocalDate.parse(dateString);
}
return null;
}
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);
}
throw new JsonParseException(
"Fineract's API normally always sends LocalDate as e.g. [2009,1,1].. or does it not?! (FINERACT-1220)");
} }
} }
@@ -172,20 +173,19 @@ 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) {
return new java.sql.Date(dateFormat.parse(date).getTime()); return new java.sql.Date(dateFormat.parse(date).getTime());
} }
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime()); return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
} catch (ParseException e) { } catch (ParseException e) {
throw new JsonParseException(e); throw new JsonParseException(e);
}
} }
} }
} }
@@ -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) {