Skip to content

Commit dae1fad

Browse files
[Java] Fixing codacy (#157)
* refactor: performance related fixes * fix: codacy medium once appreances * fix: codacy comment * tweaks --------- Co-authored-by: Artūrs Kadiķis <[email protected]>
1 parent dcd4273 commit dae1fad

File tree

16 files changed

+201
-73
lines changed

16 files changed

+201
-73
lines changed

sdk-java/src/main/java/ly/count/sdk/java/internal/CountlyFeedbackWidget.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ly.count.sdk.java.internal;
22

33
import java.util.Arrays;
4+
import java.util.Objects;
45

56
public class CountlyFeedbackWidget {
67
public String widgetId;
@@ -19,4 +20,9 @@ public boolean equals(Object o) {
1920
String str2 = gonnaCompare.widgetId + gonnaCompare.type + gonnaCompare.name + Arrays.toString(gonnaCompare.tags);
2021
return str.equals(str2);
2122
}
23+
24+
@Override
25+
public int hashCode() {
26+
return Objects.hash(widgetId, type, name);
27+
}
2228
}

sdk-java/src/main/java/ly/count/sdk/java/internal/EventImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public int hashCode() {
206206

207207
@Override
208208
public boolean equals(Object obj) {
209-
if (obj == null || !(obj instanceof EventImpl)) {
209+
if (!(obj instanceof EventImpl)) {
210210
return false;
211211
}
212212
EventImpl event = (EventImpl) obj;

sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleFeedback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ private String constructFeedbackWidgetUrlInternal(CountlyFeedbackWidget widgetIn
347347
widgetListUrl.append(internalConfig.getServerURL());
348348
widgetListUrl.append("/feedback/");
349349
widgetListUrl.append(widgetInfo.type.name());
350-
widgetListUrl.append("?");
350+
widgetListUrl.append('?');
351351
Params params = new Params()
352352
.add("widget_id", widgetInfo.widgetId)
353353
.add("device_id", internalConfig.getDeviceId().id)

sdk-java/src/main/java/ly/count/sdk/java/internal/ModuleSessions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ public void forgetSession() {
2727
@Override
2828
public void initFinished(final InternalConfig config) {
2929
try {
30-
timedEvents = Storage.read(config, new TimedEvents(L));
30+
timedEvents = Storage.read(config, new TimedEvents());
3131
if (timedEvents == null) {
32-
timedEvents = new TimedEvents(L);
32+
timedEvents = new TimedEvents();
3333
}
3434
} catch (Throwable e) {
3535
L.e("[ModuleSessions] initFinished, Cannot happen" + e);
36-
timedEvents = new TimedEvents(L);
36+
timedEvents = new TimedEvents();
3737
}
3838
}
3939

sdk-java/src/main/java/ly/count/sdk/java/internal/Params.java

Lines changed: 120 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,68 +102,133 @@ public Params(Object... objects) {
102102
}
103103
}
104104

105+
/**
106+
* Constructor
107+
*
108+
* @param params string representation of the Params object
109+
*/
105110
public Params(String params) {
106111
this.params = new StringBuilder(params);
107112
}
108113

114+
/**
115+
* Constructor
116+
*/
109117
public Params() {
110118
this.params = new StringBuilder();
111119
}
112120

121+
/**
122+
* Adds a key/value pair to the Params object
123+
*
124+
* @param objects key/value pairs
125+
* @return this Params object
126+
*/
113127
public Params add(Object... objects) {
114128
return addObjects(objects);
115129
}
116130

117-
public Params add(String key, Object value) {
131+
/**
132+
* Adds a key/value pair to the Params object
133+
*
134+
* @param key key
135+
* @param value value
136+
* @return this Params object
137+
*/
138+
public Params add(final String key, final Object value) {
118139
if (params.length() > 0) {
119-
params.append("&");
140+
params.append('&');
120141
}
121-
params.append(key).append("=");
142+
params.append(key).append('=');
122143
if (value != null) {
123144
params.append(Utils.urlencode(value.toString(), L));
124145
}
125146
return this;
126147
}
127148

128-
public Params add(Params params) {
149+
/**
150+
* Adds a Params object to the Params object
151+
*
152+
* @param params to add
153+
* @return this Params object
154+
*/
155+
public Params add(final Params params) {
129156
if (params == null || params.length() == 0) {
130157
return this;
131158
}
132159
if (this.params.length() > 0) {
133-
this.params.append("&");
160+
this.params.append('&');
134161
}
135-
this.params.append(params.toString());
162+
this.params.append(params);
136163
return this;
137164
}
138165

139-
public Params add(String string) {
166+
/**
167+
* Adds a string to the Params object
168+
*
169+
* @param string to add
170+
* @return this Params object
171+
*/
172+
public Params add(final String string) {
140173
if (params != null) {
141174
this.params.append(string);
142175
}
143176
return this;
144177
}
145178

146-
public Obj obj(String key) {
179+
/**
180+
* Returns a new Obj object
181+
*
182+
* @param key to use
183+
* @return new Obj object
184+
*/
185+
public Obj obj(final String key) {
147186
return new Obj(key, this, L);
148187
}
149188

150-
public Arr arr(String key) {
189+
/**
190+
* Returns a new Arr object
191+
*
192+
* @param key to use
193+
* @return new Arr object
194+
*/
195+
public Arr arr(final String key) {
151196
return new Arr(key, this, L);
152197
}
153198

154-
public String remove(String key) {
155-
List<String> pairs = new ArrayList<>(Arrays.asList(params.toString().split("&")));
199+
/**
200+
* Removes the provided key from the Params object
201+
*
202+
* @param key to remove
203+
* @return value of the provided key, null if not found
204+
*/
205+
public String remove(final String key) {
206+
String query = params.toString();
207+
String[] pairs = query.split("&");
208+
String result = null;
209+
StringBuilder newParams = new StringBuilder();
210+
156211
for (String pair : pairs) {
157-
String comps[] = pair.split("=");
212+
String[] comps = pair.split("=");
158213
if (comps.length == 2 && comps[0].equals(key)) {
159-
pairs.remove(pair);
160-
this.params = new StringBuilder(Utils.join(pairs, "&"));
161-
return Utils.urldecode(comps[1]);
214+
result = Utils.urldecode(comps[1]);
215+
} else {
216+
if (newParams.length() > 0) {
217+
newParams.append('&');
218+
}
219+
newParams.append(pair);
162220
}
163221
}
164-
return null;
222+
223+
params = newParams;
224+
return result;
165225
}
166226

227+
/**
228+
* Returns a map of the Params object
229+
*
230+
* @return map of the Params object
231+
*/
167232
public Map<String, String> map() {
168233
Map<String, String> map = new HashMap<>();
169234
List<String> pairs = new ArrayList<>(Arrays.asList(params.toString().split("&")));
@@ -176,7 +241,13 @@ public Map<String, String> map() {
176241
return map;
177242
}
178243

179-
public String get(String key) {
244+
/**
245+
* Returns the value of the provided key
246+
*
247+
* @param key to get value for
248+
* @return value of the provided key, null if not found
249+
*/
250+
public String get(final String key) {
180251
if (!has(key)) {
181252
return null;
182253
}
@@ -190,7 +261,13 @@ public String get(String key) {
190261
return null;
191262
}
192263

193-
public boolean has(String key) {
264+
/**
265+
* Checks if the Params object contains the provided key
266+
*
267+
* @param key to check for
268+
* @return true if the Params object contains the provided key, false otherwise
269+
*/
270+
public boolean has(final String key) {
194271
return params.indexOf("&" + key + "=") != -1 || params.indexOf(key + "=") == 0;
195272
}
196273

@@ -205,23 +282,48 @@ private Params addObjects(Object[] objects) {
205282
return this;
206283
}
207284

285+
/**
286+
* Returns the length of the Params object
287+
*
288+
* @return length of the Params object
289+
*/
208290
public int length() {
209291
return params.length();
210292
}
211293

294+
/**
295+
* Clears the Params object
296+
*/
212297
public void clear() {
213298
params = new StringBuilder();
214299
}
215300

301+
/**
302+
* Returns the string representation of the Params object
303+
*
304+
* @return string representation of the Params object
305+
*/
306+
@Override
216307
public String toString() {
217308
return params.toString();
218309
}
219310

311+
/**
312+
* Returns the hash code of the Params object
313+
*
314+
* @return hash code of the Params object
315+
*/
220316
@Override
221317
public int hashCode() {
222318
return params.hashCode();
223319
}
224320

321+
/**
322+
* Compares two Params objects
323+
*
324+
* @param obj to compare to
325+
* @return true if equal, false otherwise
326+
*/
225327
@Override
226328
public boolean equals(Object obj) {
227329
if (!(obj instanceof Params)) {

sdk-java/src/main/java/ly/count/sdk/java/internal/RemoteConfigValueStore.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ protected void mergeValues(@Nonnull Map<String, RCData> newValues, final boolean
9797
String key = entry.getKey();
9898
Object newValue = entry.getValue().value;
9999
try {
100-
JSONObject newObj = new JSONObject();
101-
newObj.put(keyValue, newValue);
102-
newObj.put(keyCacheFlag, cacheValFresh);
100+
JSONObject newObj = createValueObj(newValue);
103101
values.put(key, newObj);
104102
} catch (Exception e) {
105103
L.e("[RemoteConfigValueStore] Failed merging remote config values");
@@ -108,6 +106,13 @@ protected void mergeValues(@Nonnull Map<String, RCData> newValues, final boolean
108106
L.v("[RemoteConfigValueStore] merging done:" + values.toString());
109107
}
110108

109+
private JSONObject createValueObj(final Object newValue) {
110+
JSONObject newObj = new JSONObject();
111+
newObj.put(keyValue, newValue);
112+
newObj.put(keyCacheFlag, cacheValFresh);
113+
return newObj;
114+
}
115+
111116
//========================================
112117
// GET VALUES
113118
//========================================
@@ -152,12 +157,16 @@ protected void mergeValues(@Nonnull Map<String, RCData> newValues, final boolean
152157
}
153158
Object rcObjVal = rcObj.opt(keyValue);
154159
int rcObjCache = rcObj.getInt(keyCacheFlag);
155-
ret.put(key, new RCData(rcObjVal, rcObjCache != cacheValCached));
160+
ret.put(key, createRcData(rcObjVal, rcObjCache));
156161
} catch (Exception ex) {
157162
L.e("[RemoteConfigValueStore] Got JSON exception while calling 'getAllValues': " + ex);
158163
}
159164
}
160165

161166
return ret;
162167
}
168+
169+
private RCData createRcData(final Object value, final int cacheFlag) {
170+
return new RCData(value, cacheFlag != cacheValCached);
171+
}
163172
}

sdk-java/src/main/java/ly/count/sdk/java/internal/TimedEvents.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@
1313

1414
class TimedEvents implements Storable, EventImpl.EventRecorder {
1515

16-
private final Log L;
17-
1816
private final Map<String, EventImpl> events;
1917

20-
protected TimedEvents(Log logger) {
21-
L = logger;
18+
protected TimedEvents() {
2219
events = new HashMap<>();
2320
}
2421

sdk-java/src/main/java/ly/count/sdk/java/internal/Transport.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import java.io.PrintWriter;
1212
import java.net.HttpURLConnection;
1313
import java.net.URL;
14+
import java.nio.ByteBuffer;
15+
import java.nio.charset.StandardCharsets;
1416
import java.nio.file.Files;
1517
import java.security.KeyFactory;
1618
import java.security.KeyStore;
@@ -167,7 +169,7 @@ HttpURLConnection connection(final Request request, final User user) throws IOEx
167169
Map<String, String> map = request.params.map();
168170
for (String key : map.keySet()) {
169171
String value = Utils.urldecode(map.get(key));
170-
salting.append(key).append("=").append(value).append("&");
172+
salting.append(key).append('=').append(value).append('&');
171173
addMultipart(output, writer, boundary, "text/plain", key, value, null);
172174
}
173175

@@ -388,7 +390,7 @@ private void setPins(Set<String> keys, Set<String> certs) throws CertificateExce
388390
try {
389391
byte[] data = Utils.readStream(Transport.class.getClassLoader().getResourceAsStream(key), L);
390392
if (data != null) {
391-
String string = new String(data);
393+
String string = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(data)).toString();
392394
if (string.contains("--BEGIN")) {
393395
data = Utils.Base64.decode(trimPem(string), L);
394396
}
@@ -400,6 +402,7 @@ private void setPins(Set<String> keys, Set<String> certs) throws CertificateExce
400402
X509EncodedKeySpec spec = new X509EncodedKeySpec(data);
401403
KeyFactory kf = KeyFactory.getInstance("RSA");
402404
PublicKey k = kf.generatePublic(spec);
405+
403406
keyPins.add(k.getEncoded());
404407
} catch (InvalidKeySpecException e) {
405408
L.d("[network] Certificate in instead of public key it seems " + e);
@@ -467,7 +470,7 @@ public void checkServerTrusted(X509Certificate[] chain, String authType) throws
467470
throw new IllegalArgumentException("PublicKeyManager: X509Certificate array is null");
468471
}
469472

470-
if (!(chain.length > 0)) {
473+
if (chain.length == 0) {
471474
throw new IllegalArgumentException("PublicKeyManager: X509Certificate is empty");
472475
}
473476

0 commit comments

Comments
 (0)