public class FileProvider extends Object implements IDataProvider, ILoadable, ILoadPolicy
Implementation of a data provider for Wordnet that uses files in the file
system to back instances of its data sources. This implementation takes a
URL to a file system directory as its path argument, and uses
the resource hints from the data types and parts of speech for its content
types to examine the filenames in the that directory to determine which files
contain which data.
This implementation supports loading the wordnet files into memory,
but this is actually not that beneficial for speed. This is because the
implementation loads the file data into memory uninterpreted, and on modern
machines, the time to interpret a line of data (i.e., parse it into a Java
object) is much larger than the time it takes to load the line from disk.
Those wishing to achieve speed increases from loading Wordnet into memory
should rely on the implementation in RAMDictionary, or something
similar, which pre-processes the Wordnet data into objects before caching
them.
| Modifier and Type | Class and Description |
|---|---|
protected class |
FileProvider.JWIBackgroundLoader
A thread class which tries to load each data source in this provider.
|
IHasLifecycle.LifecycleState, IHasLifecycle.ObjectClosedException, IHasLifecycle.ObjectOpenExceptionBACKGROUND_LOAD, IMMEDIATE_LOAD, NO_LOAD| Constructor and Description |
|---|
FileProvider(File file)
Constructs the file provider pointing to the resource indicated by the
path.
|
FileProvider(File file,
int loadPolicy)
Constructs the file provider pointing to the resource indicated by the
path, with the specified load policy.
|
FileProvider(File file,
int loadPolicy,
Collection<? extends IContentType<?>> types)
Constructs the file provider pointing to the resource indicated by the
path, with the specified load policy, looking for the specified content
type.s
|
FileProvider(URL url)
Constructs the file provider pointing to the resource indicated by the
path.
|
FileProvider(URL url,
int loadPolicy)
Constructs the file provider pointing to the resource indicated by the
path, with the specified load policy.
|
FileProvider(URL url,
int loadPolicy,
Collection<? extends IContentType<?>> types)
Constructs the file provider pointing to the resource indicated by the
path, with the specified load policy, looking for the specified content
type.s
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
checkOpen()
Convenience method that throws an exception if the provider is closed.
|
void |
close()
This closes the object by disposing of data backing objects or
connections.
|
protected <T> ILoadableDataSource<T> |
createBinarySearch(File file,
IContentType<T> type)
Creates a binary search data source for the specified type, using the
specified file.
|
protected <T> ILoadableDataSource<T> |
createDataSource(File file,
IContentType<T> type,
int policy)
Creates the actual data source implementations.
|
protected <T> ILoadableDataSource<T> |
createDirectAccess(File file,
IContentType<T> type)
Creates a direct access data source for the specified type, using the
specified file.
|
protected Map<IContentType<?>,ILoadableDataSource<?>> |
createSourceMap(List<File> files,
int policy)
Creates the map that contains the content types mapped to the data
sources.
|
protected IVersion |
determineVersion(Collection<? extends IDataSource<?>> srcs)
Determines a version from the set of data sources, if possible, otherwise
returns
IVersion.NO_VERSION |
Charset |
getCharset()
Returns the character set associated with this object.
|
int |
getLoadPolicy()
Returns the load policy for this object, expressed as an integer.
|
URL |
getSource()
Returns the
URL that points to the resource location; should
never return null. |
<T> ILoadableDataSource<T> |
getSource(IContentType<T> type)
Returns a data source object for the specified content type, if one is
available; otherwise returns
null. |
Set<? extends IContentType<?>> |
getTypes()
Returns a set containing all the content types this provider looks for at
the resource location.
|
IVersion |
getVersion()
Returns the associated version for this object.
|
boolean |
isLoaded()
Returns whether this object is loaded or not.
|
static boolean |
isLocalDirectory(File dir)
A utility method for checking whether a file represents an existing local
directory.
|
static boolean |
isLocalDirectory(URL url)
A utility method for checking whether a file represents an existing local
directory.
|
boolean |
isOpen()
Returns
true if the dictionary is open, that is, ready to
accept queries; returns false otherwise |
void |
load()
Starts a simple, non-blocking load.
|
void |
load(boolean block)
Initiates the loading process.
|
boolean |
open()
This opens the object by performing any required initialization steps.
|
<T> IContentType<T> |
resolveContentType(IDataType<T> dt,
POS pos)
Returns the first content type, if any, that matches the specified data
type and pos object.
|
void |
setCharset(Charset charset)
Sets the character set associated with this dictionary.
|
void |
setLoadPolicy(int policy)
Sets the load policy for this object.
|
void |
setSource(URL url)
This method is used to set the source URL from which the provider
accesses the data from which it instantiates data sources.
|
static File |
toFile(URL url)
Transforms a URL into a File.
|
static URL |
toURL(File file)
Transforms a file into a URL.
|
public FileProvider(File file)
ILoadPolicy.NO_LOAD load policy.file - A file pointing to the wordnet directory, may not be
nullNullPointerException - if the specified file is nullpublic FileProvider(File file, int loadPolicy)
file - A file pointing to the wordnet directory, may not be
nullloadPolicy - the load policy for this provider; this provider supports the
three values defined in ILoadPolicy.NullPointerException - if the specified file is nullpublic FileProvider(File file, int loadPolicy, Collection<? extends IContentType<?>> types)
file - A file pointing to the wordnet directory, may not be
nullloadPolicy - the load policy for this provider; this provider supports the
three values defined in ILoadPolicy.types - the content types this provider will look for when it loads
its data; may not be null or emptyNullPointerException - if the file or content type collection is nullIllegalArgumentException - if the set of types is emptypublic FileProvider(URL url)
ILoadPolicy.NO_LOAD load policy.url - A file URL in UTF-8 decodable format, may not be
nullNullPointerException - if the specified URL is nullpublic FileProvider(URL url, int loadPolicy)
url - A file URL in UTF-8 decodable format, may not be
nullloadPolicy - the load policy for this provider; this provider supports the
three values defined in ILoadPolicy.NullPointerException - if the specified URL is nullpublic FileProvider(URL url, int loadPolicy, Collection<? extends IContentType<?>> types)
url - A file URL in UTF-8 decodable format, may not be
nullloadPolicy - the load policy for this provider; this provider supports the
three values defined in ILoadPolicy.types - the content types this provider will look for when it loads
its data; may not be null or emptyNullPointerException - if the url or content type collection is nullIllegalArgumentException - if the set of types is emptypublic URL getSource()
IDataProviderURL that points to the resource location; should
never return null.getSource in interface IDataProviderURL that points to the resource location; must
not be nullpublic int getLoadPolicy()
ILoadPolicygetLoadPolicy in interface ILoadPolicypublic void setSource(URL url)
IDataProviderIllegalStateException.setSource in interface IDataProviderurl - the location of the data, may not be nullpublic void setLoadPolicy(int policy)
ILoadPolicysetLoadPolicy in interface ILoadPolicypolicy - the policy to implement; may be one of NO_LOAD,
BACKGROUND_LOAD, IMMEDIATE_LOAD or
an implementation-dependent value.public IVersion getVersion()
IHasVersionnull.getVersion in interface IHasVersionnull if none.protected IVersion determineVersion(Collection<? extends IDataSource<?>> srcs)
IVersion.NO_VERSIONsrcs - the data sources to be used to determine the verisonIVersion.NO_VERSION if there is nonepublic Charset getCharset()
IHasCharsetnull.getCharset in interface IHasCharsetnullpublic void setCharset(Charset charset)
IDataProvidernull.setCharset in interface IDataProvidercharset - the possibly null character set to use when
decoding files.public <T> IContentType<T> resolveContentType(IDataType<T> dt, POS pos)
IDataProvidernull.resolveContentType in interface IDataProviderdt - the data type, possibly null, of the desired
content typepos - the part of speech, possibly null, of the desired
content typepublic boolean open()
throws IOException
IHasLifecyclefalse, then subsequent calls to
IHasLifecycle.isOpen() will return false.open in interface IHasLifecycletrue if there were no errors in initialization;
false otherwise.IOException - if there was IO error while performing initializataionpublic void load()
ILoadablepublic void load(boolean block)
throws InterruptedException
ILoadableblock is false), or return
only when the loading process is complete. If the object is already
loaded, the method returns immediately and has no effect. If the object
is in the process of loading, and the method is called in blocking mode,
the method blocks until loading is complete, even if that call of the
method did not initiate the loading process. Some implementors of this
interface may not support the immediate-return functionality.load in interface ILoadableblock - if true, the method returns only when the loading
process is complete; if false, the method returns
immediately.InterruptedException - if the method is blocking, and is interrupted while waiting
for loading to completepublic boolean isLoaded()
ILoadabletrue only if the loading process has completed and the
object is actually loaded; if the object is still in the process of
loading, or failed to load, the method should return false.protected Map<IContentType<?>,ILoadableDataSource<?>> createSourceMap(List<File> files, int policy) throws IOException
files - the files from which the data sources should be created, may
not be nullpolicy - the load policy of the providernull, of content
types mapped to data sourcesNullPointerException - if the file list is nullIOException - if there is a problem creating the data sourceprotected <T> ILoadableDataSource<T> createDataSource(File file, IContentType<T> type, int policy) throws IOException
T - the content type of the data sourcefile - the file from which the data source should be created, may not
be nulltype - the content type of the data sourcepolicy - the load policy to follow when creating the data sourceNullPointerException - if any argument is nullIOException - if there is an IO problem when creating the data sourceprotected <T> ILoadableDataSource<T> createDirectAccess(File file, IContentType<T> type) throws IOException
T - the parameter of the content typefile - the file on which the data source is based; may not be
nulltype - the data type for the data source; may not be
nullNullPointerException - if either argument is nullIOException - if there is an IO problem when creating the data source
objectprotected <T> ILoadableDataSource<T> createBinarySearch(File file, IContentType<T> type) throws IOException
T - the parameter of the content typefile - the file on which the data source is based; may not be
nulltype - the data type for the data source; may not be
nullNullPointerException - if either argument is nullIOException - if there is an IO problem when creating the data source
objectpublic boolean isOpen()
IHasLifecycletrue if the dictionary is open, that is, ready to
accept queries; returns false otherwiseisOpen in interface IHasLifecycletrue if the object is open; false
otherwisepublic void close()
IClosableprotected void checkOpen()
ObjectClosedException - if the provider is closedpublic <T> ILoadableDataSource<T> getSource(IContentType<T> type)
IDataProvidernull.getSource in interface IDataProviderT - the content type of the data sourcetype - the content type of the data source to be retrievednull if this provider has no such data sourcepublic Set<? extends IContentType<?>> getTypes()
IDataProvidergetTypes in interface IDataProvidernull, non-empty set of content types for this
providerpublic static File toFile(URL url)
URLDecoder.NullPointerException - if the url is nullIllegalArgumentException - if the url does not use the 'file' protocolpublic static URL toURL(File file)
file - the file to be transformedNullPointerException - if the specified file is nullpublic static boolean isLocalDirectory(URL url)
url - the url object to check, may not be nulltrue if the url object represents a local directory
which exists; false otherwise.NullPointerException - if the specified url object is nullpublic static boolean isLocalDirectory(File dir)
dir - the file object to check, may not be nulltrue if the file object represents a local directory
which exist; false otherwise.NullPointerException - if the specified file object is nullCopyright © 2018. All rights reserved.