Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Commit 0f4ae93

Browse files
author
ritvik
committed
Extract table-building logic into TableUtils class
1 parent 0697f5f commit 0f4ae93

File tree

2 files changed

+55
-19
lines changed

2 files changed

+55
-19
lines changed

src/main/java/org/springframework/cli/command/RoleCommands.java

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,15 @@
2121
import java.nio.file.Path;
2222
import java.util.List;
2323
import java.util.Map;
24-
import java.util.stream.Collectors;
25-
import java.util.stream.Stream;
2624

2725
import org.springframework.beans.factory.annotation.Autowired;
2826
import org.springframework.cli.SpringCliException;
2927
import org.springframework.cli.roles.RoleService;
28+
import org.springframework.cli.util.TableUtils;
3029
import org.springframework.cli.util.TerminalMessage;
3130
import org.springframework.shell.command.annotation.Command;
3231
import org.springframework.shell.command.annotation.Option;
33-
import org.springframework.shell.table.ArrayTableModel;
34-
import org.springframework.shell.table.BorderStyle;
3532
import org.springframework.shell.table.Table;
36-
import org.springframework.shell.table.TableBuilder;
37-
import org.springframework.shell.table.TableModel;
3833
import org.springframework.util.StringUtils;
3934

4035
@Command(command = "role", group = "Role")
@@ -129,19 +124,7 @@ public void roleGet(@Option(description = "Property key", required = true) Strin
129124
public Table roleList() {
130125
File directory = this.roleService.getRolesVarPath();
131126
List<String> rolesNames = this.roleService.getRoleNames(directory);
132-
Stream<String[]> header = Stream.<String[]>of(new String[] { "Name" });
133-
Stream<String[]> rows;
134-
if (rolesNames != null) {
135-
rows = rolesNames.stream().map(tr -> new String[] { tr });
136-
}
137-
else {
138-
rows = Stream.empty();
139-
}
140-
List<String[]> allRows = rows.collect(Collectors.toList());
141-
String[][] data = Stream.concat(header, allRows.stream()).toArray(String[][]::new);
142-
TableModel model = new ArrayTableModel(data);
143-
TableBuilder tableBuilder = new TableBuilder(model);
144-
return tableBuilder.addFullBorder(BorderStyle.fancy_light).build();
127+
return TableUtils.buildTable(rolesNames, "Name");
145128
}
146129

147130
public RoleService getRoleService() {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
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+
* https://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+
17+
package org.springframework.cli.util;
18+
19+
import java.util.List;
20+
import java.util.stream.Stream;
21+
22+
import org.springframework.shell.table.ArrayTableModel;
23+
import org.springframework.shell.table.BorderStyle;
24+
import org.springframework.shell.table.Table;
25+
import org.springframework.shell.table.TableBuilder;
26+
import org.springframework.shell.table.TableModel;
27+
28+
/**
29+
* Utility class for building tables to display data in the CLI.
30+
*/
31+
public final class TableUtils {
32+
33+
// Private constructor to prevent instantiation
34+
private TableUtils() {
35+
throw new AssertionError("Utility class - do not instantiate");
36+
}
37+
38+
/**
39+
* Builds a table with a single-column layout for displaying a list of items.
40+
* @param data the list of items to display in the table
41+
* @param header the header for the table column
42+
* @return a formatted Table object
43+
*/
44+
public static Table buildTable(List<String> data, String header) {
45+
Stream<String[]> headerStream = Stream.<String[]>of(new String[] { header });
46+
Stream<String[]> rows = (data != null) ? data.stream().map(item -> new String[] { item }) : Stream.empty();
47+
String[][] tableData = Stream.concat(headerStream, rows).toArray(String[][]::new);
48+
TableModel model = new ArrayTableModel(tableData);
49+
TableBuilder tableBuilder = new TableBuilder(model);
50+
return tableBuilder.addFullBorder(BorderStyle.fancy_light).build();
51+
}
52+
53+
}

0 commit comments

Comments
 (0)