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

com.tangosol.util.listener.SimpleMapListener Maven / Gradle / Ivy

There is a newer version: 24.03
Show newest version
/*
 * Copyright (c) 2000, 2020, Oracle and/or its affiliates.
 *
 * Licensed under the Universal Permissive License v 1.0 as shown at
 * http://oss.oracle.com/licenses/upl.
 */
package com.tangosol.util.listener;

import com.tangosol.util.MapEvent;
import com.tangosol.util.MapListener;
import com.tangosol.util.MultiplexingMapListener;

import java.util.function.Consumer;

/**
 * An implementation of a {@link MapListener} that delegates to the appropriate
 * {@link Consumer event handler} based on the event type.
 *
 * @author as  2015.02.12
 * @since 12.2.1
 */
public class SimpleMapListener
        implements MapListener
    {
    // ---- event handler registration methods ------------------------------

    /**
     * Add the event handler for INSERT events.
     *
     * @param onInsert  the event handler to add
     *
     * @return  this MapListener
     */
    public SimpleMapListener addInsertHandler(Consumer> onInsert)
        {
        m_onInsert = addHandler(m_onInsert,
                                (Consumer>) onInsert);
        return this;
        }

    /**
     * Add the event handler for UPDATE events.
     *
     * @param onUpdate  the event handler to execute
     *
     * @return  this MapListener
     */
    public SimpleMapListener addUpdateHandler(Consumer> onUpdate)
        {
        m_onUpdate = addHandler(m_onUpdate,
                                (Consumer>) onUpdate);
        return this;
        }

    /**
     * Add the event handler for DELETE events.
     *
     * @param onDelete  the event handler to execute
     *
     * @return  this MapListener
     */
    public SimpleMapListener addDeleteHandler(Consumer> onDelete)
        {
        m_onDelete = addHandler(m_onDelete,
                                (Consumer>) onDelete);
        return this;
        }

    /**
     * Add the event handler for all events.
     *
     * @param onEvent  the event handler to execute
     *
     * @return  this MapListener
     *
     * @see MultiplexingMapListener
     */
    public SimpleMapListener addEventHandler(Consumer> onEvent)
        {
        return addInsertHandler(onEvent)
               .addUpdateHandler(onEvent)
               .addDeleteHandler(onEvent);
        }

    // ---- MapListener interface -------------------------------------------

    @Override
    public void entryInserted(MapEvent evt)
        {
        if (m_onInsert != null)
            {
            m_onInsert.accept(evt);
            }
        }

    @Override
    public void entryUpdated(MapEvent evt)
        {
        if (m_onUpdate != null)
            {
            m_onUpdate.accept(evt);
            }
        }

    @Override
    public void entryDeleted(MapEvent evt)
        {
        if (m_onDelete != null)
            {
            m_onDelete.accept(evt);
            }
        }

    // ---- helper methods --------------------------------------------------

    /**
     * Add a handler to a handler chain.
     *
     * @param handlerChain  the existing handler chain (could be null)
     * @param handler       the handler to add
     *
     * @return new handler chain
     */
    protected Consumer> addHandler(Consumer> handlerChain,
                                                                      Consumer> handler)
        {
        return handlerChain == null
               ? handler
               : handlerChain.andThen(handler);
        }

    // ---- data members ----------------------------------------------------

    /**
     * The event handler to execute on INSERT event.
     */
    protected Consumer> m_onInsert;

    /**
     * The event handler to execute on UPDATE event.
     */
    protected Consumer> m_onUpdate;

    /**
     * The event handler to execute on DELETE event.
     */
    protected Consumer> m_onDelete;
    }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy