1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.velocity.tools

File CollectionsTool.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

24
50
20
1
347
147
34
0.68
2.5
20
1.7

Classes

Class Line # Actions
CollectionsTool 50 50 0% 34 0
1.0100%
 

Contributing tests

This file is covered by 27 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.velocity.tools;
21   
22    import java.util.ArrayList;
23    import java.util.Collection;
24    import java.util.Collections;
25    import java.util.HashMap;
26    import java.util.HashSet;
27    import java.util.LinkedHashMap;
28    import java.util.LinkedHashSet;
29    import java.util.LinkedList;
30    import java.util.List;
31    import java.util.Map;
32    import java.util.PriorityQueue;
33    import java.util.Queue;
34    import java.util.Set;
35    import java.util.SortedMap;
36    import java.util.SortedSet;
37    import java.util.TreeMap;
38    import java.util.TreeSet;
39    import java.util.concurrent.BlockingQueue;
40    import java.util.concurrent.LinkedBlockingQueue;
41   
42    import org.apache.commons.collections4.CollectionUtils;
43   
44    /**
45    * Velocity Tool allowing to create various type of collections.
46    *
47    * @version $Id: 568f900f240c20fc554ae0038474d51c0271a161 $
48    * @since 4.0M1
49    */
 
50    public class CollectionsTool
51    {
52    /**
53    * Create and return a new {@link ArrayList}, an unbounded list with constant access time and good performance for
54    * most additions at the end of the list, but which performs poorly when deleting items, when inserting a new item
55    * in the list and when appending a new item requires resizing the allocated space.
56    *
57    * @param <E> the type of the elements in the list
58    * @return a new, empty {@link ArrayList}
59    */
 
60  4 toggle public <E> List<E> getArrayList()
61    {
62  4 return new ArrayList<E>();
63    }
64   
65    /**
66    * Create and return a new {@link LinkedList}, optimized for insertion and deletion of items, and for sequential
67    * iteration over the items, but not for quick access to random positions in the list.
68    *
69    * @param <E> the type of the elements in the list
70    * @return a new, empty {@link LinkedList}
71    */
 
72  10 toggle public <E> List<E> getLinkedList()
73    {
74  10 return new LinkedList<E>();
75    }
76   
77    /**
78    * Create and return a new {@link Map}, providing good speed for insertion, retrieval and deletion of items, but
79    * with no guarantees on the order of the map.
80    *
81    * @param <K> the type of keys maintained by this map
82    * @param <V> the type of mapped values
83    * @return a new, empty {@link HashMap}
84    */
 
85  2 toggle public <K, V> Map<K, V> getMap()
86    {
87  2 return new HashMap<K, V>();
88    }
89   
90    /**
91    * Create and return a new {@link SortedMap}, which ensures that iterating the map will always return the entries in
92    * the natural order of the keys.
93    *
94    * @param <K> the type of keys maintained by this map
95    * @param <V> the type of mapped values
96    * @return a new, empty {@link TreeMap}
97    */
 
98  1 toggle public <K extends Comparable<K>, V> SortedMap<K, V> getSortedMap()
99    {
100  1 return new TreeMap<K, V>();
101    }
102   
103    /**
104    * Create and return a new {@link Map}, which ensures that iterating the map will always return the entries in the
105    * same order as they were added.
106    *
107    * @param <K> the type of keys maintained by this map
108    * @param <V> the type of mapped values
109    * @return a new, empty {@link LinkedHashMap}
110    */
 
111  1 toggle public <K, V> Map<K, V> getOrderedMap()
112    {
113  1 return new LinkedHashMap<K, V>();
114    }
115   
116    /**
117    * Create and return a new {@link Set}, providing good speed for insertion, retrieval and deletion of items, but
118    * with no guarantees on the order of the set.
119    *
120    * @param <E> the type of the elements in the set
121    * @return a new, empty {@link HashSet}
122    */
 
123  3021 toggle public <E> Set<E> getSet()
124    {
125  3021 return new HashSet<E>();
126    }
127   
128    /**
129    * Create and return a new {@link SortedSet}, which ensures that iterating the set will always return the entries in
130    * the natural order of the items.
131    *
132    * @param <E> the type of the elements in the set
133    * @return a new, empty {@link TreeSet}
134    */
 
135  2 toggle public <E extends Comparable<E>> SortedSet<E> getSortedSet()
136    {
137  2 return new TreeSet<E>();
138    }
139   
140    /**
141    * Create and return a new {@link Set}, which ensures that iterating the set will always return the entries in the
142    * same order as they were added.
143    *
144    * @param <E> the type of the elements in the set
145    * @return a new, empty {@link LinkedHashSet}
146    */
 
147  1 toggle public <E> Set<E> getOrderedSet()
148    {
149  1 return new LinkedHashSet<E>();
150    }
151   
152    /**
153    * Create and return a new {@link Queue}, an unbounded list where items are ordered in a FIFO (first-in-first-out)
154    * manner.
155    *
156    * @param <E> the type of the elements in the queue
157    * @return a new, empty {@link LinkedList}
158    */
 
159  1 toggle public <E> Queue<E> getQueue()
160    {
161  1 return new LinkedList<E>();
162    }
163   
164    /**
165    * Create and return a new {@link BlockingQueue}, an unbounded queue that additionally supports operations that wait
166    * for the queue to become non-empty when retrieving an element.
167    *
168    * @param <E> the type of the elements in the queue
169    * @return a new, empty {@link BlockingQueue}
170    */
 
171  1 toggle public <E> BlockingQueue<E> getBlockingQueue()
172    {
173  1 return new LinkedBlockingQueue<E>();
174    }
175   
176    /**
177    * Create and return a new {@link Queue}, which instead of the FIFO ordering uses the natural order of the items
178    * added to the queue, so that the retrieved item is always the lowest one. All the items added to this queue must
179    * be non-null and be comparable with the other items in the queue.
180    *
181    * @param <E> the type of the elements in the queue
182    * @return a new, empty {@link PriorityQueue}
183    */
 
184  2 toggle public <E extends Comparable<E>> Queue<E> getPriorityQueue()
185    {
186  2 return new PriorityQueue<E>();
187    }
188   
189    /**
190    * Returns an unmodifiable view of the specified list.
191    *
192    * @param <E> the type of the elements in the list
193    * @param input the list to wrap in an unmodifiable bridge
194    * @return an unmodifiable view of the list
195    */
 
196  4 toggle public <E> List<E> unmodifiable(List<E> input)
197    {
198  4 if (input == null) {
199  1 return null;
200    }
201  3 return Collections.unmodifiableList(input);
202    }
203   
204    /**
205    * Returns an unmodifiable view of the specified map.
206    *
207    * @param <K> the type of keys maintained by this map
208    * @param <V> the type of mapped values
209    * @param input the map to wrap in an unmodifiable bridge
210    * @return an unmodifiable view of the map
211    */
 
212  2 toggle public <K, V> Map<K, V> unmodifiable(Map<K, V> input)
213    {
214  2 if (input == null) {
215  1 return null;
216    }
217  1 return Collections.unmodifiableMap(input);
218    }
219   
220    /**
221    * Returns an unmodifiable view of the specified set.
222    *
223    * @param <E> the type of the elements in the set
224    * @param input the set to wrap in an unmodifiable bridge
225    * @return an unmodifiable view of the set
226    */
 
227  2 toggle public <E> Set<E> unmodifiable(Set<E> input)
228    {
229  2 if (input == null) {
230  1 return null;
231    }
232  1 return Collections.unmodifiableSet(input);
233    }
234   
235    /**
236    * Returns an unmodifiable view of the specified collection.
237    *
238    * @param <E> the type of the elements in the collection
239    * @param input the collection to wrap in an unmodifiable bridge
240    * @return an unmodifiable view of the collection
241    */
 
242  2 toggle public <E> Collection<E> unmodifiable(Collection<E> input)
243    {
244  2 if (input == null) {
245  1 return null;
246    }
247  1 return Collections.unmodifiableCollection(input);
248    }
249   
250    /**
251    * Returns a {@link Collection} containing the union of the given {@link Collection}s.
252    *
253    * @param <E> the type of the elements in the collection
254    * @param a the first collection, must be non-null
255    * @param b the second collection, must be non-null
256    * @return the union of the two collections
257    */
 
258  5 toggle public <E> Collection<E> union(Collection<E> a, Collection<E> b)
259    {
260  5 if (a == null) {
261  2 return b;
262  3 } else if (b == null) {
263  1 return a;
264    }
265  2 return CollectionUtils.union(a, b);
266    }
267   
268    /**
269    * Returns a {@link Collection} containing the intersection of the given {@link Collection}s.
270    *
271    * @param <E> the type of the elements in the collection
272    * @param a the first collection, must be non-null
273    * @param b the second collection, must be non-null
274    * @return the intersection of the two collections
275    */
 
276  5 toggle public <E> Collection<E> intersection(Collection<E> a, Collection<E> b)
277    {
278  5 if (a == null) {
279  2 return b;
280  3 } else if (b == null) {
281  1 return a;
282    }
283  2 return CollectionUtils.intersection(a, b);
284    }
285   
286    /**
287    * Returns a {@link Collection} containing the exclusive disjunction (symmetric difference) of the given
288    * {@link Collection}s.
289    *
290    * @param <E> the type of the elements in the collection
291    * @param a the first collection, must be non-null
292    * @param b the second collection, must be non-null
293    * @return the symmetric difference of the two collections
294    */
 
295  5 toggle public <E> Collection<E> disjunction(Collection<E> a, Collection<E> b)
296    {
297  5 if (a == null) {
298  2 return b;
299  3 } else if (b == null) {
300  1 return a;
301    }
302  2 return CollectionUtils.disjunction(a, b);
303    }
304   
305    /**
306    * Reverse the order of the elements within a list, so that the last element is moved to the beginning of the list,
307    * the next-to-last element to the second position, and so on. The input list is modified in place, so this
308    * operation will succeed only if the list is modifiable.
309    *
310    * @param <E> the type of the elements in the list
311    * @param input the list to reverse
312    * @return {@code true} if the list was successfully reversed, {@code false} otherwise
313    */
 
314  359 toggle public <E> boolean reverse(List<E> input)
315    {
316  359 if (input == null) {
317  1 return false;
318    }
319  358 try {
320  358 Collections.reverse(input);
321  357 return true;
322    } catch (UnsupportedOperationException ex) {
323  1 return false;
324    }
325    }
326   
327    /**
328    * Sort the elements within a list according to their natural order. The input list is modified in place, so this
329    * operation will succeed only if the list is modifiable.
330    *
331    * @param <E> the type of the elements in the list
332    * @param input the list to sort
333    * @return {@code true} if the list was successfully sorted, {@code false} otherwise
334    */
 
335  3 toggle public <E extends Comparable<E>> boolean sort(List<E> input)
336    {
337  3 if (input == null) {
338  1 return false;
339    }
340  2 try {
341  2 Collections.sort(input);
342  1 return true;
343    } catch (UnsupportedOperationException ex) {
344  1 return false;
345    }
346    }
347    }