Class ChannelId
- java.lang.Object
-
- org.cometd.bayeux.ChannelId
-
public class ChannelId extends Object
Reification of a
channel idwith methods to test properties and compare with otherChannelIds.A
ChannelIdbreaks the channel id into path segments so that, for example,/foo/barbreaks into["foo","bar"].ChannelIdcan be wild, when they end with one or two wild characters"*"; aChannelIdis shallow wild if it ends with one wild character (for example/foo/bar/*) and deep wild if it ends with two wild characters (for example/foo/bar/**).ChannelIdcan be a template, when a segment contains variable names surrounded by braces, for example/foo/{var_name}. Variable names can only be made of characters defined by the\wregular expression character class.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Map<String,String>bind(ChannelId target)If thisChannelIdis a template, and the giventargetChannelIdis non-wild and non-template, and the two have the samedepth(), then binds the variable(s) defined in this template with the values of the segments defined by the targetChannelId.intdepth()booleanequals(Object obj)StringgetId()Returns the normalized channel id string.List<String>getParameters()StringgetParent()StringgetRegularPart()Returns the regular part of thisChannelId: the part of the channel id from the beginning until the first occurrence of a parameter or a wild character.StringgetSegment(int i)List<String>getWilds()inthashCode()booleanisAncestorOf(ChannelId id)booleanisBroadcast()static booleanisBroadcast(String channelId)booleanisDeepWild()booleanisMeta()AChannelIdis a metaChannelIdif it starts with"/meta/".static booleanisMeta(String channelId)booleanisParentOf(ChannelId id)booleanisService()AChannelIdis a serviceChannelIdif it starts with"/service/".static booleanisService(String channelId)booleanisShallowWild()booleanisTemplate()booleanisWild()booleanmatches(ChannelId channelId)Tests whether thisChannelIdmatches the givenChannelId.StringtoString()
-
-
-
Field Detail
-
WILD
public static final String WILD
- See Also:
- Constant Field Values
-
DEEPWILD
public static final String DEEPWILD
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ChannelId
public ChannelId(String id)
Constructs a newChannelIdwith the given id- Parameters:
id- the channel id in string form
-
-
Method Detail
-
getId
public String getId()
Returns the normalized channel id string.
Normalization involves trimming white spaces and removing trailing slashes.
- Returns:
- the normalized channel id string
-
isWild
public boolean isWild()
- Returns:
- whether this
ChannelIdis eithershallow wildordeep wild
-
isShallowWild
public boolean isShallowWild()
Shallow wild
ChannelIds end with a single wild character"*"andmatchnon wild channels with the samedepth.Example:
/foo/*matches/foo/bar, but not/foo/bar/baz.- Returns:
- whether this
ChannelIdis a shallow wild channel id
-
isDeepWild
public boolean isDeepWild()
Deep wild
ChannelIds end with a double wild character "**" andmatchnon wild channels with the same or greaterdepth.Example:
/foo/**matches/foo/barand/foo/bar/baz.- Returns:
- whether this
ChannelIdis a deep wild channel id
-
isMeta
public boolean isMeta()
A
ChannelIdis a metaChannelIdif it starts with"/meta/".- Returns:
- whether the first segment is "meta"
-
isService
public boolean isService()
A
ChannelIdis a serviceChannelIdif it starts with"/service/".- Returns:
- whether the first segment is "service"
-
isBroadcast
public boolean isBroadcast()
-
isTemplate
public boolean isTemplate()
- Returns:
- whether this
ChannelIdis a template, that is it contains segments that identify a variable name between braces, such as/foo/{var_name}. - See Also:
bind(ChannelId),getParameters()
-
getParameters
public List<String> getParameters()
- Returns:
- the list of variable names if this
ChannelIdis a template, otherwise an empty list. - See Also:
isTemplate()
-
matches
public boolean matches(ChannelId channelId)
Tests whether this
ChannelIdmatches the givenChannelId.If the given
ChannelIdiswild, then it matches only if it is equal to thisChannelId.If this
ChannelIdis non-wild, then it matches only if it is equal to the givenChannelId.Otherwise, this
ChannelIdis either shallow or deep wild, and matchesChannelIds with the same number of equal segments (if it is shallow wild), orChannelIds with the same or a greater number of equal segments (if it is deep wild).- Parameters:
channelId- the channelId to match- Returns:
- true if this
ChannelIdmatches the givenChannelId
-
bind
public Map<String,String> bind(ChannelId target)
If this
ChannelIdis a template, and the giventargetChannelIdis non-wild and non-template, and the two have the samedepth(), then binds the variable(s) defined in this template with the values of the segments defined by the targetChannelId.For example:
// template and target match. Map<String, String> bindings = new ChannelId("/a/{var1}/c/{var2}").bind(new ChannelId("/a/foo/c/bar")); bindings: {"var1": "foo", "var2": "bar"} // template has 2 segments, target has only 1 segment. bindings = new ChannelId("/a/{var1}").bind(new ChannelId("/a")) bindings = {} // template has 2 segments, target too many segments. bindings = new ChannelId("/a/{var1}").bind(new ChannelId("/a/b/c")) bindings = {} // same number of segments, but no match on non-variable segments. bindings = new ChannelId("/a/{var1}").bind(new ChannelId("/b/c")) bindings = {}The returned map may not preserve the order of variables present in the template
ChannelId.- Parameters:
target- the non-wild, non-templateChannelIdto bind- Returns:
- a map withe the bindings, or an empty map if no binding was possible
- See Also:
isTemplate()
-
depth
public int depth()
- Returns:
- how many segments this
ChannelIdis made of - See Also:
getSegment(int)
-
isAncestorOf
public boolean isAncestorOf(ChannelId id)
- Parameters:
id- the channel to test- Returns:
- whether this
ChannelIdis an ancestor of the givenChannelId - See Also:
isParentOf(ChannelId)
-
isParentOf
public boolean isParentOf(ChannelId id)
- Parameters:
id- the channel to test- Returns:
- whether this
ChannelIdis the parent of the givenChannelId - See Also:
isAncestorOf(ChannelId)
-
getParent
public String getParent()
- Returns:
- the channel string parent of this
ChannelId, or null if thisChannelIdhas only one segment - See Also:
isParentOf(ChannelId)
-
getSegment
public String getSegment(int i)
- Parameters:
i- the segment index- Returns:
- the i-nth segment of this channel, or null if no such segment exist
- See Also:
depth()
-
getWilds
public List<String> getWilds()
- Returns:
- The list of wilds channels that match this channel, or the empty list if this channel is already wild.
-
getRegularPart
public String getRegularPart()
Returns the regular part of this
ChannelId: the part of the channel id from the beginning until the first occurrence of a parameter or a wild character.Examples:
ChannelId.regularPart Examples Channel Regular Part /foo /foo /foo/* /foo /foo/bar/** /foo/bar /foo/{p} /foo /foo/bar/{p} /foo/bar /* null/** null/{p} null- Returns:
- the regular part of this channel
-
isMeta
public static boolean isMeta(String channelId)
Helper method to test if the string form of a
ChannelIdrepresents ametaChannelId.- Parameters:
channelId- the channel id to test- Returns:
- whether the given channel id is a meta channel id
-
isService
public static boolean isService(String channelId)
Helper method to test if the string form of a
ChannelIdrepresents aserviceChannelId.- Parameters:
channelId- the channel id to test- Returns:
- whether the given channel id is a service channel id
-
-