2121import java .util .*;
2222
2323import static guru .nidi .codeassert .config .Language .JAVA ;
24+ import static java .util .Collections .singletonList ;
25+ import static java .util .stream .Collectors .toList ;
2426
25- public class ProjectLayout {
26- private final String module ;
27+ public class ProjectLayout < T extends ProjectLayout > {
28+ private final List < String > modules ;
2729 private final EnumSet <Language > languages ;
2830
29- protected ProjectLayout (String module , Language ... languages ) {
30- this .module = module ;
31- this .languages = languages .length == 0 ? EnumSet .of (JAVA ) : EnumSet .of (languages [0 ], languages );
31+ protected ProjectLayout (List <String > modules , EnumSet <Language > languages ) {
32+ this .modules = new ArrayList <>(modules );
33+ this .languages = languages ;
34+ }
35+
36+ protected ProjectLayout (List <String > modules , Language ... languages ) {
37+ this (modules , languages .length == 0 ? EnumSet .of (JAVA ) : EnumSet .of (languages [0 ], languages ));
38+ }
39+
40+ public T modules (String ... modules ) {
41+ if (this .modules .size () > 1 || (this .modules .size () == 1 && this .modules .get (0 ) != null )) {
42+ throw new IllegalStateException ("You already defined modules." );
43+ }
44+ this .modules .clear ();
45+ this .modules .addAll (Arrays .asList (modules ));
46+ return (T ) this ;
3247 }
3348
3449 public EnumSet <Language > getLanguages () {
@@ -38,35 +53,42 @@ public EnumSet<Language> getLanguages() {
3853 protected List <Path > path (String [] packs , String ... paths ) {
3954 final List <Path > res = new ArrayList <>();
4055 for (final String path : paths ) {
41- final String normPath = path (path );
42- if (packs .length == 0 ) {
43- res .add (new Path (normPath , "" ));
44- } else {
45- for (final String pack : packs ) {
46- final String normPack = pack .replace ('.' , '/' );
47- res .add (new Path (normPath , normPack ));
56+ for (final String normPath : paths (path )) {
57+ if (packs .length == 0 ) {
58+ res .add (new Path (normPath , "" ));
59+ } else {
60+ for (final String pack : packs ) {
61+ final String normPack = pack .replace ('.' , '/' );
62+ res .add (new Path (normPath , normPack ));
63+ }
4864 }
4965 }
5066 }
5167 return res ;
5268 }
5369
54- private String path (String relative ) {
55- if (module == null || module . length () == 0 || runningInModuleDir ()) {
56- return relative ;
70+ private List < String > paths (String relative ) {
71+ if (modules . isEmpty ()) {
72+ return singletonList ( relative ) ;
5773 }
58- return module .endsWith ("/" )
59- ? module + relative
60- : module + "/" + relative ;
74+ return modules .stream ().map (module ->
75+ module == null || module .length () == 0 || runningInModuleDir (module )
76+ ? relative
77+ : concat (module , relative )
78+ ).collect (toList ());
79+ }
80+
81+ private String concat (String path1 , String path2 ) {
82+ return path1 .endsWith ("/" ) ? path1 + path2 : path1 + "/" + path2 ;
6183 }
6284
63- private boolean runningInModuleDir () {
85+ private boolean runningInModuleDir (String module ) {
6486 return new File ("" ).getAbsoluteFile ().getName ().equals (module );
6587 }
6688
67- public static class Maven extends ProjectLayout {
89+ public static class Maven extends ProjectLayout < Maven > {
6890 public Maven (String module , Language ... languages ) {
69- super (module , languages );
91+ super (singletonList ( module ) , languages );
7092 }
7193
7294 public AnalyzerConfig main (String ... packages ) {
@@ -86,9 +108,9 @@ public AnalyzerConfig mainAndTest(String... packages) {
86108 }
87109 }
88110
89- public static class Gradle extends ProjectLayout {
111+ public static class Gradle extends ProjectLayout < Gradle > {
90112 public Gradle (String module , Language ... languages ) {
91- super (module , languages );
113+ super (singletonList ( module ) , languages );
92114 }
93115
94116 public AnalyzerConfig main (String ... packages ) {
0 commit comments