Factory

The basics

There is a factory class LRUCachingMapFactory which eases the trouble with constructing the caching maps with the generics issues.

Method Resulting Class Key Value
createMemoryCache(...) DefaultLRUCachingMap<K,V> K V
createFileReferenceCache(...) DefaultFilenameFileReferenceLRUCachingMap String (file.getName()) File
createIndexedXmlCache(...) DefaultFileLRUCachingMap<K extends Serializable,V> K extends Serializable V (must have XmlElementRoot annotation)
createKeyedXmlCache(...) DefaultFileLRUCachingMap<K extends Serializable,V extends Keyed<K>> K extends Serializable V (must have XmlElementRoot annotation, and extend Keyed<K>)
createFilenameXmlCache(...) DefaultFileLRUCachingMap<String,V> String (file.getName()) V (must have XmlElementRoot annotation)
createIndexedSerializedCache(...) DefaultFileLRUCachingMap<K extends Serializable,V extends Serializable> K extends Serializable V extends Serializable
createKeyedSerializedCache(...) DefaultFileLRUCachingMap<K extends Serializable,V extends Keyed<K> & Serializable> K extends Serializable V extends Serializable (must extend Keyed<K>)
createFilenameSerializedCache(...) DefaultFileLRUCachingMap<V extends Serializable> String (file.getName()) V extends Serializable
createJPACache(...) DefaultLRUCachingMap<K extends Serializable,V extends Serializable> K extends Serializable V extends Serializable
createJPAGroupedCache(...) DefaultLRUCachingMap<K extends Serializable,V extends Serializable> K extends Serializable V extends Serializable


Factory example usages

LRUCachingMap<String,String> cache = LRUCachingMapFactory.createMemoryCache(configuration);

LRUCachingMap<String,File> cache = LRUCachingMapFactory.createFileReferenceCache(configuration, cachingDirectory,PersistencesRestoreMethod.DELETE_ALL);

DefaultFileLRUCachingMap<String,Book> cache = LRUCachingMapFactory.createFilenameSerializedCache(configuration, cachingDirectory, PersistencesRestoreMethod.LOAD_ALL);

DefaultFileLRUCachingMap<String,Book> cache = LRUCachingMapFactory.createFilenameXmlCache(configuration, cachingDirectory, Book.class, PersistencesRestoreMethod.LOAD_ALL);

DefaultFileLRUCachingMap<String,Book> cache = LRUCachingMapFactory.createIndexedSerializedCache(configuration, cachingDirectory,PersistencesRestoreMethod.DELETE_ALL,10);

DefaultFileLRUCachingMap<String,Book> cache = LRUCachingMapFactory.createKeyedSerializedCache(configuration, cachingDirectory,PersistencesRestoreMethod.LOAD_WITHIN_EXPIRETIME);

DefaultFileLRUCachingMap<String,Book> cache = LRUCachingMapFactory.createIndexedXmlCache(configuration, cachingDirectory,Book.class,PersistencesRestoreMethod.DELETE_ALL,10);

DefaultFileLRUCachingMap<String,Book> cache = LRUCachingMapFactory.createKeyedXmlCache(configuration, cachingDirectory,Book.class,PersistencesRestoreMethod.LOAD_WITHIN_EXPIRETIME);

DefaultLRUCachingMap<String, String> cache = LRUCachingMapFactory.createJPACache(configuration, MyCachedJPAEntity.class, entityManagerFactory, PersistencesRestoreMethod.LOAD_WITHIN_EXPIRETIME);

DefaultLRUCachingMap<String, String> cache = LRUCachingMapFactory.createJPAGroupedCache(configuration, "group01", MyCachedGroupJPAEntity.class, entityManagerFactory, PersistencesRestoreMethod.LOAD_WITHIN_EXPIRETIME);


Factory implementation

Just to show that is not really that hard to make your own specialized instances, but have to be keenly aware of your generics, here is the actual implementation of LRUCachingMapFactory.

public final class LRUCachingMapFactory {
	

	public static <K, V> DefaultLRUCachingMap<K, V> createMemoryCache(LRUCachingMapConfiguration configuration) throws NullPointerException {
		return new DefaultLRUCachingMap<K,V>(
				configuration,
				new DefaultMemoryValueManager<K,V>()
			);
	}

	public static DefaultFilenameFileReferenceLRUCachingMap createFileReferenceCache(LRUCachingMapConfiguration configuration,CachingDirectory cachingDirectory,PersistencesRestoreMethod persistencesRestoreMethod) {		
		return new DefaultFilenameFileReferenceLRUCachingMap(
				configuration,
				cachingDirectory, 
				persistencesRestoreMethod
			);
	}	

	public static <K extends Serializable,V> DefaultFileLRUCachingMap<K,V> createIndexedXmlCache(LRUCachingMapConfiguration configuration,CachingDirectory cachingDirectory,Class<V> xmlClazz,PersistencesRestoreMethod persistencesRestoreMethod,int indexSaveInterval) {
		return new DefaultFileLRUCachingMap<K,V>(
				configuration,
				new DefaultIndexedXmlFileValueManager<K,V>(
						cachingDirectory,
						xmlClazz,
						persistencesRestoreMethod,
						indexSaveInterval
					)
				);
	}		
	
	public static <K extends Serializable,V extends Keyed<K>> DefaultFileLRUCachingMap<K,V> createKeyedXmlCache(LRUCachingMapConfiguration configuration,CachingDirectory cachingDirectory,Class<V> xmlClazz,PersistencesRestoreMethod persistencesRestoreMethod) {
		return new DefaultFileLRUCachingMap<K,V>(
				configuration,
				new DefaultKeyedXmlFileValueManager<K,V>(
						cachingDirectory,
						xmlClazz,
						persistencesRestoreMethod
					)
				);
	}

	public static <V> DefaultFileLRUCachingMap<String,V> createFilenameXmlCache(LRUCachingMapConfiguration configuration,CachingDirectory cachingDirectory,Class<V> xmlClazz,PersistencesRestoreMethod persistencesRestoreMethod) {
		return new DefaultFileLRUCachingMap<String,V>(configuration,
				                                      new DefaultFilenameXmlFileValueManager<V>(
				                                    		  cachingDirectory,
				                                    		  xmlClazz,
				                                    		  persistencesRestoreMethod
				                                    	  )
				                                      );
	}

	public static <K extends Serializable,V extends Serializable> DefaultFileLRUCachingMap<K,V> createIndexedSerializedCache(LRUCachingMapConfiguration configuration,CachingDirectory cachingDirectory,PersistencesRestoreMethod persistencesRestoreMethod,int indexSaveInterval) {
		return new DefaultFileLRUCachingMap<K,V>(
				configuration,
				new DefaultIndexedSerializableFileValueManager<K,V>(
						cachingDirectory,
						persistencesRestoreMethod,
						indexSaveInterval
					)
				);
	}	
	
	public static <K extends Serializable,V extends Keyed<K> & Serializable> DefaultFileLRUCachingMap<K,V> createKeyedSerializedCache(LRUCachingMapConfiguration configuration,CachingDirectory cachingDirectory,PersistencesRestoreMethod persistencesRestoreMethod) {
		return new DefaultFileLRUCachingMap<K,V>(
				configuration,
				new DefaultKeyedSerializableFileValueManager<K,V>(
						cachingDirectory,
						persistencesRestoreMethod
					)
				);
	}

	public static <V extends Serializable> DefaultFileLRUCachingMap<String,V> createFilenameSerializedCache(LRUCachingMapConfiguration configuration,CachingDirectory cachingDirectory,PersistencesRestoreMethod persistencesRestoreMethod) {
		return new DefaultFileLRUCachingMap<String,V>(
				configuration,
				new DefaultFilenameSerializableFileValueManager<V>(
						cachingDirectory,
						persistencesRestoreMethod
					)
				);
	}

	public static <K extends Serializable,V extends Serializable> DefaultLRUCachingMap<K,V> createJPACache(LRUCachingMapConfiguration configuration,Class<? extends CachedJPAEntity<K,V>> entityClazz,EntityManagerFactory entityManagerFactory,PersistencesRestoreMethod persistencesRestoreMethod) {
		return new DefaultLRUCachingMap<K,V>(
				configuration,
				new DefaultJPAEntityValueManager<K,V>(
						new DefaultCachedJPAEntityRepository<K,V>(
								entityClazz,
								entityManagerFactory
								),
							persistencesRestoreMethod
						)
					);
	}
	
	public static <K extends Serializable,V extends Serializable> DefaultLRUCachingMap<K,V> createJPAGroupedCache(LRUCachingMapConfiguration configuration,final String group,Class<? extends CachedGroupJPAEntity<K,V>> entityGroupClazz,EntityManagerFactory entityManagerFactory,PersistencesRestoreMethod persistencesRestoreMethod) {
		return new DefaultLRUCachingMap<K,V>(
				configuration,
				new DefaultGroupJPAEntityValueManager<K,V>(
						new DefaultCachedGroupJPAEntityRepository<K,V>(
								group,
								entityGroupClazz,
								entityManagerFactory
							),
						persistencesRestoreMethod
					)
				);
	}
	
}