View Javadoc
1   /*
2    * CSVeed (https://github.com/42BV/CSVeed)
3    *
4    * Copyright 2013-2023 CSVeed.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of The Apache Software License,
8    * Version 2.0 which accompanies this distribution, and is available at
9    * https://www.apache.org/licenses/LICENSE-2.0.txt
10   */
11  package org.csveed.report;
12  
13  import java.util.ArrayList;
14  import java.util.List;
15  
16  /**
17   * The Class RowReport.
18   */
19  public class RowReport {
20  
21      /** The row. */
22      private String row;
23  
24      /** The start. */
25      private int start;
26  
27      /** The end. */
28      private int end;
29  
30      /**
31       * Instantiates a new row report.
32       *
33       * @param row
34       *            the row
35       * @param start
36       *            the start
37       * @param end
38       *            the end
39       */
40      public RowReport(String row, int start, int end) {
41          this.row = row;
42          this.start = start;
43          this.end = start == end && start < row.length() ? start + 1 : end;
44      }
45  
46      /**
47       * Gets the row.
48       *
49       * @return the row
50       */
51      public String getRow() {
52          return row;
53      }
54  
55      /**
56       * Gets the start.
57       *
58       * @return the start
59       */
60      public int getStart() {
61          return start;
62      }
63  
64      /**
65       * Gets the end.
66       *
67       * @return the end
68       */
69      public int getEnd() {
70          return end;
71      }
72  
73      /**
74       * Tokenize.
75       *
76       * @return the list
77       */
78      public List<RowPart> tokenize() {
79          List<RowPart> lines = new ArrayList<>();
80          if (start > 0) {
81              lines.add(new RowPart(row.substring(0, start), false));
82          }
83          if (end - start > 0) {
84              lines.add(new RowPart(row.substring(start, end), true));
85          }
86          if (end < row.length()) {
87              lines.add(new RowPart(row.substring(end), false));
88          }
89          return lines;
90      }
91  
92      /**
93       * Gets the printable lines.
94       *
95       * @return the printable lines
96       */
97      public List<String> getPrintableLines() {
98          List<String> lines = new ArrayList<>();
99  
100         List<RowPart> parts = tokenize();
101 
102         lines.add(createContentLine(parts));
103         lines.add(createFocusLine(parts));
104 
105         return lines;
106     }
107 
108     /**
109      * Creates the content line.
110      *
111      * @param parts
112      *            the parts
113      *
114      * @return the string
115      */
116     private String createContentLine(List<RowPart> parts) {
117         StringBuilder line = new StringBuilder();
118         for (RowPart token : parts) {
119             line.append(token.getToken());
120         }
121         return line.toString();
122     }
123 
124     /**
125      * Creates the focus line.
126      *
127      * @param parts
128      *            the parts
129      *
130      * @return the string
131      */
132     private String createFocusLine(List<RowPart> parts) {
133         StringBuilder line = new StringBuilder();
134         boolean placedMarkers = false;
135         for (RowPart token : parts) {
136             if (token.isHighlight()) {
137                 line.append(printUnderscoredPart(token));
138                 placedMarkers = true;
139             } else {
140                 line.append(printEmptyPart(token));
141             }
142         }
143         if (!placedMarkers) { // Essentially only at the end-of-line
144             line.append('^');
145         }
146         return line.toString();
147     }
148 
149     /**
150      * Prints the empty part.
151      *
152      * @param token
153      *            the token
154      *
155      * @return the string
156      */
157     private String printEmptyPart(RowPart token) {
158         StringBuilder linePart = new StringBuilder();
159         for (int i = 0; i < token.getToken().length(); i++) {
160             linePart.append(' ');
161         }
162         return linePart.toString();
163     }
164 
165     /**
166      * Prints the underscored part.
167      *
168      * @param token
169      *            the token
170      *
171      * @return the string
172      */
173     private String printUnderscoredPart(RowPart token) {
174         StringBuilder linePart = new StringBuilder();
175         for (int i = 0; i < token.getToken().length(); i++) {
176             if (i == 0 || i == token.getToken().length() - 1) {
177                 linePart.append('^');
178             } else {
179                 linePart.append('-');
180             }
181         }
182         return linePart.toString();
183     }
184 
185 }