View Javadoc

1   /**
2    * Copyright 2010-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Map of artifacts (stub in mavenproject) group by their license.
35   *
36   * @author tchemit <chemit@codelutin.com>
37   * @since 1.0
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          // handle multiple values as a set to avoid duplicates
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  }