Changeset 2448 in josm for trunk/src/org/openstreetmap/josm/data/osm
- Timestamp:
- 2009-11-14T17:59:10+01:00 (15 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/data/osm
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitive.java
r2439 r2448 811 811 * @throws IllegalArgumentException thrown if other is null. 812 812 * @throws DataIntegrityProblemException thrown if either this is new and other is not, or other is new and this is not 813 * @throws DataIntegrityProblemException thrown if other is new and other.getId() != this.getId()813 * @throws DataIntegrityProblemException thrown if other isn't new and other.getId() != this.getId() 814 814 */ 815 815 public void mergeFrom(OsmPrimitive other) { … … 1025 1025 public abstract void updatePosition(); 1026 1026 1027 /** 1028 * Replies the unique primitive id for this primitive 1029 * 1030 * @return the unique primitive id for this primitive 1031 */ 1032 public PrimitiveId getPrimitiveId() { 1033 return new SimplePrimitiveId(getUniqueId(), getType()); 1034 } 1027 1035 } 1028 1036 -
trunk/src/org/openstreetmap/josm/data/osm/SimplePrimitiveId.java
r2399 r2448 20 20 } 21 21 22 @Override 23 public int hashCode() { 24 final int prime = 31; 25 int result = 1; 26 result = prime * result + (int) (id ^ (id >>> 32)); 27 result = prime * result + ((type == null) ? 0 : type.hashCode()); 28 return result; 29 } 22 30 23 31 @Override 32 public boolean equals(Object obj) { 33 if (this == obj) 34 return true; 35 if (obj == null) 36 return false; 37 if (getClass() != obj.getClass()) 38 return false; 39 SimplePrimitiveId other = (SimplePrimitiveId) obj; 40 if (id != other.id) 41 return false; 42 if (type == null) { 43 if (other.type != null) 44 return false; 45 } else if (!type.equals(other.type)) 46 return false; 47 return true; 48 } 24 49 } -
trunk/src/org/openstreetmap/josm/data/osm/history/History.java
r2242 r2448 9 9 import java.util.Date; 10 10 import java.util.List; 11 import java.util.NoSuchElementException;12 11 13 12 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 14 13 import org.openstreetmap.josm.data.osm.PrimitiveId; 14 import org.openstreetmap.josm.data.osm.SimplePrimitiveId; 15 16 /** 17 * Represents the history of an OSM primitive. The history consists 18 * of a list of object snapshots with a specific version. 19 * 20 */ 15 21 public class History{ 16 22 private static interface FilterPredicate { … … 25 31 } 26 32 } 27 return new History(history.id, out); 28 } 29 33 return new History(history.id, history.type,out); 34 } 35 36 /** the list of object snapshots */ 30 37 private ArrayList<HistoryOsmPrimitive> versions; 31 long id; 32 33 protected History(long id, List<HistoryOsmPrimitive> versions) { 38 /** the object id */ 39 private long id; 40 private OsmPrimitiveType type; 41 42 /** 43 * Creates a new history for an OSM primitive 44 * 45 * @param id the id. >0 required. 46 * @param type the primitive type. Must not be null. 47 * @param versions a list of versions. Can be null. 48 * @throws IllegalArgumentException thrown if id <= 0 49 * @throws IllegalArgumentException if type is null 50 * 51 */ 52 protected History(long id, OsmPrimitiveType type, List<HistoryOsmPrimitive> versions) { 53 if (id <= 0) 54 throw new IllegalArgumentException(tr("Parameter ''{0}'' > 0 expected, got {1}", "id", id)); 55 if (type == null) 56 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "type")); 34 57 this.id = id; 58 this.type = type; 35 59 this.versions = new ArrayList<HistoryOsmPrimitive>(); 36 this.versions.addAll(versions); 60 if (versions != null) { 61 this.versions.addAll(versions); 62 } 37 63 } 38 64 … … 47 73 } 48 74 ); 49 return new History(id, copy);75 return new History(id, type, copy); 50 76 } 51 77 … … 60 86 } 61 87 ); 62 return new History(id, copy);88 return new History(id, type,copy); 63 89 } 64 90 … … 139 165 public long getId() { 140 166 return id; 167 } 168 169 /** 170 * Replies the primitive id for this history. 171 * 172 * @return the primitive id 173 */ 174 public PrimitiveId getPrimitmiveId() { 175 return new SimplePrimitiveId(id, type); 141 176 } 142 177 … … 149 184 } 150 185 186 /** 187 * Replies the history primitive with version <code>version</code>. null, 188 * if no such primitive exists. 189 * 190 * @param version the version 191 * @return the history primitive with version <code>version</code> 192 */ 151 193 public HistoryOsmPrimitive getByVersion(long version) { 152 194 for (HistoryOsmPrimitive primitive: versions) { … … 154 196 return primitive; 155 197 } 156 throw new NoSuchElementException(tr("There's no primitive with version {0} in this history.", version));198 return null; 157 199 } 158 200 159 201 public HistoryOsmPrimitive getByDate(Date date) { 160 sortAscending();161 162 if ( versions.isEmpty())163 throw new NoSuchElementException(tr("There's no version valid at date ''{0}'' in this history.", date));164 if ( get(0).getTimestamp().compareTo(date)> 0)165 throw new NoSuchElementException(tr("There's no version valid at date ''{0}'' in this history.", date));166 for (int i = 1; i < versions.size();i++) {167 if ( get(i-1).getTimestamp().compareTo(date) <= 0168 && get(i).getTimestamp().compareTo(date) >= 0)169 return get(i);170 } 171 return getLatest();202 History h = sortAscending(); 203 204 if (h.versions.isEmpty()) 205 return null; 206 if (h.get(0).getTimestamp().compareTo(date)> 0) 207 return null; 208 for (int i = 1; i < h.versions.size();i++) { 209 if (h.get(i-1).getTimestamp().compareTo(date) <= 0 210 && h.get(i).getTimestamp().compareTo(date) >= 0) 211 return h.get(i); 212 } 213 return h.getLatest(); 172 214 } 173 215 … … 180 222 public HistoryOsmPrimitive getEarliest() { 181 223 if (isEmpty()) 182 throw new NoSuchElementException(tr("No earliest version found. History is empty."));224 return null; 183 225 return sortAscending().versions.get(0); 184 226 } … … 186 228 public HistoryOsmPrimitive getLatest() { 187 229 if (isEmpty()) 188 throw new NoSuchElementException(tr("No latest version found. History is empty."));230 return null; 189 231 return sortDescending().versions.get(0); 190 232 } … … 199 241 200 242 public OsmPrimitiveType getType() { 201 if (isEmpty()) 202 throw new NoSuchElementException(tr("No type found. History is empty.")); 203 return versions.get(0).getType(); 243 return type; 204 244 } 205 245 } -
trunk/src/org/openstreetmap/josm/data/osm/history/HistoryDataSet.java
r2163 r2448 6 6 import java.util.ArrayList; 7 7 import java.util.HashMap; 8 import java.util.NoSuchElementException;9 8 import java.util.concurrent.CopyOnWriteArrayList; 9 import java.util.logging.Logger; 10 11 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 12 import org.openstreetmap.josm.data.osm.PrimitiveId; 13 import org.openstreetmap.josm.data.osm.SimplePrimitiveId; 10 14 11 15 /** … … 15 19 */ 16 20 public class HistoryDataSet { 21 private final static Logger logger = Logger.getLogger(HistoryDataSet.class.getName()); 17 22 18 23 /** the unique instance */ … … 32 37 33 38 /** the history data */ 34 private HashMap< Long, ArrayList<HistoryOsmPrimitive>> data;39 private HashMap<PrimitiveId, ArrayList<HistoryOsmPrimitive>> data; 35 40 private CopyOnWriteArrayList<HistoryDataSetListener> listeners; 36 41 37 42 public HistoryDataSet() { 38 data = new HashMap< Long, ArrayList<HistoryOsmPrimitive>>();43 data = new HashMap<PrimitiveId, ArrayList<HistoryOsmPrimitive>>(); 39 44 listeners = new CopyOnWriteArrayList<HistoryDataSetListener>(); 40 45 } … … 56 61 } 57 62 58 protected void fireHistoryUpdated( longid) {63 protected void fireHistoryUpdated(SimplePrimitiveId id) { 59 64 for (HistoryDataSetListener l : listeners) { 60 65 l.historyUpdated(this, id); … … 64 69 /** 65 70 * Replies the history primitive for the primitive with id <code>id</code> 66 * and version <code>version</code> 71 * and version <code>version</code>. null, if no such primitive exists. 67 72 * 68 * @param id the id of the primitive 69 * @param version the version of the primitive 70 * @return the history primitive for the primitive with id <code>id</code> 71 * and version <code>version</code> 72 * @throws NoSuchElementException thrown if this dataset doesn't include the respective 73 * history primitive 73 * @param id the id of the primitive. > 0 required. 74 * @param type the primitive type. Must not be null. 75 * @param version the version of the primitive. > 0 required 76 * @return the history primitive for the primitive with id <code>id</code>, 77 * type <code>type</code>, and version <code>version</code> 74 78 */ 75 public HistoryOsmPrimitive get(long id, long version) throws NoSuchElementException{ 76 ArrayList<HistoryOsmPrimitive> versions = data.get(id); 79 public HistoryOsmPrimitive get(long id, OsmPrimitiveType type, long version){ 80 if (id <= 0) 81 throw new IllegalArgumentException(tr("Parameter ''{0}'' > 0 expected, got {1}", "id", id)); 82 if (type == null) 83 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "type")); 84 if (version <= 0) 85 throw new IllegalArgumentException(tr("Parameter ''{0}'' > 0 expected, got {1}", "version", version)); 86 87 SimplePrimitiveId pid = new SimplePrimitiveId(id, type); 88 ArrayList<HistoryOsmPrimitive> versions = data.get(pid); 77 89 if (versions == null) 78 throw new NoSuchElementException(tr("Didn't find an primitive with id {0} in this dataset", id)); 79 90 return null; 80 91 for (HistoryOsmPrimitive primitive: versions) { 81 92 if (primitive.matches(id, version)) 82 93 return primitive; 83 94 } 84 throw new NoSuchElementException(tr("Didn't find an primitive with id {0} and version {1} in this dataset", id, version));95 return null; 85 96 } 86 97 … … 91 102 */ 92 103 public void put(HistoryOsmPrimitive primitive) { 93 if (data.get(primitive.getId()) == null) { 94 data.put(primitive.getId(), new ArrayList<HistoryOsmPrimitive>()); 104 SimplePrimitiveId id = new SimplePrimitiveId(primitive.getId(), primitive.getType()); 105 if (data.get(id) == null) { 106 data.put(id, new ArrayList<HistoryOsmPrimitive>()); 95 107 } 96 data.get( primitive.getId()).add(primitive);97 fireHistoryUpdated( primitive.getId());108 data.get(id).add(primitive); 109 fireHistoryUpdated(id); 98 110 } 99 111 100 112 /** 101 113 * Replies the history for a given primitive with id <code>id</code> 114 * and type <code>type</code>. 102 115 * 103 * @param id the id 104 * @return the history 116 * @param id the id the if of the primitive. > 0 required 117 * @param type the type of the primitive. Must not be null. 118 * @return the history. null, if there isn't a history for <code>id</code> and 119 * <code>type</code>. 120 * @throws IllegalArgumentException thrown if id <= 0 121 * @throws IllegalArgumentException thrown if type is null 105 122 */ 106 public History getHistory(long id) { 107 ArrayList<HistoryOsmPrimitive> versions = data.get(id); 123 public History getHistory(long id, OsmPrimitiveType type) throws IllegalArgumentException{ 124 if (id <= 0) 125 throw new IllegalArgumentException(tr("Parameter ''{0}'' > 0 expected, got {1}", "id", id)); 126 if (type == null) 127 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "type")); 128 SimplePrimitiveId pid = new SimplePrimitiveId(id, type); 129 return getHistory(pid); 130 } 131 132 /** 133 * Replies the history for a primitive with id <code>id</code>. null, if no 134 * such history exists. 135 * 136 * @param pid the primitive id. Must not be null. 137 * @return the history for a primitive with id <code>id</code>. null, if no 138 * such history exists 139 * @throws IllegalArgumentException thrown if pid is null 140 */ 141 public History getHistory(PrimitiveId pid) throws IllegalArgumentException{ 142 if (pid == null) 143 throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "pid")); 144 ArrayList<HistoryOsmPrimitive> versions = data.get(pid); 108 145 if (versions == null) 109 146 return null; 110 return new History( id, versions);147 return new History(pid.getUniqueId(), pid.getType(), versions); 111 148 } 112 149 … … 119 156 if (other == null) 120 157 return; 121 for ( Longid : other.data.keySet()) {158 for (PrimitiveId id : other.data.keySet()) { 122 159 this.data.put(id, other.data.get(id)); 123 160 } 124 fireHistoryUpdated( 0);161 fireHistoryUpdated(null); 125 162 } 126 163 } -
trunk/src/org/openstreetmap/josm/data/osm/history/HistoryDataSetListener.java
r2019 r2448 2 2 package org.openstreetmap.josm.data.osm.history; 3 3 4 import org.openstreetmap.josm.data.osm.PrimitiveId; 5 4 6 public interface HistoryDataSetListener { 5 void historyUpdated(HistoryDataSet source, long primitiveId);7 void historyUpdated(HistoryDataSet source, PrimitiveId id); 6 8 } -
trunk/src/org/openstreetmap/josm/data/osm/history/HistoryOsmPrimitive.java
r2242 r2448 10 10 11 11 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 12 import org.openstreetmap.josm.data.osm.PrimitiveId; 13 import org.openstreetmap.josm.data.osm.SimplePrimitiveId; 12 14 13 15 /** … … 45 47 * @param user the user (! null required) 46 48 * @param uid the user id (> 0 required) 47 * @param changesetId the changeset id ( > 0 required)49 * @param changesetId the changeset id (may be null if the changeset isn't known) 48 50 * @param timestamp the timestamp (! null required) 49 51 * … … 53 55 ensurePositiveLong(id, "id"); 54 56 ensurePositiveLong(version, "version"); 55 if(uid != -1) /* allow -1 for anonymous users */57 if(uid != -1) { 56 58 ensurePositiveLong(uid, "uid"); 57 ensurePositiveLong(changesetId, "changesetId");59 } 58 60 ensureNotNull(user, "user"); 59 61 ensureNotNull(timestamp, "timestamp"); … … 63 65 this.user = user; 64 66 this.uid = uid; 67 // FIXME: restrict to IDs > 0 as soon as OsmPrimitive holds the 68 // changeset id too 65 69 this.changesetId = changesetId; 66 70 this.timestamp = timestamp; … … 71 75 return id; 72 76 } 77 78 public PrimitiveId getPrimitiveId() { 79 return new SimplePrimitiveId(id, getType()); 80 } 81 73 82 public boolean isVisible() { 74 83 return visible; … … 103 112 public int compareTo(HistoryOsmPrimitive o) { 104 113 if (this.id != o.id) 105 throw new ClassCastException(tr("Can' t compare primitive with ID ''{0}'' to primitive with ID ''{1}''.", o.id, this.id));114 throw new ClassCastException(tr("Can''t compare primitive with ID ''{0}'' to primitive with ID ''{1}''.", o.id, this.id)); 106 115 return new Long(this.version).compareTo(o.version); 107 116 } … … 121 130 public Map<String,String> getTags() { 122 131 return Collections.unmodifiableMap(tags); 132 } 133 134 /** 135 * Sets the tags for this history primitive. Removes all 136 * tags if <code>tags</code> is null. 137 * 138 * @param tags the tags. May be null. 139 */ 140 public void setTags(Map<String,String> tags) { 141 if (tags == null) { 142 this.tags = new HashMap<String, String>(); 143 } else { 144 this.tags = new HashMap<String, String>(tags); 145 } 123 146 } 124 147
Note:
See TracChangeset
for help on using the changeset viewer.