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  public class ChannelId
18  {
19      public final static String WILD="*";
20      public final static String WILDWILD="**";
21  
22      private static final String[] ROOT={};
23      String _name;
24      String[] _segments;
25      int _wild;
26  
27      public ChannelId(String name)
28      {
29          _name=name;
30          if (name == null || name.length() == 0 || name.charAt(0) != '/')
31              throw new IllegalArgumentException(name);
32  
33          if ("/".equals(name))
34          {
35              _segments=ROOT;
36          }
37          else
38          {
39              if (name.charAt(name.length() - 1) == '/')
40                  throw new IllegalArgumentException(name);
41  
42              _segments=name.substring(1).split("/");
43          }
44  
45          if (_segments.length == 0)
46              _wild=0;
47          else if (WILD.equals(_segments[_segments.length - 1]))
48              _wild=1;
49          else if (WILDWILD.equals(_segments[_segments.length - 1]))
50              _wild=2;
51      }
52  
53      public boolean isWild()
54      {
55          return _wild > 0;
56      }
57  
58      @Override
59      public boolean equals(Object obj)
60      {
61          if (this == obj)
62              return true;
63  
64          if (obj instanceof ChannelId)
65          {
66              ChannelId other=(ChannelId)obj;
67              if (isWild())
68              {
69                  if (other.isWild())
70                      return _name.equals(other._name);
71                  return matches(other);
72              }
73              else
74              {
75                  if (other.isWild())
76                      return other.matches(this);
77                  return _name.equals(other._name);
78              }
79          }
80          else if (obj instanceof String)
81          {
82              if (isWild())
83                  return matches((String)obj);
84              return _name.equals(obj);
85          }
86  
87          return false;
88      }
89  
90      public boolean matches(ChannelId name)
91      {
92          if (name.isWild())
93              return equals(name);
94  
95          switch(_wild)
96          {
97              case 0:
98                  return equals(name);
99              case 1:
100                 if (name._segments.length != _segments.length)
101                     return false;
102                 for (int i=_segments.length - 1; i-- > 0;)
103                     if (!_segments[i].equals(name._segments[i]))
104                         return false;
105                 return true;
106 
107             case 2:
108                 if (name._segments.length < _segments.length)
109                     return false;
110                 for (int i=_segments.length - 1; i-- > 0;)
111                     if (!_segments[i].equals(name._segments[i]))
112                         return false;
113                 return true;
114         }
115         return false;
116     }
117 
118     public boolean matches(String name)
119     {
120         if (_wild == 0)
121             return _name.equals(name);
122 
123         // TODO more efficient?
124         return matches(new ChannelId(name));
125     }
126 
127     @Override
128     public int hashCode()
129     {
130         return _name.hashCode();
131     }
132 
133     @Override
134     public String toString()
135     {
136         return _name;
137     }
138 
139     public int depth()
140     {
141         return _segments.length;
142     }
143 
144     public boolean isParentOf(ChannelId id)
145     {
146         if (isWild() || depth() >= id.depth())
147             return false;
148        
149         for (int i = _segments.length-1; i >= 0; i--)
150         {
151             if (!_segments[i].equals(id._segments[i]))
152                 return false;
153         }
154         return true;
155     }
156 
157     public String getSegment(int i)
158     {
159         if (i > _segments.length)
160             return null;
161         return _segments[i];
162     }
163 }