KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > repository > spi > Key


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.repository.spi;
23
24 import java.text.ParseException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.TreeSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 import org.jboss.repository.spi.CommonNames;
35 import org.jboss.util.JBossStringBuilder;
36
37 /**
38  @author Scott.Stark@jboss.org
39  @version $Revision: 42147 $
40  */

41 public class Key implements Comparable JavaDoc
42 {
43    private String JavaDoc[] name;
44    private Map JavaDoc attributes;
45    private int level;
46
47    public Key(String JavaDoc nameExpr)
48       throws ParseException JavaDoc
49    {
50       parseName(nameExpr);
51       defineLevel();
52    }
53    public Key(String JavaDoc[] name, Map JavaDoc attributes)
54    {
55       this.name = name;
56       this.attributes = attributes;
57       defineLevel();
58    }
59
60    public Key(String JavaDoc name, Map JavaDoc attributes)
61    {
62       this.name = new String JavaDoc[] {name};
63       this.attributes = attributes;
64       defineLevel();
65    }
66
67    public String JavaDoc[] getName()
68    {
69       return name;
70    }
71    public int getLevel()
72    {
73       return level;
74    }
75    public Map JavaDoc getAttributes()
76    {
77       return Collections.unmodifiableMap(attributes);
78    }
79
80
81    public int compareTo(Object JavaDoc obj)
82    {
83       if( obj instanceof Key == false )
84          throw new ClassCastException JavaDoc("Argument is not a Key, type="+obj.getClass());
85
86       Key key = (Key) obj;
87       int compare = -name.length + key.name.length;
88       if (compare != 0)
89       {
90          return compare;
91       }
92       
93       for (int i = 0 ; i < name.length ; i++)
94       {
95          compare = name[i].compareTo(key.name[i]);
96          if (compare != 0)
97          {
98             return compare;
99          }
100       }
101       
102       if( compare == 0 )
103       {
104          Map JavaDoc keyAttrs = key.attributes;
105          if( attributes == null )
106             compare = keyAttrs == null ? 0 : keyAttrs.size();
107          else if( keyAttrs == null )
108             compare = attributes == null ? 0 : attributes.size();
109          else
110          {
111             TreeSet JavaDoc set = new TreeSet JavaDoc(attributes.keySet());
112             set.addAll(keyAttrs.keySet());
113             Iterator JavaDoc keys = set.iterator();
114             while( keys.hasNext() && compare == 0 )
115             {
116                String JavaDoc key1 = (String JavaDoc) keys.next();
117                String JavaDoc value1 = (String JavaDoc) attributes.get(key1);
118                String JavaDoc value2 = (String JavaDoc) keyAttrs.get(key1);
119                
120                if (value1 == null && value2 == null)
121                   compare = 0;
122                if( value1 == null )
123                   compare = -1;
124                else if( value2 == null )
125                   compare = 1;
126                else
127                   compare = value1.compareTo(value2);
128             }
129          }
130       }
131       return compare;
132    }
133    public boolean equals(Object JavaDoc obj)
134    {
135       return compareTo(obj) == 0;
136    }
137    public int hashCode()
138    {
139       int hashCode = attributes.hashCode();
140       for (int i = 0 ; i < name.length ; i++)
141       {
142          hashCode += name[i].hashCode();
143       }
144       return hashCode;
145    }
146
147    protected void defineLevel()
148    {
149       level = CommonNames.DOMAIN_LEVEL;
150       if( attributes.containsKey(CommonNames.DOMAIN) )
151          level = CommonNames.DOMAIN_LEVEL;
152       if( attributes.containsKey(CommonNames.CLUSTER) )
153          level = CommonNames.CLUSTER_LEVEL;
154       if( attributes.containsKey(CommonNames.SERVER) )
155          level = CommonNames.SERVER_LEVEL;
156       if( attributes.containsKey(CommonNames.APPLICATION) )
157          level = CommonNames.APPLICATION_LEVEL;
158       if( attributes.containsKey(CommonNames.DEPLOYMENT) )
159          level = CommonNames.DEPLOYMENT_LEVEL;
160       if( attributes.containsKey(CommonNames.SESSION) )
161          level = CommonNames.SESSION_LEVEL;
162    }
163    
164    protected void parseName(String JavaDoc nameExpr)
165       throws ParseException JavaDoc
166    {
167       int colon = nameExpr.indexOf(':');
168       if( colon < 0 )
169          parseNamePart(nameExpr);
170       else
171       {
172          parseNamePart(nameExpr.substring(0, colon));
173          // Parse the key=value pairs
174
StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(nameExpr.substring(colon + 1), ",=");
175          while( tokenizer.hasMoreTokens() )
176          {
177             String JavaDoc key = tokenizer.nextToken();
178             if( tokenizer.hasMoreTokens() == false )
179             {
180                throw new ParseException JavaDoc("No value for key: "+key, attributes.size());
181             }
182             String JavaDoc value = tokenizer.nextToken();
183             
184             if (attributes == null)
185             {
186                attributes = new HashMap JavaDoc();
187             }
188             attributes.put(key, value);
189          }
190       }
191    }
192    
193    private void parseNamePart(String JavaDoc namePart)
194    {
195       ArrayList JavaDoc names = new ArrayList JavaDoc();
196       StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(namePart, ",");
197       while (tokenizer.hasMoreTokens())
198       {
199          names.add(tokenizer.nextToken());
200       }
201       name = (String JavaDoc[])names.toArray(new String JavaDoc[names.size()]);
202    }
203    
204    public String JavaDoc toString()
205    {
206       JBossStringBuilder sb = new JBossStringBuilder();
207       sb.append("[Key[");
208       sb.append(Arrays.asList(name));
209       sb.append(":");
210       
211       boolean first = true;
212       for (Iterator JavaDoc it = attributes.keySet().iterator() ; it.hasNext() ; )
213       {
214          Object JavaDoc key = it.next();
215          
216          if (first)
217          {
218             first = false;
219          }
220          else
221          {
222             sb.append(", ");
223          }
224          
225          sb.append(key);
226          sb.append("=");
227          sb.append(attributes.get(key));
228       }
229       
230       sb.append("]]");
231       return sb.toString();
232    }
233
234 }
235
Popular Tags