1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.uiextension.internal.filter

File SortByParameterFilter.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

6
19
3
2
117
56
8
0.42
6.33
1.5
2.67

Classes

Class Line # Actions
SortByParameterFilter 45 5 0% 3 1
0.87587.5%
SortByParameterFilter.UIExtensionParameterComparator 52 14 0% 5 4
0.880%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Sort a list of {@link UIExtension} by the value of one of their parameters. The values of the chosen parameter must
37    * be a string.
38    *
39    * @version $Id: 3a6a100424195021c214972646910118cbf31efb $
40    * @since 4.3M1
41    */
42    @Component
43    @Singleton
44    @Named("sortByParameter")
 
45    public class SortByParameterFilter implements UIExtensionFilter
46    {
47    /**
48    * Comparator comparing the values of the parameter with the key set at the {@link SortByParameterFilter} level.
49    * Parameter values can be String or Integers, if both types are mixed the Integer values will be sorted first, in
50    * the correct numeric order.
51    */
 
52    public class UIExtensionParameterComparator implements Comparator<UIExtension>
53    {
54    /**
55    * The key of the parameter to compare.
56    */
57    private final String parameterKey;
58   
59    /**
60    * Default constructor.
61    *
62    * @param parameterKey The key of the parameter to use for the comparison
63    */
 
64  5670 toggle public UIExtensionParameterComparator(String parameterKey)
65    {
66  5670 this.parameterKey = parameterKey;
67    }
68   
 
69  147 toggle @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    // If the source extensions doesn't have the parameter we want it to be put at the end of the collection
78  0 result = Integer.MAX_VALUE;
79  147 } else if (targetValue == null) {
80    // If the target extensions doesn't have the parameter we want it to be put at the end of the collection
81  0 result = Integer.MIN_VALUE;
82    } else {
83  147 try {
84    // The parameter values might be integers.
85  147 int sourceInt = Integer.parseInt(sourceValue);
86  88 int targetInt = Integer.parseInt(targetValue);
87  85 result = sourceInt - targetInt;
88    } catch (NumberFormatException e) {
89    // They're not both integers so we compare the 2 string values instead.
90  62 result = sourceValue.compareToIgnoreCase(targetValue);
91    }
92    }
93   
94  147 return result;
95    }
96    }
97   
98    /**
99    *
100    * @param extensions The list of {@link UIExtension}s to filter
101    * @param parameterKey The first argument will be used as the key to use to select the parameter used for ordering.
102    * Additional arguments are ignored.
103    * @return The filtered list
104    */
 
105  5669 toggle @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    }