|
20 | 20 | import lombok.RequiredArgsConstructor;
|
21 | 21 | import org.springframework.stereotype.Component;
|
22 | 22 |
|
| 23 | +import java.util.List; |
23 | 24 | import java.util.ArrayList;
|
24 | 25 | import java.util.Comparator;
|
25 |
| -import java.util.List; |
26 |
| -import java.util.Optional; |
27 | 26 |
|
| 27 | +/** |
| 28 | + * Calculates and returns the base package when given a list of JavaSource |
| 29 | + * |
| 30 | + * @author Soumya Prakash Behera |
| 31 | + */ |
28 | 32 | @Component
|
29 | 33 | @RequiredArgsConstructor
|
30 | 34 | public class BasePackageCalculator {
|
31 | 35 |
|
32 | 36 | private final SbmApplicationProperties sbmApplicationProperties;
|
33 | 37 |
|
34 | 38 | public String calculateBasePackage(List<JavaSource> javaSources) {
|
35 |
| - if(javaSources.isEmpty()) return sbmApplicationProperties.getDefaultBasePackage(); |
| 39 | + if(javaSources.isEmpty()) { |
| 40 | + return sbmApplicationProperties.getDefaultBasePackage(); |
| 41 | + } |
| 42 | + |
| 43 | + if(javaSources.size() == 1) { |
| 44 | + return javaSources.get(0).getPackageName(); |
| 45 | + } |
| 46 | + |
| 47 | + List<String[]> javaSourcesAsStringArray = new ArrayList<>(); |
| 48 | + |
| 49 | + for(JavaSource javaSource : javaSources) { |
| 50 | + javaSourcesAsStringArray.add(javaSource.getPackageName().split("\\.")); |
| 51 | + } |
| 52 | + |
| 53 | + javaSourcesAsStringArray.sort(Comparator.comparingInt(str -> str.length)); |
36 | 54 |
|
37 |
| - List<JavaSource> sortableJavaSources = new ArrayList<>(); |
38 |
| - sortableJavaSources.addAll(javaSources); |
39 |
| - javaSources = sortableJavaSources; |
40 |
| - javaSources.sort(Comparator.comparing(js -> js.getPackageName().split("\\.").length)); |
41 |
| - JavaSource javaSourceInBasePackage = javaSources.get(0); |
42 |
| - if(javaSources.size() > 1) { |
43 |
| - JavaSource shortestPackage = javaSourceInBasePackage; |
44 |
| - String shortestPackageName = shortestPackage.getPackageName(); |
| 55 | + String[] shortestPackage = javaSourcesAsStringArray.get(0); |
45 | 56 |
|
46 |
| - Optional<JavaSource> javaSourceInDifferentBasePackage = javaSources.stream() |
47 |
| - .filter(js -> !js.getPackageName().startsWith(shortestPackageName)) |
48 |
| - .findFirst(); |
| 57 | + StringBuilder sb = new StringBuilder(); |
49 | 58 |
|
50 |
| - if(javaSourceInDifferentBasePackage.isPresent()) { |
51 |
| - return sbmApplicationProperties.getDefaultBasePackage(); |
52 |
| - /* |
53 |
| - throw new RuntimeException(String.format("Could not calculate base package. Found at least two conflicting candidates: [%s] and [%s] found in these resources [%s] and [%s]", |
54 |
| - javaSources.get(0).getPackageName(), |
55 |
| - javaSourceInDifferentBasePackage.get().getPackageName(), |
56 |
| - javaSources.get(0).getAbsolutePath(), |
57 |
| - javaSourceInDifferentBasePackage.get().getAbsolutePath())); |
58 |
| - */ |
| 59 | + for(int i = 0; i < shortestPackage.length; i++) { |
| 60 | + for (String[] strArray : javaSourcesAsStringArray) { |
| 61 | + if(!strArray[i].equals(shortestPackage[i])) { |
| 62 | + return sb.isEmpty() ? "" : sb.substring(1); |
| 63 | + } |
59 | 64 | }
|
| 65 | + sb.append(".").append(shortestPackage[i]); |
60 | 66 | }
|
61 | 67 |
|
62 |
| - return javaSourceInBasePackage.getPackageName(); |
| 68 | + return String.join(".", shortestPackage); |
63 | 69 | }
|
64 | 70 |
|
65 | 71 | }
|
0 commit comments