Sitelet https://github.com/jsr107/RI/commit/f25658eaf593c0b34433b0cf5defa4879287f745
Skip to content

Commit f25658e

Browse files
committed
Align behavior of RI to spec changes regarding old value availability
1 parent 9f8d8b9 commit f25658e

3 files changed

Lines changed: 56 additions & 13 deletions

File tree

‎cache-ri-impl/src/main/java/org/jsr107/ri/RICache.java‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ private void processExpiries(K key, RICacheEventDispatcher<K, V> dispatcher,
997997
V expiredValue) {
998998
entries.remove(key);
999999
dispatcher.addEvent(CacheEntryExpiredListener.class,
1000-
new RICacheEntryEvent<K, V>(this, key, expiredValue, EXPIRED));
1000+
new RICacheEntryEvent<K, V>(this, key, expiredValue, expiredValue, EXPIRED));
10011001
}
10021002

10031003
/**
@@ -1029,7 +1029,7 @@ public boolean remove(K key) {
10291029
RICacheEventDispatcher<K, V> dispatcher = new RICacheEventDispatcher<K,
10301030
V>();
10311031
dispatcher.addEvent(CacheEntryRemovedListener.class,
1032-
new RICacheEntryEvent<K, V>(this, key, value, REMOVED));
1032+
new RICacheEntryEvent<K, V>(this, key, value, value, REMOVED));
10331033
dispatcher.dispatch(listenerRegistrations);
10341034

10351035
result = true;
@@ -1079,7 +1079,7 @@ public boolean remove(K key, V oldValue) {
10791079
RICacheEventDispatcher<K, V> dispatcher = new
10801080
RICacheEventDispatcher<K, V>();
10811081
dispatcher.addEvent(CacheEntryRemovedListener.class,
1082-
new RICacheEntryEvent<K, V>(this, key, oldValue, REMOVED));
1082+
new RICacheEntryEvent<K, V>(this, key, oldValue, oldValue, REMOVED));
10831083
dispatcher.dispatch(listenerRegistrations);
10841084

10851085
result = true;
@@ -1141,7 +1141,7 @@ public V getAndRemove(K key) {
11411141
RICacheEventDispatcher<K, V> dispatcher = new RICacheEventDispatcher<K,
11421142
V>();
11431143
dispatcher.addEvent(CacheEntryRemovedListener.class,
1144-
new RICacheEntryEvent<K, V>(this, key, result, REMOVED));
1144+
new RICacheEntryEvent<K, V>(this, key, result, result, REMOVED));
11451145
dispatcher.dispatch(listenerRegistrations);
11461146
}
11471147
} finally {
@@ -1427,7 +1427,7 @@ public void removeAll(Set<? extends K> keys) {
14271427
processExpiries(key, dispatcher, value);
14281428
} else {
14291429
dispatcher.addEvent(CacheEntryRemovedListener.class,
1430-
new RICacheEntryEvent<K, V>(this, key, value, REMOVED));
1430+
new RICacheEntryEvent<K, V>(this, key, value, value, REMOVED));
14311431
}
14321432
}
14331433
}
@@ -1450,7 +1450,7 @@ public void removeAll(Set<? extends K> keys) {
14501450
processExpiries(key, dispatcher, value);
14511451
} else {
14521452
dispatcher.addEvent(CacheEntryRemovedListener.class,
1453-
new RICacheEntryEvent<K, V>(this, key, value, REMOVED));
1453+
new RICacheEntryEvent<K, V>(this, key, value, value, REMOVED));
14541454
}
14551455
}
14561456

@@ -1537,7 +1537,7 @@ public void removeAll() {
15371537
processExpiries(key, dispatcher, value);
15381538
} else {
15391539
dispatcher.addEvent(CacheEntryRemovedListener.class,
1540-
new RICacheEntryEvent<K, V>(this, key, value, REMOVED));
1540+
new RICacheEntryEvent<K, V>(this, key, value, value, REMOVED));
15411541
size++;
15421542
}
15431543
}
@@ -1729,7 +1729,7 @@ public <T> T invoke(K key, javax.cache.processor.EntryProcessor<K, V,
17291729
oldValue = cachedValue == null ? null : valueConverter.fromInternal(cachedValue.get());
17301730
entries.remove(internalKey);
17311731

1732-
dispatcher.addEvent(CacheEntryRemovedListener.class, new RICacheEntryEvent<K, V>(this, key, oldValue, REMOVED));
1732+
dispatcher.addEvent(CacheEntryRemovedListener.class, new RICacheEntryEvent<K, V>(this, key, oldValue, oldValue, REMOVED));
17331733

17341734
if (statisticsEnabled()) {
17351735
statistics.increaseCacheRemovals(1);
@@ -2168,7 +2168,7 @@ public void remove() {
21682168
RICacheEventDispatcher<K, V>();
21692169
dispatcher.addEvent(CacheEntryRemovedListener.class,
21702170
new RICacheEntryEvent<K, V>(RICache.this, lastEntry.getKey(),
2171-
lastEntry.getValue(), REMOVED));
2171+
lastEntry.getValue(), lastEntry.getValue(), REMOVED));
21722172
dispatcher.dispatch(listenerRegistrations);
21732173

21742174
} finally {

‎cache-ri-impl/src/main/java/org/jsr107/ri/event/RICacheEntryEvent.java‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ public RICacheEntryEvent(Cache<K, V> source, K key, V value, V oldValue, EventTy
6969
this.oldValueAvailable = true;
7070
}
7171

72+
/**
73+
* Constructs a cache entry event from a given cache as source
74+
* with an old value, explicitly specifying whether old value is available
75+
*
76+
* @param source the cache that originated the event
77+
* @param key the key
78+
* @param value the value
79+
* @param oldValue the oldValue
80+
* @param oldValueAvailable indicates whether old value is available
81+
*/
82+
public RICacheEntryEvent(Cache<K, V> source, K key, V value, V oldValue, EventType eventType, boolean oldValueAvailable) {
83+
super(source, eventType);
84+
this.key = key;
85+
this.value = value;
86+
this.oldValue = oldValue;
87+
this.oldValueAvailable = oldValueAvailable;
88+
}
89+
7290
/**
7391
* {@inheritDoc}
7492
*/
@@ -93,7 +111,7 @@ public V getOldValue() throws UnsupportedOperationException {
93111
if (isOldValueAvailable()) {
94112
return oldValue;
95113
} else {
96-
throw new UnsupportedOperationException("Old value is not available for key");
114+
return null;
97115
}
98116
}
99117

‎cache-ri-impl/src/main/java/org/jsr107/ri/event/RICacheEventDispatcher.java‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
import javax.cache.event.CacheEntryListenerException;
2626
import javax.cache.event.CacheEntryRemovedListener;
2727
import javax.cache.event.CacheEntryUpdatedListener;
28+
import javax.cache.event.EventType;
2829
import java.util.ArrayList;
30+
import java.util.List;
2931
import java.util.concurrent.ConcurrentHashMap;
3032

3133
/**
@@ -120,7 +122,7 @@ public void dispatch(Iterable<RICacheEntryListenerRegistration<K, V>> registrati
120122

121123
CacheEntryListener<? super K, ? super V> listener = registration.getCacheEntryListener();
122124
if (listener instanceof CacheEntryExpiredListener) {
123-
((CacheEntryExpiredListener) listener).onExpired(iterable);
125+
((CacheEntryExpiredListener) listener).onExpired(cloneEvents(registration, iterable));
124126
}
125127
}
126128
}
@@ -150,7 +152,7 @@ public void dispatch(Iterable<RICacheEntryListenerRegistration<K, V>> registrati
150152

151153
CacheEntryListener<? super K, ? super V> listener = registration.getCacheEntryListener();
152154
if (listener instanceof CacheEntryUpdatedListener) {
153-
((CacheEntryUpdatedListener) listener).onUpdated(iterable);
155+
((CacheEntryUpdatedListener) listener).onUpdated(cloneEvents(registration, iterable));
154156
}
155157
}
156158
}
@@ -165,7 +167,7 @@ public void dispatch(Iterable<RICacheEntryListenerRegistration<K, V>> registrati
165167

166168
CacheEntryListener<? super K, ? super V> listener = registration.getCacheEntryListener();
167169
if (listener instanceof CacheEntryRemovedListener) {
168-
((CacheEntryRemovedListener) listener).onRemoved(iterable);
170+
((CacheEntryRemovedListener) listener).onRemoved(cloneEvents(registration, iterable));
169171
}
170172
}
171173
}
@@ -178,5 +180,28 @@ public void dispatch(Iterable<RICacheEntryListenerRegistration<K, V>> registrati
178180
}
179181
}
180182

183+
private List<CacheEntryEvent<K, V>> cloneEvents(RICacheEntryListenerRegistration<K, V> registration,
184+
Iterable<CacheEntryEvent<K, V>> events) {
185+
List<CacheEntryEvent<K, V>> dispatchedEvents = new ArrayList<CacheEntryEvent<K, V>>();
186+
// clone events, setting or not the old value depending on registration properties
187+
for (CacheEntryEvent event : events) {
188+
RICacheEntryEvent dispatchedEvent;
189+
if (registration.isOldValueRequired()) {
190+
dispatchedEvent = new RICacheEntryEvent(event.getSource(), event.getKey(), event.getValue(), event.getOldValue(), event.getEventType());
191+
} else {
192+
if (event.getEventType() == EventType.REMOVED || event.getEventType() == EventType.EXPIRED) {
193+
// Since JCache 1.1, removed & expired events have to return oldValue or null when oldValueRequired == false
194+
// RI chooses to return null as old value and oldValueAvailable is false in this case
195+
dispatchedEvent = new RICacheEntryEvent(event.getSource(), event.getKey(), null, null,
196+
event.getEventType(), false);
197+
} else {
198+
dispatchedEvent = new RICacheEntryEvent(event.getSource(), event.getKey(), event.getValue(), null,
199+
event.getEventType(), false);
200+
}
201+
}
202+
dispatchedEvents.add(dispatchedEvent);
203+
}
204+
return dispatchedEvents;
205+
}
181206

182207
}

0 commit comments

Comments
 (0)