]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/load/ContentCacher.java
Version 20.4, May 2021
[GpsPrune.git] / src / tim / prune / load / ContentCacher.java
1 package tim.prune.load;
2
3 import java.util.ArrayList;
4
5 /**
6  * General point data cacher
7  */
8 public abstract class ContentCacher
9 {
10         /** Array to hold lines of file */
11         private String[] _contentArray = null;
12
13
14         /**
15          * @return Contents of the file as array of non-blank Strings
16          */
17         public String[] getContents()
18         {
19                 return _contentArray;
20         }
21
22
23         /**
24          * Get the top section of the file for preview
25          * @param inNumRows number of lines to extract
26          * @param inMaxWidth max length of Strings (longer ones will be chopped)
27          * @return String array containing non-blank lines from the file
28          */
29         public String[] getSnippet(int inNumRows, int inMaxWidth)
30         {
31                 final int MIN_SNIPPET_SIZE = 3;
32                 // Check size is within sensible limits
33                 int numToCopy = inNumRows;
34                 if (numToCopy > getNumLines()) numToCopy = getNumLines();
35                 int size = numToCopy;
36                 if (size < MIN_SNIPPET_SIZE) size = MIN_SNIPPET_SIZE;
37                 String[] result = new String[size];
38                 // Copy Strings across
39                 System.arraycopy(_contentArray, 0, result, 0, numToCopy);
40                 // Chop Strings to max width if necessary
41                 if (inMaxWidth > 10)
42                 {
43                         for (int i=0; i<size; i++)
44                         {
45                                 if (result[i] == null)
46                                         result[i] = "";
47                                 else
48                                 {
49                                         if (result[i].length() > inMaxWidth)
50                                                 result[i] = result[i].trim();
51                                         if (result[i].length() > inMaxWidth)
52                                                 result[i] = result[i].substring(0, inMaxWidth);
53                                 }
54                         }
55                 }
56                 return result;
57         }
58
59         /**
60          * @return the number of non-blank lines in the file
61          */
62         public int getNumLines()
63         {
64                 return _contentArray.length;
65         }
66
67
68         /**
69          * Clear the memory
70          */
71         public void clear()
72         {
73                 _contentArray = null;
74         }
75
76         /**
77          * Populate the string array
78          * @param inList list of lines
79          */
80         protected void setContents(ArrayList<String> inList)
81         {
82                 // Convert into String array for keeps
83                 int numLines = inList.size();
84                 _contentArray = new String[numLines];
85                 for (int i=0; i<numLines; i++)
86                 {
87                         _contentArray[i] = inList.get(i);
88                 }
89         }
90 }