Class ChannelId


  • public class ChannelId
    extends java.lang.Object

    Reification of a channel id with methods to test properties and compare with other ChannelIds.

    A ChannelId breaks the channel id into path segments so that, for example, /foo/bar breaks into ["foo","bar"].

    ChannelId can be wild, when they end with one or two wild characters "*"; a ChannelId is 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/**).

    ChannelId can 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 \w regular expression character class.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String DEEPWILD  
      static java.lang.String WILD  
    • Constructor Summary

      Constructors 
      Constructor Description
      ChannelId​(java.lang.String id)
      Constructs a new ChannelId with the given id
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.Map<java.lang.String,java.lang.String> bind​(ChannelId target)
      If this ChannelId is a template, and the given target ChannelId is non-wild and non-template, and the two have the same depth(), then binds the variable(s) defined in this template with the values of the segments defined by the target ChannelId.
      int depth()  
      boolean equals​(java.lang.Object obj)  
      java.lang.String getId()
      Returns the normalized channel id string.
      java.util.List<java.lang.String> getParameters()  
      java.lang.String getParent()  
      java.lang.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.
      java.lang.String getSegment​(int i)  
      java.util.List<java.lang.String> getWilds()  
      int hashCode()  
      boolean isAncestorOf​(ChannelId id)  
      boolean isBroadcast()  
      static boolean isBroadcast​(java.lang.String channelId)
      Helper method to test if the string form of a ChannelId represents a broadcast ChannelId.
      boolean isDeepWild()
      Deep wild ChannelIds end with a double wild character "**" and match non wild channels with the same or greater depth.
      boolean isMeta()
      A ChannelId is a meta ChannelId if it starts with "/meta/".
      static boolean isMeta​(java.lang.String channelId)
      Helper method to test if the string form of a ChannelId represents a meta ChannelId.
      boolean isParentOf​(ChannelId id)  
      boolean isService()
      A ChannelId is a service ChannelId if it starts with "/service/".
      static boolean isService​(java.lang.String channelId)
      Helper method to test if the string form of a ChannelId represents a service ChannelId.
      boolean isShallowWild()
      Shallow wild ChannelIds end with a single wild character "*" and match non wild channels with the same depth.
      boolean isTemplate()  
      boolean isWild()  
      boolean matches​(ChannelId channelId)
      Tests whether this ChannelId matches the given ChannelId.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • ChannelId

        public ChannelId​(java.lang.String id)
        Constructs a new ChannelId with the given id
        Parameters:
        id - the channel id in string form
    • Method Detail

      • getId

        public java.lang.String getId()

        Returns the normalized channel id string.

        Normalization involves trimming white spaces and removing trailing slashes.

        Returns:
        the normalized channel id string
      • isShallowWild

        public boolean isShallowWild()

        Shallow wild ChannelIds end with a single wild character "*" and match non wild channels with the same depth.

        Example: /foo/* matches /foo/bar, but not /foo/bar/baz.

        Returns:
        whether this ChannelId is a shallow wild channel id
      • isDeepWild

        public boolean isDeepWild()

        Deep wild ChannelIds end with a double wild character "**" and match non wild channels with the same or greater depth.

        Example: /foo/** matches /foo/bar and /foo/bar/baz.

        Returns:
        whether this ChannelId is a deep wild channel id
      • isMeta

        public boolean isMeta()

        A ChannelId is a meta ChannelId if it starts with "/meta/".

        Returns:
        whether the first segment is "meta"
      • isService

        public boolean isService()

        A ChannelId is a service ChannelId if it starts with "/service/".

        Returns:
        whether the first segment is "service"
      • isBroadcast

        public boolean isBroadcast()
        Returns:
        whether this ChannelId is neither meta nor service
      • isTemplate

        public boolean isTemplate()
        Returns:
        whether this ChannelId is 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 java.util.List<java.lang.String> getParameters()
        Returns:
        the list of variable names if this ChannelId is a template, otherwise an empty list.
        See Also:
        isTemplate()
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • matches

        public boolean matches​(ChannelId channelId)

        Tests whether this ChannelId matches the given ChannelId.

        If the given ChannelId is wild, then it matches only if it is equal to this ChannelId.

        If this ChannelId is non-wild, then it matches only if it is equal to the given ChannelId.

        Otherwise, this ChannelId is either shallow or deep wild, and matches ChannelIds with the same number of equal segments (if it is shallow wild), or ChannelIds 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 ChannelId matches the given ChannelId
      • bind

        public java.util.Map<java.lang.String,java.lang.String> bind​(ChannelId target)

        If this ChannelId is a template, and the given target ChannelId is non-wild and non-template, and the two have the same depth(), then binds the variable(s) defined in this template with the values of the segments defined by the target ChannelId.

        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-template ChannelId to bind
        Returns:
        a map withe the bindings, or an empty map if no binding was possible
        See Also:
        isTemplate()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • depth

        public int depth()
        Returns:
        how many segments this ChannelId is made of
        See Also:
        getSegment(int)
      • isAncestorOf

        public boolean isAncestorOf​(ChannelId id)
        Parameters:
        id - the channel to test
        Returns:
        whether this ChannelId is an ancestor of the given ChannelId
        See Also:
        isParentOf(ChannelId)
      • isParentOf

        public boolean isParentOf​(ChannelId id)
        Parameters:
        id - the channel to test
        Returns:
        whether this ChannelId is the parent of the given ChannelId
        See Also:
        isAncestorOf(ChannelId)
      • getParent

        public java.lang.String getParent()
        Returns:
        the channel string parent of this ChannelId, or null if this ChannelId has only one segment
        See Also:
        isParentOf(ChannelId)
      • getSegment

        public java.lang.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 java.util.List<java.lang.String> getWilds()
        Returns:
        The list of wilds channels that match this channel, or the empty list if this channel is already wild.
      • getRegularPart

        public java.lang.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​(java.lang.String channelId)

        Helper method to test if the string form of a ChannelId represents a meta ChannelId.

        Parameters:
        channelId - the channel id to test
        Returns:
        whether the given channel id is a meta channel id
      • isService

        public static boolean isService​(java.lang.String channelId)

        Helper method to test if the string form of a ChannelId represents a service ChannelId.

        Parameters:
        channelId - the channel id to test
        Returns:
        whether the given channel id is a service channel id
      • isBroadcast

        public static boolean isBroadcast​(java.lang.String channelId)

        Helper method to test if the string form of a ChannelId represents a broadcast ChannelId.

        Parameters:
        channelId - the channel id to test
        Returns:
        whether the given channel id is a broadcast channel id