com.base.Helpers.implode(String,List)   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.2
cc 4
1
package com.base;
2
3
import java.util.Iterator;
4
import java.util.List;
5
6
public class Helpers {
0 ignored issues
show
Best Practice introduced by
This looks like a utility class. You may want to hide the implict public constructor behind a private one, so the class cannot be instantiated,
Loading history...
7
8
    /**
9
     * Join List Elements with a String.
10
     *
11
     * @param glue String to glue list elements.
12
     * @param list List of elements.
13
     * @param <T>  List element type.
14
     * @return String
15
     */
16
    public static <T> String implode(String glue, List<T> list) {
17
        // list is empty, return empty string
18
        if (list == null || list.isEmpty()) {
19
            return "";
20
        }
21
22
        Iterator<T> iter = list.iterator();
23
24
        // init the builder with the first element
25
        StringBuilder sb = new StringBuilder();
26
        sb.append(iter.next());
27
28
        // concat each element
29
        while (iter.hasNext()) {
30
            sb.append(glue).append(iter.next());
31
        }
32
33
        // return result
34
        return sb.toString();
35
    }
36
37
    /**
38
     * Build URL with Query
39
     *
40
     * @param url
41
     * @param parameters
42
     * @return URL
43
     */
44
    public static String buildUrlWithQuery(String url, List<String> parameters) {
45
        if (parameters.size() < 1) {
0 ignored issues
show
Best Practice introduced by
Use isEmpty() to check if a collection is empty instead of checking its size, since it is optimized for this task.
Loading history...
46
            return url;
47
        }
48
49
        return url.concat("?").concat(Helpers.implode("&", parameters));
50
    }
51
}
52