KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > mcast > DomainName


1 package org.sapia.ubik.mcast;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6
7 /**
8  * Models a "domain name" that is expressed by a path. Allows to filter
9  * multicast events on a per-domain basis. Supports the notion of domain
10  * partition (where a domain can have subdomains - or partitions).
11  * <p>
12  * Partionning is expressed through a path notation, where the parent domain
13  * can "contain" subdomains (that necessarily include their parent's path).
14  * <p>
15  * This notion of containment is used to determine to which domain or partition in
16  * a domain a multicast event is destined. For example, an event that is
17  * multicast to domain "parent" will also be received by the following partitions
18  * "parent/partition1", "parent/partition2". Yet, the opposite would not be true:
19  * an event targeted at "parent/partition2" would not be received by the others - since
20  * the latter do not "contain" or "include" the former.
21  * <p>
22  * An instance of this class is created as follows:
23  * <pre>
24  * DomainName dn = DomainName.parse("domain/partition");
25  * </pre>
26  * Specifying subdomains/partitions is not mandatory.
27  *
28  * @author Yanick Duchesne
29  * <dl>
30  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
31  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
32  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
33  * </dl>
34  */

35 public class DomainName implements java.io.Serializable JavaDoc {
36   public static final char DELIM = '/';
37   private List JavaDoc _segments;
38
39   private DomainName(List JavaDoc segments) {
40     _segments = segments;
41   }
42
43   /**
44     * Returns the number of "components" in this instance's name.
45     *
46     * @return the number of "components" in this instance's name.
47     */

48   public int size() {
49     return _segments.size();
50   }
51
52   /**
53     * Returns the component whose index is given.
54     *
55     * @param i an index.
56     * @return a domain name component.
57     */

58   public String JavaDoc get(int i) {
59     return (String JavaDoc) _segments.get(i);
60   }
61
62   /**
63     * Creates an instance of this class out of the given name/path.
64     *
65     * @param a domain name/path.
66     * @return a <code>DomainName</code> object.
67     */

68   public static DomainName parse(String JavaDoc name) {
69     List JavaDoc _segments = new ArrayList JavaDoc(5);
70
71     int idx = 0;
72     int lastIdx = 0;
73
74     while ((idx = name.indexOf(DELIM, idx)) > -1) {
75       if (idx > 0) {
76         _segments.add(name.substring(lastIdx, idx));
77       }
78
79       lastIdx = ++idx;
80     }
81
82     if (idx < name.length()) {
83       _segments.add(name.substring(lastIdx));
84     }
85
86     return new DomainName(_segments);
87   }
88
89   /**
90    * Returns <code>true</code> if this instance "contains" or "includes"
91    * the domain name passed in.
92    *
93    * @param other a <code>DomainName</code>.
94    */

95   public boolean contains(DomainName other) {
96     if (_segments.size() >= other.size()) {
97       for (int i = 0; i < other.size(); i++) {
98         if (!other.get(i).equals(get(i))) {
99           return false;
100         }
101       }
102
103       return true;
104     } else {
105       return false;
106     }
107   }
108
109   /**
110    * Compares this instance with another <code>DomainName</code>.
111    *
112    * @param an <code>Object</code>.
113    *
114    * @return <code>true</code> if the passed in object is a
115    * <code>DomainName</code> instance and if it has the same domain name
116    * string as this instance's.
117    */

118   public boolean equals(Object JavaDoc other) {
119     try {
120       DomainName dn = (DomainName) other;
121
122       if (_segments.size() == dn.size()) {
123         for (int i = 0; i < dn.size(); i++) {
124           if (!dn.get(i).equals(get(i))) {
125             return false;
126           }
127         }
128
129         return true;
130       } else {
131         return false;
132       }
133     } catch (ClassCastException JavaDoc e) {
134       return false;
135     }
136   }
137
138   /**
139    * Returns a string representation of this instance - or, more
140    * precisely, this instance's path representation, where subdomains/partitions
141    * are separated by '/' characters.
142    *
143    * @return this instance's string representation.
144    */

145   public String JavaDoc toString() {
146     StringBuffer JavaDoc s = new StringBuffer JavaDoc(_segments.size() * 8);
147
148     for (int i = 0; i < _segments.size(); i++) {
149       s.append((String JavaDoc) _segments.get(i));
150
151       if (i < (_segments.size() - 1)) {
152         s.append(DELIM);
153       }
154     }
155
156     return s.toString();
157   }
158 }
159
Popular Tags