1 22 package org.jboss.repository.spi; 23 24 import java.text.ParseException ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import java.util.StringTokenizer ; 30 import java.util.Collections ; 31 import java.util.TreeSet ; 32 import java.util.Iterator ; 33 34 import org.jboss.repository.spi.CommonNames; 35 import org.jboss.util.JBossStringBuilder; 36 37 41 public class Key implements Comparable 42 { 43 private String [] name; 44 private Map attributes; 45 private int level; 46 47 public Key(String nameExpr) 48 throws ParseException 49 { 50 parseName(nameExpr); 51 defineLevel(); 52 } 53 public Key(String [] name, Map attributes) 54 { 55 this.name = name; 56 this.attributes = attributes; 57 defineLevel(); 58 } 59 60 public Key(String name, Map attributes) 61 { 62 this.name = new String [] {name}; 63 this.attributes = attributes; 64 defineLevel(); 65 } 66 67 public String [] getName() 68 { 69 return name; 70 } 71 public int getLevel() 72 { 73 return level; 74 } 75 public Map getAttributes() 76 { 77 return Collections.unmodifiableMap(attributes); 78 } 79 80 81 public int compareTo(Object obj) 82 { 83 if( obj instanceof Key == false ) 84 throw new ClassCastException ("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 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 set = new TreeSet (attributes.keySet()); 112 set.addAll(keyAttrs.keySet()); 113 Iterator keys = set.iterator(); 114 while( keys.hasNext() && compare == 0 ) 115 { 116 String key1 = (String ) keys.next(); 117 String value1 = (String ) attributes.get(key1); 118 String value2 = (String ) 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 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 nameExpr) 165 throws ParseException 166 { 167 int colon = nameExpr.indexOf(':'); 168 if( colon < 0 ) 169 parseNamePart(nameExpr); 170 else 171 { 172 parseNamePart(nameExpr.substring(0, colon)); 173 StringTokenizer tokenizer = new StringTokenizer (nameExpr.substring(colon + 1), ",="); 175 while( tokenizer.hasMoreTokens() ) 176 { 177 String key = tokenizer.nextToken(); 178 if( tokenizer.hasMoreTokens() == false ) 179 { 180 throw new ParseException ("No value for key: "+key, attributes.size()); 181 } 182 String value = tokenizer.nextToken(); 183 184 if (attributes == null) 185 { 186 attributes = new HashMap (); 187 } 188 attributes.put(key, value); 189 } 190 } 191 } 192 193 private void parseNamePart(String namePart) 194 { 195 ArrayList names = new ArrayList (); 196 StringTokenizer tokenizer = new StringTokenizer (namePart, ","); 197 while (tokenizer.hasMoreTokens()) 198 { 199 names.add(tokenizer.nextToken()); 200 } 201 name = (String [])names.toArray(new String [names.size()]); 202 } 203 204 public String 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 it = attributes.keySet().iterator() ; it.hasNext() ; ) 213 { 214 Object 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 |