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
54 SortedSet<MavenProject> valueList = get(key);
55 if (valueList == null) {
56
57 valueList = new TreeSet<MavenProject>(projectComparator);
58 }
59
60 valueList.add(value);
61 return put(key, valueList);
62 }
63
64 public SortedMap<MavenProject, String[]> toDependencyMap() {
65 SortedMap<MavenProject, Set<String>> tmp = new TreeMap<MavenProject, Set<String>>(projectComparator);
66
67 for (Map.Entry<String, SortedSet<MavenProject>> entry : entrySet()) {
68 String license = entry.getKey();
69 SortedSet<MavenProject> set = entry.getValue();
70 for (MavenProject p : set) {
71 Set<String> list = tmp.get(p);
72 if (list == null) {
73 list = new HashSet<String>();
74 tmp.put(p, list);
75 }
76 list.add(license);
77 }
78 }
79
80 SortedMap<MavenProject, String[]> result = new TreeMap<MavenProject, String[]>(projectComparator);
81 for (Map.Entry<MavenProject, Set<String>> entry : tmp.entrySet()) {
82 List<String> value = new ArrayList<String>(entry.getValue());
83 Collections.sort(value);
84 result.put(entry.getKey(), value.toArray(new String[value.size()]));
85 }
86 tmp.clear();
87 return result;
88 }
89
90 public static String getUnknownLicenseMessage() {
91 return unknownLicenseMessage;
92 }
93
94 }