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

org.wildfly.clustering.server.infinispan.scheduler.CacheEntriesTask Maven / Gradle / Ivy

/*
 * Copyright The WildFly Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package org.wildfly.clustering.server.infinispan.scheduler;

import java.util.Iterator;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

import org.infinispan.Cache;
import org.wildfly.clustering.cache.Key;
import org.wildfly.clustering.cache.infinispan.embedded.distribution.CacheStreamFilter;

/**
 * Invokes a task against cache entries matching a filter.
 * @author Paul Ferraro
 * @param  cache key type
 * @param  cache value type
 */
public class CacheEntriesTask implements Consumer>> {
	private final Cache cache;
	private final Predicate> filter;
	private final Consumer> task;

	public static , V, M> CacheEntriesTask schedule(Cache cache, Predicate> filter, CacheEntryScheduler scheduler) {
		return new CacheEntriesTask<>(cache, filter, scheduler::schedule);
	}

	public static , V, M> CacheEntriesTask cancel(Cache cache, Predicate> filter, CacheEntryScheduler scheduler) {
		org.wildfly.clustering.cache.function.Consumer cancel = scheduler::cancel;
		Function, K> key = Map.Entry::getKey;
		return new CacheEntriesTask<>(cache, filter, cancel.map(key.andThen(Key::getId)));
	}

	public CacheEntriesTask(Cache cache, Predicate> filter, Consumer> task) {
		this.cache = cache;
		this.filter = filter;
		this.task = task;
	}

	@Override
	public void accept(CacheStreamFilter> filter) {
		// Iterate over filtered entries
		try (Stream> stream = filter.apply(this.cache.entrySet().stream()).filter(this.filter)) {
			Iterator> entries = stream.iterator();
			while (entries.hasNext()) {
				if (Thread.currentThread().isInterrupted()) break;
				this.task.accept(entries.next());
			}
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy