Class MatchExpression
- java.lang.Object
-
- com.smartfoxserver.v2.entities.match.MatchExpression
-
- All Implemented Interfaces:
java.io.Serializable
public class MatchExpression extends java.lang.Object implements java.io.Serializable
Overview
Match Expressions are built like "if" conditions 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. These expressions are extremely easy to create and concatenate and they can be used for many different filtering operations within the SFS2X framework.This is a quick example:
MatchExpression exp = new MatchExpression('rank', NumberMatch.GREATER_THAN, 5).and('country', StringMatch.EQUALS, 'Italy')
Expressions are made of three elements:
- Variable name
- Match operator
- Value
The search options are not just limited to User/Room Variables name. In fact the Matching engine provides two extra classes, RoomProperties and UserProperties, where you can access many specific attributes of the Room and User class.
This is an example of matching specific Room properties and Variables:
// Prepare match expression MatchExpression exp = new MatchExpression(RoomProperties.IS_GAME, BoolMatch.EQUALS, true).and (RoomProperties.HAS_FREE_PLAYER_SLOTS, BoolMatch.EQUALS, true).and ("isGameStarted", BoolMatch.EQUALS, false); // Search Rooms List<Rooms> joinableRooms = sfsApi.findRooms(zone.getRoomListFromGroup("chess"), exp, 0);
Advanced features
the Match expression offer advanced capabilities of searching through nested data structures such as SFSObject and SFSArray. This is done via a very simple dot-syntax. Here's an example of how it works:MatchExpression exp = new MatchExpression("europe.italy.capital", StringMatch.EQUALS, "Rome")
The above example goes down deep into an SFSObject called europe, taking the italy object (another SFSObject) and finally reading its String field capital and matching it with another String. Here is one more examples using SFSObject and SFSArray:
MatchExpression exp = new MatchExpression("europe.italy.majorCities.3.name", StringMatch.EQUALS, "Milan")
From the italy object we obtain a majorCities SFSArray and we grab the third item in it (the .3 expression means 'give me the element at index == 3'). The item is again an SFSObject whose name property we finally compare to a String.
The power of Match Expression doesn't end here. You can run multiple passes of matching if you need complex searches to be performed. For example you can run a first match and obtain a list of filtered Rooms and then use it to apply another expression to further refine your search, and so on and so forth.
Also you will learn more interesting usages of Match Expressions in conjunction with the SFSGame class later in this very article.- See Also:
RoomProperties
,UserProperties
, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description MatchExpression(java.lang.String varName, com.smartfoxserver.v2.entities.match.IMatcher condition, java.lang.Object value)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description MatchExpression
and(java.lang.String varName, com.smartfoxserver.v2.entities.match.IMatcher condition, java.lang.Object value)
Concatenates two expressions with the logical AND operator.static MatchExpression
fromSFSArray(ISFSArray sfsa)
boolean
hasNext()
Check if the current expression is concatenated to another one via a logic operatorMatchExpression
next()
Get the next expression chained to the current one.MatchExpression
or(java.lang.String varName, com.smartfoxserver.v2.entities.match.IMatcher condition, java.lang.Object value)
Concatenates two expressions with the logical OR operator.MatchExpression
rewind()
Rewinds the cursor to the first expression in the chain and return the MatchExpression at the top of the chain of expressionsISFSArray
toSFSArray()
privatejava.lang.String
toString()
-
-
-
Method Detail
-
and
public MatchExpression and(java.lang.String varName, com.smartfoxserver.v2.entities.match.IMatcher condition, java.lang.Object value)
Concatenates two expressions with the logical AND operator.- Parameters:
varName
- name of the variable or propertycondition
- the match conditionvalue
- the value to match- Returns:
- the new resulting MatchExpression
- See Also:
LogicOperator
-
or
public MatchExpression or(java.lang.String varName, com.smartfoxserver.v2.entities.match.IMatcher condition, java.lang.Object value)
Concatenates two expressions with the logical OR operator.- Parameters:
varName
- name of the variable or propertycondition
- the match conditionvalue
- the value to match- Returns:
- the new resulting MatchExpression
- See Also:
LogicOperator
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
hasNext
public boolean hasNext()
Check if the current expression is concatenated to another one via a logic operator- Returns:
- true if the current expression is concatenated to another one via a logic operator
- See Also:
LogicOperator
-
next
public MatchExpression next()
Get the next expression chained to the current one.- Returns:
- the next expression chained to the current one.
-
rewind
public MatchExpression rewind()
Rewinds the cursor to the first expression in the chain and return the MatchExpression at the top of the chain of expressions- Returns:
- the MatchExpression at the top of the chain of expressions
-
fromSFSArray
public static MatchExpression fromSFSArray(ISFSArray sfsa)
-
toSFSArray
public ISFSArray toSFSArray()
private
-
-