View Javadoc

    1   // ========================================================================
    2   // Copyright 2007 Mort Bay Consulting Pty. Ltd.
    3   // ------------------------------------------------------------------------
    4   // Licensed under the Apache License, Version 2.0 (the "License");
    5   // you may not use this file except in compliance with the License.
    6   // You may obtain a copy of the License at
    7   // http://www.apache.org/licenses/LICENSE-2.0
    8   // Unless required by applicable law or agreed to in writing, software
    9   // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  //========================================================================
    14
    15  package org.mortbay.cometd;
    16
    17
    18
    19
    20  public class ChannelId
    21  {
    22      public final static String WILD="*";
    23      public final static String WILDWILD="**";
    24
    25      private static final String[] ROOT = {};
    26      String _name;
    27      String[] _segments;
    28      int _wild;
    29
    30      public ChannelId(String name)
    31      {
    32          _name=name;
    33          if (name==null || name.length()==0 || name.charAt(0)!='/')
    34              throw new IllegalArgumentException(name);
    35
    36          if ("/".equals(name))
    37          {
    38              _segments=ROOT;
    39          }
    40          else
    41          {
    42              if (name.charAt(name.length()-1)=='/')
    43                  throw new IllegalArgumentException(name);
    44
    45              _segments=name.substring(1).split("/");
    46          }
    47
    48          if (_segments.length==0)
    49              _wild=0;
    50          else if (WILD.equals(_segments[_segments.length-1]))
    51              _wild=1;
    52          else if (WILDWILD.equals(_segments[_segments.length-1]))
    53              _wild=2;
    54      }
    55
    56      public boolean isWild()
    57      {
    58          return _wild>0;
    59      }
    60
    61      @Override
    62      public boolean equals(Object obj)
    63      {
    64          if (this==obj)
    65              return true;
    66
    67          if (obj instanceof ChannelId)
    68          {
    69              ChannelId other=(ChannelId)obj;
    70              if (isWild())
    71              {
    72                  if (other.isWild())
    73                      return _name.equals(other._name);
    74                  return matches(other);
    75              }
    76              else
    77              {
    78                  if (other.isWild())
    79                      return other.matches(this);
    80                  return _name.equals(other._name);
    81              }
    82          }
    83          else if (obj instanceof String)
    84          {
    85              if (isWild())
    86                  return matches((String)obj);
    87              return _name.equals(obj);
    88          }
    89
    90          return false;
    91      }
    92
    93      public boolean matches(ChannelId name)
    94      {
    95          if (name.isWild())
    96              return equals(name);
    97
    98          switch(_wild)
    99          {
    100             case 0:
    101                 return equals(name);
    102             case 1:
    103                 if (name._segments.length!=_segments.length)
    104                     return false;
    105                 for (int i=_segments.length-1;i-->0;)
    106                     if (!_segments[i].equals(name._segments[i]))
    107                         return false;
    108                 return true;
    109
    110             case 2:
    111                 if (name._segments.length<_segments.length)
    112                     return false;
    113                 for (int i=_segments.length-1;i-->0;)
    114                     if (!_segments[i].equals(name._segments[i]))
    115                         return false;
    116                 return true;
    117         }
    118         return false;
    119     }
    120
    121     public boolean matches(String name)
    122     {
    123         if (_wild==0)
    124             return _name.equals(name);
    125
    126         // TODO more efficient?
    127         return matches(new ChannelId(name));
    128     }
    129
    130     @Override
    131     public int hashCode()
    132     {
    133         return _name.hashCode();
    134     }
    135
    136     @Override
    137     public String toString()
    138     {
    139         return _name;
    140     }
    141
    142     public int depth()
    143     {
    144         return _segments.length;
    145     }
    146
    147     public boolean isParentOf(ChannelId id)
    148     {
    149         if (isWild() || depth()>=id.depth())
    150             return false;
    151
    152         for (int i=_segments.length-1;i-->0;)
    153             if (!_segments[i].equals(id._segments[i]))
    154                 return false;
    155
    156         return true;
    157     }
    158
    159     public String getSegment(int i)
    160     {
    161         if (i>_segments.length)
    162             return null;
    163         return _segments[i];
    164     }
    165 }