View Javadoc

1   /*
2    * #%L
3    * License Maven Plugin
4    *
5    * $Id: LicenseMap.java 14409 2011-08-10 15:30:41Z tchemit $
6    * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/model/LicenseMap.java $
7    * %%
8    * Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as
12   * published by the Free Software Foundation, either version 3 of the
13   * License, or (at your option) any later version.
14   *
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   *
20   * You should have received a copy of the GNU General Lesser Public
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
23   * #L%
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   * Map of artifacts (stub in mavenproject) group by their license.
44   *
45   * @author tchemit <chemit@codelutin.com>
46   * @since 1.0
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          // handle multiple values as a set to avoid duplicates
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 }