KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > archie > impl > AttributeNamePart


1 package org.sapia.archie.impl;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.Properties JavaDoc;
6
7 import org.sapia.archie.NamePart;
8
9 /**
10  * @author Yanick Duchesne
11  * <dl>
12  * <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>
13  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
14  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
15  * </dl>
16  */

17 public class AttributeNamePart implements NamePart{
18   
19   static final String JavaDoc EMPTY_NAME = "";
20   
21   private String JavaDoc _name = EMPTY_NAME;
22   
23   private Properties JavaDoc _attributes = new Properties JavaDoc();
24   
25   void setName(String JavaDoc name){
26     _name = name;
27   }
28   
29   void addProperty(String JavaDoc name, String JavaDoc val){
30      _attributes.setProperty(name, val);
31   }
32   
33   /**
34    * @return this instance's name (without the attributes).
35    */

36   public String JavaDoc getName(){
37     return _name;
38   }
39   
40   /**
41    * @see java.lang.Object#hashCode()
42    */

43   public int hashCode() {
44     return _name.hashCode();
45   }
46   /**
47    * @see java.lang.Object#equals(java.lang.Object)
48    */

49   public boolean equals(Object JavaDoc o){
50     try{
51       return _name.equals(((AttributeNamePart)o).getName());
52     }catch(ClassCastException JavaDoc e){
53       return false;
54     }
55   }
56   
57   /**
58    * Returns <code>true</code> if this instance's name is equal to the name
59    * of the instance passed in, AND if the passed in instance's attributes are
60    * also contained by this instance.
61    *
62    * @param other the <code>AttributeNamePart</code> used to perform the test.
63    * @return <code>true</code> if this instance "matches" the passed in instance.
64    */

65   public boolean matches(AttributeNamePart other){
66     Map.Entry JavaDoc entry;
67     Iterator JavaDoc entries = other._attributes.entrySet().iterator();
68     String JavaDoc value;
69     if(!other.getName().equals(_name)){
70       return false;
71     }
72     while(entries.hasNext()){
73       entry = (Map.Entry JavaDoc)entries.next();
74       value = _attributes.getProperty(entry.getKey().toString());
75       if(value == null && entry.getValue() == null){
76         continue;
77       }
78       else if(value == null && entry.getValue() != null){
79         return false;
80       }
81       else if(value != null && entry.getValue() == null){
82         return false;
83       }
84       else if(value != null && entry.getValue() != null &&
85               entry.getValue().toString().equals(value)){
86         continue;
87       }
88       else{
89         return false;
90       }
91     }
92     return true;
93   }
94   
95   /**
96    * @return this instance's attributes.
97    */

98   public Properties JavaDoc getAttributes(){
99     return _attributes;
100   }
101   
102   /**
103    * @see org.sapia.archie.NamePart#asString()
104    */

105   public String JavaDoc asString() {
106     StringBuffer JavaDoc buf = new StringBuffer JavaDoc(_name);
107     if (_attributes.size() > 0) {
108       Map.Entry JavaDoc entry;
109       buf.append(AttributeNameParser.QMARK);
110
111       Iterator JavaDoc itr = _attributes.entrySet().iterator();
112       int count = 0;
113
114       while (itr.hasNext()) {
115         entry = (Map.Entry JavaDoc) itr.next();
116
117         if (count > 0) {
118           buf.append(AttributeNameParser.AMP);
119         }
120
121         buf.append(entry.getKey().toString()).append(AttributeNameParser.EQ)
122            .append(entry.getValue());
123         count++;
124       }
125     }
126     return buf.toString();
127   }
128   
129   public String JavaDoc toString(){
130     return asString();
131   }
132   
133 }
134
Popular Tags