1 18 19 package com.hp.hpl.jena.util; 22 23 24 import java.util.*; 27 28 import com.hp.hpl.jena.util.iterator.NullIterator; 29 30 31 32 39 public class OneToManyMap 40 implements Map 41 { 42 45 46 49 50 53 54 private Map m_table = new HashMap(); 55 56 57 60 63 public OneToManyMap() { 64 } 65 66 67 73 public OneToManyMap( OneToManyMap map ) { 74 for (Iterator i = map.keySet().iterator(); i.hasNext(); ) { 78 Object key = i.next(); 79 80 for (Iterator j = map.getAll( key ); j.hasNext(); ) { 81 put( key, j.next() ); 82 } 83 } 84 } 85 86 87 88 89 92 95 public void clear() { 96 m_table.clear(); 97 } 98 99 100 106 public boolean containsKey( Object key ) { 107 return m_table.containsKey( key ); 108 } 109 110 111 119 public boolean containsValue( Object value ) { 120 for (Iterator values = m_table.values().iterator(); values.hasNext(); ) { 121 Object x = values.next(); 122 123 if (x.equals( value )) { 124 return true; 125 } 126 else if (x instanceof List && ((List) x).contains( value )) { 127 return true; 128 } 129 } 130 131 return false; 132 } 133 134 135 143 public boolean contains( Object key, Object value ) { 144 for (Iterator i = getAll( key ); i.hasNext(); ) { 145 if (i.next().equals( value )) { 146 return true; 147 } 148 } 149 return false; 150 } 151 152 153 159 public Set entrySet() { 160 Set s = CollectionFactory.createHashedSet(); 161 162 for (Iterator e0 = m_table.keySet().iterator(); e0.hasNext(); ) { 163 Object key = e0.next(); 164 List values = (List) m_table.get( key ); 165 166 for (ListIterator e1 = values.listIterator(); e1.hasNext(); ) { 168 s.add( new Entry( key, e1.next() ) ); 169 } 170 } 171 172 return s; 173 } 174 175 176 188 public boolean equals( Object o ) { 189 if (o instanceof java.util.Map ) { 190 return entrySet().equals( ((Map) o).entrySet() ); 191 } 192 else 193 return false; 194 } 195 196 197 207 public Object get( Object key ) { 208 ArrayList entry = (ArrayList) m_table.get( key ); 209 210 if (entry != null) { 211 if (!entry.isEmpty()) { 212 return entry.get( 0 ); 213 } 214 } 215 216 return null; 218 } 219 220 221 228 public Iterator getAll( Object key ) { 229 ArrayList entry = (ArrayList) m_table.get( key ); 230 return (entry != null) ? entry.iterator() : NullIterator.instance; 231 } 232 233 234 241 public int hashCode() { 242 int hc = 0; 243 244 for (Iterator i = entrySet().iterator(); i.hasNext(); ) { 245 hc ^= i.next().hashCode(); 246 } 247 248 return hc; 249 } 250 251 252 257 public boolean isEmpty() { 258 return m_table.isEmpty(); 259 } 260 261 262 267 public Set keySet() { 268 return m_table.keySet(); 269 } 270 271 272 282 public Object put( Object key, Object value ) { 283 ArrayList entries = (ArrayList) m_table.get( key ); 284 entries = entries == null ? new ArrayList() : entries; 285 286 entries.add( value ); 288 m_table.put( key, entries ); 289 290 return null; 291 } 292 293 294 299 public void putAll( Map m ) { 300 boolean many = (m instanceof OneToManyMap); 301 302 for (Iterator i = m.keySet().iterator(); i.hasNext(); ) { 303 Object key = i.next(); 304 if (many) { 305 for (Iterator j = ((OneToManyMap) m).getAll( key ); j.hasNext(); ) { 306 put( key, j.next() ); 307 } 308 } 309 else { 310 put( key, m.get( key ) ); 311 } 312 } 313 } 314 315 316 326 public Object remove( Object key ) { 327 m_table.remove( key ); 328 return null; 329 } 330 331 332 341 public void remove( Object key, Object value ) { 342 List entries = (List) m_table.get( key ); 343 344 if (entries != null) { 345 entries.remove( value ); 346 347 if (entries.isEmpty()) { 348 m_table.remove( key ); 349 } 350 } 351 } 352 353 354 358 public int size() { 359 int size = 0; 360 361 for (Iterator i = m_table.keySet().iterator(); i.hasNext(); ) { 362 size += ((List) m_table.get( i.next() )).size(); 363 } 364 365 return size; 366 } 367 368 369 375 public Collection values() { 376 Set s = CollectionFactory.createHashedSet(); 377 378 for (Iterator e = m_table.keySet().iterator(); e.hasNext(); ) { 379 s.addAll( (List) m_table.get(e.next()) ); 380 } 381 382 return s; 383 } 384 385 389 public String toString() { 390 StringBuffer buf = new StringBuffer ( "OneToManyMap{" ); 391 String sep = ""; 392 393 for (Iterator i = keySet().iterator(); i.hasNext(); ) { 394 Object key = i.next(); 395 buf.append( sep ); 396 buf.append( key ); 397 buf.append( "={" ); 398 399 String sep1 = ""; 400 for (Iterator j = getAll(key); j.hasNext(); ) { 401 buf.append( sep1 ); 402 buf.append( j.next() ); 403 sep1=","; 404 } 405 buf.append("}"); 406 sep=","; 407 } 408 buf.append("}"); 409 return buf.toString(); 410 } 411 412 415 416 419 420 422 423 427 430 public static class Entry 431 implements Map.Entry 432 { 433 434 private Object m_key = null; 435 436 437 private Object m_value = null; 438 439 440 443 private Entry( Object key, Object value ) { 444 m_key = key; 445 m_value = value; 446 } 447 448 449 465 public boolean equals( Object x ) { 466 if (x instanceof java.util.Map.Entry) { 467 Map.Entry e1 = (Map.Entry) x; 468 469 return (e1.getKey()==null ? 470 m_key==null : e1.getKey().equals(m_key)) && 471 (e1.getValue()==null ? 472 m_value == null : e1.getValue().equals(m_value)); 473 } 474 else 475 return false; 476 } 477 478 479 484 public Object getKey() { 485 return m_key; 486 } 487 488 489 494 public Object getValue() { 495 return m_value; 496 } 497 498 499 502 public Object setValue( Object value ) 503 throws UnsupportedOperationException 504 { 505 throw new UnsupportedOperationException ( "not implemented" ); 506 } 507 508 509 518 public int hashCode() { 519 return (getKey()==null ? 0 : getKey().hashCode()) ^ 520 (getValue()==null ? 0 : getValue().hashCode()); 521 } 522 523 524 } 525 526 } 527 528 553 | Popular Tags |