21
21
import java .util .*;
22
22
23
23
import static guru .nidi .codeassert .config .Language .JAVA ;
24
+ import static java .util .Collections .singletonList ;
25
+ import static java .util .stream .Collectors .toList ;
24
26
25
- public class ProjectLayout {
26
- private final String module ;
27
+ public class ProjectLayout < T extends ProjectLayout > {
28
+ private final List < String > modules ;
27
29
private final EnumSet <Language > languages ;
28
30
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 ;
32
47
}
33
48
34
49
public EnumSet <Language > getLanguages () {
@@ -38,35 +53,42 @@ public EnumSet<Language> getLanguages() {
38
53
protected List <Path > path (String [] packs , String ... paths ) {
39
54
final List <Path > res = new ArrayList <>();
40
55
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
+ }
48
64
}
49
65
}
50
66
}
51
67
return res ;
52
68
}
53
69
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 ) ;
57
73
}
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 ;
61
83
}
62
84
63
- private boolean runningInModuleDir () {
85
+ private boolean runningInModuleDir (String module ) {
64
86
return new File ("" ).getAbsoluteFile ().getName ().equals (module );
65
87
}
66
88
67
- public static class Maven extends ProjectLayout {
89
+ public static class Maven extends ProjectLayout < Maven > {
68
90
public Maven (String module , Language ... languages ) {
69
- super (module , languages );
91
+ super (singletonList ( module ) , languages );
70
92
}
71
93
72
94
public AnalyzerConfig main (String ... packages ) {
@@ -86,9 +108,9 @@ public AnalyzerConfig mainAndTest(String... packages) {
86
108
}
87
109
}
88
110
89
- public static class Gradle extends ProjectLayout {
111
+ public static class Gradle extends ProjectLayout < Gradle > {
90
112
public Gradle (String module , Language ... languages ) {
91
- super (module , languages );
113
+ super (singletonList ( module ) , languages );
92
114
}
93
115
94
116
public AnalyzerConfig main (String ... packages ) {
0 commit comments