1 | /* |
---|
2 | * Copyright (C) 2014 CLARIN |
---|
3 | * |
---|
4 | * This program is free software: you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation, either version 3 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | */ |
---|
17 | package eu.clarin.cmdi.vlo.service.impl; |
---|
18 | |
---|
19 | import com.google.common.collect.HashMultiset; |
---|
20 | import com.google.common.collect.Multiset; |
---|
21 | import eu.clarin.cmdi.vlo.CommonUtils; |
---|
22 | import eu.clarin.cmdi.vlo.FacetConstants; |
---|
23 | import eu.clarin.cmdi.vlo.pojo.ResourceType; |
---|
24 | import eu.clarin.cmdi.vlo.pojo.ResourceTypeCount; |
---|
25 | import eu.clarin.cmdi.vlo.service.ResourceTypeCountingService; |
---|
26 | import java.util.ArrayList; |
---|
27 | import java.util.Collection; |
---|
28 | import java.util.Collections; |
---|
29 | import java.util.regex.Pattern; |
---|
30 | |
---|
31 | /** |
---|
32 | * Counts resource types in a resource string collection |
---|
33 | * |
---|
34 | * @author twagoo |
---|
35 | */ |
---|
36 | public class ResourceTypeCountingServiceImpl implements ResourceTypeCountingService { |
---|
37 | |
---|
38 | private final static String SPLIT_PATTERN = Pattern.quote(FacetConstants.FIELD_RESOURCE_SPLIT_CHAR); |
---|
39 | |
---|
40 | @Override |
---|
41 | public Collection<ResourceTypeCount> countResourceTypes(Collection<String> resources) { |
---|
42 | if (resources == null || resources.isEmpty()) { |
---|
43 | return Collections.emptySet(); |
---|
44 | } |
---|
45 | |
---|
46 | final Multiset<ResourceType> countBag = HashMultiset.<ResourceType>create(ResourceType.values().length); |
---|
47 | |
---|
48 | // loop over resources and count types |
---|
49 | for (String resourceString : resources) { |
---|
50 | // split resource string to find mime type |
---|
51 | final String[] tokens = resourceString.split(SPLIT_PATTERN, 2); |
---|
52 | final String mimeType = tokens[0]; |
---|
53 | // normalise |
---|
54 | final String normalizeMimeType = CommonUtils.normalizeMimeType(mimeType); |
---|
55 | // map to ResourceType and add to bag (TODO: normalize to ResourceType directly?) |
---|
56 | if (normalizeMimeType.equals(FacetConstants.RESOURCE_TYPE_ANNOTATION)) { |
---|
57 | countBag.add(ResourceType.ANNOTATION); |
---|
58 | } else if (normalizeMimeType.equals(FacetConstants.RESOURCE_TYPE_AUDIO)) { |
---|
59 | countBag.add(ResourceType.AUDIO); |
---|
60 | } else if (normalizeMimeType.equals(FacetConstants.RESOURCE_TYPE_IMAGE)) { |
---|
61 | countBag.add(ResourceType.IMAGE); |
---|
62 | } else if (normalizeMimeType.equals(FacetConstants.RESOURCE_TYPE_TEXT)) { |
---|
63 | countBag.add(ResourceType.TEXT); |
---|
64 | } else if (normalizeMimeType.equals(FacetConstants.RESOURCE_TYPE_VIDEO)) { |
---|
65 | countBag.add(ResourceType.VIDEO); |
---|
66 | } else{ |
---|
67 | countBag.add(ResourceType.OTHER); |
---|
68 | } |
---|
69 | } |
---|
70 | |
---|
71 | // count items in bag for each resource type |
---|
72 | final Collection<ResourceTypeCount> counts = new ArrayList<ResourceTypeCount>(countBag.elementSet().size()); |
---|
73 | for (ResourceType type : ResourceType.values()) { |
---|
74 | final int count = countBag.count(type); |
---|
75 | // don't add zero counts |
---|
76 | if (count > 0) { |
---|
77 | counts.add(new ResourceTypeCount(type, count)); |
---|
78 | } |
---|
79 | } |
---|
80 | return counts; |
---|
81 | } |
---|
82 | |
---|
83 | } |
---|