-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve response and request error handling.
- Loading branch information
Showing
5 changed files
with
186 additions
and
11 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
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
127 changes: 127 additions & 0 deletions
127
src/main/java/io/sinistral/proteus/utilities/TablePrinter.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,127 @@ | ||
/** | ||
* | ||
*/ | ||
package io.sinistral.proteus.utilities; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* @author jbauer | ||
*/ | ||
public class TablePrinter | ||
{ | ||
private final int TABLEPADDING = 4; | ||
|
||
|
||
private List<String> headers; | ||
private List<List<String>> table; | ||
private List<Integer> maxLength; | ||
|
||
public TablePrinter(List<String> headersIn, List<List<String>> content) | ||
{ | ||
this.headers = headersIn; | ||
this.maxLength = new ArrayList<Integer>(); | ||
for (int i = 0; i < headers.size(); i++) | ||
{ | ||
maxLength.add(headers.get(i).length()); | ||
} | ||
this.table = content; | ||
|
||
updateMaxLengths(); | ||
} | ||
|
||
public void updateField(int row, int col, String input) | ||
{ | ||
table.get(row).set(col, input); | ||
updateMaxColumnLength(col); | ||
} | ||
|
||
|
||
public String toString() | ||
{ | ||
StringBuilder sb = new StringBuilder(); | ||
StringBuilder rowSeparatorBuilder = new StringBuilder(); | ||
String padder = ""; | ||
String rowSeperator = ""; | ||
|
||
for (int i = 0; i < 4; i++) | ||
{ | ||
padder += " "; | ||
} | ||
|
||
for (int i = 0; i < maxLength.size(); i++) | ||
{ | ||
for (int j = 0; j < maxLength.get(i) + (TABLEPADDING * 2); j++) | ||
{ | ||
rowSeparatorBuilder.append("-"); | ||
} | ||
} | ||
|
||
rowSeperator = rowSeparatorBuilder.toString(); | ||
|
||
sb.append("\n"); | ||
|
||
for (int i = 0; i < headers.size(); i++) | ||
{ | ||
sb.append(padder); | ||
sb.append(headers.get(i)); | ||
for (int k = 0; k < (maxLength.get(i) - headers.get(i).length()); k++) | ||
{ | ||
sb.append(" "); | ||
} | ||
sb.append(padder); | ||
} | ||
|
||
sb.append("\n"); | ||
sb.append(rowSeperator); | ||
sb.append("\n"); | ||
|
||
for (int i = 0; i < table.size(); i++) | ||
{ | ||
List<String> tempRow = table.get(i); | ||
for (int j = 0; j < tempRow.size(); j++) | ||
{ | ||
sb.append(padder); | ||
sb.append(tempRow.get(j)); | ||
for (int k = 0; k < (maxLength.get(j) - tempRow.get(j).length()); k++) | ||
{ | ||
sb.append(" "); | ||
} | ||
sb.append(padder); | ||
} | ||
sb.append("\n"); | ||
} | ||
|
||
return sb.toString(); | ||
} | ||
|
||
private void updateMaxLengths() | ||
{ | ||
for (int i = 0; i < table.size(); i++) | ||
{ | ||
List<String> temp = table.get(i); | ||
for (int j = 0; j < temp.size(); j++) | ||
{ | ||
if (temp.get(j).length() > maxLength.get(j)) | ||
{ | ||
maxLength.set(j, temp.get(j).length()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void updateMaxColumnLength(int col) | ||
{ | ||
for (int i = 0; i < table.size(); i++) | ||
{ | ||
if (table.get(i).get(col).length() > maxLength.get(col)) | ||
{ | ||
maxLength.set(col, table.get(i).get(col).length()); | ||
} | ||
} | ||
} | ||
|
||
} |