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

net.sf.eBus.client.ENotifyFeed Maven / Gradle / Ivy

There is a newer version: 7.6.0
Show newest version
//
// Copyright 2015, 2016, 2019 Charles W. Rapp
//
// 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 net.sf.eBus.client;

import net.sf.eBus.util.Validator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Abstract base class for {@link EPublishFeed} and
 * {@link ESubscribeFeed} classes. Tracks the current
 * {@link EFeedState feed state}.
 *
 * @author Charles W. Rapp
 */

public abstract class ENotifyFeed
    extends ESingleFeed
{
//---------------------------------------------------------------
// Member data.
//

    //-----------------------------------------------------------
    // Statics.
    //

    /**
     * Logging subsystem interface.
     */
    private static final Logger sLogger =
        LoggerFactory.getLogger(ENotifyFeed.class);

//---------------------------------------------------------------
// Member methods.
//

    //-----------------------------------------------------------
    // Constructors.
    //

    /**
     * Creates a new notification feed instance based on the
     * builder's configuration.
     * @param builder contains feed configuration.
     */
    protected ENotifyFeed(final Builder builder)
    {
        super (builder);
    } // end of ENotifyFeed(Builder)

    //
    // end of Constructors.
    //-----------------------------------------------------------

    //-----------------------------------------------------------
    // Abstract Method Declarations.
    //

    /**
     * Issues the appropriate client callback, informing the
     * client that the feed state is changed. For a publish
     * feed, issues a publish state callback; for a subscribe
     * feed, issues a subscribe state callback.
     * @param feedState the updated feed state.
     */
    /* package */ abstract void update(EFeedState feedState);

    //
    // end of Abstract Method Declarations.
    //-----------------------------------------------------------

    //-----------------------------------------------------------
    // Set Methods.
    //

    /**
     * Sets the initial activation count to the given value and
     * sets the feed state accordingly.
     * @param actCount the initial activation count.
     * @return the feed's state as a result of this activation
     * count.
     */
    /* package */ EFeedState updateActivate(final int actCount)
    {
        final EFeedState retval;

        mActivationCount += actCount;
        if (mActivationCount < 0)
        {
            sLogger.warn(
                "{} client {}, feed {}: negative activation count ({}), setting to zero ({}).",
                mEClient.location(),
                mEClient.clientId(),
                mFeedId,
                key(),
                mActivationCount,
                mScope);

            mActivationCount = 0;
        }

        // If the activation count is zero, that means this feed
        // is down due to no contra-feeds. If the count is > 0,
        // then the feed is up.
        retval = (mActivationCount == 0 ?
                  EFeedState.DOWN :
                  EFeedState.UP);

        // Did the feed state change?
        if (retval != mFeedState)
        {
            // Yes. Update the feed.
            update(retval);
        }

        sLogger.trace(
            "{} client {}, feed {}: {} feed state={}, activation count={} ({}).",
            mEClient.location(),
            mEClient.clientId(),
            mFeedId,
            key(),
            retval,
            mActivationCount,
            mScope);

        return (retval);
    } // end of updateActivation(int)

    //
    // end of Set Methods.
    //-----------------------------------------------------------

//---------------------------------------------------------------
// Inner classes.
//

    /**
     * Base class for notification publish and subscribe feeds.
     *
     * @param  feed type.
     * @param  {@link EObject} sub-type.
     * @param  builder leaf type.
     */
    protected abstract static class Builder>
        extends ESingleFeed.Builder
    {
    //-----------------------------------------------------------
    // Member data.
    //

        //-------------------------------------------------------
        // Locals.
        //

        /**
         * Set to {@code true} if this is part of a multiple key
         * feed. If {@code true}, this feed is not
         * added to the client feed list.
         * 

* Defaults to {@code false}. *

*/ protected boolean mIsMultiFlag; //----------------------------------------------------------- // Member methods. // //------------------------------------------------------- // Constructors. // /** * Creates a notification feed builder instance. * @param feedType target feed type. * @param targetClass target builder class. */ protected Builder(final FeedType feedType, final Class targetClass) { super (feedType, targetClass); mIsMultiFlag = false; } // end of Builder(FeedType, Class) // // end of Constructors. //------------------------------------------------------- //------------------------------------------------------- // Abstract Method Overrides. // /** * Returns eBus subject for the given message key. * @return eBus feed subject. */ @Override protected final ESubject getSubject() { return (ENotifySubject.findOrCreate(mKey)); } // end of getSubject() /** * Checks if message key is a notification and the * message key and scope are in agreement. * @param problems place invalid configuration settings * in this problems list. * @return {@code problems} to allow for method chaining. */ @Override @SuppressWarnings ({"java:S1067"}) protected Validator validate(final Validator problems) { return (super.validate(problems) .requireTrue((mKey != null && mKey.isNotification()), "messageKey", "messageKey is not a notification") .requireTrue( (k, s) -> (mKey != null && mScope != null && (!mKey.isLocalOnly() || mScope == FeedScope.LOCAL_ONLY)), mKey, mScope, "messageKey", "scope", String.format( "%s is local-only but feed scope is %s", mKey, mScope))); } // end of validate(Validator) // // end of Abstract Method Overrides. //------------------------------------------------------- } // end of class Builder } // end of class ENotifyFeed




© 2015 - 2025 Weber Informatics LLC | Privacy Policy