Skip to content

Commit e627753

Browse files
committed
added new class AstronomyParams for Astronomy API
1 parent d672e15 commit e627753

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package io.ipgeolocation.api;
2+
3+
import io.ipgeolocation.api.exceptions.IPGeolocationError;
4+
5+
import java.math.BigDecimal;
6+
import java.util.Objects;
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
9+
10+
public class AstronomyParams {
11+
private final String location;
12+
private final BigDecimal latitude;
13+
private final BigDecimal longitude;
14+
private final String ipAddress;
15+
private final String date;
16+
private final String lang;
17+
18+
private AstronomyParams(
19+
String location,
20+
BigDecimal latitude,
21+
BigDecimal longitude,
22+
String ipAddress,
23+
String date,
24+
String lang) {
25+
this.location = location;
26+
this.latitude = latitude;
27+
this.longitude = longitude;
28+
this.ipAddress = ipAddress;
29+
this.date = date;
30+
this.lang = lang;
31+
}
32+
33+
public String getLocation() {
34+
return location;
35+
}
36+
37+
public BigDecimal getLatitude() {
38+
return latitude;
39+
}
40+
41+
public BigDecimal getLongitude() {
42+
return longitude;
43+
}
44+
45+
public String getIpAddress() {
46+
return ipAddress;
47+
}
48+
49+
public String getDate() {
50+
return date;
51+
}
52+
53+
public String getLang() {
54+
return lang;
55+
}
56+
57+
public static AstronomyParamsBuilder builder() {
58+
return new AstronomyParamsBuilder();
59+
}
60+
61+
public static class AstronomyParamsBuilder {
62+
private String location;
63+
private BigDecimal latitude;
64+
private BigDecimal longitude;
65+
private String ipAddress;
66+
private String date;
67+
private String lang;
68+
private AstronomyParamsBuilder() {
69+
location = "";
70+
ipAddress = "";
71+
latitude = null;
72+
longitude = null;
73+
lang = "en";
74+
date = "";
75+
}
76+
77+
public AstronomyParamsBuilder withLocation(String location) {
78+
this.location = location;
79+
return this;
80+
}
81+
82+
public AstronomyParamsBuilder withCoordinates(BigDecimal latitude, BigDecimal longitude) {
83+
if (latitude.compareTo(BigDecimal.valueOf(-90.0)) < 0
84+
|| latitude.compareTo(BigDecimal.valueOf(90.0)) > 0) {
85+
throw new IPGeolocationError("'latitude' must be between -90.0 and 90.0");
86+
}
87+
88+
if (longitude.compareTo(BigDecimal.valueOf(-180.0)) < 0
89+
|| longitude.compareTo(BigDecimal.valueOf(180.0)) > 0) {
90+
throw new IPGeolocationError("'longitude' must be between -180.0 and 180.0");
91+
}
92+
93+
this.latitude = latitude;
94+
this.longitude = longitude;
95+
return this;
96+
}
97+
98+
public AstronomyParamsBuilder withIPAddress(String ipAddress) {
99+
this.ipAddress = ipAddress;
100+
return this;
101+
}
102+
103+
public AstronomyParamsBuilder withDate(String date) {
104+
if (Strings.isNullOrEmpty(date) || !isValidDateFormat(date)) {
105+
throw new IPGeolocationError("'date' must be in YYYY-MM-DD format");
106+
}
107+
this.date = date;
108+
return this;
109+
}
110+
111+
public AstronomyParamsBuilder withLang(String lang) {
112+
this.lang = lang;
113+
return this;
114+
}
115+
116+
public AstronomyParams build() {
117+
return new AstronomyParams(location, latitude, longitude, ipAddress, date, lang);
118+
}
119+
}
120+
// Method to validate date format
121+
public static boolean isValidDateFormat(String date) {
122+
String regex = "\\d{4}-\\d{2}-\\d{2}";
123+
Pattern pattern = Pattern.compile(regex);
124+
Matcher matcher = pattern.matcher(date);
125+
return matcher.matches();
126+
}
127+
128+
}

0 commit comments

Comments
 (0)