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

org.infinispan.util.WriteableCacheCollectionMapper Maven / Gradle / Ivy

There is a newer version: 15.1.0.Dev04
Show newest version
package org.infinispan.util;

import java.util.Collection;
import java.util.function.Function;
import java.util.function.Predicate;

import org.infinispan.CacheCollection;
import org.infinispan.CacheStream;
import org.infinispan.commons.util.CloseableIterator;
import org.infinispan.commons.util.CloseableSpliterator;
import org.infinispan.commons.util.InjectiveFunction;
import org.infinispan.commons.util.IteratorMapper;
import org.infinispan.commons.util.SpliteratorMapper;

/**
 * A writeable cache collection mapper that also has constant time operations for things such as
 * {@link Collection#contains(Object)} if the underlying Collection does.
 * 

* This collection should be used for cases when a simple transformation of a element to another is all that is * needed by the underlying collection. *

* Note this class allows for a different function specifically for values returned from an iterator. This * can be useful to intercept calls such as {@link java.util.Map.Entry#setValue(Object)} and update appropriately. * @author wburns * @since 9.2 * @param the original collection type - referred to as old in some methods * @param the resulting collection type - referred to as new in some methods */ public class WriteableCacheCollectionMapper extends CollectionMapper implements CacheCollection { protected final CacheCollection realCacheCollection; protected final Function toNewTypeIteratorFunction; protected final Function fromNewTypeFunction; protected final InjectiveFunction keyFilterMapper; public WriteableCacheCollectionMapper(CacheCollection realCollection, Function toNewTypeFunction, Function fromNewTypeFunction, InjectiveFunction keyFilterFunction) { super(realCollection, toNewTypeFunction); this.realCacheCollection = realCollection; this.toNewTypeIteratorFunction = toNewTypeFunction; this.fromNewTypeFunction = fromNewTypeFunction; this.keyFilterMapper = keyFilterFunction; } public WriteableCacheCollectionMapper(CacheCollection realCollection, Function toNewTypeFunction, Function toNewTypeIteratorFunction, Function fromNewTypeFunction, InjectiveFunction keyFilterFunction) { super(realCollection, toNewTypeFunction); this.realCacheCollection = realCollection; this.toNewTypeIteratorFunction = toNewTypeIteratorFunction; this.fromNewTypeFunction = fromNewTypeFunction; this.keyFilterMapper = keyFilterFunction; } @Override public CloseableIterator iterator() { return new IteratorMapper<>(realCollection.iterator(), toNewTypeIteratorFunction); } @Override public boolean contains(Object o) { return realCollection.contains(fromNewTypeFunction.apply((R) o)); } @Override public boolean containsAll(Collection c) { return realCollection.containsAll(new CollectionMapper<>((Collection) c, fromNewTypeFunction)); } @Override public boolean add(R e) { return realCollection.add(fromNewTypeFunction.apply(e)); } @Override public boolean addAll(Collection c) { return realCollection.addAll(new CollectionMapper<>((Collection) c, fromNewTypeFunction)); } @Override public boolean remove(Object o) { return realCollection.remove(fromNewTypeFunction.apply((R) o)); } @Override public boolean removeAll(Collection c) { return realCollection.removeAll(new CollectionMapper<>((Collection) c, fromNewTypeFunction)); } @Override public boolean retainAll(Collection c) { return realCollection.retainAll(new CollectionMapper<>((Collection) c, fromNewTypeFunction)); } @Override public boolean removeIf(Predicate filter) { return realCollection.removeIf(e -> filter.test(mapper.apply(e))); } @Override public void clear() { realCollection.clear(); } @Override public CloseableSpliterator spliterator() { return new SpliteratorMapper<>(realCacheCollection.spliterator(), mapper); } private CacheStream getStream(CacheStream parentStream) { return new MappedCacheStream<>(parentStream.map(mapper), keyFilterMapper); } @Override public CacheStream stream() { return getStream(realCacheCollection.stream()); } @Override public CacheStream parallelStream() { return getStream(realCacheCollection.parallelStream()); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy