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