Skip to content

Commit 0223075

Browse files
committed
Redesigned in builder pattern
1 parent c149da8 commit 0223075

File tree

1 file changed

+91
-46
lines changed

1 file changed

+91
-46
lines changed
Lines changed: 91 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,114 @@
11
package io.ipgeolocation.api;
22

3+
import io.ipgeolocation.api.exceptions.IPGeolocationError;
4+
import java.math.BigDecimal;
5+
36
public class TimezoneParams {
4-
private String timezone;
5-
private String ip;
6-
private String lang;
7-
private Double latitude;
8-
private Double longitude;
9-
private String location;
7+
private final String timeZone;
8+
private final String ipAddress;
9+
private final BigDecimal latitude;
10+
private final BigDecimal longitude;
11+
private final String location;
12+
private final String lang;
1013

11-
public TimezoneParams() {
12-
this.timezone = "";
13-
this.ip = "";
14-
this.lang = "en";
15-
this.location = "";
16-
this.latitude = 1000.00;
17-
this.longitude = 1000.00;
18-
}
14+
private TimezoneParams(
15+
String timeZone,
16+
String ipAddress,
17+
BigDecimal latitude,
18+
BigDecimal longitude,
19+
String location,
20+
String lang) {
21+
this.timeZone = timeZone;
22+
this.ipAddress = ipAddress;
23+
this.latitude = latitude;
24+
this.longitude = longitude;
25+
this.location = location;
26+
this.lang = lang;
27+
}
1928

20-
public void setTimezone(String timezone) {
21-
this.timezone = Strings.nullToEmpty(timezone);
22-
}
29+
public String getTimeZone() {
30+
return timeZone;
31+
}
2332

24-
public String getTimezone() {
25-
return this.timezone;
26-
}
33+
public String getIPAddress() {
34+
return ipAddress;
35+
}
2736

28-
public void setIPAddress(String ip) {
29-
this.ip = Strings.nullToEmpty(ip);
30-
}
37+
public BigDecimal getLatitude() {
38+
return latitude;
39+
}
3140

32-
public String getIPAddress() {
33-
return this.ip;
34-
}
41+
public BigDecimal getLongitude() {
42+
return longitude;
43+
}
3544

36-
public void setLang(String lang) {
37-
this.lang = lang;
38-
}
45+
public String getLocation() {
46+
return location;
47+
}
3948

40-
public String getLang() {
41-
return this.lang;
49+
public String getLang() {
50+
return lang;
51+
}
52+
53+
public static TimezoneParamsBuilder builder() {
54+
return new TimezoneParamsBuilder();
55+
}
56+
57+
public static class TimezoneParamsBuilder {
58+
private String timeZone;
59+
private String ipAddress;
60+
private BigDecimal latitude;
61+
private BigDecimal longitude;
62+
private String location;
63+
private String lang;
64+
65+
private TimezoneParamsBuilder() {
66+
timeZone = "";
67+
ipAddress = "";
68+
latitude = null;
69+
longitude = null;
70+
location = "";
71+
lang = "en";
4272
}
4373

44-
public void setCoordinates(Double latitude, Double longitude) {
45-
if ((latitude >= -90 && latitude <= 90) && (longitude >= -180 && longitude <= 180)) {
46-
this.latitude = latitude;
47-
this.longitude = longitude;
48-
} else {
49-
throw new IllegalArgumentException("Coordinate value is out of range!");
50-
}
74+
public TimezoneParamsBuilder withTimeZone(String timeZone) {
75+
this.timeZone = timeZone;
76+
return this;
5177
}
5278

53-
public Double getLatitude() {
54-
return this.latitude;
79+
public TimezoneParamsBuilder withIPAddress(String ipAddress) {
80+
this.ipAddress = ipAddress;
81+
return this;
5582
}
5683

57-
public Double getLongitude() {
58-
return this.longitude;
84+
public TimezoneParamsBuilder withCoordinates(BigDecimal latitude, BigDecimal longitude) {
85+
if (latitude.compareTo(BigDecimal.valueOf(-90.0)) < 0
86+
|| latitude.compareTo(BigDecimal.valueOf(90.0)) > 0) {
87+
throw new IPGeolocationError("'latitude' must be between -90.0 and 90.0");
88+
}
89+
90+
if (longitude.compareTo(BigDecimal.valueOf(-180.0)) < 0
91+
|| longitude.compareTo(BigDecimal.valueOf(180.0)) > 0) {
92+
throw new IPGeolocationError("'longitude' must be between -180.0 and 180.0");
93+
}
94+
95+
this.latitude = latitude;
96+
this.longitude = longitude;
97+
return this;
5998
}
6099

61-
public String getLocation() {
62-
return location;
100+
public TimezoneParamsBuilder withLocation(String location) {
101+
this.location = location;
102+
return this;
63103
}
64104

65-
public void setLocation(String location) {
66-
this.location = location;
105+
public TimezoneParamsBuilder withLang(String lang) {
106+
this.lang = lang;
107+
return this;
67108
}
68109

110+
public TimezoneParams build() {
111+
return new TimezoneParams(timeZone, ipAddress, latitude, longitude, location, lang);
112+
}
113+
}
69114
}

0 commit comments

Comments
 (0)