-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSummaryHelper.java
More file actions
93 lines (83 loc) · 2.61 KB
/
SummaryHelper.java
File metadata and controls
93 lines (83 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.mindee.parsing;
import com.mindee.parsing.standard.LineItemField;
import java.text.DecimalFormat;
import java.util.List;
import java.util.stream.Collectors;
/**
* Various static methods to help generate the prediction summaries.
*/
public final class SummaryHelper {
private SummaryHelper() {
throw new IllegalStateException("Utility class");
}
public static String cleanSummary(String summaryToClean) {
return summaryToClean.replace(String.format(" %n"), String.format("%n"));
}
public static String formatAmount(Double amountValue) {
if (amountValue == null) {
return "";
}
DecimalFormat df = new DecimalFormat("0.00###");
df.setMinimumFractionDigits(2);
df.setMaximumFractionDigits(5);
return df.format(amountValue);
}
public static String formatString(String str) {
return str != null ? str : "";
}
/**
* Given a list of fields, return a string.
*/
public static <T> String arrayToString(List<T> list, String delimiter) {
return list.stream()
.map(T::toString)
.collect(Collectors.joining(String.format(delimiter)));
}
public static <T extends LineItemField> String arrayToString(List<T> lineItems, int[] columnSizes) {
return lineItems.stream()
.map(T::toTableLine)
.collect(Collectors.joining(
String.format("%n%s%n ", SummaryHelper.lineSeparator(columnSizes, "-")))
);
}
/**
* Truncates a string if it's too long for the requested width.
*/
public static String formatForDisplay(String inputValue, Integer maxColSize) {
if (inputValue == null || inputValue.isEmpty()) {
return "";
}
String outputValue = inputValue
.replace("\n", "\\n")
.replace("\t", "\\t")
.replace("\r", "\\r");
if (maxColSize == null || outputValue.length() <= maxColSize) {
return outputValue;
} else {
return outputValue.substring(0, maxColSize - 3) + "...";
}
}
/**
* Truncates a boolean string if it's too long for the requested width.
*/
public static String formatForDisplay(Boolean inputValue, Integer maxColSize) {
if (inputValue == null) {
return "";
}
if (maxColSize == null || 3 <= maxColSize) {
return inputValue ? "True" : "False";
} else {
return inputValue ? "Y" : "N";
}
}
/**
* Format an rST table line separator.
*/
public static String lineSeparator(int[] columnSizes, String str) {
StringBuilder outStr = new StringBuilder(" +");
for (int size : columnSizes) {
outStr.append(String.format("%" + size + "s+", "").replace(" ", str));
}
return outStr.toString();
}
}