1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.uiextension.internal.filter; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Collections; |
24 |
|
import java.util.Comparator; |
25 |
|
import java.util.List; |
26 |
|
|
27 |
|
import javax.inject.Named; |
28 |
|
import javax.inject.Singleton; |
29 |
|
|
30 |
|
import org.apache.commons.lang3.StringUtils; |
31 |
|
import org.xwiki.component.annotation.Component; |
32 |
|
import org.xwiki.uiextension.UIExtension; |
33 |
|
import org.xwiki.uiextension.UIExtensionFilter; |
34 |
|
|
35 |
|
|
36 |
|
@link |
37 |
|
|
38 |
|
|
39 |
|
@version |
40 |
|
@since |
41 |
|
|
42 |
|
@Component |
43 |
|
@Singleton |
44 |
|
@Named("sortByParameter") |
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 3 |
Complexity Density: 0.6 |
|
45 |
|
public class SortByParameterFilter implements UIExtensionFilter |
46 |
|
{ |
47 |
|
|
48 |
|
@link |
49 |
|
|
50 |
|
|
51 |
|
|
|
|
| 80% |
Uncovered Elements: 4 (20) |
Complexity: 5 |
Complexity Density: 0.36 |
|
52 |
|
public class UIExtensionParameterComparator implements Comparator<UIExtension> |
53 |
|
{ |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
private final String parameterKey; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
@param |
63 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
64 |
5670 |
public UIExtensionParameterComparator(String parameterKey)... |
65 |
|
{ |
66 |
5670 |
this.parameterKey = parameterKey; |
67 |
|
} |
68 |
|
|
|
|
| 76.5% |
Uncovered Elements: 4 (17) |
Complexity: 4 |
Complexity Density: 0.31 |
|
69 |
147 |
@Override... |
70 |
|
public int compare(UIExtension source, UIExtension target) |
71 |
|
{ |
72 |
147 |
int result = 0; |
73 |
|
|
74 |
147 |
String sourceValue = source.getParameters().get(parameterKey); |
75 |
147 |
String targetValue = target.getParameters().get(parameterKey); |
76 |
147 |
if (sourceValue == null) { |
77 |
|
|
78 |
0 |
result = Integer.MAX_VALUE; |
79 |
147 |
} else if (targetValue == null) { |
80 |
|
|
81 |
0 |
result = Integer.MIN_VALUE; |
82 |
|
} else { |
83 |
147 |
try { |
84 |
|
|
85 |
147 |
int sourceInt = Integer.parseInt(sourceValue); |
86 |
88 |
int targetInt = Integer.parseInt(targetValue); |
87 |
85 |
result = sourceInt - targetInt; |
88 |
|
} catch (NumberFormatException e) { |
89 |
|
|
90 |
62 |
result = sourceValue.compareToIgnoreCase(targetValue); |
91 |
|
} |
92 |
|
} |
93 |
|
|
94 |
147 |
return result; |
95 |
|
} |
96 |
|
} |
97 |
|
|
98 |
|
|
99 |
|
|
100 |
|
@param@link |
101 |
|
@param |
102 |
|
|
103 |
|
@return |
104 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
105 |
5669 |
@Override... |
106 |
|
public List<UIExtension> filter(List<UIExtension> extensions, String... parameterKey) |
107 |
|
{ |
108 |
5668 |
List<UIExtension> results = new ArrayList<UIExtension>(); |
109 |
5666 |
results.addAll(extensions); |
110 |
|
|
111 |
5668 |
if (parameterKey.length > 0 && !StringUtils.isBlank(parameterKey[0])) { |
112 |
5668 |
Collections.sort(results, new UIExtensionParameterComparator(parameterKey[0])); |
113 |
|
} |
114 |
|
|
115 |
5669 |
return results; |
116 |
|
} |
117 |
|
} |