1 package com.simpligility.maven.plugins.android;
2
3 import java.util.Collection;
4 import java.util.List;
5
6 import com.android.annotations.NonNull;
7 import com.android.annotations.Nullable;
8 import com.google.common.base.Function;
9 import com.google.common.base.Predicate;
10 import com.google.common.base.Splitter;
11 import org.apache.maven.artifact.Artifact;
12
13 import static com.google.common.collect.FluentIterable.from;
14
15 public class InclusionExclusionResolver
16 {
17
18 private InclusionExclusionResolver()
19 {
20 }
21
22
23
24
25
26
27
28
29
30
31
32
33 public static Collection< Artifact > filterArtifacts( @NonNull Iterable< Artifact > artifacts,
34 final boolean skipDependencies, @Nullable final Collection< String > includeArtifactTypes,
35 @Nullable final Collection< String > excludeArtifactTypes,
36 @Nullable final Collection< String > includeArtifactQualifiers,
37 @Nullable final Collection< String > excludeArtifactQualifiers )
38 {
39 final boolean hasIncludeTypes = includeArtifactTypes != null;
40 final boolean hasExcludeTypes = excludeArtifactTypes != null;
41 final boolean hasIncludeQualifier = includeArtifactQualifiers != null;
42 final boolean hasExcludeQualifier = excludeArtifactQualifiers != null;
43 return from( artifacts )
44 .filter( new Predicate<Artifact>() {
45 @Override
46 public boolean apply( Artifact artifact )
47 {
48 final boolean includedByType = hasIncludeTypes
49 && includeArtifactTypes.contains( artifact.getType() );
50 final boolean includedByQualifier = hasIncludeQualifier
51 && match( artifact, includeArtifactQualifiers );
52 final boolean excludedByType = hasExcludeTypes
53 && excludeArtifactTypes.contains( artifact.getType() );
54 final boolean excludedByQualifier = hasExcludeQualifier
55 && match( artifact, excludeArtifactQualifiers );
56 if ( !skipDependencies )
57 {
58 return !excludedByType && !excludedByQualifier
59 || includedByQualifier
60 || includedByType && !excludedByQualifier;
61 }
62 else
63 {
64 return includedByQualifier
65 || includedByType && hasExcludeQualifier && !excludedByQualifier
66 || includedByType;
67 }
68 }
69 } )
70 .toSet();
71 }
72
73 private static boolean match( final Artifact artifact, Iterable< String > artifactQualifiers )
74 {
75 return from( artifactQualifiers )
76 .filter( MUST_NOT_BE_BLANK )
77 .anyMatch( new Predicate< String >() {
78 @Override
79 public boolean apply( String artifactQualifier )
80 {
81 return match( artifact, artifactQualifier );
82 }
83 } );
84 }
85
86 private static boolean match( Artifact artifact, String artifactQualifier )
87 {
88 final List< String > split = from( COLON_SPLITTER.split( artifactQualifier ) ).transform( TRIMMER ).toList();
89 final int count = split.size();
90 if ( split.isEmpty() || count > 3 )
91 {
92 throw new IllegalArgumentException( "Invalid artifact qualifier: " + artifactQualifier );
93 }
94
95 final String groupId = split.get( 0 );
96 if ( !groupId.equals( artifact.getGroupId() ) )
97 {
98 return false;
99 }
100 if ( count == 1 )
101 {
102 return true;
103 }
104
105 final String artifactId = split.get( 1 );
106 if ( !artifactId.equals( artifact.getArtifactId() ) )
107 {
108 return false;
109 }
110 if ( count == 2 )
111 {
112 return true;
113 }
114
115 final String version = split.get( 2 );
116 return version.equals( artifact.getVersion() );
117 }
118
119 private static final Splitter COLON_SPLITTER = Splitter.on( ':' );
120
121 private static final Function< String, String > TRIMMER = new Function< String, String >()
122 {
123 @Override
124 public String apply( String value )
125 {
126 return value.trim();
127 }
128 };
129
130 private static final Predicate< String > MUST_NOT_BE_BLANK = new Predicate< String >()
131 {
132 @Override
133 public boolean apply( String value )
134 {
135 return !value.trim().isEmpty();
136 }
137 };
138
139 }