001 /**
002 * Copyright 2010-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.codehaus.mojo.license.model;
017
018 import java.util.ArrayList;
019 import java.util.Collections;
020 import java.util.Comparator;
021 import java.util.HashSet;
022 import java.util.List;
023 import java.util.Map;
024 import java.util.Set;
025 import java.util.SortedMap;
026 import java.util.SortedSet;
027 import java.util.TreeMap;
028 import java.util.TreeSet;
029
030 import org.apache.maven.project.MavenProject;
031 import org.codehaus.mojo.license.MojoHelper;
032
033 /**
034 * Map of artifacts (stub in mavenproject) group by their license.
035 *
036 * @author tchemit <chemit@codelutin.com>
037 * @since 1.0
038 */
039 public class LicenseMap extends TreeMap<String, SortedSet<MavenProject>> {
040
041 private static final long serialVersionUID = 864199843545688069L;
042
043 public static final String unknownLicenseMessage = "Unknown license";
044
045 private final Comparator<MavenProject> projectComparator;
046
047 public LicenseMap() {
048 projectComparator = MojoHelper.newMavenProjectComparator();
049 }
050
051 public SortedSet<MavenProject> put(String key, MavenProject value) {
052 // handle multiple values as a set to avoid duplicates
053 SortedSet<MavenProject> valueList = get(key);
054 if (valueList == null) {
055 valueList = new TreeSet<MavenProject>(projectComparator);
056 }
057 valueList.add(value);
058 return put(key, valueList);
059 }
060
061 public SortedMap<MavenProject, String[]> toDependencyMap() {
062 SortedMap<MavenProject, Set<String>> tmp = new TreeMap<MavenProject, Set<String>>(projectComparator);
063
064 for (Map.Entry<String, SortedSet<MavenProject>> entry : entrySet()) {
065 String license = entry.getKey();
066 SortedSet<MavenProject> set = entry.getValue();
067 for (MavenProject p : set) {
068 Set<String> list = tmp.get(p);
069 if (list == null) {
070 list = new HashSet<String>();
071 tmp.put(p, list);
072 }
073 list.add(license);
074 }
075 }
076
077 SortedMap<MavenProject, String[]> result = new TreeMap<MavenProject, String[]>(projectComparator);
078 for (Map.Entry<MavenProject, Set<String>> entry : tmp.entrySet()) {
079 List<String> value = new ArrayList<String>(entry.getValue());
080 Collections.sort(value);
081 result.put(entry.getKey(), value.toArray(new String[value.size()]));
082 }
083 tmp.clear();
084 return result;
085 }
086
087 public static String getUnknownLicenseMessage() {
088 return unknownLicenseMessage;
089 }
090
091 }