edu.vt.middleware.dictionary
Class FileWordList

java.lang.Object
  extended by edu.vt.middleware.dictionary.AbstractWordList
      extended by edu.vt.middleware.dictionary.FileWordList
All Implemented Interfaces:
WordList

public class FileWordList
extends AbstractWordList

Provides an implementation of a WordList that is backed by a file. Each word is read from the file for every get, though the implementation supports a simple memory cache to improve read performance.

Version:
$Revision: 1252 $ $Date: 2010-04-16 17:24:23 -0400 (Fri, 16 Apr 2010) $
Author:
Middleware Services

Field Summary
protected  java.util.TreeMap<java.lang.Integer,java.lang.Long> cache
          cache of indexes to file positions.
static int DEFAULT_CACHE_SIZE
          default cache size.
protected  java.io.RandomAccessFile file
          file containing words.
protected  int size
          size of the file.
 
Fields inherited from class edu.vt.middleware.dictionary.AbstractWordList
comparator
 
Constructor Summary
FileWordList(java.io.RandomAccessFile raf)
          Creates a new case-sensitive word list from the supplied file.
FileWordList(java.io.RandomAccessFile raf, boolean caseSensitive)
          Creates a new word list from the supplied file.
FileWordList(java.io.RandomAccessFile raf, boolean caseSensitive, int cachePercent)
          Creates a new word list from the supplied file.
 
Method Summary
 void close()
          Closes the underlying file and make the cache available for garbage collection.
 java.lang.String get(int index)
          Gets the word at the given 0-based index.
 java.io.RandomAccessFile getFile()
          Returns the file backing this list.
 int size()
          Gets the number of words in the list.
 
Methods inherited from class edu.vt.middleware.dictionary.AbstractWordList
checkIsString, checkRange, getComparator, iterator, medianIterator
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CACHE_SIZE

public static final int DEFAULT_CACHE_SIZE
default cache size.

See Also:
Constant Field Values

file

protected java.io.RandomAccessFile file
file containing words.


size

protected int size
size of the file.


cache

protected java.util.TreeMap<java.lang.Integer,java.lang.Long> cache
cache of indexes to file positions.

Constructor Detail

FileWordList

public FileWordList(java.io.RandomAccessFile raf)
             throws java.io.IOException
Creates a new case-sensitive word list from the supplied file. The input file is read on initialization and is maintained by this class.

NOTE Attempts to close the source file will cause IOException when get(int) is called subsequently.

Parameters:
raf - File containing words, one per line.
Throws:
java.io.IOException - if an error occurs reading the supplied file

FileWordList

public FileWordList(java.io.RandomAccessFile raf,
                    boolean caseSensitive)
             throws java.io.IOException
Creates a new word list from the supplied file. The input file is read on initialization and is maintained by this class.

NOTE Attempts to close the source file will cause IOException when get(int) is called subsequently.

Parameters:
raf - File containing words, one per line.
caseSensitive - Set to true to create case-sensitive word list, false otherwise.
Throws:
java.io.IOException - if an error occurs reading the supplied file

FileWordList

public FileWordList(java.io.RandomAccessFile raf,
                    boolean caseSensitive,
                    int cachePercent)
             throws java.io.IOException
Creates a new word list from the supplied file. The input file is read on initialization and is maintained by this class.

NOTE Attempts to close the source file will cause IOException when get(int) is called subsequently.

Parameters:
raf - File containing words, one per line.
caseSensitive - Set to true to create case-sensitive word list, false otherwise.
cachePercent - Percent (0-100) of file to cache in memory for improved read performance.
Throws:
java.lang.IllegalArgumentException - if cache percent is out of range.
java.io.IOException - if an error occurs reading the supplied file
Method Detail

get

public java.lang.String get(int index)
Gets the word at the given 0-based index.

Parameters:
index - 0-based index.
Returns:
Word at given index.

size

public int size()
Gets the number of words in the list.

Returns:
Total number of words in list.

getFile

public java.io.RandomAccessFile getFile()
Returns the file backing this list.

Returns:
RandomAccessFile that is backing this list

close

public void close()
           throws java.io.IOException
Closes the underlying file and make the cache available for garbage collection.

Throws:
java.io.IOException - if an error occurs closing the file


Copyright © 2003-2010 Virginia Tech. All Rights Reserved.