litesoft
@ 822
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.core.hierarchicaldata; import java.io.*; import java.util.*; import org.litesoft.core.typeutils.*; /** * A Map 'like' class that is type safe for <String,String>, <String,HDSList>, & <String,HDSMap>. * <p/> * It does NOT quite really implement the Map interface because proper Concurrent Modification is not recognized. * For this reason, it is NOT coded to be Thread Safe! * <p/> * Note: AFAIK, Generics do not support multiple different types. */ public class HDSMap extends AbstractMap<String, Object> implements Serializable { private static final long serialVersionUID = 1L; public static final String TYPE = "HDSMap"; private static final String UNSUPPORTED_OPERATION_WHY = "Syncronized collection and too lazy to code protection / proxy to inhibit back door"; public static class Entry extends HDSList.Entry implements Map.Entry<String, Object>, Serializable, Comparable<Entry> { private static final long serialVersionUID = 1L; public static final Entry[] EMPTY_ARRAY = new Entry[0]; private String mName; /** * @deprecated - for Serialization */ protected Entry() { } private Entry( Object pOwner, String pName, Object pValue ) { super( pOwner, pValue ); mName = HierarchicalData.validateName( "Name", pName ); } public String getName() { return mName; } public String getKey() { return getName(); } public Object setValue( Object value ) { throw new UnsupportedOperationException( UNSUPPORTED_OPERATION_WHY ); } public boolean equals( Object o ) { return (this == o) || // ((o instanceof Entry) && equals( (Entry) o )); } public boolean equals( Entry them ) { return (this == them) || // ((them != null) // && this.mName.equals( them.mName ) // && this.getValue().equals( them.getValue() ) // ); } public int hashCode() { return mName.hashCode(); } public int compareTo( Entry them ) { return this.mName.compareTo( them.mName ); } public String toString() { return mName + '=' + getValue(); } public void desiccate( HierarchicalDataSink.Node pParentNode ) { HierarchicalDataSink.Node zNode = pParentNode.createChildNode( mName ); if ( getValueAsLocalString() != null ) { zNode.setAttribute( "of", getValueAsLocalString() ); } else if ( getValueAsChildMap() != null ) { getValueAsChildMap().desiccate( zNode ); } else { getValueAsChildList().desiccate( zNode ); } zNode.done(); } private Entry( HierarchicalDataSource.Node pNode ) { mName = HierarchicalData.validateName( "Name", pNode.getName() ); if ( null == (mValueAsLocalString = pNode.getAttributeValue( "of" )) ) { HierarchicalDataSource.Node zChild = pNode.getNextChild(); if ( zChild == null ) { throw new IllegalArgumentException( "Missing Child Node under node: " + mName ); } String zName = zChild.getName(); if ( HDSMap.TYPE.equals( zName ) ) { mValueAsChildMap = new HDSMap( zChild ); } else if ( HDSList.TYPE.equals( zName ) ) { mValueAsChildList = new HDSList( zChild ); } else { throw new IllegalArgumentException( "Unrecognized " + TYPE + " Entry Node of: " + zName ); } } pNode.assertNoMoreChildren(); } } private HashMap<String, Entry> mNameEntries = new HashMap<String, Entry>(); public HDSMap() { } public HDSMap( Entry[] pEntries ) { putAll( pEntries ); } /** * Shallow Copy! * * @param pHDSMap null OK */ public HDSMap( HDSMap pHDSMap ) { if ( pHDSMap != null ) { putAll( pHDSMap.getEntries() ); } } /** * Get the String that corresponds to the path provided (null if path is not appropriate) * * @param pPath null / No entries = return null * * @return null or String from "tree" that matches the "path" indicated. */ public String getString( String... pPath ) { return Objects.isNotNullOrEmpty( pPath ) ? LLgetString( pPath, 0 ) : null; } private String LLgetString( String[] pPath, int pIndex ) { Entry zEntry = mNameEntries.get( Strings.deNull( pPath[pIndex++] ) ); if ( (zEntry == null) ) { return null; } if ( pIndex == pPath.length ) { return zEntry.getValueAsLocalString(); } HDSMap zChildTree = zEntry.getValueAsChildMap(); return (zChildTree != null) ? zChildTree.LLgetString( pPath, pIndex ) : null; } /** * Update the String that corresponds to the path provided * * @param pValue null = IllegalArgumentException * @param pPath null / No entries = IllegalArgumentException * * @return previous String at pPath * * @throws IllegalArgumentException Bad Value / Bad Path (which may indicate that the Entries for the Path are incompatible */ public String putString( String pValue, String... pPath ) throws IllegalArgumentException { Objects.assertNotNull( "Value", pValue ); Strings.assertNotNullNotEmpty( "Path", pPath ); String zPathSep = ""; String zPath = ""; for ( String zKey : pPath ) { HierarchicalData.validateName( "Path (" + (zPath += zPathSep + zKey) + ")", zKey ); zPathSep = "/"; } return LLputString( pValue, pPath, 0 ); } private String LLputString( String pValue, String[] pPath, int pIndex ) { String zName = Strings.deNull( pPath[pIndex++] ); Entry zEntry = getEntry( zName ); if ( pIndex == pPath.length ) // Leaf! { String rv = null; if ( zEntry != null ) { if ( null == (rv = zEntry.getValueAsLocalString()) ) { throw new IllegalArgumentException( "Expected Leaf, but not Leaf at: " + Objects.toString( pPath, "/" ) ); } } mNameEntries.put( zName, new Entry( this, zName, pValue ) ); return rv; } HDSMap zChildTree; if ( zEntry != null ) { if ( null == (zChildTree = zEntry.getValueAsChildMap()) ) { throw new IllegalArgumentException( "Non Leaf of Path (@ " + pIndex + "), not a HDSMap, Path: " + Objects.toString( pPath, "/" ) ); } } else { mNameEntries.put( zName, new Entry( this, zName, zChildTree = new HDSMap() ) ); } return zChildTree.LLputString( pValue, pPath, pIndex ); } /** * Remove the String that corresponds to the path provided * * @param pPath null / No entries = IllegalArgumentException * * @return previous String at pPath / null if No String at Path * * @throws IllegalArgumentException Bad Path (which may indicate that the Entries for the Path are incompatible */ public String removeString( String... pPath ) throws IllegalArgumentException { Strings.assertNotNullNotEmpty( "Path", pPath ); return LLremoveString( pPath, 0 ); } private String LLremoveString( String[] pPath, int pIndex ) { String zName = Strings.deNull( pPath[pIndex++] ); Entry zEntry = getEntry( zName ); if ( (zEntry == null) ) { return null; } if ( pIndex == pPath.length ) { String rv = zEntry.getValueAsLocalString(); if ( rv == null ) { throw new IllegalArgumentException( "Expected Leaf, but not Leaf at: " + Objects.toString( pPath, "/" ) ); } mNameEntries.remove( zName ); // Remove Entry return rv; } HDSMap zChildTree = zEntry.getValueAsChildMap(); if ( zChildTree == null ) { return null; } String rv = zChildTree.LLremoveString( pPath, pIndex ); if ( zChildTree.isEmpty() ) { mNameEntries.remove( zName ); // Remove Empty ChildMap } return rv; } /** * Adds all the non-Empty Entries into this Map, replacing any current matching mappings * * @param pEntries null / empty & empty entries OK */ public void putAll( Entry[] pEntries ) { if ( pEntries != null ) { for ( Entry pEntry : pEntries ) { put( pEntry ); } } } /** * @return an array of the Entries in this map sorted by thier Key */ public Entry[] getEntries() { if ( isEmpty() ) { return Entry.EMPTY_ARRAY; } String[] zKeys = getKeys(); Entry[] rv = new Entry[zKeys.length]; for ( int i = 0; i < zKeys.length; i++ ) { String zKey = zKeys[i]; rv[i] = getEntry( zKey ); } return rv; } public Entry getEntry( String pKey ) { return mNameEntries.get( pKey ); } /** * Add/replace the Entry in the map. * * @param pEntry null OK (will not effect the Map) * * @return the previous value for the key if a mapping existed, otherwise null. */ public Object put( Entry pEntry ) { return pEntry != null ? unParent( unwrapEntry( mNameEntries.put( pEntry.getName(), pEntry ) ) ) : null; } /** * @return an array of the sorted Keys in this map */ public String[] getKeys() { int zLength = mNameEntries.size(); if ( zLength == 0 ) { return Strings.EMPTY_ARRAY; } String[] rv = mNameEntries.keySet().toArray( new String[zLength] ); Arrays.sort( rv ); return rv; } /** * Returns the number of key-value mappings in this map. If the * map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns * <tt>Integer.MAX_VALUE</tt>. * * @return the number of key-value mappings in this map. */ @Override public int size() { return mNameEntries.size(); } /** * Returns <tt>true</tt> if this map contains a mapping for the specified * key. More formally, returns <tt>true</tt> if and only if * this map contains a mapping for a key <tt>k</tt> such that * <tt>(key==null ? k==null : key.equals(k))</tt>. (There can be * at most one such mapping.) * * @param pKey key whose presence in this map is to be tested. * * @return <tt>true</tt> if this map contains a mapping for the specified * key. * * @throws ClassCastException if the key is of an inappropriate type for * this map (optional). * @throws NullPointerException if the key is <tt>null</tt> and this map * does not permit <tt>null</tt> keys (optional). */ @Override public boolean containsKey( Object pKey ) { return mNameEntries.containsKey( checkKey( pKey ) ); } /** * Returns <tt>true</tt> if this map maps one or more keys to the * specified value. More formally, returns <tt>true</tt> if and only if * this map contains at least one mapping to a value <tt>v</tt> such that * <tt>(value==null ? v==null : value.equals(v))</tt>. This operation * will probably require time linear in the map size for most * implementations of the <tt>Map</tt> interface. * * @param pValue value whose presence in this map is to be tested. * * @return <tt>true</tt> if this map maps one or more keys to the * specified value. * * @throws ClassCastException if the value is of an inappropriate type for * this map (optional). * @throws NullPointerException if the value is <tt>null</tt> and this map * does not permit <tt>null</tt> values (optional). */ @Override public boolean containsValue( Object pValue ) { if ( pValue instanceof String ) { for ( Entry zEntry : mNameEntries.values() ) { if ( pValue.equals( zEntry.getValueAsLocalString() ) ) { return true; } } } else if ( pValue instanceof HDSMap ) { for ( Entry zEntry : mNameEntries.values() ) { if ( pValue.equals( zEntry.getValueAsChildMap() ) ) { return true; } } } else if ( pValue instanceof HDSList ) { for ( Entry zEntry : mNameEntries.values() ) { if ( pValue.equals( zEntry.getValueAsChildList() ) ) { return true; } } } return false; } /** * Returns the value to which this map maps the specified key. Returns * <tt>null</tt> if the map contains no mapping for this key. A return * value of <tt>null</tt> does not <i>necessarily</i> indicate that the * map contains no mapping for the key; it's also possible that the map * explicitly maps the key to <tt>null</tt>. The <tt>containsKey</tt> * operation may be used to distinguish these two cases. * <p/> * <p>More formally, if this map contains a mapping from a key * <tt>k</tt> to a value <tt>v</tt> such that <tt>(key==null ? k==null : * key.equals(k))</tt>, then this method returns <tt>v</tt>; otherwise * it returns <tt>null</tt>. (There can be at most one such mapping.) * * @param pKey key whose associated value is to be returned. * * @return the value to which this map maps the specified key, or * <tt>null</tt> if the map contains no mapping for this key. * * @throws ClassCastException if the key is of an inappropriate type for * this map (optional). * @throws NullPointerException if the key is <tt>null</tt> and this map * does not permit <tt>null</tt> keys (optional). * @see #containsKey(Object) */ @Override public Object get( Object pKey ) { return unwrapEntry( getEntry( checkKey( pKey ) ) ); } // Modification Operations /** * Associates the specified value with the specified key in this map * (optional operation). If the map previously contained a mapping for * this key, the old value is replaced by the specified value. (A map * <tt>m</tt> is said to contain a mapping for a key <tt>k</tt> if and only * if {@link #containsKey(Object) m.containsKey(k)} would return * <tt>true</tt>.)) * * @param pKey key with which the specified value is to be associated. * @param pValue value to be associated with the specified key. * * @return previous value associated with specified key, or <tt>null</tt> * if there was no mapping for key. A <tt>null</tt> return can * also indicate that the map previously associated <tt>null</tt> * with the specified key, if the implementation supports * <tt>null</tt> values. * * @throws UnsupportedOperationException if the <tt>put</tt> operation is * not supported by this map. * @throws ClassCastException if the class of the specified key or value * prevents it from being stored in this map. * @throws IllegalArgumentException if some aspect of this key or value * prevents it from being stored in this map. * @throws NullPointerException if this map does not permit <tt>null</tt> * keys or values, and the specified key or value is * <tt>null</tt>. */ @Override public Object put( String pKey, Object pValue ) { String zKey = Strings.assertNotNullNotEmpty( "Key", checkKey( pKey ) ); return unParent( unwrapEntry( mNameEntries.put( zKey, new Entry( this, zKey, pValue ) ) ) ); } /** * Removes the mapping for this key from this map if it is present * (optional operation). More formally, if this map contains a mapping * from key <tt>k</tt> to value <tt>v</tt> such that * <code>(key==null ? k==null : key.equals(k))</code>, that mapping * is removed. (The map can contain at most one such mapping.) * <p/> * <p>Returns the value to which the map previously associated the key, or * <tt>null</tt> if the map contained no mapping for this key. (A * <tt>null</tt> return can also indicate that the map previously * associated <tt>null</tt> with the specified key if the implementation * supports <tt>null</tt> values.) The map will not contain a mapping for * the specified key once the call returns. * * @param pKey key whose mapping is to be removed from the map. * * @return previous value associated with specified key, or <tt>null</tt> * if there was no mapping for key. * * @throws ClassCastException if the key is of an inappropriate type for * this map (optional). * @throws NullPointerException if the key is <tt>null</tt> and this map * does not permit <tt>null</tt> keys (optional). * @throws UnsupportedOperationException if the <tt>remove</tt> method is * not supported by this map. */ @Override public Object remove( Object pKey ) { return unParent( unwrapEntry( mNameEntries.remove( checkKey( pKey ) ) ) ); } // Views private transient Set<Map.Entry<String, Object>> entrySet = null; /** * Returns a set view of the mappings contained in this map. Each element * in the returned set is a {@link Map.Entry}. The set is backed by the * map, so changes to the map are reflected in the set, and vice-versa. * If the map is modified while an iteration over the set is in progress * (except through the iterator's own <tt>remove</tt> operation, or through * the <tt>setValue</tt> operation on a map entry returned by the iterator) * the results of the iteration are undefined. The set supports element * removal, which removes the corresponding mapping from the map, via the * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>, <tt>removeAll</tt>, * <tt>retainAll</tt> and <tt>clear</tt> operations. It does not support * the <tt>add</tt> or <tt>addAll</tt> operations. * * @return a set view of the mappings contained in this map. */ public Set<Map.Entry<String, Object>> entrySet() { return (entrySet != null) ? entrySet : (entrySet = new EntrySet()); } private final class EntrySet extends AbstractSet<Map.Entry<String, Object>> { public Iterator<Map.Entry<String, Object>> iterator() { return new Iterator<Map.Entry<String, Object>>() { private String[] mKeys = HDSMap.this.getKeys(); private int mIndex = 0; private Entry mNextEntry = null; private Entry mCurEntry = null; public boolean hasNext() { while ( (mNextEntry == null) && (mIndex < mKeys.length) ) { mNextEntry = HDSMap.this.getEntry( mKeys[mIndex++] ); } return (mNextEntry != null); } public Map.Entry<String, Object> next() { if ( !hasNext() ) { throw new NoSuchElementException( "No More Entries" ); } mCurEntry = mNextEntry; mNextEntry = null; return mCurEntry; } public void remove() { if ( mCurEntry == null ) { throw new IllegalStateException( "next not called - No Current Entry" ); } HDSMap.this.remove( mCurEntry.getKey() ); mCurEntry = null; } }; } @Override public boolean contains( Object o ) { if ( !(o instanceof Entry) ) { return false; } Entry e = (Entry) o; Entry candidate = getEntry( e.getKey() ); return e.equals( candidate ); } @Override public boolean remove( Object o ) { return (o instanceof Map.Entry) && (null != HDSMap.this.remove( ((Map.Entry) o).getKey() )); } public int size() { return HDSMap.this.size(); } } public void desiccate( HierarchicalDataSink pSink ) { populate( pSink.createRootNode( TYPE ) ); } public void desiccate( HierarchicalDataSink.Node pParentNode ) { populate( pParentNode.createChildNode( TYPE ) ); } private void populate( HierarchicalDataSink.Node pNode ) { Entry[] zEntries = getEntries(); pNode.setAttribute( "size", "" + zEntries.length ); for ( Entry zEntry : zEntries ) { zEntry.desiccate( pNode ); } pNode.done(); } public HDSMap( HierarchicalDataSource.Node pNode ) { String zStrSize = Strings.assertNotNullNotEmpty( TYPE + " 'size' attribute", pNode.getAttributeValue( "size" ) ); int zExpectedSize; try { zExpectedSize = Integer.parseInt( zStrSize ); } catch ( NumberFormatException e ) { throw new IllegalArgumentException( TYPE + " 'size' attribute Not Numeric", e ); } int zActualSize = 0; for ( HierarchicalDataSource.Node zNode; null != (zNode = pNode.getNextChild()); ) { zActualSize++; put( new Entry( zNode ) ); } if ( zActualSize != zExpectedSize ) { throw new IllegalArgumentException( TYPE + " rehydrate, expected '" + zExpectedSize + "' members, but got: " + zActualSize ); } } private String checkKey( Object pKey ) { if ( pKey == null ) { throw new NullPointerException( "key" ); } return pKey.toString(); } private Object unParent( Object pObject ) { if ( pObject != null ) { if ( pObject instanceof HDSMap ) { ((HDSMap) pObject).setHDSParent( null ); } else if ( pObject instanceof HDSList ) { ((HDSList) pObject).setHDSParent( null ); } } return pObject; } private Object unwrapEntry( Entry pEntry ) { return (pEntry != null) ? pEntry.getValue() : null; } private transient Object mHDSParent = null; void setHDSParent( Object pHDSParent ) { if ( (pHDSParent != null) && (mHDSParent != null) ) { throw new IllegalStateException( "An " + TYPE + " may only be 'held' by ONE other HDS 'type' (" + TYPE + " or " + HDSList.TYPE + ")" ); } mHDSParent = pHDSParent; } } |
Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/hierarchicaldata/HDSMap.java
Revision | Author | Commited | Message |
---|---|---|---|
822 Diff | GeorgeS | Sun 19 Aug, 2012 01:03:51 +0000 | |
821 Diff | GeorgeS | Sun 19 Aug, 2012 00:08:41 +0000 | |
809 Diff | GeorgeS | Thu 16 Aug, 2012 04:10:46 +0000 | |
804 Diff | GeorgeS | Wed 15 Aug, 2012 12:48:51 +0000 | |
802 Diff | GeorgeS | Wed 15 Aug, 2012 04:04:47 +0000 | |
800 Diff | GeorgeS | Wed 15 Aug, 2012 03:33:38 +0000 | |
151 Diff | GeorgeS | Thu 17 Mar, 2011 04:16:22 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |