Class FileFontAttributesCache
- java.lang.Object
-
- com.levigo.jadice.demo.font.attribute.FileFontAttributesCache
-
- All Implemented Interfaces:
FontAttributesCache<String,FileNotFoundException>,Provider<Collection<FontSource>,FileNotFoundException>
public class FileFontAttributesCache extends Object implements FontAttributesCache<String,FileNotFoundException>
A basic implementation for aFontAttributesCachewhich keeps the contents of the cache in a file. The cache will be read during the creationg of theFileFontAttributesCacheobject and kept in the memory afterwards. To persist the changes made to the cache thewriteCache()method has to be called.
This implementation does not check if the file at a path to be added exists or is accessible.
-
-
Constructor Summary
Constructors Constructor Description FileFontAttributesCache(File cacheFile)Creates a newFileFontAttributesCacheand fills the cache by deserializing the contents of the given File.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(String absoluteFontFilePath, FontAttributeSet attributes)Adds the absolute path to a font and theFontAttributeSetto be stored for that font to the cache.Collection<FontSource>get()To simplify the use of theFileFontAttributesCachethis method gives easy access to the fonts included in this cache in the form of a Collection ofFontSources.
Please note that this implementation does not verify that fonts that have been registered exist nor does it verify that theSeekableInputStreams in theFontSources returned are valid.Map<String,FontAttributeSet>getAllEntries()Get all entries which are currently stored in the cacheFontAttributeSetgetFontAttributesEntry(String absoluteFontFilePath)Get theFontAttributeSetcached for the font identified by the given identifiervoidinvalidateCache()Removes all entries from the cache.voidremove(String absoluteFontFilePath)Removes theFontAttributeSetfor the font identified by the given identifier.voidwriteCache()Persists the cache.
-
-
-
Constructor Detail
-
FileFontAttributesCache
public FileFontAttributesCache(File cacheFile) throws Exception
Creates a newFileFontAttributesCacheand fills the cache by deserializing the contents of the given File. If any error occurs while filling the cache from the file the cache will be empty.- Parameters:
cacheFile- theFileto store the cache at. Must not benull.- Throws:
IllegalArgumentException- is thrown if the given File is null.Exception
-
-
Method Detail
-
add
public void add(String absoluteFontFilePath, FontAttributeSet attributes)
Adds the absolute path to a font and theFontAttributeSetto be stored for that font to the cache.
Please note that this implementation does not verify that a file exists at the given path.- Specified by:
addin interfaceFontAttributesCache<String,FileNotFoundException>- Parameters:
absoluteFontFilePath- an absolute path to the font to be added.attributes- theFontAttributeSetfor the font to be added
-
remove
public void remove(String absoluteFontFilePath)
Description copied from interface:FontAttributesCacheRemoves theFontAttributeSetfor the font identified by the given identifier. If there was no information stored for the given identifier nothing happens.- Specified by:
removein interfaceFontAttributesCache<String,FileNotFoundException>- Parameters:
absoluteFontFilePath- the identifier of the entry to remove the information for.
-
invalidateCache
public void invalidateCache()
Description copied from interface:FontAttributesCacheRemoves all entries from the cache.- Specified by:
invalidateCachein interfaceFontAttributesCache<String,FileNotFoundException>
-
getAllEntries
public Map<String,FontAttributeSet> getAllEntries()
Description copied from interface:FontAttributesCacheGet all entries which are currently stored in the cache- Specified by:
getAllEntriesin interfaceFontAttributesCache<String,FileNotFoundException>- Returns:
- the current state of the Map containing all entries.
-
getFontAttributesEntry
public FontAttributeSet getFontAttributesEntry(String absoluteFontFilePath)
Description copied from interface:FontAttributesCacheGet theFontAttributeSetcached for the font identified by the given identifier- Specified by:
getFontAttributesEntryin interfaceFontAttributesCache<String,FileNotFoundException>- Parameters:
absoluteFontFilePath- The identifier of the font- Returns:
- The
FontAttributeSetfor the font identified by the given identifier as stored in the cache.
nullmay be returned if either there is no entry for the identifier or if theFontAttributeSetfor the font arenull.
-
writeCache
public void writeCache() throws IOExceptionDescription copied from interface:FontAttributesCachePersists the cache.- Specified by:
writeCachein interfaceFontAttributesCache<String,FileNotFoundException>- Throws:
IOException
-
get
public Collection<FontSource> get() throws FileNotFoundException
To simplify the use of theFileFontAttributesCachethis method gives easy access to the fonts included in this cache in the form of a Collection ofFontSources.
Please note that this implementation does not verify that fonts that have been registered exist nor does it verify that theSeekableInputStreams in theFontSources returned are valid. This may lead to errors while reading from theSeekableInputStreams provided in theFontSources if invalid data was added to the cache or if the registered font no longer exists when requested. Additionally this method is not thread-safe.- Specified by:
getin interfaceProvider<Collection<FontSource>,FileNotFoundException>- Returns:
- A
CollectionofFontSources based on the absolute paths and the respectiveFontAttributeSetstored in this cache. - Throws:
FileNotFoundException- if an error occurs while creating theFontStreamProviderfor one of the fonts.
-
-