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

File ExtensionQuery.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

0
42
23
5
333
145
23
0.55
1.83
4.6
1

Classes

Class Line # Actions
ExtensionQuery 32 25 0% 15 3
0.92592.5%
ExtensionQuery.ORDER 39 0 - 0 0
-1.0 -
ExtensionQuery.COMPARISON 57 0 - 0 0
-1.0 -
ExtensionQuery.SortClause 75 4 0% 3 7
0.00%
ExtensionQuery.Filter 113 13 0% 5 8
0.555555655.6%
 

Contributing tests

This file is covered by 11 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.search;
21   
22    import java.util.ArrayList;
23    import java.util.Collection;
24    import java.util.List;
25   
26    /**
27    * A query to an extension repository.
28    *
29    * @version $Id: 5af22c7e170f583a271af41a812636819409b36c $
30    * @since 7.0M2
31    */
 
32    public class ExtensionQuery
33    {
34    /**
35    * The order in which extensions should be sorted.
36    *
37    * @version $Id: 5af22c7e170f583a271af41a812636819409b36c $
38    */
 
39    public enum ORDER
40    {
41    /**
42    * Descending order.
43    */
44    DESC,
45   
46    /**
47    * Ascending order.
48    */
49    ASC;
50    }
51   
52    /**
53    * The comparison to apply.
54    *
55    * @version $Id: 5af22c7e170f583a271af41a812636819409b36c $
56    */
 
57    public enum COMPARISON
58    {
59    /**
60    * The value is the same than the filter one.
61    */
62    EQUAL,
63   
64    /**
65    * The value contains what is in the filter.
66    */
67    MATCH;
68    }
69   
70    /**
71    * The sort criteria.
72    *
73    * @version $Id: 5af22c7e170f583a271af41a812636819409b36c $
74    */
 
75    public static class SortClause
76    {
77    private final String field;
78   
79    private final ORDER order;
80   
81    /**
82    * @param field the name of the field
83    * @param order the order in which extensions should be sorted
84    */
 
85  0 toggle public SortClause(String field, ORDER order)
86    {
87  0 this.field = field;
88  0 this.order = order;
89    }
90   
91    /**
92    * @return the name of the field
93    */
 
94  0 toggle public String getField()
95    {
96  0 return this.field;
97    }
98   
99    /**
100    * @return the order in which extensions should be sorted.
101    */
 
102  0 toggle public ORDER getOrder()
103    {
104  0 return this.order;
105    }
106    }
107   
108    /**
109    * A filter to apply on an extension field.
110    *
111    * @version $Id: 5af22c7e170f583a271af41a812636819409b36c $
112    */
 
113    public static class Filter
114    {
115    private final String field;
116   
117    private final Object value;
118   
119    private final COMPARISON comparison;
120   
121    /**
122    * @param field the name of the field
123    * @param value the value to compare to
124    * @param comparison the comparison to apply
125    */
 
126  6 toggle public Filter(String field, Object value, COMPARISON comparison)
127    {
128  6 this.field = field;
129  6 this.value = value;
130  6 this.comparison = comparison;
131    }
132   
133    /**
134    * @return the name of the field
135    */
 
136  85 toggle public String getField()
137    {
138  85 return this.field;
139    }
140   
141    /**
142    * @return the value to compare to
143    */
 
144  142 toggle public Object getValue()
145    {
146  142 return this.value;
147    }
148   
149    /**
150    * @return the comparison to apply
151    */
 
152  112 toggle public COMPARISON getComparison()
153    {
154  112 return this.comparison;
155    }
156   
 
157  0 toggle @Override
158    public String toString()
159    {
160  0 StringBuilder builder = new StringBuilder();
161   
162  0 builder.append(getField());
163  0 builder.append(' ');
164  0 builder.append(getComparison());
165  0 builder.append(' ');
166  0 builder.append(getValue());
167   
168  0 return builder.toString();
169    }
170    }
171   
172    private String query;
173   
174    private int limit = -1;
175   
176    private int offset;
177   
178    private List<SortClause> sortClauses = new ArrayList<>();
179   
180    private List<Filter> filters = new ArrayList<>();
181   
182    /**
183    * No filtering. Usually return everything.
184    */
 
185  5 toggle public ExtensionQuery()
186    {
187    }
188   
189    /**
190    * @param query the query to execute
191    */
 
192  109 toggle public ExtensionQuery(String query)
193    {
194  109 this.query = query;
195    }
196   
197    /**
198    * @param query the query to duplicate
199    */
 
200  13 toggle public ExtensionQuery(ExtensionQuery query)
201    {
202  13 this.query = query.getQuery();
203   
204  13 setOffset(query.getOffset());
205  13 setLimit(query.getLimit());
206   
207  13 setSortClauses(query.getSortClauses());
208  13 setFilters(query.getFilters());
209    }
210   
211    /**
212    * @return the query statement
213    */
 
214  117 toggle public String getQuery()
215    {
216  117 return this.query;
217    }
218   
219    /**
220    * @param limit the number of results to return when querying extensions
221    * @return this query.
222    */
 
223  122 toggle public ExtensionQuery setLimit(int limit)
224    {
225  122 this.limit = limit;
226   
227  122 return this;
228    }
229   
230    /**
231    * @param offset offset of query result to set (skip first "offset" rows).
232    * @return this query.
233    */
 
234  122 toggle public ExtensionQuery setOffset(int offset)
235    {
236  122 this.offset = offset;
237   
238  122 return this;
239    }
240   
241    /**
242    * @return the filters
243    */
 
244  545 toggle public List<Filter> getFilters()
245    {
246  545 return this.filters;
247    }
248   
249    /**
250    * @param filters the filters
251    * @return this
252    */
 
253  13 toggle public ExtensionQuery setFilters(Collection<Filter> filters)
254    {
255  13 this.filters = new ArrayList<>(filters);
256   
257  13 return this;
258    }
259   
260    /**
261    * @param field the name of the field
262    * @param value the value to compare to
263    * @param comparison the comparison to apply
264    * @return this
265    */
 
266  5 toggle public ExtensionQuery addFilter(String field, Object value, COMPARISON comparison)
267    {
268  5 this.filters.add(new Filter(field, value, comparison));
269   
270  5 return this;
271    }
272   
273    /**
274    * @param newFilters the filters to add
275    * @return this
276    * @since 8.3RC1
277    */
 
278  13 toggle public ExtensionQuery addFilters(List<Filter> newFilters)
279    {
280  13 this.filters.addAll(newFilters);
281   
282  13 return this;
283    }
284   
285    /**
286    * @return the criteria used to sort the result
287    */
 
288  93 toggle public List<SortClause> getSortClauses()
289    {
290  93 return this.sortClauses;
291    }
292   
293    /**
294    * @param sortClauses the criteria used to sort the result
295    * @return this
296    */
 
297  13 toggle public ExtensionQuery setSortClauses(Collection<SortClause> sortClauses)
298    {
299  13 this.sortClauses = new ArrayList<>(sortClauses);
300   
301  13 return this;
302    }
303   
304    /**
305    * @param field the name of the field
306    * @param order the order in which extensions should be sorted
307    * @return this
308    */
 
309  0 toggle public ExtensionQuery addSort(String field, ORDER order)
310    {
311  0 this.sortClauses.add(new SortClause(field, order));
312   
313  0 return this;
314    }
315   
316    /**
317    * @return limit limit of result list.
318    * @see #setLimit(int)
319    */
 
320  204 toggle public int getLimit()
321    {
322  204 return this.limit;
323    }
324   
325    /**
326    * @return offset offset of query result.
327    * @see #setOffset(int)
328    */
 
329  234 toggle public int getOffset()
330    {
331  234 return this.offset;
332    }
333    }