1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.mojo.buildplan.display;
17
18 import static org.codehaus.plexus.util.StringUtils.defaultString;
19
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.EnumMap;
23 import java.util.Map;
24 import org.apache.maven.lifecycle.DefaultLifecycles;
25 import org.apache.maven.lifecycle.Lifecycle;
26 import org.apache.maven.plugin.MojoExecution;
27 import org.codehaus.mojo.buildplan.util.LinkedMultimap;
28 import org.codehaus.mojo.buildplan.util.Multimap;
29
30 public abstract class AbstractTableDescriptor implements TableDescriptor {
31
32 protected static Map<TableColumn, Integer> findMaxSize(
33 Collection<MojoExecution> executions, DefaultLifecycles defaultLifecycles, TableColumn... columns) {
34
35 Map<TableColumn, Integer> result = new EnumMap<>(TableColumn.class);
36
37 Multimap<TableColumn, Integer> count = new LinkedMultimap<>();
38 executions.stream().map(MojoExecutionDisplay::new).forEach(execution -> {
39 for (TableColumn column : columns) {
40 switch (column) {
41 case ARTIFACT_ID:
42 count.put(column, safeLength(execution.getArtifactId()));
43 break;
44 case EXECUTION_ID:
45 count.put(column, safeLength(execution.getExecutionId()));
46 break;
47 case GOAL:
48 count.put(column, safeLength(execution.getGoal()));
49 break;
50 case PHASE:
51 count.put(column, safeLength(execution.getPhase()));
52 break;
53 case LIFECYCLE:
54 Lifecycle lifecycle = defaultLifecycles.get(execution.getPhase());
55 count.put(column, lifecycle == null ? 0 : safeLength(lifecycle.getId()));
56 break;
57 case VERSION:
58 count.put(column, safeLength(execution.getVersion()));
59 break;
60 }
61 }
62 });
63 for (TableColumn column : TableColumn.values()) {
64 count.put(column, column.title().length());
65 }
66
67 for (TableColumn key : count.keySet()) {
68 result.put(key, Collections.max(count.get(key)));
69 }
70
71 return result;
72 }
73
74 private static int safeLength(String string) {
75 return defaultString(string).length();
76 }
77 }