001 /** 002 * Copyright 2010-2012 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 053 // handle multiple values as a set to avoid duplicates 054 SortedSet<MavenProject> valueList = get(key); 055 if (valueList == null) { 056 057 valueList = new TreeSet<MavenProject>(projectComparator); 058 } 059 060 valueList.add(value); 061 return put(key, valueList); 062 } 063 064 public SortedMap<MavenProject, String[]> toDependencyMap() { 065 SortedMap<MavenProject, Set<String>> tmp = new TreeMap<MavenProject, Set<String>>(projectComparator); 066 067 for (Map.Entry<String, SortedSet<MavenProject>> entry : entrySet()) { 068 String license = entry.getKey(); 069 SortedSet<MavenProject> set = entry.getValue(); 070 for (MavenProject p : set) { 071 Set<String> list = tmp.get(p); 072 if (list == null) { 073 list = new HashSet<String>(); 074 tmp.put(p, list); 075 } 076 list.add(license); 077 } 078 } 079 080 SortedMap<MavenProject, String[]> result = new TreeMap<MavenProject, String[]>(projectComparator); 081 for (Map.Entry<MavenProject, Set<String>> entry : tmp.entrySet()) { 082 List<String> value = new ArrayList<String>(entry.getValue()); 083 Collections.sort(value); 084 result.put(entry.getKey(), value.toArray(new String[value.size()])); 085 } 086 tmp.clear(); 087 return result; 088 } 089 090 public static String getUnknownLicenseMessage() { 091 return unknownLicenseMessage; 092 } 093 094 }