Class MatchExpression


  • public class MatchExpression
    extends java.lang.Object
    The MatchExpression class represents a matching expression used to compare custom variables or predefined properties when searching for users or Rooms.

    The matching expressions are built like "if" statements in any common programming language. They work like queries in a database and can be used to search for Rooms or users using custom criteria: in fact a matching expression can compare predefined properties of the Room and user entities (see the RoomProperties and UserProperties classes), but also custom Room or User Variables.

    These expressions are easy to create and concatenate, and they can be used for many different filtering operations within the SmartFoxServer 2X framework, for example to invite players to join a game (see the CreateSFSGameRequest request description), to look for specific Rooms or users (see the FindRoomsRequest and FindUsersRequest requests descriptions), etc.

    Additionally (see the examples for more informations):

    • any number of expressions can be linked together with the logical AND and OR operators to create complex queries;
    • searching through nested data structures such as SFSObject and SFSArray can be done via a very simple dot-syntax.

    Example
    The following example shows how to create a simple matching expression made of two concatenated conditions: it compares the custom "rank" and "country" User Variables to the passed values. This expression could be used during the creation of a Game Room, to filter the users that the server should take into account when sending the invitations to join the game (only italian users with a ranking greater than 5 - whatever this number means to our game):

     var exp:MatchExpression = new MatchExpression('rank', NumberMatch.GREATER_THAN, 5).and('country', StringMatch.EQUALS, 'Italy');
     

    Example
    The following example creates a matching expression made of three concatenated conditions which compare two predefined Room properties and the custom "isGameStarted" Room Variable to the passed values; this expression could be used to retrieve all the Game Rooms still waiting for players to join them:

     MatchExpression exp = new MatchExpression(RoomProperties.IS_GAME, BoolMatch.EQUALS, true)
                                          .and(RoomProperties.HAS_FREE_PLAYER_SLOTS, BoolMatch.EQUALS, true)
                                          .and("isGameStarted", BoolMatch.EQUALS, false);
     

    Example
    The following example creates a matching expression which compares a nested property in a complex data structure; an SFSObject called "avatarData" (could be a User Variable for example) contains the "shield" object (a nested SFSObject) which in turn contains, among others, the "inUse" property which could be used to retrieve all user whose avatars are currently equipped with a shield:

     MatchExpression exp = new MatchExpression("avatarData.shield.inUse", BoolMatch.EQUALS, true);
     

    Example
    The following example is similar to the previous one, but it involves an SFSArray. The "avatarData" object contains the "weapons" SFSArray, from which the expression retrieves the third element (that .3 means "give me the element at index == 3") that we know being the weapon the user avatar has in his right hand. Again, this element is an SFSObject containing, among the others, the "name" property which can be compared to the passed string. This example could be used to retrieve all users whose avatars have the Narsil sword in the right hand:

     MatchExpression exp = new MatchExpression("avatarData.weapons.3.name", StringMatch.EQUALS, "Narsil");
     
    See Also:
    RoomProperties, UserProperties, CreateSFSGameRequest, FindRoomsRequest, FindUsersRequest
    • Constructor Summary

      Constructors 
      Constructor Description
      MatchExpression​(java.lang.String varName, IMatcher condition, java.lang.Object value)
      Creates a new MatchExpression instance.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      MatchExpression and​(java.lang.String varName, IMatcher condition, java.lang.Object value)
      Concatenates the current expression with a new one using the logical AND operator.
      java.lang.String asString()
      * API internal usage only *
      IMatcher getCondition()
      Returns the matching criteria used during values comparison.
      LogicOperator getLogicOp()
      In case of concatenated expressions, returns the current logical operator.
      MatchExpression getNext()
      Returns the next matching expression concatenated to the current one.
      java.lang.Object getValue()
      Returns the value against which the variable or property corresponding to varName is compared.
      java.lang.String getVarName()
      Returns the name of the variable or property against which the comparison is made.
      boolean hasNext()
      Checks if the current matching expression is concatenated to another one through a logical operator.
      MatchExpression or​(java.lang.String varName, IMatcher condition, java.lang.Object value)
      Concatenates the current expression with a new one using the logical OR operator.
      MatchExpression rewind()
      Moves the iterator cursor to the first matching expression in the chain.
      com.smartfoxserver.v2.entities.data.ISFSArray toSFSArray()
      * API internal usage only *
      java.lang.String toString()
      Returns a string representation of the matching expression.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • MatchExpression

        public MatchExpression​(java.lang.String varName,
                               IMatcher condition,
                               java.lang.Object value)
        Creates a new MatchExpression instance.
        Parameters:
        varName - Name of the variable or property to match.
        condition - The matching condition.
        value - The value to compare against the variable or property during the matching.
        See Also:
        varName, condition, value
    • Method Detail

      • and

        public MatchExpression and​(java.lang.String varName,
                                   IMatcher condition,
                                   java.lang.Object value)
        Concatenates the current expression with a new one using the logical AND operator.
        Parameters:
        varName - The name of the additional variable or property to match.
        condition - The additional matching condition.
        value - The value to compare against the additional variable or property during the matching.
        Returns:
        A new MatchExpression resulting from the concatenation of the current expression with a new one generated from the specified parameters.
        See Also:
        varName, condition, value, LogicOperator.AND
      • or

        public MatchExpression or​(java.lang.String varName,
                                  IMatcher condition,
                                  java.lang.Object value)
        Concatenates the current expression with a new one using the logical OR operator.
        Parameters:
        varName - The name of the additional variable or property to match.
        condition - The additional matching condition.
        value - The value to compare against the additional variable or property during the matching.
        Returns:
        A new MatchExpression resulting from the concatenation of the current expression with a new one generated from the specified parameters.
        See Also:
        varName, condition, value, LogicOperator.OR
      • getVarName

        public java.lang.String getVarName()
        Returns the name of the variable or property against which the comparison is made.

        Depending what the matching expression is used for (searching a user or a Room), this can be the name of a User Variable or a Room Variable, or it can be one of the constants contained in the UserProperties or RoomProperties classes, representing some of the predefined properties of the user and Room entities respectively.

        See Also:
        RoomProperties, UserProperties, RoomVariable, UserVariable, Room, User
      • getCondition

        public IMatcher getCondition()
        Returns the matching criteria used during values comparison.

        Different objects implementing the IMatcher interface can be used, depending on the type of the variable or property to check.

        See Also:
        BoolMatch, NumberMatch, StringMatch
      • getValue

        public java.lang.Object getValue()
        Returns the value against which the variable or property corresponding to varName is compared.
      • getLogicOp

        public LogicOperator getLogicOp()
        In case of concatenated expressions, returns the current logical operator.

        The default value is null

      • hasNext

        public boolean hasNext()
        Checks if the current matching expression is concatenated to another one through a logical operator.
        Returns:
        true if the current matching expression is concatenated to another one.
        See Also:
        getLogicOp()
      • getNext

        public MatchExpression getNext()
        Returns the next matching expression concatenated to the current one.
      • rewind

        public MatchExpression rewind()
        Moves the iterator cursor to the first matching expression in the chain.
        Returns:
        The MatchExpression object at the top of the chain of matching expressions.
      • asString

        public java.lang.String asString()
        * API internal usage only *
      • toString

        public java.lang.String toString()
        Returns a string representation of the matching expression.
        Overrides:
        toString in class java.lang.Object
        Returns:
        The string representation of the MatchExpression object.
      • toSFSArray

        public com.smartfoxserver.v2.entities.data.ISFSArray toSFSArray()
        * API internal usage only *