- Timestamp:
- 2009-11-19T16:36:39+01:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java
r2481 r2482 10 10 import java.util.Date; 11 11 import java.util.HashMap; 12 import java.util.HashSet; 12 13 import java.util.LinkedHashSet; 13 14 import java.util.LinkedList; … … 15 16 import java.util.Locale; 16 17 import java.util.Map; 18 import java.util.Set; 17 19 import java.util.Map.Entry; 18 20 import java.util.concurrent.atomic.AtomicLong; … … 595 597 * 596 598 */ 597 private Object keys; 598 private static final String DEFAULT_KEY = "created_by"; 599 private String[] keys; 599 600 600 601 /** 601 602 * Replies the map of key/value pairs. Never replies null. The map can be empty, though. 602 603 * 603 * @return Keys of this primitive. Changes made in returned map are not mapped604 * @return tags of this primitive. Changes made in returned map are not mapped 604 605 * back to the primitive, use setKeys() to modify the keys 605 606 * @since 1924 … … 608 609 Map<String, String> result = new HashMap<String, String>(); 609 610 if (keys != null) { 610 if (keys instanceof String) { 611 result.put(DEFAULT_KEY, (String)keys); 612 } else { 613 String[] map = (String[])keys; 614 if (map[0] != null) { 615 result.put(DEFAULT_KEY, map[0]); 616 } 617 for (int i=0; i<map.length / 2; i++) { 618 result.put(map[i * 2 + 1], map[i * 2 + 2]); 619 } 611 for (int i=0; i<keys.length ; i+=2) { 612 result.put(keys[i], keys[i + 1]); 620 613 } 621 614 } … … 633 626 if (keys == null) { 634 627 this.keys = null; 635 } else { 636 String[] newKeys = new String[keys.size() * 2 + (keys.containsKey(DEFAULT_KEY)?-1:1)]; 637 int index = 1; 638 for (Entry<String, String> entry:keys.entrySet()) { 639 if (DEFAULT_KEY.equals(entry.getKey())) { 640 newKeys[0] = entry.getValue(); 641 } else { 642 newKeys[index++] = entry.getKey(); 643 newKeys[index++] = entry.getValue(); 644 } 645 } 646 this.keys = newKeys; 647 } 628 return; 629 } 630 String[] newKeys = new String[keys.size() * 2]; 631 int index = 0; 632 for (Entry<String, String> entry:keys.entrySet()) { 633 newKeys[index++] = entry.getKey(); 634 newKeys[index++] = entry.getValue(); 635 } 636 this.keys = newKeys; 648 637 keysChangedImpl(); 649 638 } … … 663 652 else if (value == null) { 664 653 remove(key); 654 } else if (keys == null || keys.length == 0){ 655 keys = new String[] {key, value}; 665 656 } else { 666 // TODO More effective implementation 667 Map<String, String> keys = getKeys(); 668 if (!value.equals(keys.put(key, value))) { 669 setKeys(keys); 670 } 657 for (int i=0; i<keys.length;i+=2) { 658 if (keys[i].equals(key)) { 659 keys[i+1] = value; 660 return; 661 } 662 } 663 String[] newKeys = new String[keys.length + 2]; 664 for (int i=0; i< keys.length;i+=2) { 665 newKeys[i] = keys[i]; 666 newKeys[i+1] = keys[i+1]; 667 } 668 newKeys[keys.length] = key; 669 newKeys[keys.length + 1] = value; 670 keys = newKeys; 671 671 } 672 672 } … … 677 677 */ 678 678 public final void remove(String key) { 679 Map<String, String> keys = getKeys(); 680 if (keys.remove(key) != null) { 681 // TODO More effective implemenation 682 setKeys(keys); 683 } 679 if (key == null || keys == null || keys.length == 0 ) return; 680 if (!hasKey(key)) 681 return; 682 if (keys.length == 2) { 683 keys = null; 684 return; 685 } 686 String[] newKeys = new String[keys.length - 2]; 687 int j=0; 688 for (int i=0; i < keys.length; i+=2) { 689 if (!keys[i].equals(key)) { 690 newKeys[j++] = keys[i]; 691 newKeys[j++] = keys[i+1]; 692 } 693 } 694 keys = newKeys; 684 695 } 685 696 … … 704 715 if (key == null) 705 716 return null; 706 707 if (keys == null) 717 if (keys == null || keys.length == 0) 708 718 return null; 709 710 if (keys instanceof String) 711 return DEFAULT_KEY.equals(key)?(String)keys:null; 712 713 String[] map = (String[])keys; 714 if (DEFAULT_KEY.equals(key)) 715 return map[0]; 716 else { 717 for (int i=0; i<map.length/2; i++) { 718 if (key.equals(map[i * 2 + 1])) 719 return map[i * 2 + 2]; 720 } 721 } 722 return null; 723 } 724 719 for (int i=0; i<keys.length;i+=2) { 720 if (keys[i].equals(key)) return keys[i+1]; 721 } 722 return null; 723 } 724 725 // FIXME: why replying a collection of entries? Should be replaced by 726 // a method Map<String,String> getTags() 727 // 725 728 public final Collection<Entry<String, String>> entrySet() { 726 if (keys == null )729 if (keys == null || keys.length ==0) 727 730 return Collections.emptySet(); 728 else if (keys instanceof String) 729 return Collections.<Entry<String, String>>singleton(new KeysEntry(DEFAULT_KEY, (String)keys)); 730 else { 731 String[] map = (String[])keys; 732 List<Entry<String, String>> result = new ArrayList<Entry<String,String>>(); 733 if (map[0] != null) { 734 result.add(new KeysEntry(DEFAULT_KEY, map[0])); 735 } 736 for (int i=0; i<map.length / 2; i++) { 737 result.add(new KeysEntry(map[i * 2 + 1], map[i * 2 + 2])); 738 } 739 return result; 740 } 731 Set<Entry<String, String>> result = new HashSet<Entry<String,String>>(); 732 for (int i=0; i<keys.length; i+=2) { 733 result.add(new KeysEntry(keys[i], keys[i+1])); 734 } 735 return result; 741 736 } 742 737 743 738 public final Collection<String> keySet() { 744 if (keys == null )739 if (keys == null || keys.length == 0) 745 740 return Collections.emptySet(); 746 else if (keys instanceof String) 747 return Collections.singleton(DEFAULT_KEY); 748 else { 749 String[] map = (String[])keys; 750 List<String> result = new ArrayList<String>(map.length / 2 + 1); 751 if (map[0] != null) { 752 result.add(DEFAULT_KEY); 753 } 754 for (int i=0; i<map.length / 2; i++) { 755 result.add(map[i * 2 + 1]); 756 } 757 return result; 758 } 741 Set<String> result = new HashSet<String>(keys.length / 2); 742 for (int i=0; i<keys.length; i+=2) { 743 result.add(keys[i]); 744 } 745 return result; 759 746 } 760 747 … … 766 753 */ 767 754 public final boolean hasKeys() { 768 return keys != null ;755 return keys != null && keys.length != 0; 769 756 } 770 757 … … 778 765 } 779 766 767 /** 768 * Replies true if this primitive has a tag with key <code>key</code> 769 * 770 * @param key the key 771 * @return true, if his primitive has a tag with key <code>key</code> 772 */ 773 public boolean hasKey(String key) { 774 if (key == null) return false; 775 if (keys == null) return false; 776 for (int i=0; i< keys.length;i+=2) { 777 if (keys[i].equals(key)) return true; 778 } 779 return false; 780 } 781 782 /** 783 * Replies true if other isn't null and has the same tags (key/value-pairs) as this. 784 * 785 * @param other the other object primitive 786 * @return true if other isn't null and has the same tags (key/value-pairs) as this. 787 */ 788 public boolean hasSameTags(OsmPrimitive other) { 789 return entrySet().equals(other.entrySet()); 790 } 780 791 781 792 /*------------ … … 939 950 if (incomplete && ! other.incomplete || !incomplete && other.incomplete) 940 951 return false; 941 return keySet().equals(other.keySet()); 952 // can't do an equals check on the internal keys array because it is not ordered 953 // 954 return hasSameTags(other); 942 955 } 943 956
Note:
See TracChangeset
for help on using the changeset viewer.