File: ContentCacher.java

package info (click to toggle)
gpsprune 26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,824 kB
  • sloc: java: 52,154; sh: 25; makefile: 21; python: 15
file content (90 lines) | stat: -rw-r--r-- 1,935 bytes parent folder | download | duplicates (6)
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
package tim.prune.load;

import java.util.ArrayList;

/**
 * General point data cacher
 */
public abstract class ContentCacher
{
	/** Array to hold lines of file */
	private String[] _contentArray = null;


	/**
	 * @return Contents of the file as array of non-blank Strings
	 */
	public String[] getContents()
	{
		return _contentArray;
	}


	/**
	 * Get the top section of the file for preview
	 * @param inNumRows number of lines to extract
	 * @param inMaxWidth max length of Strings (longer ones will be chopped)
	 * @return String array containing non-blank lines from the file
	 */
	public String[] getSnippet(int inNumRows, int inMaxWidth)
	{
		final int MIN_SNIPPET_SIZE = 3;
		// Check size is within sensible limits
		int numToCopy = inNumRows;
		if (numToCopy > getNumLines()) numToCopy = getNumLines();
		int size = numToCopy;
		if (size < MIN_SNIPPET_SIZE) size = MIN_SNIPPET_SIZE;
		String[] result = new String[size];
		// Copy Strings across
		System.arraycopy(_contentArray, 0, result, 0, numToCopy);
		// Chop Strings to max width if necessary
		if (inMaxWidth > 10)
		{
			for (int i=0; i<size; i++)
			{
				if (result[i] == null)
					result[i] = "";
				else
				{
					if (result[i].length() > inMaxWidth)
						result[i] = result[i].trim();
					if (result[i].length() > inMaxWidth)
						result[i] = result[i].substring(0, inMaxWidth);
				}
			}
		}
		return result;
	}

	/**
	 * @return the number of non-blank lines in the file
	 */
	public int getNumLines()
	{
		return _contentArray.length;
	}


	/**
	 * Clear the memory
	 */
	public void clear()
	{
		_contentArray = null;
	}

	/**
	 * Populate the string array
	 * @param inList list of lines
	 */
	protected void setContents(ArrayList<String> inList)
	{
		// Convert into String array for keeps
		int numLines = inList.size();
		_contentArray = new String[numLines];
		for (int i=0; i<numLines; i++)
		{
			_contentArray[i] = inList.get(i);
		}
	}
}