001 /*
002 * #%L
003 * License Maven Plugin
004 *
005 * $Id: LicenseMap.java 14409 2011-08-10 15:30:41Z tchemit $
006 * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/model/LicenseMap.java $
007 * %%
008 * Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
009 * %%
010 * This program is free software: you can redistribute it and/or modify
011 * it under the terms of the GNU Lesser General Public License as
012 * published by the Free Software Foundation, either version 3 of the
013 * License, or (at your option) any later version.
014 *
015 * This program is distributed in the hope that it will be useful,
016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018 * GNU General Lesser Public License for more details.
019 *
020 * You should have received a copy of the GNU General Lesser Public
021 * License along with this program. If not, see
022 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
023 * #L%
024 */
025 package org.codehaus.mojo.license.model;
026
027 import java.util.ArrayList;
028 import java.util.Collections;
029 import java.util.Comparator;
030 import java.util.HashSet;
031 import java.util.List;
032 import java.util.Map;
033 import java.util.Set;
034 import java.util.SortedMap;
035 import java.util.SortedSet;
036 import java.util.TreeMap;
037 import java.util.TreeSet;
038
039 import org.apache.maven.project.MavenProject;
040 import org.codehaus.mojo.license.MojoHelper;
041
042 /**
043 * Map of artifacts (stub in mavenproject) group by their license.
044 *
045 * @author tchemit <chemit@codelutin.com>
046 * @since 1.0
047 */
048 public class LicenseMap extends TreeMap<String, SortedSet<MavenProject>> {
049
050 private static final long serialVersionUID = 864199843545688069L;
051
052 public static final String unknownLicenseMessage = "Unknown license";
053
054 private final Comparator<MavenProject> projectComparator;
055
056 public LicenseMap() {
057 projectComparator = MojoHelper.newMavenProjectComparator();
058 }
059
060 public SortedSet<MavenProject> put(String key, MavenProject value) {
061
062 // handle multiple values as a set to avoid duplicates
063 SortedSet<MavenProject> valueList = get(key);
064 if (valueList == null) {
065
066 valueList = new TreeSet<MavenProject>(projectComparator);
067 }
068
069 valueList.add(value);
070 return put(key, valueList);
071 }
072
073 public SortedMap<MavenProject, String[]> toDependencyMap() {
074 SortedMap<MavenProject, Set<String>> tmp = new TreeMap<MavenProject, Set<String>>(projectComparator);
075
076 for (Map.Entry<String, SortedSet<MavenProject>> entry : entrySet()) {
077 String license = entry.getKey();
078 SortedSet<MavenProject> set = entry.getValue();
079 for (MavenProject p : set) {
080 Set<String> list = tmp.get(p);
081 if (list == null) {
082 list = new HashSet<String>();
083 tmp.put(p, list);
084 }
085 list.add(license);
086 }
087 }
088
089 SortedMap<MavenProject, String[]> result = new TreeMap<MavenProject, String[]>(projectComparator);
090 for (Map.Entry<MavenProject, Set<String>> entry : tmp.entrySet()) {
091 List<String> value = new ArrayList<String>(entry.getValue());
092 Collections.sort(value);
093 result.put(entry.getKey(), value.toArray(new String[value.size()]));
094 }
095 tmp.clear();
096 return result;
097 }
098
099 public static String getUnknownLicenseMessage() {
100 return unknownLicenseMessage;
101 }
102
103 }