All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.molgenis.pathways.service.CacheFactory Maven / Gradle / Ivy

There is a newer version: 5.2.2
Show newest version
package org.molgenis.pathways.service;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.molgenis.pathways.model.Pathway;

import java.rmi.RemoteException;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Helper methods to create {@link LoadingCache}s.
 */
public class CacheFactory
{

	/**
	 * Creates a {@link LoadingCache} for a {@link FunctionalInterface} that may throw a {@link RemoteException}
	 *
	 * @param loader {@link RemoteFunction} that loads a value for the cache
	 * @return {@link LoadingCache}
	 */
	public static  LoadingCache loadingCache(RemoteFunction loader)
	{
		return CacheBuilder.newBuilder().maximumSize(Integer.MAX_VALUE).refreshAfterWrite(1, TimeUnit.DAYS)
				.build(new CacheLoader()
				{
					@Override
					public V load(K key) throws Exception
					{
						return loader.apply(key);
					}

				});
	}

	/**
	 * Creates a {@link LoadingCache} for {@link Pathway}s
	 *
	 * @param loader             {@link RemoteFunction} that loads the pathways
	 * @param filter             {@link BiPredicate} to filter the loaded pathways
	 * @param pathwayTransformer {@link Function} that transforms the loaded pathways to {@link Pathway}s
	 * @return {@link List} containing the filtered and transformed {@link Pathway}s
	 */
	public static  LoadingCache> loadingPathwayCache(
			RemoteFunction loader, BiPredicate filter,
			Function pathwayTransformer)
	{
		return loadingCache(
				params -> Arrays.stream(loader.apply(params)).filter((result) -> filter.test(params, result))
						.map(pathwayTransformer).collect(Collectors.toCollection(LinkedHashSet::new)));
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy