@Test public void testRemoveElement() { Element element = new Element("key", "value"); Ehcache underlyingCache = buildMockCache(); when(underlyingCache.getQuiet((Object)"key")).thenReturn(new Element("key", "other"), element); when(underlyingCache.remove((Object)"key")).thenReturn(true); StronglyConsistentCacheAccessor cacheAccessor = new StronglyConsistentCacheAccessor(underlyingCache); assertThat(cacheAccessor.removeElement(element), is(false)); assertThat(cacheAccessor.removeElement(element), is(true)); try { cacheAccessor.removeElement(new Element(null, null)); fail("Expected NPE with null key"); } catch (NullPointerException e) { } }
@Override public boolean removeElement(Element element) throws NullPointerException { Object objectKey = element.getObjectKey(); if (objectKey == null) { throw new NullPointerException(); } acquireWriteLockOnKey(objectKey); try { Element current = getQuiet(objectKey); if (elementComparator.equals(current, element)) { return super.remove(objectKey); } } finally { releaseWriteLockOnKey(objectKey); } return false; }
StronglyConsistentCacheAccessor extends EhcacheDecoratorAdapter { @Override public boolean removeElement(Element element) throws NullPointerException { Object objectKey = element.getObjectKey(); if (objectKey == null) { throw new NullPointerException(); } acquireWriteLockOnKey(objectKey); try { Element current = getQuiet(objectKey); if (elementComparator.equals(current, element)) { return super.remove(objectKey); } } finally { releaseWriteLockOnKey(objectKey); } return false; } }
StronglyConsistentCacheAccessor extends EhcacheDecoratorAdapter { @Override public boolean removeElement(Element element) throws NullPointerException { Object objectKey = element.getObjectKey(); if (objectKey == null) { throw new NullPointerException(); } acquireWriteLockOnKey(objectKey); try { Element current = getQuiet(objectKey); if (elementComparator.equals(current, element)) { return super.remove(objectKey); } } finally { releaseWriteLockOnKey(objectKey); } return false; } StronglyConsistentCacheAccessor(Ehcache underlyingCache); }
StronglyConsistentCacheAccessor extends EhcacheDecoratorAdapter { @Override public boolean removeElement(Element element) throws NullPointerException { Object objectKey = element.getObjectKey(); if (objectKey == null) { throw new NullPointerException(); } acquireWriteLockOnKey(objectKey); try { Element current = getQuiet(objectKey); if (elementComparator.equals(current, element)) { return super.remove(objectKey); } } finally { releaseWriteLockOnKey(objectKey); } return false; } StronglyConsistentCacheAccessor(Ehcache underlyingCache); @Override Element putIfAbsent(Element element, boolean doNotNotifyCacheReplicators); @Override Element putIfAbsent(Element element); @Override boolean replace(Element old, Element element); @Override Element replace(Element element); @Override boolean removeElement(Element element); @Override void put(Element element, boolean doNotNotifyCacheReplicators); @Override void put(Element element); @Override void putAll(Collection<Element> elements); @Override void putQuiet(Element element); @Override void putWithWriter(Element element); @Override boolean remove(Object key, boolean doNotNotifyCacheReplicators); @Override void removeAll(Collection<?> keys); @Override boolean remove(Object key); @Override void removeAll(Collection<?> keys, boolean doNotNotifyCacheReplicators); @Override boolean remove(Serializable key, boolean doNotNotifyCacheReplicators); @Override boolean remove(Serializable key); @Override boolean removeQuiet(Object key); @Override boolean removeQuiet(Serializable key); @Override boolean removeWithWriter(Object key); @Override Element removeAndReturnElement(Object key); @Override Element get(Object key); @Override Map<Object, Element> getAll(Collection<?> keys); @Override Element get(Serializable key); @Override Element getQuiet(Object key); @Override Element getQuiet(Serializable key); @Override Element getWithLoader(Object key, CacheLoader loader, Object loaderArgument); @Override Map getAllWithLoader(Collection keys, Object loaderArgument); }
StronglyConsistentCacheAccessor extends EhcacheDecoratorAdapter { @Override public boolean removeElement(Element element) throws NullPointerException { Object objectKey = element.getObjectKey(); if (objectKey == null) { throw new NullPointerException(); } acquireWriteLockOnKey(objectKey); try { Element current = getQuiet(objectKey); if (elementComparator.equals(current, element)) { return super.remove(objectKey); } } finally { releaseWriteLockOnKey(objectKey); } return false; } StronglyConsistentCacheAccessor(Ehcache underlyingCache); @Override Element putIfAbsent(Element element, boolean doNotNotifyCacheReplicators); @Override Element putIfAbsent(Element element); @Override boolean replace(Element old, Element element); @Override Element replace(Element element); @Override boolean removeElement(Element element); @Override void put(Element element, boolean doNotNotifyCacheReplicators); @Override void put(Element element); @Override void putAll(Collection<Element> elements); @Override void putQuiet(Element element); @Override void putWithWriter(Element element); @Override boolean remove(Object key, boolean doNotNotifyCacheReplicators); @Override void removeAll(Collection<?> keys); @Override boolean remove(Object key); @Override void removeAll(Collection<?> keys, boolean doNotNotifyCacheReplicators); @Override boolean remove(Serializable key, boolean doNotNotifyCacheReplicators); @Override boolean remove(Serializable key); @Override boolean removeQuiet(Object key); @Override boolean removeQuiet(Serializable key); @Override boolean removeWithWriter(Object key); @Override Element removeAndReturnElement(Object key); @Override Element get(Object key); @Override Map<Object, Element> getAll(Collection<?> keys); @Override Element get(Serializable key); @Override Element getQuiet(Object key); @Override Element getQuiet(Serializable key); @Override Element getWithLoader(Object key, CacheLoader loader, Object loaderArgument); @Override Map getAllWithLoader(Collection keys, Object loaderArgument);  }