Skip to content

Commit 843b279

Browse files
7ossam7atem1adamsaghy
authored andcommitted
FINERACT-2081: Replace switch statements with if statements for better readability
1 parent 4c66151 commit 843b279

File tree

1 file changed

+49
-50
lines changed
  • fineract-client/src/main/java/org/apache/fineract/client/util

1 file changed

+49
-50
lines changed

fineract-client/src/main/java/org/apache/fineract/client/util/JSON.java

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@
3737
import java.time.format.DateTimeFormatter;
3838
import java.util.Date;
3939
import java.util.Objects;
40+
import lombok.Getter;
4041
import okhttp3.RequestBody;
4142
import okhttp3.ResponseBody;
4243
import org.apache.fineract.client.models.ExternalId;
4344
import org.apache.fineract.client.util.adapter.ExternalIdAdapter;
44-
import org.jetbrains.annotations.NotNull;
4545
import retrofit2.Converter;
4646
import retrofit2.Retrofit;
4747
import retrofit2.converter.gson.GsonConverterFactory;
@@ -51,6 +51,7 @@
5151
// The original JSON class is deleted during the build (see FINERACT-1231).
5252
public class JSON {
5353

54+
@Getter
5455
private final Gson gson;
5556
private final DateTypeAdapter dateTypeAdapter = new DateTypeAdapter();
5657
private final SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter();
@@ -66,10 +67,6 @@ public JSON() {
6667
.create();
6768
}
6869

69-
public Gson getGson() {
70-
return gson;
71-
}
72-
7370
/**
7471
* GSON TypeAdapter for JSR-310 OffsetDateTime type.
7572
*/
@@ -88,17 +85,16 @@ public void write(JsonWriter out, OffsetDateTime date) throws IOException {
8885

8986
@Override
9087
public OffsetDateTime read(JsonReader in) throws IOException {
91-
switch (in.peek()) {
92-
case NULL:
93-
in.nextNull();
94-
return null;
95-
default:
96-
String date = in.nextString();
97-
if (date.endsWith("+0000")) {
98-
date = date.substring(0, date.length() - 5) + "Z";
99-
}
100-
return OffsetDateTime.parse(date, formatter);
88+
JsonToken token = in.peek();
89+
if (token == JsonToken.NULL) {
90+
in.nextNull();
91+
return null;
92+
}
93+
String date = in.nextString();
94+
if (date.endsWith("+0000")) {
95+
date = date.substring(0, date.length() - 5) + "Z";
10196
}
97+
return OffsetDateTime.parse(date, formatter);
10298
}
10399
}
104100

@@ -123,27 +119,32 @@ public void write(JsonWriter out, LocalDate date) throws IOException {
123119

124120
@Override
125121
public LocalDate read(JsonReader in) throws IOException {
126-
switch (in.peek()) {
127-
case NULL:
128-
in.nextNull();
129-
return null;
130-
case STRING:
131-
String dateString = in.nextString();
132-
if (dateString != null && dateString.length() != 0) {
133-
return LocalDate.parse(dateString);
134-
}
135-
return null;
136-
case BEGIN_ARRAY:
137-
in.beginArray();
138-
int year = in.nextInt();
139-
int month = in.nextInt();
140-
int day = in.nextInt();
141-
in.endArray();
142-
return LocalDate.of(year, month, day);
143-
default:
144-
throw new JsonParseException(
145-
"Fineract's API normally always sends LocalDate as e.g. [2009,1,1].. or does it not?! (FINERACT-1220)");
122+
JsonToken token = in.peek();
123+
124+
if (token == JsonToken.NULL) {
125+
in.nextNull();
126+
return null;
127+
}
128+
129+
if (token == JsonToken.STRING) {
130+
String dateString = in.nextString();
131+
if (dateString != null && !dateString.isEmpty()) {
132+
return LocalDate.parse(dateString);
133+
}
134+
return null;
146135
}
136+
137+
if (token == JsonToken.BEGIN_ARRAY) {
138+
in.beginArray();
139+
int year = in.nextInt();
140+
int month = in.nextInt();
141+
int day = in.nextInt();
142+
in.endArray();
143+
return LocalDate.of(year, month, day);
144+
}
145+
146+
throw new JsonParseException(
147+
"Fineract's API normally always sends LocalDate as e.g. [2009,1,1].. or does it not?! (FINERACT-1220)");
147148
}
148149
}
149150

@@ -172,20 +173,19 @@ public void write(JsonWriter out, java.sql.Date date) throws IOException {
172173

173174
@Override
174175
public java.sql.Date read(JsonReader in) throws IOException {
175-
switch (in.peek()) {
176-
case NULL:
177-
in.nextNull();
178-
return null;
179-
default:
180-
String date = in.nextString();
181-
try {
182-
if (dateFormat != null) {
183-
return new java.sql.Date(dateFormat.parse(date).getTime());
184-
}
185-
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
186-
} catch (ParseException e) {
187-
throw new JsonParseException(e);
188-
}
176+
JsonToken token = in.peek();
177+
if (token == JsonToken.NULL) {
178+
in.nextNull();
179+
return null;
180+
}
181+
String date = in.nextString();
182+
try {
183+
if (dateFormat != null) {
184+
return new java.sql.Date(dateFormat.parse(date).getTime());
185+
}
186+
return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime());
187+
} catch (ParseException e) {
188+
throw new JsonParseException(e);
189189
}
190190
}
191191
}
@@ -226,7 +226,6 @@ public Date read(JsonReader in) throws IOException {
226226
}
227227
}
228228

229-
@NotNull
230229
private Date parseDate(String date) {
231230
try {
232231
if (null != dateFormat) {

0 commit comments

Comments
 (0)