Skip to content

Commit bfd7550

Browse files
committed
added model classes for Astronomy API
1 parent e627753 commit bfd7550

File tree

2 files changed

+243
-0
lines changed

2 files changed

+243
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package io.ipgeolocation.api;
2+
3+
import org.json.JSONObject;
4+
5+
import java.math.BigDecimal;
6+
import java.util.Objects;
7+
8+
public class Astronomy {
9+
private final String date;
10+
private final String currentTime;
11+
private final String sunrise;
12+
private final String sunset;
13+
private final String sunStatus;
14+
private final String solarNoon;
15+
private final String dayLength;
16+
private final BigDecimal sunAltitude;
17+
private final BigDecimal sunDistance;
18+
private final BigDecimal sunAzimuth;
19+
private final String moonrise;
20+
private final String moonset;
21+
private final String moonStatus;
22+
private final BigDecimal moonAltitude;
23+
private final BigDecimal moonDistance;
24+
private final BigDecimal moonAzimuth;
25+
private final BigDecimal moonParallacticAngle;
26+
private AstronomyLocation location;
27+
private final JSONObject json;
28+
29+
Astronomy(JSONObject json) {
30+
if (Objects.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.date = json.getString("date");
39+
this.currentTime = json.getString("current_time");
40+
this.sunrise = json.getString("sunrise");
41+
this.sunset = json.getString("sunset");
42+
this.sunStatus = json.getString("sun_status");
43+
this.solarNoon = json.getString("solar_noon");
44+
this.dayLength = json.getString("day_length");
45+
this.sunAltitude = json.getBigDecimal("sun_altitude");
46+
this.sunDistance = json.getBigDecimal("sun_distance");
47+
this.sunAzimuth = json.getBigDecimal("sun_azimuth");
48+
this.moonrise = json.getString("moonrise");
49+
this.moonset = json.getString("moonset");
50+
this.moonStatus = json.getString("moon_status");
51+
this.moonAltitude = json.getBigDecimal("moon_altitude");
52+
this.moonDistance = json.getBigDecimal("moon_distance");
53+
this.moonAzimuth = json.getBigDecimal("moon_azimuth");
54+
this.moonParallacticAngle = json.getBigDecimal("moon_parallactic_angle");
55+
56+
if (json.has("location")) {
57+
this.location = new AstronomyLocation(json.getJSONObject("location"));
58+
}
59+
this.json = json;
60+
}
61+
62+
public String getDate() {
63+
return date;
64+
}
65+
66+
public String getCurrentTime() {
67+
return currentTime;
68+
}
69+
70+
public String getSunrise() {
71+
return sunrise;
72+
}
73+
74+
public String getSunset() {
75+
return sunset;
76+
}
77+
78+
public String getSunStatus() {
79+
return sunStatus;
80+
}
81+
82+
public String getSolarNoon() {
83+
return solarNoon;
84+
}
85+
86+
public String getDayLength() {
87+
return dayLength;
88+
}
89+
90+
public BigDecimal getSunAltitude() {
91+
return sunAltitude;
92+
}
93+
94+
public BigDecimal getSunDistance() {
95+
return sunDistance;
96+
}
97+
98+
public BigDecimal getSunAzimuth() {
99+
return sunAzimuth;
100+
}
101+
102+
public String getMoonrise() {
103+
return moonrise;
104+
}
105+
106+
public String getMoonset() {
107+
return moonset;
108+
}
109+
110+
public String getMoonStatus() {
111+
return moonStatus;
112+
}
113+
114+
public BigDecimal getMoonAltitude() {
115+
return moonAltitude;
116+
}
117+
118+
public BigDecimal getMoonDistance() {
119+
return moonDistance;
120+
}
121+
122+
public BigDecimal getMoonAzimuth() {
123+
return moonAzimuth;
124+
}
125+
126+
public BigDecimal getMoonParallacticAngle() {
127+
return moonParallacticAngle;
128+
}
129+
130+
public AstronomyLocation getLocation() {
131+
return location;
132+
}
133+
134+
@Override
135+
public String toString() {
136+
return json.toString(2);
137+
}
138+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package io.ipgeolocation.api;
2+
3+
import org.json.JSONObject;
4+
5+
import java.math.BigDecimal;
6+
import java.util.Objects;
7+
8+
public class AstronomyLocation {
9+
private final String location;
10+
private final BigDecimal latitude;
11+
private final BigDecimal longitude;
12+
private final String zipcode;
13+
private final String countryCode2;
14+
private final String countryCode3;
15+
private final String countryName;
16+
private final String countryNameOfficial;
17+
private final String stateProv;
18+
private final String stateCode;
19+
private final String district;
20+
private final String locality;
21+
private final String city;
22+
private final JSONObject json;
23+
24+
AstronomyLocation(JSONObject json) {
25+
if (Objects.isNull(json)) {
26+
throw new IllegalArgumentException("'json' must not be null");
27+
}
28+
29+
if (json.isEmpty()) {
30+
throw new IllegalArgumentException("'json' must not be empty");
31+
}
32+
33+
this.location = json.optString("location");
34+
this.countryCode2 = json.optString("country_code2");
35+
this.countryCode3 = json.optString("country_code3");
36+
this.countryNameOfficial = json.optString("country_name_official");
37+
this.countryName = json.optString("country_name", json.optString("country"));
38+
this.stateProv = json.optString("state_prov", json.optString("state"));
39+
this.stateCode = json.optString("state_code");
40+
this.latitude = json.getBigDecimal("latitude");
41+
this.longitude = json.getBigDecimal("longitude");
42+
this.zipcode = json.optString("zipcode");
43+
this.district = json.optString("district");
44+
this.locality = json.optString("locality");
45+
this.city = json.optString("city");
46+
this.json = json;
47+
}
48+
49+
public String getLocation() {
50+
return location;
51+
}
52+
53+
public BigDecimal getLatitude() {
54+
return latitude;
55+
}
56+
57+
public BigDecimal getLongitude() {
58+
return longitude;
59+
}
60+
61+
public String getZipcode() {
62+
return zipcode;
63+
}
64+
65+
public String getCountryCode2() {
66+
return countryCode2;
67+
}
68+
69+
public String getCountryCode3() {
70+
return countryCode3;
71+
}
72+
73+
public String getCountryName() {
74+
return countryName;
75+
}
76+
77+
public String getCountryNameOfficial() {
78+
return countryNameOfficial;
79+
}
80+
81+
public String getStateProv() {
82+
return stateProv;
83+
}
84+
85+
public String getStateCode() {
86+
return stateCode;
87+
}
88+
89+
public String getDistrict() {
90+
return district;
91+
}
92+
93+
public String getLocality() {
94+
return locality;
95+
}
96+
97+
public String getCity() {
98+
return city;
99+
}
100+
101+
@Override
102+
public String toString() {
103+
return json.toString(2);
104+
}
105+
}

0 commit comments

Comments
 (0)