1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.mojo.license.model;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.Comparator;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.SortedMap;
26 import java.util.SortedSet;
27 import java.util.TreeMap;
28 import java.util.TreeSet;
29
30 import org.apache.maven.project.MavenProject;
31 import org.codehaus.mojo.license.MojoHelper;
32
33
34
35
36
37
38
39 public class LicenseMap extends TreeMap<String, SortedSet<MavenProject>> {
40
41 private static final long serialVersionUID = 864199843545688069L;
42
43 public static final String unknownLicenseMessage = "Unknown license";
44
45 private final Comparator<MavenProject> projectComparator;
46
47 public LicenseMap() {
48 projectComparator = MojoHelper.newMavenProjectComparator();
49 }
50
51 public SortedSet<MavenProject> put(String key, MavenProject value) {
52
53 SortedSet<MavenProject> valueList = get(key);
54 if (valueList == null) {
55 valueList = new TreeSet<MavenProject>(projectComparator);
56 }
57 valueList.add(value);
58 return put(key, valueList);
59 }
60
61 public SortedMap<MavenProject, String[]> toDependencyMap() {
62 SortedMap<MavenProject, Set<String>> tmp = new TreeMap<MavenProject, Set<String>>(projectComparator);
63
64 for (Map.Entry<String, SortedSet<MavenProject>> entry : entrySet()) {
65 String license = entry.getKey();
66 SortedSet<MavenProject> set = entry.getValue();
67 for (MavenProject p : set) {
68 Set<String> list = tmp.get(p);
69 if (list == null) {
70 list = new HashSet<String>();
71 tmp.put(p, list);
72 }
73 list.add(license);
74 }
75 }
76
77 SortedMap<MavenProject, String[]> result = new TreeMap<MavenProject, String[]>(projectComparator);
78 for (Map.Entry<MavenProject, Set<String>> entry : tmp.entrySet()) {
79 List<String> value = new ArrayList<String>(entry.getValue());
80 Collections.sort(value);
81 result.put(entry.getKey(), value.toArray(new String[value.size()]));
82 }
83 tmp.clear();
84 return result;
85 }
86
87 public static String getUnknownLicenseMessage() {
88 return unknownLicenseMessage;
89 }
90
91 }