Class FileText
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<String>
-
- com.puppycrawl.tools.checkstyle.api.FileText
-
public final class FileText extends AbstractList<String>
Represents the text contents of a file of arbitrary plain text type.This class will be passed to instances of class FileSetCheck by Checker. It implements a string list to ensure backwards compatibility, but can be extended in the future to allow more flexible, more powerful or more efficient handling of certain situations.
- Author:
- Martin von Gagern
-
-
Field Summary
-
Fields inherited from class java.util.AbstractList
modCount
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static FileText
fromLines(File file, List<String> lines)
Compatibility conversion.String
get(int lineNo)
Retrieves a line of the text by its number.Charset
getCharset()
Get the character set which was used to read the file.File
getFile()
Get the name of the file.CharSequence
getFullText()
Retrieve the full text of the file.LineColumn
lineColumn(int pos)
Determine line and column numbers in full text.int
size()
Counts the lines of the text.String[]
toLinesArray()
Returns an array of all lines.-
Methods inherited from class java.util.AbstractList
add, add, addAll, clear, equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange, set, subList
-
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.util.List
addAll, contains, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, sort, spliterator, toArray, toArray
-
-
-
-
Constructor Detail
-
FileText
public FileText(File file, String charsetName) throws IOException
Creates a new file text representation.The file will be read using the specified encoding, replacing malformed input and unmappable characters with the default replacement character.
- Parameters:
file
- the name of the filecharsetName
- the encoding to use when reading the file- Throws:
NullPointerException
- if the text is nullIOException
- if the file could not be read
-
-
Method Detail
-
fromLines
public static FileText fromLines(File file, List<String> lines)
Compatibility conversion.This method can be used to convert the arguments passed to
FileSetCheck.process(File,List)
to a FileText object. If the list of lines already is a FileText, it is returned as is. Otherwise, a new FileText is constructed by joining the lines using line feed characters.- Parameters:
file
- the name of the filelines
- the lines of the text, without terminators- Returns:
- an object representing the denoted text file
-
getFile
public File getFile()
Get the name of the file.- Returns:
- an object containing the name of the file
-
getCharset
public Charset getCharset()
Get the character set which was used to read the file. Will benull
for a file reconstructed from its lines.- Returns:
- the charset used when the file was read
-
getFullText
public CharSequence getFullText()
Retrieve the full text of the file.- Returns:
- the full text of the file
-
toLinesArray
public String[] toLinesArray()
Returns an array of all lines.text.toLinesArray()
is equivalent totext.toArray(new String[text.size()])
.- Returns:
- an array of all lines of the text
-
lineColumn
public LineColumn lineColumn(int pos)
Determine line and column numbers in full text.- Parameters:
pos
- the character position in the full text- Returns:
- the line and column numbers of this character
-
get
public String get(int lineNo)
Retrieves a line of the text by its number. The returned line will not contain a trailing terminator.
-
size
public int size()
Counts the lines of the text.- Specified by:
size
in interfaceCollection<String>
- Specified by:
size
in interfaceList<String>
- Specified by:
size
in classAbstractCollection<String>
- Returns:
- the number of lines in the text
-
-