Skip to content

Commit 7401df1

Browse files
committed
Pass a JSONObject instead of a Map in the constructor. Use primitive types instead of non-primtives. Save 'json' to return as String.
1 parent 331e5bc commit 7401df1

File tree

1 file changed

+132
-127
lines changed

1 file changed

+132
-127
lines changed
Lines changed: 132 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,138 @@
11
package io.ipgeolocation.api;
22

33
import java.math.BigDecimal;
4-
import java.util.HashMap;
5-
import java.util.Map;
6-
7-
import static java.util.Objects.isNull;
4+
import java.util.Objects;
5+
import org.json.JSONObject;
86

97
public class Timezone {
10-
private final String timezone;
11-
private final Integer timezoneOffset;
12-
private final Integer timezoneOffsetWithDST;
13-
private final String date;
14-
private final String dateTime;
15-
private final String dateTimeTxt;
16-
private final String dateTimeWti;
17-
private final String dateTimeYmd;
18-
private final BigDecimal dateTimeUnix;
19-
private final String time24;
20-
private final String time12;
21-
private final Integer week;
22-
private final Integer month;
23-
private final Integer year;
24-
private final String yearAbbr;
25-
private final Boolean isDST;
26-
private final Integer dstSavings;
27-
private TimezoneGeo timezoneGeo;
28-
29-
Timezone(Map<String, Object> json) {
30-
if (isNull(json)) {
31-
throw new IllegalArgumentException("'json' must not be null.");
32-
}
33-
34-
if (json.isEmpty()) {
35-
throw new IllegalArgumentException("'json' must not be empty.");
36-
}
37-
38-
this.timezone = (String) json.get("timezone");
39-
this.timezoneOffset = (Integer) json.get("timezone_offset");
40-
this.timezoneOffsetWithDST = (Integer) json.get("timezone_offset_with_dst");
41-
this.date = (String) json.get("date");
42-
this.dateTime = (String) json.get("date_time");
43-
this.dateTimeTxt = (String) json.get("date_time_txt");
44-
this.dateTimeWti = (String) json.get("date_time_wti");
45-
this.dateTimeYmd = (String) json.get("date_time_ymd");
46-
this.dateTimeUnix = (BigDecimal) json.get("date_time_unix");
47-
this.time24 = (String) json.get("time_24");
48-
this.time12 = (String) json.get("time_12");
49-
this.week = (Integer) json.get("week");
50-
this.month = (Integer) json.get("month");
51-
this.year = (Integer) json.get("year");
52-
this.yearAbbr = (String) json.get("year_abbr");
53-
this.isDST = (Boolean) json.get("is_dst");
54-
this.dstSavings = (Integer) json.get("dst_savings");
55-
if (json.get("geo") instanceof HashMap) {
56-
Map<String, Object> geoJson = (HashMap) json.get("geo");
57-
this.timezoneGeo = new TimezoneGeo(geoJson);
58-
}
59-
}
60-
61-
public String getTimezone() {
62-
return timezone;
63-
}
64-
65-
public Integer getTimezoneOffset() {
66-
return timezoneOffset;
67-
}
68-
69-
public Integer getTimezoneOffsetWithDST() {
70-
return timezoneOffsetWithDST;
71-
}
72-
73-
public String getDate() {
74-
return date;
75-
}
76-
77-
public String getDateTime() {
78-
return dateTime;
79-
}
80-
81-
public String getDateTimeTxt() {
82-
return dateTimeTxt;
83-
}
84-
85-
public String getDateTimeWti() {
86-
return dateTimeWti;
87-
}
88-
89-
public String getDateTimeYmd() {
90-
return dateTimeYmd;
91-
}
92-
93-
public BigDecimal getDateTimeUnix() {
94-
return dateTimeUnix;
95-
}
96-
97-
public String getTime24() {
98-
return time24;
99-
}
100-
101-
public String getTime12() {
102-
return time12;
103-
}
104-
105-
public Integer getWeek() {
106-
return week;
107-
}
108-
109-
public Integer getMonth() {
110-
return month;
111-
}
112-
113-
public Integer getYear() {
114-
return year;
115-
}
116-
117-
public String getYearAbbr() {
118-
return yearAbbr;
119-
}
120-
121-
public Boolean isDST() {
122-
return isDST;
123-
}
124-
125-
public Integer getDSTSavings() {
126-
return dstSavings;
127-
}
128-
129-
public TimezoneGeo getTimezoneGeo() {
130-
return timezoneGeo;
131-
}
132-
8+
private final String timezone;
9+
private final int timezoneOffset;
10+
private final int timezoneOffsetWithDST;
11+
private final String date;
12+
private final String dateTime;
13+
private final String dateTimeTxt;
14+
private final String dateTimeWti;
15+
private final String dateTimeYmd;
16+
private final BigDecimal dateTimeUnix;
17+
private final String time24;
18+
private final String time12;
19+
private final int week;
20+
private final int month;
21+
private final int year;
22+
private final String yearAbbr;
23+
private final boolean isDST;
24+
private final int dstSavings;
25+
private TimezoneGeo geo;
26+
private final JSONObject json;
27+
28+
Timezone(JSONObject json) {
29+
if (Objects.isNull(json)) {
30+
throw new IllegalArgumentException("'json' must not be null");
31+
}
32+
33+
if (json.isEmpty()) {
34+
throw new IllegalArgumentException("'json' must not be empty");
35+
}
36+
37+
this.timezone = json.getString("timezone");
38+
this.timezoneOffset = json.getInt("timezone_offset");
39+
this.timezoneOffsetWithDST = json.getInt("timezone_offset_with_dst");
40+
this.date = json.getString("date");
41+
this.dateTime = json.getString("date_time");
42+
this.dateTimeTxt = json.getString("date_time_txt");
43+
this.dateTimeWti = json.getString("date_time_wti");
44+
this.dateTimeYmd = json.getString("date_time_ymd");
45+
this.dateTimeUnix = json.getBigDecimal("date_time_unix");
46+
this.time24 = json.getString("time_24");
47+
this.time12 = json.getString("time_12");
48+
this.week = json.getInt("week");
49+
this.month = json.getInt("month");
50+
this.year = json.getInt("year");
51+
this.yearAbbr = json.getString("year_abbr");
52+
this.isDST = json.getBoolean("is_dst");
53+
this.dstSavings = json.getInt("dst_savings");
54+
55+
if (json.has("geo")) {
56+
this.geo = new TimezoneGeo(json.getJSONObject("geo"));
57+
}
58+
59+
this.json = json;
60+
}
61+
62+
public String getTimezone() {
63+
return timezone;
64+
}
65+
66+
public int getTimezoneOffset() {
67+
return timezoneOffset;
68+
}
69+
70+
public int getTimezoneOffsetWithDST() {
71+
return timezoneOffsetWithDST;
72+
}
73+
74+
public String getDate() {
75+
return date;
76+
}
77+
78+
public String getDateTime() {
79+
return dateTime;
80+
}
81+
82+
public String getDateTimeTxt() {
83+
return dateTimeTxt;
84+
}
85+
86+
public String getDateTimeWti() {
87+
return dateTimeWti;
88+
}
89+
90+
public String getDateTimeYmd() {
91+
return dateTimeYmd;
92+
}
93+
94+
public BigDecimal getDateTimeUnix() {
95+
return dateTimeUnix;
96+
}
97+
98+
public String getTime24() {
99+
return time24;
100+
}
101+
102+
public String getTime12() {
103+
return time12;
104+
}
105+
106+
public int getWeek() {
107+
return week;
108+
}
109+
110+
public int getMonth() {
111+
return month;
112+
}
113+
114+
public int getYear() {
115+
return year;
116+
}
117+
118+
public String getYearAbbr() {
119+
return yearAbbr;
120+
}
121+
122+
public boolean isDST() {
123+
return isDST;
124+
}
125+
126+
public int getDSTSavings() {
127+
return dstSavings;
128+
}
129+
130+
public TimezoneGeo getGeo() {
131+
return geo;
132+
}
133+
134+
@Override
135+
public String toString() {
136+
return json.toString(2);
137+
}
133138
}

0 commit comments

Comments
 (0)