1 package tim.prune.load;
3 import java.util.ArrayList;
6 * General point data cacher
8 public abstract class ContentCacher
10 /** Array to hold lines of file */
11 private String[] _contentArray = null;
15 * @return Contents of the file as array of non-blank Strings
17 public String[] getContents()
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
29 public String[] getSnippet(int inNumRows, int inMaxWidth)
31 final int MIN_SNIPPET_SIZE = 3;
32 // Check size is within sensible limits
33 int numToCopy = inNumRows;
34 if (numToCopy > getNumLines()) numToCopy = getNumLines();
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
43 for (int i=0; i<size; i++)
45 if (result[i] == null)
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);
60 * @return the number of non-blank lines in the file
62 public int getNumLines()
64 return _contentArray.length;
77 * Populate the string array
78 * @param inList list of lines
80 protected void setContents(ArrayList<String> inList)
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++)
87 _contentArray[i] = inList.get(i);