1 package org.apache.maven.plugin.ear.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.apache.maven.plugin.ear.EarModule;
28
29
30
31
32
33
34
35
36
37
38 public class ModuleIdentifierValidator
39 {
40
41 private List<EarModule> earModules;
42
43 private Map<String, List<EarModule>> result;
44
45
46
47
48 public ModuleIdentifierValidator( List<EarModule> earModules )
49 {
50 if ( earModules == null )
51 {
52 throw new IllegalArgumentException( "Not allowed to give null for earModules." );
53 }
54 this.earModules = earModules;
55 this.result = new HashMap<String, List<EarModule>>();
56 }
57
58
59
60
61
62
63 public boolean existDuplicateArtifacts()
64 {
65 return !result.isEmpty();
66 }
67
68
69
70
71
72
73 public ModuleIdentifierValidator checkForDuplicateArtifacts()
74 {
75 analyze();
76 return this;
77 }
78
79 private void analyze()
80 {
81 final Map<String, List<EarModule>> newList = new HashMap<String, List<EarModule>>();
82
83 for ( EarModule earModule : earModules )
84 {
85 String earId = earModule.getArtifact().getArtifactId() + ":" + earModule.getArtifact().getVersion();
86
87 if ( newList.containsKey( earId ) )
88 {
89 newList.get( earId ).add( earModule );
90 }
91 else
92 {
93 List<EarModule> list = new ArrayList<EarModule>();
94 list.add( earModule );
95 newList.put( earId, list );
96 }
97 }
98
99 result.clear();
100 for ( Map.Entry<String, List<EarModule>> item : newList.entrySet() )
101 {
102 if ( item.getValue().size() > 1 )
103 {
104 result.put( item.getKey(), item.getValue() );
105 }
106 }
107
108 }
109
110
111
112
113 public Map<String, List<EarModule>> getDuplicateArtifacts()
114 {
115 return result;
116 }
117
118
119
120
121 public List<EarModule> getEarModules()
122 {
123 return earModules;
124 }
125
126
127
128
129
130 public ModuleIdentifierValidator setEarModules( List<EarModule> paramEarModules )
131 {
132 if ( paramEarModules == null )
133 {
134 throw new IllegalArgumentException( "Not allowed to give null for earModules." );
135 }
136 this.earModules = paramEarModules;
137 return this;
138 }
139 }