|
1 | 1 | package io.ipgeolocation.api; |
2 | 2 |
|
| 3 | +import io.ipgeolocation.api.exceptions.IPGeolocationError; |
| 4 | +import java.math.BigDecimal; |
| 5 | + |
3 | 6 | 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; |
10 | 13 |
|
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 | + } |
19 | 28 |
|
20 | | - public void setTimezone(String timezone) { |
21 | | - this.timezone = Strings.nullToEmpty(timezone); |
22 | | - } |
| 29 | + public String getTimeZone() { |
| 30 | + return timeZone; |
| 31 | + } |
23 | 32 |
|
24 | | - public String getTimezone() { |
25 | | - return this.timezone; |
26 | | - } |
| 33 | + public String getIPAddress() { |
| 34 | + return ipAddress; |
| 35 | + } |
27 | 36 |
|
28 | | - public void setIPAddress(String ip) { |
29 | | - this.ip = Strings.nullToEmpty(ip); |
30 | | - } |
| 37 | + public BigDecimal getLatitude() { |
| 38 | + return latitude; |
| 39 | + } |
31 | 40 |
|
32 | | - public String getIPAddress() { |
33 | | - return this.ip; |
34 | | - } |
| 41 | + public BigDecimal getLongitude() { |
| 42 | + return longitude; |
| 43 | + } |
35 | 44 |
|
36 | | - public void setLang(String lang) { |
37 | | - this.lang = lang; |
38 | | - } |
| 45 | + public String getLocation() { |
| 46 | + return location; |
| 47 | + } |
39 | 48 |
|
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"; |
42 | 72 | } |
43 | 73 |
|
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; |
51 | 77 | } |
52 | 78 |
|
53 | | - public Double getLatitude() { |
54 | | - return this.latitude; |
| 79 | + public TimezoneParamsBuilder withIPAddress(String ipAddress) { |
| 80 | + this.ipAddress = ipAddress; |
| 81 | + return this; |
55 | 82 | } |
56 | 83 |
|
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; |
59 | 98 | } |
60 | 99 |
|
61 | | - public String getLocation() { |
62 | | - return location; |
| 100 | + public TimezoneParamsBuilder withLocation(String location) { |
| 101 | + this.location = location; |
| 102 | + return this; |
63 | 103 | } |
64 | 104 |
|
65 | | - public void setLocation(String location) { |
66 | | - this.location = location; |
| 105 | + public TimezoneParamsBuilder withLang(String lang) { |
| 106 | + this.lang = lang; |
| 107 | + return this; |
67 | 108 | } |
68 | 109 |
|
| 110 | + public TimezoneParams build() { |
| 111 | + return new TimezoneParams(timeZone, ipAddress, latitude, longitude, location, lang); |
| 112 | + } |
| 113 | + } |
69 | 114 | } |
0 commit comments