org.ehcache.transactions.xa.internal.StoreEventSourceWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ehcache-transactions Show documentation
Show all versions of ehcache-transactions Show documentation
The transactions module of Ehcache 3
/*
* Copyright Terracotta, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ehcache.transactions.xa.internal;
import org.ehcache.core.spi.store.events.StoreEvent;
import org.ehcache.core.spi.store.events.StoreEventFilter;
import org.ehcache.core.spi.store.events.StoreEventListener;
import org.ehcache.core.spi.store.events.StoreEventSource;
import org.ehcache.event.EventType;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* StoreEventSourceWrapper
*/
class StoreEventSourceWrapper implements StoreEventSource {
private final StoreEventSource> underlying;
private final Map, StoreEventListener>> listenersMap = new ConcurrentHashMap<>(10);
StoreEventSourceWrapper(StoreEventSource> underlying) {
this.underlying = underlying;
underlying.addEventFilter((type, key, oldValue, newValue) -> {
if (newValue != null) {
return newValue.getOldValue() != null;
} else if (oldValue != null) {
return oldValue.getOldValue() != null;
}
return false;
});
}
@Override
public void addEventListener(final StoreEventListener eventListener) {
StoreEventListenerWrapper listenerWrapper = new StoreEventListenerWrapper<>(eventListener);
listenersMap.put(eventListener, listenerWrapper);
underlying.addEventListener(listenerWrapper);
}
@Override
public void removeEventListener(StoreEventListener eventListener) {
StoreEventListener> listenerWrapper = listenersMap.get(eventListener);
if (listenerWrapper != null) {
underlying.removeEventListener(listenerWrapper);
}
}
@Override
public void addEventFilter(final StoreEventFilter eventFilter) {
underlying.addEventFilter((type, key, oldValue, newValue) -> {
V unwrappedOldValue = null;
V unwrappedNewValue = null;
if (oldValue != null) {
unwrappedOldValue = oldValue.getOldValue();
}
if (newValue != null) {
unwrappedNewValue = newValue.getOldValue();
}
if (unwrappedNewValue == null && unwrappedOldValue == null) {
return false;
}
return eventFilter.acceptEvent(type, key, unwrappedOldValue, unwrappedNewValue);
});
}
@Override
public void setEventOrdering(boolean ordering) {
underlying.setEventOrdering(ordering);
}
@Override
public void setSynchronous(boolean synchronous) throws IllegalArgumentException {
underlying.setSynchronous(synchronous);
}
@Override
public boolean isEventOrdering() {
return underlying.isEventOrdering();
}
private static class StoreEventListenerWrapper implements StoreEventListener> {
private final StoreEventListener wrappedOne;
private StoreEventListenerWrapper(StoreEventListener wrappedOne) {
if (wrappedOne == null) {
throw new NullPointerException("Wrapped StoreEventListener cannot be null");
}
this.wrappedOne = wrappedOne;
}
@Override
public void onEvent(StoreEvent> event) {
wrappedOne.onEvent(new XaEvent<>(event));
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
StoreEventListenerWrapper, ?> that = (StoreEventListenerWrapper, ?>) o;
return wrappedOne.equals(that.wrappedOne);
}
@Override
public int hashCode() {
return wrappedOne.hashCode();
}
}
static class XaEvent implements StoreEvent {
private final StoreEvent> delegate;
XaEvent(StoreEvent> delegate) {
this.delegate = delegate;
}
@Override
public EventType getType() {
return delegate.getType();
}
@Override
public K getKey() {
return delegate.getKey();
}
@Override
public V getNewValue() {
SoftLock newValue = delegate.getNewValue();
if (newValue == null) {
return null;
} else {
return newValue.getOldValue();
}
}
@Override
public V getOldValue() {
SoftLock oldValue = delegate.getOldValue();
if (oldValue == null) {
return null;
} else {
return oldValue.getOldValue();
}
}
}
}