-
Notifications
You must be signed in to change notification settings - Fork 928
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ARTEMIS-4743 Split lines on ./artemis queue stat for large names
- Loading branch information
1 parent
86f7250
commit c06e8a1
Showing
4 changed files
with
158 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
artemis-commons/src/main/java/org/apache/activemq/artemis/utils/TableOut.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.activemq.artemis.utils; | ||
|
||
import java.io.PrintStream; | ||
import java.util.ArrayList; | ||
|
||
public class TableOut { | ||
|
||
String separator; | ||
int[] columnSizes; | ||
|
||
public TableOut(String separator, int... columnSizes) { | ||
this.separator = separator; | ||
this.columnSizes = columnSizes; | ||
} | ||
|
||
|
||
public void print(PrintStream stream, String... columns) { | ||
ArrayList<String>[] splitColumns = new ArrayList[columns.length]; | ||
for (int i = 0; i < columns.length; i++) { | ||
splitColumns[i] = splitLine(columns[i], columnSizes[i]); | ||
} | ||
|
||
|
||
boolean hasMoreLines; | ||
int lineNumber = 0; | ||
do { | ||
hasMoreLines = false; | ||
stream.print(separator); | ||
for (int column = 0; column < columns.length; column++) { | ||
StringBuilder cell = new StringBuilder(); | ||
if (lineNumber < splitColumns[column].size()) { | ||
cell.append(splitColumns[column].get(lineNumber)); | ||
} | ||
if (lineNumber + 1 < splitColumns[column].size()) { | ||
hasMoreLines = true; | ||
} | ||
while (cell.length() < columnSizes[column]) { | ||
cell.append(" "); | ||
} | ||
stream.print(cell); | ||
stream.print(separator); | ||
} | ||
stream.println(); | ||
lineNumber++; | ||
} while (hasMoreLines); | ||
|
||
} | ||
|
||
public static ArrayList<String> splitLine(final String column, int size) { | ||
ArrayList<String> cells = new ArrayList<>(); | ||
|
||
for (int position = 0; position < column.length();) { | ||
int maxPosition = Math.min(size, column.length() - position); | ||
cells.add(column.substring(position, position + maxPosition)); | ||
position += maxPosition; | ||
} | ||
|
||
return cells; | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
artemis-commons/src/test/java/org/apache/activemq/artemis/utils/TableOutTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.activemq.artemis.utils; | ||
|
||
import java.util.ArrayList; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class TableOutTest { | ||
|
||
@Test | ||
public void testSplitString() { | ||
String bigCell = "1234554321321"; | ||
ArrayList<String> lines = TableOut.splitLine(bigCell, 5); | ||
Assert.assertEquals(3, lines.size()); | ||
Assert.assertEquals("12345", lines.get(0)); | ||
Assert.assertEquals("54321", lines.get(1)); | ||
Assert.assertEquals("321", lines.get(2)); | ||
} | ||
|
||
@Test | ||
public void testOutLine() { | ||
|
||
// the output is visual, however this test is good to make sure the output at least works without any issues | ||
TableOut tableOut = new TableOut("|", 10, 3, 3); | ||
tableOut.print(System.out, "This is a big title", "1234567", "1234"); | ||
} | ||
|
||
} |