org.codehaus.mojo.license.model.LicenseMap Maven / Gradle / Ivy
package org.codehaus.mojo.license.model;
/*
* #%L
* License Maven Plugin
* %%
* Copyright (C) 2008 - 2011 CodeLutin, Codehaus, Tony Chemit
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import org.apache.maven.project.MavenProject;
import org.codehaus.mojo.license.utils.MojoHelper;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* Map of artifacts (stub in mavenproject) group by their license.
*
* @author tchemit [email protected]
* @since 1.0
*/
public class LicenseMap
extends TreeMap>
{
private static final long serialVersionUID = 864199843545688069L;
public static final String UNKNOWN_LICENSE_MESSAGE = "Unknown license";
private final Comparator projectComparator;
/**
* Default contructor.
*/
public LicenseMap()
{
projectComparator = MojoHelper.newMavenProjectComparator();
}
/**
* Store in the license map a project to a given license.
*
* @param key the license on which to associate the gieven project
* @param value project to register in the license map
* @return the set of projects using the given license
*/
public SortedSet put( String key, MavenProject value )
{
// handle multiple values as a set to avoid duplicates
SortedSet valueList = get( key );
if ( valueList == null )
{
valueList = new TreeSet( projectComparator );
}
valueList.add( value );
return put( key, valueList );
}
/**
* Store in the license other licenseMap.
*
* @param licenseMap license map to put
*/
public void putAll( LicenseMap licenseMap)
{
for (Map.Entry> entry : licenseMap.entrySet()) {
String key = entry.getKey();
// handle multiple values as a set to avoid duplicates
SortedSet valueList = get( key );
if ( valueList == null )
{
valueList = new TreeSet( projectComparator );
}
valueList.addAll( entry.getValue() );
put( key, valueList );
}
}
/**
* Build a dependencies map from the license map, this is a map of license for each project registered in the
* license map.
*
* @return the generated dependencies map
*/
public SortedMap toDependencyMap()
{
SortedMap> tmp = new TreeMap>( projectComparator );
for ( Map.Entry> entry : entrySet() )
{
String license = entry.getKey();
SortedSet set = entry.getValue();
for ( MavenProject p : set )
{
Set list = tmp.get( p );
if ( list == null )
{
list = new HashSet();
tmp.put( p, list );
}
list.add( license );
}
}
SortedMap result = new TreeMap( projectComparator );
for ( Map.Entry> entry : tmp.entrySet() )
{
List value = new ArrayList( entry.getValue() );
Collections.sort( value );
result.put( entry.getKey(), value.toArray( new String[value.size()] ) );
}
tmp.clear();
return result;
}
public LicenseMap toLicenseMapOrderByName()
{
LicenseMap result = new LicenseMap();
Comparator mavenProjectComparator = MojoHelper.newMavenProjectComparatorByName();
for ( Map.Entry> entry : entrySet() )
{
String licenseKey = entry.getKey();
SortedSet projects =
new TreeSet( mavenProjectComparator );
projects.addAll( entry.getValue() );
result.put( licenseKey, projects );
}
return result;
}
public void removeProject( MavenProject project )
{
for ( Map.Entry> entry : entrySet() )
{
SortedSet projects = entry.getValue();
for ( MavenProject mavenProject : projects )
{
if (project.equals( mavenProject )) {
get(entry.getKey()).remove( project);
break;
}
}
}
}
public void removeEmptyLicenses() {
Set keys = new HashSet<>(keySet());
for (String key : keys) {
if (get(key).isEmpty()) {
remove(key);
}
}
}
}