Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
SortByCustomOrderFilter | 44 | 11 | 0% | 2 | 0 |
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.Arrays; | |
24 | import java.util.Collections; | |
25 | import java.util.List; | |
26 | ||
27 | import javax.inject.Named; | |
28 | import javax.inject.Singleton; | |
29 | ||
30 | import org.xwiki.component.annotation.Component; | |
31 | import org.xwiki.uiextension.UIExtension; | |
32 | import org.xwiki.uiextension.UIExtensionFilter; | |
33 | ||
34 | /** | |
35 | * Sort a list of {@link UIExtension} by following a custom order strategy. If some extension IDs are not passed through | |
36 | * through {@link #filter(java.util.List, String...)} they will be put at the end of the list. | |
37 | * | |
38 | * @version $Id: 47ee5477ecebfdc4af33e7624e839ce07870096e $ | |
39 | * @since 4.3M1 | |
40 | */ | |
41 | @Component | |
42 | @Named("sortByCustomOrder") | |
43 | @Singleton | |
44 | public class SortByCustomOrderFilter implements UIExtensionFilter | |
45 | { | |
46 | /** | |
47 | * @param extensions The list of {@link UIExtension}s to filter | |
48 | * @param ids The IDs of {@link UIExtension} to display first, in the desired order | |
49 | * @return The filtered list | |
50 | */ | |
51 | 1 | @Override |
52 | public List<UIExtension> filter(List<UIExtension> extensions, String... ids) | |
53 | { | |
54 | 1 | List<String> extensionIds = Arrays.asList(ids); |
55 | 1 | Collections.reverse(extensionIds); |
56 | 1 | List<UIExtension> results = new ArrayList<UIExtension>(); |
57 | 1 | results.addAll(extensions); |
58 | ||
59 | 1 | for (String id : extensionIds) { |
60 | 2 | for (UIExtension extension : results) { |
61 | 8 | if (id.equals(extension.getId())) { |
62 | 2 | results.remove(extension); |
63 | 2 | results.add(0, extension); |
64 | 2 | break; |
65 | } | |
66 | } | |
67 | } | |
68 | 1 | return results; |
69 | } | |
70 | } |