001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.osm; 003 004import java.time.Instant; 005import java.util.Collections; 006import java.util.List; 007import java.util.Map; 008 009import org.openstreetmap.josm.data.IQuadBucketType; 010import org.openstreetmap.josm.data.osm.visitor.PrimitiveVisitor; 011import org.openstreetmap.josm.tools.LanguageInfo; 012 013/** 014 * IPrimitive captures the common functions of {@link OsmPrimitive} and {@link PrimitiveData}. 015 * @since 4098 016 */ 017public interface IPrimitive extends IQuadBucketType, Tagged, PrimitiveId, Stylable, Comparable<IPrimitive> { 018 019 /** 020 * Replies <code>true</code> if the object has been modified since it was loaded from 021 * the server. In this case, on next upload, this object will be updated. 022 * <p> 023 * Deleted objects are deleted from the server. If the objects are added (id=0), 024 * the modified is ignored and the object is added to the server. 025 * 026 * @return <code>true</code> if the object has been modified since it was loaded from 027 * the server 028 */ 029 boolean isModified(); 030 031 /** 032 * Marks this primitive as being modified. 033 * 034 * @param modified true, if this primitive is to be modified 035 */ 036 void setModified(boolean modified); 037 038 /** 039 * Set the status of the referrers 040 * @param referrersDownloaded {@code true} if all referrers for this object have been downloaded 041 * @since 19078 042 */ 043 default void setReferrersDownloaded(boolean referrersDownloaded) { 044 throw new UnsupportedOperationException(this.getClass().getName() + " does not support referrers status"); 045 } 046 047 /** 048 * Checks if object is known to the server. 049 * Replies true if this primitive is either unknown to the server (i.e. its id 050 * is 0) or it is known to the server and it hasn't be deleted on the server. 051 * Replies false, if this primitive is known on the server and has been deleted 052 * on the server. 053 * 054 * @return <code>true</code>, if the object is visible on server. 055 * @see #setVisible(boolean) 056 */ 057 boolean isVisible(); 058 059 /** 060 * Sets whether this primitive is visible, i.e. whether it is known on the server 061 * and not deleted on the server. 062 * @param visible {@code true} if this primitive is visible 063 * 064 * @throws IllegalStateException if visible is set to false on an primitive with id==0 065 * @see #isVisible() 066 */ 067 void setVisible(boolean visible); 068 069 /** 070 * Replies <code>true</code>, if the object has been deleted. 071 * 072 * @return <code>true</code>, if the object has been deleted. 073 * @see #setDeleted(boolean) 074 */ 075 boolean isDeleted(); 076 077 /** 078 * Sets whether this primitive is deleted or not. 079 * <p> 080 * Also marks this primitive as modified if deleted is true. 081 * 082 * @param deleted true, if this primitive is deleted; false, otherwise 083 */ 084 void setDeleted(boolean deleted); 085 086 /** 087 * Determines if this primitive is fully downloaded 088 * @return {@code true} if the primitive is fully downloaded and all parents and children should be available. 089 * {@code false} otherwise. 090 * @since 19078 091 */ 092 default boolean isReferrersDownloaded() { 093 return false; 094 } 095 096 /** 097 * Determines if this primitive is incomplete. 098 * @return {@code true} if this primitive is incomplete, {@code false} otherwise 099 */ 100 boolean isIncomplete(); 101 102 /** 103 * Replies <code>true</code> if the object has been deleted on the server and was undeleted by the user. 104 * @return <code>true</code> if the object has been undeleted 105 */ 106 boolean isUndeleted(); 107 108 /** 109 * Replies <code>true</code>, if the object is usable 110 * (i.e. complete and not deleted). 111 * 112 * @return <code>true</code>, if the object is usable. 113 * @see #setDeleted(boolean) 114 */ 115 boolean isUsable(); 116 117 /** 118 * Determines if this primitive is new or undeleted. 119 * @return True if primitive is new or undeleted 120 * @see #isNew() 121 * @see #isUndeleted() 122 */ 123 boolean isNewOrUndeleted(); 124 125 /** 126 * Replies true, if this primitive is disabled. (E.g. a filter applies) 127 * @return {@code true} if this object has the "disabled" flag enabled 128 * @since 13662 129 */ 130 default boolean isDisabled() { 131 return false; 132 } 133 134 /** 135 * Replies true, if this primitive is disabled and marked as completely hidden on the map. 136 * @return {@code true} if this object has both the "disabled" and "hide if disabled" flags enabled 137 * @since 13662 138 */ 139 default boolean isDisabledAndHidden() { 140 return false; 141 } 142 143 /** 144 * Replies true, if this primitive is preserved from filtering. 145 * @return {@code true} if this object has the "preserved" flag enabled 146 * @since 13764 147 */ 148 default boolean isPreserved() { 149 return false; 150 } 151 152 /** 153 * Determines if this object is selectable. 154 * <p> 155 * A primitive can be selected if all conditions are met: 156 * <ul> 157 * <li>it is drawable 158 * <li>it is not disabled (greyed out) by a filter. 159 * </ul> 160 * @return {@code true} if this object is selectable 161 * @since 13664 162 */ 163 default boolean isSelectable() { 164 return true; 165 } 166 167 /** 168 * Determines if this object is drawable. 169 * <p> 170 * A primitive is drawable if all conditions are met: 171 * <ul> 172 * <li>type and id is known 173 * <li>tags are known 174 * <li>it is not deleted 175 * <li>it is not hidden by a filter 176 * <li>for nodes: lat/lon are known 177 * <li>for ways: all nodes are known and complete 178 * <li>for relations: all members are known and complete 179 * </ul> 180 * @return {@code true} if this object is drawable 181 * @since 13664 182 */ 183 default boolean isDrawable() { 184 return true; 185 } 186 187 /** 188 * Determines whether the primitive is selected 189 * @return whether the primitive is selected 190 * @since 13664 191 */ 192 default boolean isSelected() { 193 return false; 194 } 195 196 /** 197 * Determines if this primitive is a member of a selected relation. 198 * @return {@code true} if this primitive is a member of a selected relation, {@code false} otherwise 199 * @since 13664 200 */ 201 default boolean isMemberOfSelected() { 202 return false; 203 } 204 205 /** 206 * Determines if this primitive is an outer member of a selected multipolygon relation. 207 * @return {@code true} if this primitive is an outer member of a selected multipolygon relation, {@code false} otherwise 208 * @since 13664 209 */ 210 default boolean isOuterMemberOfSelected() { 211 return false; 212 } 213 214 /** 215 * Replies the id of this primitive. 216 * 217 * @return the id of this primitive. 218 */ 219 long getId(); 220 221 /** 222 * Replies the OSM id of this primitive. 223 * By default, returns the same value as {@link #getId}. 224 * Can be overridden by primitive implementations handling an internal id different from the OSM one. 225 * 226 * @return the OSM id of this primitive. 227 * @since 13924 228 */ 229 default long getOsmId() { 230 return getId(); 231 } 232 233 /** 234 * Replies the OSM primitive id for this primitive. 235 * 236 * @return the OSM primitive id for this primitive 237 * @see #getOsmId 238 * @since 13924 239 */ 240 default PrimitiveId getOsmPrimitiveId() { 241 return new SimplePrimitiveId(getOsmId(), getType()); 242 } 243 244 /** 245 * Replies the unique primitive id for this primitive. 246 * 247 * @return the unique primitive id for this primitive 248 * @see #getUniqueId 249 */ 250 default PrimitiveId getPrimitiveId() { 251 return new SimplePrimitiveId(getUniqueId(), getType()); 252 } 253 254 /** 255 * Replies the version number as returned by the API. The version is 0 if the id is 0 or 256 * if this primitive is incomplete. 257 * @return the version number as returned by the API 258 * 259 * @see PrimitiveData#setVersion(int) 260 */ 261 int getVersion(); 262 263 /** 264 * Sets the id and the version of this primitive if it is known to the OSM API. 265 * <p> 266 * Since we know the id and its version it can't be incomplete anymore. incomplete 267 * is set to false. 268 * 269 * @param id the id. > 0 required 270 * @param version the version > 0 required 271 * @throws IllegalArgumentException if id <= 0 272 * @throws IllegalArgumentException if version <= 0 273 * @throws DataIntegrityProblemException if id is changed and primitive was already added to the dataset 274 */ 275 void setOsmId(long id, int version); 276 277 /** 278 * Replies the user who has last touched this object. May be null. 279 * 280 * @return the user who has last touched this object. May be null. 281 */ 282 User getUser(); 283 284 /** 285 * Sets the user who has last touched this object. 286 * 287 * @param user the user 288 */ 289 void setUser(User user); 290 291 /** 292 * Time of last modification to this object. This is not set by JOSM but 293 * read from the server and delivered back to the server unmodified. It is 294 * used to check against edit conflicts. 295 * 296 * @return date of last modification 297 * @see #setInstant 298 */ 299 Instant getInstant(); 300 301 /** 302 * Time of last modification to this object. This is not set by JOSM but 303 * read from the server and delivered back to the server unmodified. It is 304 * used to check against edit conflicts. 305 * 306 * @return last modification as timestamp 307 * @see #setRawTimestamp 308 */ 309 int getRawTimestamp(); 310 311 /** 312 * Sets time of last modification to this object 313 * @param timestamp date of last modification 314 * @see #getInstant 315 */ 316 void setInstant(Instant timestamp); 317 318 /** 319 * Sets time of last modification to this object 320 * @param timestamp date of last modification 321 * @see #getRawTimestamp 322 */ 323 void setRawTimestamp(int timestamp); 324 325 /** 326 * Determines if this primitive has no timestamp information. 327 * @return {@code true} if this primitive has no timestamp information 328 * @see #getInstant 329 * @see #getRawTimestamp 330 */ 331 boolean isTimestampEmpty(); 332 333 /** 334 * Replies the id of the changeset this primitive was last uploaded to. 335 * 0 if this primitive wasn't uploaded to a changeset yet or if the 336 * changeset isn't known. 337 * 338 * @return the id of the changeset this primitive was last uploaded to. 339 */ 340 int getChangesetId(); 341 342 /** 343 * Sets the changeset id of this primitive. Can't be set on a new primitive. 344 * 345 * @param changesetId the id. >= 0 required. 346 * @throws IllegalStateException if this primitive is new. 347 * @throws IllegalArgumentException if id < 0 348 */ 349 void setChangesetId(int changesetId); 350 351 /** 352 * Makes the given visitor visit this primitive. 353 * @param visitor visitor 354 */ 355 void accept(PrimitiveVisitor visitor); 356 357 /** 358 * <p>Visits {@code visitor} for all referrers.</p> 359 * 360 * @param visitor the visitor. Ignored, if null. 361 * @since 13806 362 */ 363 void visitReferrers(PrimitiveVisitor visitor); 364 365 /** 366 * Replies the name of this primitive. The default implementation replies the value 367 * of the tag <code>name</code> or null, if this tag is not present. 368 * 369 * @return the name of this primitive 370 */ 371 default String getName() { 372 return get("name"); 373 } 374 375 /** 376 * Replies a localized name for this primitive given by the value of the name tags 377 * accessed from very specific (language variant) to more generic (default name). 378 * 379 * @return the name of this primitive, <code>null</code> if no name exists 380 * @see LanguageInfo#getOSMLocaleCodes 381 */ 382 default String getLocalName() { 383 for (String s : LanguageInfo.getOSMLocaleCodes("name:")) { 384 String val = get(s); 385 if (val != null) 386 return val; 387 } 388 389 return getName(); 390 } 391 392 /** 393 * Get an object to synchronize the style cache on. This <i>should</i> be a field that does not change during paint. 394 * By default, it returns the current object, but should be overridden to avoid some performance issues. 395 * @return A non-{@code null} object to synchronize on when painting 396 */ 397 default Object getStyleCacheSyncObject() { 398 return this; 399 } 400 401 /** 402 * Replies the display name of a primitive formatted by <code>formatter</code> 403 * @param formatter formatter to use 404 * 405 * @return the display name 406 * @since 13564 407 */ 408 String getDisplayName(NameFormatter formatter); 409 410 /** 411 * Gets the type this primitive is displayed at 412 * @return A {@link OsmPrimitiveType} 413 * @since 13564 414 */ 415 default OsmPrimitiveType getDisplayType() { 416 return getType(); 417 } 418 419 /** 420 * Updates the highlight flag for this primitive. 421 * @param highlighted The new highlight flag. 422 * @since 13664 423 */ 424 void setHighlighted(boolean highlighted); 425 426 /** 427 * Checks if the highlight flag for this primitive was set 428 * @return The highlight flag. 429 * @since 13664 430 */ 431 boolean isHighlighted(); 432 433 /** 434 * Determines if this object is considered "tagged". To be "tagged", an object 435 * must have one or more "interesting" tags. "created_by" and "source" 436 * are typically considered "uninteresting" and do not make an object "tagged". 437 * @return true if this object is considered "tagged" 438 * @since 13662 439 */ 440 boolean isTagged(); 441 442 /** 443 * Determines if this object is considered "annotated". To be "annotated", an object 444 * must have one or more "work in progress" tags, such as "note" or "fixme". 445 * @return true if this object is considered "annotated" 446 * @since 13662 447 */ 448 boolean isAnnotated(); 449 450 /** 451 * Determines if this object is a relation and behaves as a multipolygon. 452 * @return {@code true} if it is a real multipolygon or a boundary relation 453 * @since 13667 454 */ 455 default boolean isMultipolygon() { 456 return false; 457 } 458 459 /** 460 * true if this object has direction dependent tags (e.g. oneway) 461 * @return {@code true} if this object has direction dependent tags 462 * @since 13662 463 */ 464 boolean hasDirectionKeys(); 465 466 /** 467 * true if this object has the "reversed direction" flag enabled 468 * @return {@code true} if this object has the "reversed direction" flag enabled 469 * @since 13662 470 */ 471 boolean reversedDirection(); 472 473 /** 474 * Fetches the bounding box of the primitive. 475 * Since 17752, the returned bounding box might be immutable, i.e., modifying calls throw an {@link UnsupportedOperationException}. 476 * @return Bounding box of the object 477 * @since 13764 478 */ 479 @Override 480 BBox getBBox(); 481 482 /** 483 * Gets a list of all primitives in the current dataset that reference this primitive. 484 * @return The referrers 485 * @since 13764 486 */ 487 default List<? extends IPrimitive> getReferrers() { 488 return getReferrers(false); 489 } 490 491 /** 492 * Find primitives that reference this primitive. Returns only primitives that are included in the same 493 * dataset as this primitive. <br> 494 * 495 * For example following code will add wnew as referer to all nodes of existingWay, but this method will 496 * not return wnew because it's not part of the dataset <br> 497 * 498 * <code>Way wnew = new Way(existingWay)</code> 499 * 500 * @param allowWithoutDataset If true, method will return empty list if primitive is not part of the dataset. If false, 501 * exception will be thrown in this case 502 * 503 * @return a collection of all primitives that reference this primitive. 504 * @since 13808 505 */ 506 List<? extends IPrimitive> getReferrers(boolean allowWithoutDataset); 507 508 /** 509 * Returns the parent data set of this primitive. 510 * @return OsmData this primitive is part of. 511 * @since 13807 512 */ 513 OsmData<?, ?, ?, ?> getDataSet(); 514 515 /** 516 * Returns {@link #getKeys()} for which {@code key} does not fulfill uninteresting criteria. 517 * @return A map of interesting tags 518 * @since 13809 519 */ 520 Map<String, String> getInterestingTags(); 521 522 /** 523 * Replies true if other isn't null and has the same interesting tags (key/value-pairs) as this. 524 * 525 * @param other the other object primitive 526 * @return true if other isn't null and has the same interesting tags (key/value-pairs) as this. 527 * @since 13809 528 */ 529 default boolean hasSameInterestingTags(IPrimitive other) { 530 return (!hasKeys() && !other.hasKeys()) 531 || getInterestingTags().equals(other.getInterestingTags()); 532 } 533 534 /** 535 * Resets primitive children, if applicable. 536 * @param p primitive 537 * @since 17981 538 */ 539 static void resetPrimitiveChildren(IPrimitive p) { 540 if (p instanceof IWay<?>) { 541 ((IWay<?>) p).setNodes(null); 542 } else if (p instanceof IRelation<?>) { 543 ((IRelation<?>) p).setMembers(null); 544 } 545 } 546 547 /** 548 * Get child primitives that are referred by this primitive. 549 * {@link Relation}: Members of the relation 550 * {@link Way}: Nodes used by the way 551 * {@link Node}: None 552 * @return List of child primitives 553 * @since 18814 554 */ 555 default List<? extends IPrimitive> getChildren() { 556 return Collections.emptyList(); 557 } 558}