Coherence Equal Between And Filter Example

In this example you will see how to use Equal, Between and And filter all together to fetch the data’s from cache and iterate through using for loop. This example is just for reference and assumed your cache is already setup and you are able to connect Oracle Coherence Cache:

  • CacheFilterExample.java:
package com.wfs.otc.cache.cachemanager;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import com.tangosol.net.NamedCache;
import com.tangosol.util.Filter;
import com.tangosol.util.filter.AndFilter;
import com.tangosol.util.filter.BetweenFilter;
import com.tangosol.util.filter.EqualsFilter;
import com.javahonk.cib.spt.datautil.CacheRegistry;
import com.wfs.mercury.datafabric.common.cache.dynamicColumns.AttributeItemExtractor;
import com.wfs.mercury.datafabric.common.cache.dynamicColumns.ResultsDataEntry;

public class CacheFilterExample {

	public static void main(String[] args) {
		getCacheByFilter();

	}
	
	@SuppressWarnings("unchecked")
	public static void getCacheByFilter() {
		
		NamedCache cache = (NamedCache) CacheRegistry.getNamedCache("Cache Name");
		
		//between filter
		LocalDateTime localDateToStart = LocalDate.now().atStartOfDay();				
		LocalDateTime localDateToEnd	= localDateToStart.plusDays(1).minus(1, ChronoUnit.SECONDS);
		
		Date dateToStart = Date.from(localDateToStart.atZone(ZoneId.systemDefault()).toInstant());
		Date dateToEnd = Date.from(localDateToEnd.atZone(ZoneId.systemDefault()).toInstant());
		
		AttributeItemExtractor assetType = new AttributeItemExtractor(306);
		AttributeItemExtractor eventDate = new AttributeItemExtractor(563);
		
		Filter productFilter = new EqualsFilter(assetType, "Stock");
		Filter currentDateFilter = new BetweenFilter(eventDate, dateToStart, dateToEnd);
		
		Filter combinedFilter = new AndFilter(productFilter, currentDateFilter);
		
		AttributeItemExtractor underLierAssetType = new AttributeItemExtractor(5);
		AttributeItemExtractor processStatus = new AttributeItemExtractor(570);
		
		Filter productFilter1 = new EqualsFilter(underLierAssetType, "Option");
		Filter productFilter2 = new EqualsFilter(processStatus, "NEW");
		
		Filter combinedFilter2 = new AndFilter(productFilter1, productFilter2);
		
		Filter filter = new AndFilter(combinedFilter, combinedFilter2);

		Set<Map.Entry<String, ResultsDataEntry>> entries = cache.entrySet(filter);
		
		ResultsDataEntry marketData = null;
		for (Entry<String, ResultsDataEntry> entry : entries) {
			marketData = entry.getValue();
			System.out.println(marketData.getMap());
			//break;
		}
	}

}

Leave a Reply

Your email address will not be published. Required fields are marked *