Skip to content

Commit c149da8

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

File tree

1 file changed

+122
-80
lines changed

1 file changed

+122
-80
lines changed
Lines changed: 122 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,151 @@
11
package io.ipgeolocation.api;
22

33
public class GeolocationParams {
4-
private String ip;
5-
private String[] ips;
4+
private final String ipAddress;
5+
private final String lang;
6+
private final String fields;
7+
private final Boolean includeHostname;
8+
private final boolean includeLiveHostname;
9+
private final boolean includeHostnameFallbackLive;
10+
private final boolean includeSecurity;
11+
private final boolean includeUserAgentDetail;
12+
private final String excludes;
13+
14+
public GeolocationParams(
15+
String ipAddress,
16+
String lang,
17+
String fields,
18+
String excludes,
19+
boolean includeHostname,
20+
boolean includeLiveHostname,
21+
boolean includeHostnameFallbackLive,
22+
boolean includeSecurity,
23+
boolean includeUserAgentDetail) {
24+
this.ipAddress = ipAddress;
25+
this.lang = lang;
26+
this.fields = fields;
27+
this.includeHostname = includeHostname;
28+
this.includeLiveHostname = includeLiveHostname;
29+
this.includeHostnameFallbackLive = includeHostnameFallbackLive;
30+
this.includeSecurity = includeSecurity;
31+
this.includeUserAgentDetail = includeUserAgentDetail;
32+
this.excludes = excludes;
33+
}
34+
35+
public String getIPAddress() {
36+
return ipAddress;
37+
}
38+
39+
public String getFields() {
40+
return fields;
41+
}
42+
43+
public String getLang() {
44+
return lang;
45+
}
46+
47+
public boolean isIncludeHostname() {
48+
return includeHostname;
49+
}
50+
51+
public boolean isIncludeLiveHostname() {
52+
return includeLiveHostname;
53+
}
54+
55+
public boolean isIncludeHostnameFallbackLive() {
56+
return includeHostnameFallbackLive;
57+
}
58+
59+
public boolean isIncludeSecurity() {
60+
return includeSecurity;
61+
}
62+
63+
public boolean isIncludeUserAgentDetail() {
64+
return includeUserAgentDetail;
65+
}
66+
67+
public String getExcludes() {
68+
return excludes;
69+
}
70+
71+
public static GeolocationParamsBuilder builder() {
72+
return new GeolocationParamsBuilder();
73+
}
74+
75+
public static class GeolocationParamsBuilder {
76+
private String ipAddress;
677
private String lang;
778
private String fields;
8-
private Boolean includeHostname = false;
9-
private Boolean includeLiveHostname = false;
10-
private Boolean includeHostnameFallbackLive = false;
11-
private Boolean includeSecurity = false;
12-
private Boolean includeUserAgentDetail = false;
79+
private boolean includeHostname;
80+
private boolean includeLiveHostname;
81+
private boolean includeHostnameFallbackLive;
82+
private boolean includeSecurity;
83+
private boolean includeUserAgentDetail;
1384
private String excludes;
1485

15-
public GeolocationParams() {
16-
this.ip = "";
17-
this.ips = new String[0];
18-
this.lang = "en";
19-
this.fields = "";
20-
this.includeHostname = false;
21-
this.includeSecurity = false;
22-
this.includeUserAgentDetail = false;
23-
this.excludes = "";
86+
private GeolocationParamsBuilder() {
87+
ipAddress = "";
88+
lang = "en";
89+
fields = "*";
90+
excludes = "";
2491
}
2592

26-
public void setIPAddress(String ip) {
27-
this.ip = Strings.nullToEmpty(ip);
93+
public GeolocationParamsBuilder withIPAddress(String ipAddress) {
94+
this.ipAddress = ipAddress;
95+
return this;
2896
}
2997

30-
public String getIPAddress() {
31-
return ip;
98+
public GeolocationParamsBuilder withLang(String lang) {
99+
this.lang = lang;
100+
return this;
32101
}
33102

34-
public void setFields(String fields) {
35-
this.fields = Strings.nullToEmpty(fields);
103+
public GeolocationParamsBuilder withFields(String fields) {
104+
this.fields = fields;
105+
return this;
36106
}
37107

38-
public String getFields() {
39-
return fields;
108+
public GeolocationParamsBuilder withExcludes(String excludes) {
109+
this.excludes = excludes;
110+
return this;
40111
}
41112

42-
public void setLang(String lang) {
43-
this.lang = lang;
113+
public GeolocationParamsBuilder includeHostname() {
114+
this.includeHostname = Boolean.TRUE;
115+
return this;
44116
}
45117

46-
public String getLang() {
47-
return lang;
118+
public GeolocationParamsBuilder includeLiveHostname() {
119+
this.includeLiveHostname = Boolean.TRUE;
120+
return this;
48121
}
49122

50-
public void setIncludeHostname(Boolean includeHostname) {
51-
this.includeHostname = includeHostname;
123+
public GeolocationParamsBuilder includeHostnameFallbackLive() {
124+
this.includeHostnameFallbackLive = Boolean.TRUE;
125+
return this;
52126
}
53127

54-
public boolean isIncludeHostname() {
55-
return includeHostname;
128+
public GeolocationParamsBuilder includeSecurity() {
129+
this.includeSecurity = Boolean.TRUE;
130+
return this;
56131
}
57132

58-
public void setIncludeLiveHostname(Boolean includeLiveHostname) {
59-
this.includeLiveHostname = includeLiveHostname;
133+
public GeolocationParamsBuilder includeUserAgentDetail() {
134+
this.includeUserAgentDetail = Boolean.TRUE;
135+
return this;
60136
}
61137

62-
public Boolean isIncludeLiveHostname() {
63-
return includeLiveHostname;
64-
}
65-
66-
public void setIncludeHostnameFallbackLive(Boolean includeHostnameFallbackLive) {
67-
this.includeHostnameFallbackLive = includeHostnameFallbackLive;
68-
}
69-
70-
public Boolean isIncludeHostnameFallbackLive() {
71-
return includeHostnameFallbackLive;
72-
}
73-
74-
public void setIncludeSecurity(Boolean includeSecurity) {
75-
this.includeSecurity = includeSecurity;
76-
}
77-
78-
public Boolean isIncludeSecurity() {
79-
return includeSecurity;
80-
}
81-
82-
public void setIncludeUserAgentDetail(Boolean includeUserAgentDetail) {
83-
this.includeUserAgentDetail = includeUserAgentDetail;
84-
}
85-
86-
public Boolean isIncludeUserAgentDetail() {
87-
return includeUserAgentDetail;
88-
}
89-
90-
public void setExcludes(String excludes) {
91-
this.excludes = excludes;
92-
}
93-
94-
public String getExcludes() {
95-
return excludes;
96-
}
97-
98-
public void setIPAddresses(String[] ips) throws IllegalArgumentException {
99-
if (ips.length > 50) {
100-
throw new IllegalArgumentException("Max. number of IP addresses cannot be more than 50.");
101-
} else {
102-
this.ips = ips;
103-
}
104-
}
105-
106-
public String[] getIPAddresses() {
107-
return ips;
138+
public GeolocationParams build() {
139+
return new GeolocationParams(
140+
ipAddress,
141+
lang,
142+
fields,
143+
excludes,
144+
includeHostname,
145+
includeLiveHostname,
146+
includeHostnameFallbackLive,
147+
includeSecurity,
148+
includeUserAgentDetail);
108149
}
150+
}
109151
}

0 commit comments

Comments
 (0)