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  
53          // handle multiple values as a set to avoid duplicates
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  }