VersionAwareMapMergePolicy implements MapMergePolicy { public Object merge(String mapName, EntryView mergingEntry, EntryView existingEntry) { final Object existingValue = existingEntry != null ? existingEntry.getValue() : null; final Object mergingValue = mergingEntry.getValue(); if (existingValue != null && existingValue instanceof CacheEntry && mergingValue != null && mergingValue instanceof CacheEntry) { final CacheEntry existingCacheEntry = (CacheEntry) existingValue; final CacheEntry mergingCacheEntry = (CacheEntry) mergingValue; final Object mergingVersionObject = mergingCacheEntry.getVersion(); final Object existingVersionObject = existingCacheEntry.getVersion(); if (mergingVersionObject != null && existingVersionObject != null && mergingVersionObject instanceof Comparable && existingVersionObject instanceof Comparable) { final Comparable mergingVersion = (Comparable) mergingVersionObject; final Comparable existingVersion = (Comparable) existingVersionObject; if (mergingVersion.compareTo(existingVersion) > 0) { return mergingValue; } else { return existingValue; } } } return mergingValue; }  Object merge(String mapName, EntryView mergingEntry, EntryView existingEntry); @Override void writeData(ObjectDataOutput out); @Override void readData(ObjectDataInput in);  }
@Test public void merge_mergingUptodate() { CacheEntry existing = cacheEntryWithVersion(versionOld); CacheEntry merging = cacheEntryWithVersion(versionNew); EntryView entryExisting = entryWithGivenValue(existing); EntryView entryMerging = entryWithGivenValue(merging); assertEquals(merging, policy.merge("map", entryMerging, entryExisting)); }
@Test public void merge_mergingStale() { CacheEntry existing = cacheEntryWithVersion(versionNew); CacheEntry merging = cacheEntryWithVersion(versionOld); EntryView entryExisting = entryWithGivenValue(existing); EntryView entryMerging = entryWithGivenValue(merging); assertEquals(existing, policy.merge("map", entryMerging, entryExisting)); }
@Test public void merge_mergingNull() { CacheEntry existing = null; CacheEntry merging = cacheEntryWithVersion(versionNew); EntryView entryExisting = entryWithGivenValue(existing); EntryView entryMerging = entryWithGivenValue(merging); assertEquals(merging, policy.merge("map", entryMerging, entryExisting)); }