All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ehcache.transactions.xa.internal.StoreEventSourceWrapper Maven / Gradle / Ivy

There is a newer version: 3.10.8
Show newest version
/*
 * 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.event.EventType;
import org.ehcache.impl.internal.concurrent.ConcurrentHashMap;
import org.ehcache.impl.internal.events.StoreEventImpl;
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 java.util.Map;

import static org.ehcache.impl.internal.events.StoreEvents.createEvent;
import static org.ehcache.impl.internal.events.StoreEvents.updateEvent;

/**
 * StoreEventSourceWrapper
 */
class StoreEventSourceWrapper implements StoreEventSource {

  private final StoreEventSource> underlying;
  private final Map, StoreEventListener>> listenersMap = new ConcurrentHashMap, StoreEventListener>>(10);

  StoreEventSourceWrapper(StoreEventSource> underlying) {
    this.underlying = underlying;
    underlying.addEventFilter(new StoreEventFilter>() {
      @Override
      public boolean acceptEvent(EventType type, K key, SoftLock oldValue, SoftLock 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(new StoreEventFilter>() {
      @Override
      public boolean acceptEvent(EventType type, K key, SoftLock oldValue, SoftLock 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 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) {
      StoreEvent eventToPropagate = null;
      switch (event.getType()) {
        case CREATED:
          eventToPropagate = createEvent(event.getKey(), event.getNewValue().getOldValue());
          break;
        case UPDATED:
          eventToPropagate = updateEvent(event.getKey(), event.getOldValue().getOldValue(), event.getNewValue()
                .getOldValue());
          break;
        case REMOVED:
        case EXPIRED:
        case EVICTED:
          eventToPropagate = new StoreEventImpl(event.getType(), event.getKey(), event.getOldValue().getOldValue(), null);
          break;
      }
      wrappedOne.onEvent(eventToPropagate);
    }

    @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();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy