View Javadoc
1   /*
2    * Copyright (C) 2012 Jean-Christophe Gay (contact@jeanchristophegay.com)
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  }