Changeset 2465 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2009-11-16T18:51:28+01:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java
r2460 r2465 41 41 } 42 42 43 private static class KeysEntry implements Entry<String, String> { 44 45 private final String key; 46 private final String value; 47 48 private KeysEntry(String key, String value) { 49 this.key = key; 50 this.value = value; 51 } 52 53 public String getKey() { 54 return key; 55 } 56 57 public String getValue() { 58 return value; 59 } 60 61 public String setValue(String value) { 62 throw new UnsupportedOperationException(); 63 } 64 65 } 66 67 43 68 private static final int FLAG_MODIFIED = 1 << 0; 44 69 private static final int FLAG_VISIBLE = 1 << 1; … … 539 564 * 540 565 */ 541 private Map<String, String> keys; 566 private Object keys; 567 private static final String DEFAULT_KEY = "created_by"; 542 568 543 569 /** … … 549 575 */ 550 576 public Map<String, String> getKeys() { 551 // TODO More effective map 552 // fix for #3218 553 if (keys == null) 554 return new HashMap<String, String>(); 555 else 556 return new HashMap<String, String>(keys); 577 Map<String, String> result = new HashMap<String, String>(); 578 if (keys != null) { 579 if (keys instanceof String) { 580 result.put(DEFAULT_KEY, (String)keys); 581 } else { 582 String[] map = (String[])keys; 583 if (map[0] != null) { 584 result.put(DEFAULT_KEY, map[0]); 585 } 586 for (int i=0; i<map.length / 2; i++) { 587 result.put(map[i * 2 + 1], map[i * 2 + 2]); 588 } 589 } 590 } 591 return result; 557 592 } 558 593 … … 568 603 this.keys = null; 569 604 } else { 570 this.keys = new HashMap<String, String>(keys); 605 String[] newKeys = new String[keys.size() * 2 + (keys.containsKey(DEFAULT_KEY)?-1:1)]; 606 int index = 1; 607 for (Entry<String, String> entry:keys.entrySet()) { 608 if (DEFAULT_KEY.equals(entry.getKey())) { 609 newKeys[0] = entry.getValue(); 610 } else { 611 newKeys[index++] = entry.getKey(); 612 newKeys[index++] = entry.getValue(); 613 } 614 } 615 this.keys = newKeys; 571 616 } 572 617 keysChangedImpl(); … … 588 633 remove(key); 589 634 } else { 590 if (keys == null) { 591 keys = new HashMap<String, String>(); 592 } 593 keys.put(key, value); 594 } 595 keysChangedImpl(); 635 // TODO More effective implementation 636 Map<String, String> keys = getKeys(); 637 if (!value.equals(keys.put(key, value))) { 638 setKeys(keys); 639 keysChangedImpl(); 640 } 641 } 596 642 } 597 643 /** … … 601 647 */ 602 648 public final void remove(String key) { 603 if (keys != null) { 604 keys.remove(key); 605 if (keys.isEmpty()) { 606 keys = null; 607 } 608 } 609 keysChangedImpl(); 649 Map<String, String> keys = getKeys(); 650 if (keys.remove(key) != null) { 651 // TODO More effective implemenation 652 setKeys(keys); 653 keysChangedImpl(); 654 } 610 655 } 611 656 … … 628 673 */ 629 674 public final String get(String key) { 630 if (key == null) return null; 631 return keys == null ? null : keys.get(key); 675 if (key == null) 676 return null; 677 678 if (keys == null) 679 return null; 680 681 if (keys instanceof String) 682 return DEFAULT_KEY.equals(key)?(String)keys:null; 683 684 String[] map = (String[])keys; 685 if (DEFAULT_KEY.equals(key)) 686 return map[0]; 687 else { 688 for (int i=0; i<map.length/2; i++) { 689 if (key.equals(map[i * 2 + 1])) 690 return map[i * 2 + 2]; 691 } 692 } 693 return null; 632 694 } 633 695 634 696 public final Collection<Entry<String, String>> entrySet() { 635 697 if (keys == null) 636 return Collections.emptyList(); 637 return keys.entrySet(); 698 return Collections.emptySet(); 699 else if (keys instanceof String) 700 return Collections.<Entry<String, String>>singleton(new KeysEntry(DEFAULT_KEY, (String)keys)); 701 else { 702 String[] map = (String[])keys; 703 List<Entry<String, String>> result = new ArrayList<Entry<String,String>>(); 704 if (map[0] != null) { 705 result.add(new KeysEntry(DEFAULT_KEY, map[0])); 706 } 707 for (int i=0; i<map.length / 2; i++) { 708 result.add(new KeysEntry(map[i * 2 + 1], map[i * 2 + 2])); 709 } 710 return result; 711 } 638 712 } 639 713 640 714 public final Collection<String> keySet() { 641 715 if (keys == null) 642 return Collections.emptyList(); 643 return keys.keySet(); 716 return Collections.emptySet(); 717 else if (keys instanceof String) 718 return Collections.singleton(DEFAULT_KEY); 719 else { 720 String[] map = (String[])keys; 721 List<String> result = new ArrayList<String>(map.length / 2 + 1); 722 if (map[0] != null) { 723 result.add(DEFAULT_KEY); 724 } 725 for (int i=0; i<map.length / 2; i++) { 726 result.add(map[i * 2 + 1]); 727 } 728 return result; 729 } 730 } 731 732 /** 733 * Replies true, if the map of key/value pairs of this primitive is not empty. 734 * 735 * @return true, if the map of key/value pairs of this primitive is not empty; false 736 * otherwise 737 */ 738 public final boolean hasKeys() { 739 return keys != null; 740 } 741 742 private void keysChangedImpl() { 743 clearCached(); 744 updateHasDirectionKeys(); 745 updateTagged(); 746 if (dataSet != null) { 747 dataSet.fireTagsChanged(this); 748 } 644 749 } 645 750 … … 748 853 749 854 /** 750 * Replies true, if the map of key/value pairs of this primitive is not empty.751 *752 * @return true, if the map of key/value pairs of this primitive is not empty; false753 * otherwise754 *755 * @since 1843756 */757 public final boolean hasKeys() {758 return keys != null && !keys.isEmpty();759 }760 761 private void keysChangedImpl() {762 clearCached();763 updateHasDirectionKeys();764 updateTagged();765 if (dataSet != null) {766 dataSet.fireTagsChanged(this);767 }768 }769 770 /**771 855 * Get and write all attributes from the parameter. Does not fire any listener, so 772 856 * use this only in the data initializing phase 773 857 */ 774 858 public void cloneFrom(OsmPrimitive osm) { 775 keys = osm.keys == null ? null : new HashMap<String, String>(osm.keys);859 setKeys(osm.getKeys()); 776 860 id = osm.id; 777 861 timestamp = osm.timestamp; … … 801 885 if (! other.isNew() && other.getId() != id) 802 886 throw new DataIntegrityProblemException(tr("Can''t merge primitives with different ids. This id is {0}, the other is {1}", id, other.getId())); 803 keys = other.keys == null ? null : new HashMap<String, String>(other.keys);887 setKeys(other.getKeys()); 804 888 timestamp = other.timestamp; 805 889 version = other.version; … … 860 944 getUninterestingKeys(); 861 945 if (keys != null) { 862 for (Entry<String,String> e : keys.entrySet()) {946 for (Entry<String,String> e : getKeys().entrySet()) { 863 947 if (!uninteresting.contains(e.getKey())) { 864 948 flags |= FLAG_TAGGED; … … 883 967 getDirectionKeys(); 884 968 if (keys != null) { 885 for (Entry<String,String> e : keys.entrySet()) {969 for (Entry<String,String> e : getKeys().entrySet()) { 886 970 if (directionKeys.contains(e.getKey())) { 887 971 flags |= FLAG_HAS_DIRECTIONS;
Note:
See TracChangeset
for help on using the changeset viewer.