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

File SortClauseComparator.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart3.png
80% of files have more coverage

Code metrics

6
11
3
1
74
36
7
0.64
3.67
3
2.33

Classes

Class Line # Actions
SortClauseComparator 36 11 0% 7 15
0.2525%
 

Contributing tests

This file is covered by 10 tests. .

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.extension.repository.internal;
21   
22    import java.util.Collection;
23    import java.util.Comparator;
24   
25    import org.apache.commons.lang3.ObjectUtils;
26    import org.xwiki.extension.Extension;
27    import org.xwiki.extension.repository.search.ExtensionQuery.ORDER;
28    import org.xwiki.extension.repository.search.ExtensionQuery.SortClause;
29   
30    /**
31    * A custom {@link Comparator} for {@link Extension} based on {@link SortClause}.
32    *
33    * @version $Id: 3086536ea441228d599db7d7798b5ffe2ac1b996 $
34    * @since 7.0M2
35    */
 
36    public class SortClauseComparator implements Comparator<Extension>
37    {
38    private final Collection<SortClause> sortClauses;
39   
40    /**
41    * @param sortClauses the sort clauses
42    */
 
43  52 toggle public SortClauseComparator(Collection<SortClause> sortClauses)
44    {
45  52 this.sortClauses = sortClauses;
46    }
47   
 
48  353 toggle @Override
49    public int compare(Extension o1, Extension o2)
50    {
51  353 for (SortClause sortClause : this.sortClauses) {
52  0 int result = compare(o1, o2, sortClause);
53   
54  0 if (result != 0) {
55  0 return result;
56    }
57    }
58   
59  353 return 0;
60    }
61   
 
62  0 toggle private int compare(Extension o1, Extension o2, SortClause sortClause)
63    {
64  0 Object value1 = o1.get(sortClause.getField());
65  0 Object value2 = o2.get(sortClause.getField());
66   
67  0 if (value1 instanceof Comparable && value2 instanceof Comparable) {
68  0 return ObjectUtils.compare((Comparable) value1, (Comparable) value2)
69  0 + (sortClause.getOrder() == ORDER.ASC ? 1 : -1);
70    }
71   
72  0 return 0;
73    }
74    }