Class AbstractTokenizedActivationStrategy

java.lang.Object
org.togglz.core.activation.AbstractTokenizedActivationStrategy
All Implemented Interfaces:
ActivationStrategy

public abstract class AbstractTokenizedActivationStrategy extends Object implements ActivationStrategy

An abstract activation strategy that is designed to support cases where a specific parameter contains comma-separated tokens that can be negated by prefixing the value with the NOT operator (!).

AbstractTokenizedActivationStrategy makes no real assumptions on how implementations will use the tokens to determine whether the feature is active and, instead, simply tokenizes the parameter value. It even allows for the token values to be transformed during this process by specifying a AbstractTokenizedActivationStrategy.TokenTransformer.

Implementations are responsible for honoring the negated state of any tokens. Fortunately, this is very simple to do:

 @Override
 protected boolean isActive(FeatureState featureState, FeatureUser user, List<Token> tokens) {
     for (Token token : tokens) {
         boolean active = doSomeCheckOnTokenValue(token.getValue());
         if (active != token.isNegated()) {
             return true;
         }
     }

     return false;
 }
 
Author:
Alasdair Mercer
See Also:
  • Constructor Details

    • AbstractTokenizedActivationStrategy

      public AbstractTokenizedActivationStrategy()
  • Method Details

    • isActive

      public final boolean isActive(FeatureState featureState, FeatureUser user)
      Description copied from interface: ActivationStrategy
      This method is responsible to decide whether a feature is active or not. The implementation can use the custom configuration parameters of the strategy stored in the feature state and information from the currently acting user to find a decision.
      Specified by:
      isActive in interface ActivationStrategy
      Parameters:
      featureState - The feature state which represents the current configuration of the feature. The implementation of the method typically uses FeatureState.getParameter(String) to access custom configuration parameter values.
      user - The user for which to decide whether the feature is active. May be null if the user could not be identified by the UserProvider.
      Returns:
      true if the feature should be active, else false
    • isActive

      protected abstract boolean isActive(FeatureState featureState, FeatureUser user, List<AbstractTokenizedActivationStrategy.Token> tokens)

      This method is called by isActive(FeatureState, FeatureUser) with the parsed tokens to make the decision as to whether the feature is active.

      Parameters:
      featureState - the FeatureState which represents the current configuration of the feature
      user - the user for which to decide whether the feature is active (may be null)
      tokens - the List of Tokens parsed from the parameter value
      Returns:
      true if the feature should be active; otherwise false.
    • tokenize

      Looks up and tokenizes the value of the parameter with the given name on the feature.

      If transformer is not null, it will be asked to transform each individual token value.

      Parameters:
      featureState - the FeatureState which represents the current configuration of the feature
      parameterName - the name of the parameter whose value is to be tokenized
      transformer - the AbstractTokenizedActivationStrategy.TokenTransformer to be used to transform the value of each token (may be null to use the token values as-provided)
      Returns:
      A List of Tokens extracted from the value of the parameter with the specified name.
    • getTokenParameterName

      public abstract String getTokenParameterName()

      Returns the name of the parameter whose value is to be tokenized.

      Returns:
      The name of the parameter containing tokens.
    • getTokenParameterTransformer

      public AbstractTokenizedActivationStrategy.TokenTransformer getTokenParameterTransformer()

      Returns the transformer to be used to transform the value of each AbstractTokenizedActivationStrategy.Token.

      By default, this method returns null, meaning that the token values are used as-provided.

      Returns:
      The AbstractTokenizedActivationStrategy.TokenTransformer to be used to transform token values or null to use the original values.