Class OsmPrimitive

java.lang.Object
org.openstreetmap.josm.data.osm.AbstractPrimitive
org.openstreetmap.josm.data.osm.OsmPrimitive
All Implemented Interfaces:
Comparable<IPrimitive>, IQuadBucketType, IFilterablePrimitive, IPrimitive, PrimitiveId, Stylable, Tagged, TemplateEngineDataProvider
Direct Known Subclasses:
Node, Relation, Way

public abstract class OsmPrimitive extends AbstractPrimitive implements TemplateEngineDataProvider
The base class for OSM objects (Node, Way, Relation).

It can be created, deleted and uploaded to the OSM-Server.

Although OsmPrimitive is designed as a base class, it is not to be meant to subclass it by any other than from the package org.openstreetmap.josm.data.osm. The available primitives are a fixed set that are given by the server environment and not an extendable data stuff.

  • Field Details

  • Constructor Details

    • OsmPrimitive

      protected OsmPrimitive(long id, boolean allowNegativeId)
      Creates a new primitive for the given id.

      If allowNegativeId is set, provided id can be < 0 and will be set to primitive without any processing. If allowNegativeId is not set, then id will have to be 0 (in that case new unique id will be generated) or positive number.

      Parameters:
      id - the id
      allowNegativeId - true to allow negative id
      Throws:
      IllegalArgumentException - if id < 0 and allowNegativeId is false
    • OsmPrimitive

      protected OsmPrimitive(long id, int version, boolean allowNegativeId)
      Creates a new primitive for the given id and version.

      If allowNegativeId is set, provided id can be < 0 and will be set to primitive without any processing. If allowNegativeId is not set, then id will have to be 0 (in that case new unique id will be generated) or positive number.

      If id is not > 0 version is ignored and set to 0.

      Parameters:
      id - the id
      version - the version (positive integer)
      allowNegativeId - true to allow negative id
      Throws:
      IllegalArgumentException - if id < 0 and allowNegativeId is false
  • Method Details

    • getReferrer

      public static Set<OsmPrimitive> getReferrer(Collection<? extends OsmPrimitive> primitives)
      Replies the collection of referring primitives for the primitives in primitives.
      Parameters:
      primitives - the collection of primitives.
      Returns:
      the collection of referring primitives for the primitives in primitives; empty set if primitives is null or if there are no referring primitives
    • getCachedStyle

      public final StyleCache getCachedStyle(ElemStyles elemStyles)
      Description copied from interface: Stylable
      Returns the cached style.
      Specified by:
      getCachedStyle in interface Stylable
      Parameters:
      elemStyles - styles for which the data is retrieved
      Returns:
      the cached style
    • setCachedStyle

      public final void setCachedStyle(ElemStyles elemStyles, StyleCache mappaintStyle)
      Description copied from interface: Stylable
      Sets the cached style.
      Specified by:
      setCachedStyle in interface Stylable
      Parameters:
      elemStyles - styles for which the data is stored
      mappaintStyle - the cached style
    • isCachedStyleUpToDate

      public final boolean isCachedStyleUpToDate(ElemStyles elemStyles)
      Description copied from interface: Stylable
      Check if the cached style for this primitive is up to date.
      Specified by:
      isCachedStyleUpToDate in interface Stylable
      Parameters:
      elemStyles - styles for which the data is checked
      Returns:
      true if the cached style for this primitive is up to date
    • declareCachedStyleUpToDate

      public final void declareCachedStyleUpToDate(ElemStyles styles)
      Description copied from interface: Stylable
      Declare that the cached style for this primitive is up to date.
      Specified by:
      declareCachedStyleUpToDate in interface Stylable
      Parameters:
      styles - styles for which the data is handled
    • clearCachedStyle

      public void clearCachedStyle()
      Description copied from interface: Stylable
      Clears the cached style. This should not be called from outside. Fixing the UI to add relevant get/set functions calling this implicitly is preferred, so we can have transparent cache handling in the future.
      Specified by:
      clearCachedStyle in interface Stylable
    • setDataset

      void setDataset(DataSet dataSet)
      This method should never ever by called from somewhere else than Dataset.addPrimitive or removePrimitive methods
      Parameters:
      dataSet - the parent dataset
    • getDataSet

      public DataSet getDataSet()
      Description copied from interface: IPrimitive
      Returns the parent data set of this primitive.
      Specified by:
      getDataSet in interface IPrimitive
      Returns:
      OsmData this primitive is part of.
    • checkDataset

      public void checkDataset()
      Throws exception if primitive is not part of the dataset
    • checkDatasetNotReadOnly

      protected final void checkDatasetNotReadOnly()
      Throws exception if primitive is in a read-only dataset
    • writeLock

      protected boolean writeLock()
    • writeUnlock

      protected void writeUnlock(boolean locked)
    • setOsmId

      public void setOsmId(long id, int version)
      Sets the id and the version of this primitive if it is known to the OSM API.

      Since we know the id and its version it can't be incomplete anymore. incomplete is set to false.

      Specified by:
      setOsmId in interface IPrimitive
      Overrides:
      setOsmId in class AbstractPrimitive
      Parameters:
      id - the id. > 0 required
      version - the version > 0 required
      Throws:
      IllegalArgumentException - if id <= 0
      IllegalArgumentException - if version <= 0
      DataIntegrityProblemException - if id is changed and primitive was already added to the dataset
    • clearOsmMetadata

      public void clearOsmMetadata()
      Clears the metadata, including id and version known to the OSM API. The id is a new unique id. The version, changeset and timestamp are set to 0. incomplete and deleted are set to false. It's preferred to use copy constructor with clearMetadata set to true instead

      Caution: Do not use this method on primitives which are already added to a DataSet.

      Overrides:
      clearOsmMetadata in class AbstractPrimitive
      Throws:
      DataIntegrityProblemException - If primitive was already added to the dataset
      Since:
      6140
    • setUser

      public void setUser(User user)
      Description copied from interface: IPrimitive
      Sets the user who has last touched this object.
      Specified by:
      setUser in interface IPrimitive
      Overrides:
      setUser in class AbstractPrimitive
      Parameters:
      user - the user
    • setChangesetId

      public void setChangesetId(int changesetId)
      Description copied from interface: IPrimitive
      Sets the changeset id of this primitive. Can't be set on a new primitive.
      Specified by:
      setChangesetId in interface IPrimitive
      Overrides:
      setChangesetId in class AbstractPrimitive
      Parameters:
      changesetId - the id. >= 0 required.
    • updateFlagsNoLock

      private void updateFlagsNoLock(short flag, boolean value)
    • updateFlags

      protected final void updateFlags(short flag, boolean value)
      Overrides:
      updateFlags in class AbstractPrimitive
    • setDisabledState

      public boolean setDisabledState(boolean hidden)
      Description copied from interface: IFilterablePrimitive
      Make the primitive disabled (e.g. if a filter applies). To enable the primitive again, use unsetDisabledState.
      Specified by:
      setDisabledState in interface IFilterablePrimitive
      Overrides:
      setDisabledState in class AbstractPrimitive
      Parameters:
      hidden - if the primitive should be completely hidden from view or just shown in gray color.
      Returns:
      true, any flag has changed; false if you try to set the disabled state to the value that is already preset
    • unsetDisabledState

      public boolean unsetDisabledState()
      Remove the disabled flag from the primitive. Afterwards, the primitive is displayed normally and can be selected again.
      Specified by:
      unsetDisabledState in interface IFilterablePrimitive
      Overrides:
      unsetDisabledState in class AbstractPrimitive
      Returns:
      true if a change occurred
    • setPreserved

      public void setPreserved(boolean isPreserved)
      Set binary property used internally by the filter mechanism.
      Parameters:
      isPreserved - new "preserved" flag value
      Since:
      13309
    • isSelectable

      public boolean isSelectable()
      Description copied from interface: IPrimitive
      Determines if this object is selectable.

      A primitive can be selected if all conditions are met:

      • it is drawable
      • it is not disabled (greyed out) by a filter.
      Specified by:
      isSelectable in interface IPrimitive
      Returns:
      true if this object is selectable
    • setModified

      public void setModified(boolean modified)
      Description copied from interface: IPrimitive
      Marks this primitive as being modified.
      Specified by:
      setModified in interface IPrimitive
      Overrides:
      setModified in class AbstractPrimitive
      Parameters:
      modified - true, if this primitive is to be modified
    • setVisible

      public void setVisible(boolean visible)
      Description copied from interface: IPrimitive
      Sets whether this primitive is visible, i.e. whether it is known on the server and not deleted on the server.
      Specified by:
      setVisible in interface IPrimitive
      Overrides:
      setVisible in class AbstractPrimitive
      Parameters:
      visible - true if this primitive is visible
      See Also:
    • setDeleted

      public void setDeleted(boolean deleted)
      Description copied from interface: IPrimitive
      Sets whether this primitive is deleted or not.

      Also marks this primitive as modified if deleted is true.

      Specified by:
      setDeleted in interface IPrimitive
      Overrides:
      setDeleted in class AbstractPrimitive
      Parameters:
      deleted - true, if this primitive is deleted; false, otherwise
    • setIncomplete

      protected final void setIncomplete(boolean incomplete)
      Description copied from class: AbstractPrimitive
      If set to true, this object is incomplete, which means only the id and type is known (type is the objects instance class)
      Overrides:
      setIncomplete in class AbstractPrimitive
      Parameters:
      incomplete - incomplete flag value
    • isSelected

      public boolean isSelected()
      Description copied from interface: IPrimitive
      Determines whether the primitive is selected
      Specified by:
      isSelected in interface IPrimitive
      Returns:
      whether the primitive is selected
    • isMemberOfSelected

      public boolean isMemberOfSelected()
      Description copied from interface: IPrimitive
      Determines if this primitive is a member of a selected relation.
      Specified by:
      isMemberOfSelected in interface IPrimitive
      Returns:
      true if this primitive is a member of a selected relation, false otherwise
    • isOuterMemberOfSelected

      public boolean isOuterMemberOfSelected()
      Description copied from interface: IPrimitive
      Determines if this primitive is an outer member of a selected multipolygon relation.
      Specified by:
      isOuterMemberOfSelected in interface IPrimitive
      Returns:
      true if this primitive is an outer member of a selected multipolygon relation, false otherwise
    • isOuterMemberOfMultipolygon

    • setHighlighted

      public void setHighlighted(boolean highlighted)
      Description copied from interface: IPrimitive
      Updates the highlight flag for this primitive.
      Specified by:
      setHighlighted in interface IPrimitive
      Parameters:
      highlighted - The new highlight flag.
    • compileDirectionKeys

      private static SearchCompiler.Match compileDirectionKeys(String prefName, String defaultValue) throws AssertionError
      Throws:
      AssertionError
    • updateTagged

      private void updateTagged()
    • updateAnnotated

      private void updateAnnotated()
    • updateDirectionFlags

      protected void updateDirectionFlags()
    • setKeys

      public final void setKeys(TagMap keys)
      Description copied from class: AbstractPrimitive
      Copy the keys from a TagMap.
      Overrides:
      setKeys in class AbstractPrimitive
      Parameters:
      keys - The new key map.
    • setKeys

      public final void setKeys(Map<String,String> keys)
      Description copied from class: AbstractPrimitive
      Sets the keys of this primitives to the key/value pairs in keys. Old key/value pairs are removed. If keys is null, clears existing key/value pairs.

      Note that this method, like all methods that modify keys, is not synchronized and may lead to data corruption when being used from multiple threads.

      Specified by:
      setKeys in interface Tagged
      Overrides:
      setKeys in class AbstractPrimitive
      Parameters:
      keys - the key/value pairs to set. If null, removes all existing key/value pairs.
    • put

      public final void put(String key, String value)
      Description copied from class: AbstractPrimitive
      Set the given value to the given key. If key is null, does nothing. If value is null, removes the key and behaves like AbstractPrimitive.remove(String).

      Note that this method, like all methods that modify keys, is not synchronized and may lead to data corruption when being used from multiple threads.

      Specified by:
      put in interface Tagged
      Overrides:
      put in class AbstractPrimitive
      Parameters:
      key - The key, for which the value is to be set. Can be null or empty, does nothing in this case.
      value - The value for the key. If null, removes the respective key/value pair.
      See Also:
    • remove

      public final void remove(String key)
      Description copied from class: AbstractPrimitive
      Remove the given key from the list

      Note that this method, like all methods that modify keys, is not synchronized and may lead to data corruption when being used from multiple threads.

      Specified by:
      remove in interface Tagged
      Overrides:
      remove in class AbstractPrimitive
      Parameters:
      key - the key to be removed. Ignored, if key is null.
    • removeAll

      public final void removeAll()
      Description copied from class: AbstractPrimitive
      Removes all keys from this primitive.

      Note that this method, like all methods that modify keys, is not synchronized and may lead to data corruption when being used from multiple threads.

      Specified by:
      removeAll in interface Tagged
      Overrides:
      removeAll in class AbstractPrimitive
    • keysChangedImpl

      protected void keysChangedImpl(Map<String,String> originalKeys)
      Description copied from class: AbstractPrimitive
      What to do, when the tags have changed by one of the tag-changing methods.
      Specified by:
      keysChangedImpl in class AbstractPrimitive
      Parameters:
      originalKeys - original tags
    • addReferrer

      protected void addReferrer(OsmPrimitive referrer)
      Add new referrer. If referrer is already included then no action is taken
      Parameters:
      referrer - The referrer to add
    • removeReferrer

      protected void removeReferrer(OsmPrimitive referrer)
      Remove referrer. No action is taken if referrer is not registered
      Parameters:
      referrer - The referrer to remove
    • referrers

      private <T extends OsmPrimitive> Stream<T> referrers(boolean allowWithoutDataset, Class<T> filter)
    • referrers

      public final <T extends OsmPrimitive> Stream<T> referrers(Class<T> filter)
      Gets all primitives in the current dataset that reference this primitive.
      Type Parameters:
      T - type of primitives
      Parameters:
      filter - restrict primitives to subclasses
      Returns:
      the referrers as Stream
      Since:
      14654
    • getReferrers

      public final List<OsmPrimitive> getReferrers(boolean allowWithoutDataset)
      Description copied from interface: IPrimitive
      Find primitives that reference this primitive. Returns only primitives that are included in the same dataset as this primitive.
      For example following code will add wnew as referer to all nodes of existingWay, but this method will not return wnew because it's not part of the dataset
      Way wnew = new Way(existingWay)
      Specified by:
      getReferrers in interface IPrimitive
      Parameters:
      allowWithoutDataset - If true, method will return empty list if primitive is not part of the dataset. If false, exception will be thrown in this case
      Returns:
      a collection of all primitives that reference this primitive.
    • getReferrers

      public final List<OsmPrimitive> getReferrers()
      Description copied from interface: IPrimitive
      Gets a list of all primitives in the current dataset that reference this primitive.
      Specified by:
      getReferrers in interface IPrimitive
      Returns:
      The referrers
    • visitReferrers

      public void visitReferrers(OsmPrimitiveVisitor visitor)

      Visits visitor for all referrers.

      Parameters:
      visitor - the visitor. Ignored, if null.
      Since:
      12809
    • visitReferrers

      public void visitReferrers(PrimitiveVisitor visitor)
      Description copied from interface: IPrimitive

      Visits visitor for all referrers.

      Specified by:
      visitReferrers in interface IPrimitive
      Parameters:
      visitor - the visitor. Ignored, if null.
    • doVisitReferrers

      private void doVisitReferrers(Consumer<OsmPrimitive> visitor)
    • isNodeReferredByWays

      protected final boolean isNodeReferredByWays(int n)
      Return true, if this primitive is a node referred by at least n ways
      Parameters:
      n - Minimal number of ways to return true. Must be positive
      Returns:
      true if this primitive is referred by at least n ways
    • accept

      public abstract void accept(OsmPrimitiveVisitor visitor)
      Implementation of the visitor scheme. Subclasses have to call the correct visitor function.
      Parameters:
      visitor - The visitor from which the visit() function must be called.
      Since:
      12809
    • cloneFrom

      public final void cloneFrom(OsmPrimitive other)
      Get and write all attributes from the parameter. Does not fire any listener, so use this only in the data initializing phase
      Parameters:
      other - other primitive
    • cloneFrom

      protected void cloneFrom(OsmPrimitive other, boolean copyChildren)
      Get and write all attributes from the parameter. Does not fire any listener, so use this only in the data initializing phase
      Parameters:
      other - other primitive
      copyChildren - whether to copy child primitives too
      Since:
      16212
    • mergeFrom

      public void mergeFrom(OsmPrimitive other)
      Merges the technical and semantic attributes from other onto this.

      Both this and other must be new, or both must be assigned an OSM ID. If both this and other have an assigned OSM id, the IDs have to be the same.

      Parameters:
      other - the other primitive. Must not be null.
      Throws:
      IllegalArgumentException - if other is null.
      DataIntegrityProblemException - if either this is new and other is not, or other is new and this is not
      DataIntegrityProblemException - if other isn't new and other.getId() != this.getId()
    • hasEqualSemanticAttributes

      public final boolean hasEqualSemanticAttributes(OsmPrimitive other)
      Replies true if this primitive and other are equal with respect to their semantic attributes.
      1. equal id
      2. both are complete or both are incomplete
      3. both have the same tags
      Parameters:
      other - other primitive to compare
      Returns:
      true if this primitive and other are equal with respect to their semantic attributes.
    • hasEqualSemanticFlags

    • hasEqualSemanticAttributes

      boolean hasEqualSemanticAttributes(OsmPrimitive other, boolean testInterestingTagsOnly)
    • hasEqualTechnicalAttributes

      public boolean hasEqualTechnicalAttributes(OsmPrimitive other)
      Replies true if this primitive and other are equal with respect to their technical attributes. The attributes:
      1. deleted
      2. modified
      3. timestamp
      4. version
      5. visible
      6. user
      have to be equal
      Parameters:
      other - the other primitive
      Returns:
      true if this primitive and other are equal with respect to their technical attributes
    • load

      public void load(PrimitiveData data)
      Loads (clone) this primitive from provided PrimitiveData
      Parameters:
      data - The object which should be cloned
    • save

      public abstract PrimitiveData save()
      Save parameters of this primitive to the transport object
      Returns:
      The saved object data
    • saveCommonAttributes

      protected void saveCommonAttributes(PrimitiveData data)
      Save common parameters of primitives to the transport object
      Parameters:
      data - The object to save the data into
    • updatePosition

      public abstract void updatePosition()
      Called by Dataset to update cached position information of primitive (bbox, cached EarthNorth, ...)
    • getFlagsAsString

      protected String getFlagsAsString()
      Overrides:
      getFlagsAsString in class AbstractPrimitive
    • equals

      public boolean equals(Object obj)
      Equal if the id (and class) are equal.

      A primitive is equal to its incomplete counterpart.

      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Return the id plus the class type encoded as hashcode or super's hashcode if id is 0.

      A primitive has the same hashcode as its incomplete counterpart.

      Overrides:
      hashCode in class Object
    • getTemplateKeys

      Description copied from interface: TemplateEngineDataProvider
      Get the collection of all keys that can be mapped to values.
      Specified by:
      getTemplateKeys in interface TemplateEngineDataProvider
      Returns:
      all keys that can be mapped to values
    • getTemplateValue

      public Object getTemplateValue(String name, boolean special)
      Description copied from interface: TemplateEngineDataProvider
      Map a key to a value given the properties of the object.
      Specified by:
      getTemplateValue in interface TemplateEngineDataProvider
      Parameters:
      name - the key to map
      special - if the key is a "special:*" keyword that is used to get certain information or automated behavior
      Returns:
      a value that the key is mapped to or "special" information in case special is true
    • evaluateCondition

      public boolean evaluateCondition(SearchCompiler.Match condition)
      Description copied from interface: TemplateEngineDataProvider
      Check if a condition holds for the object represented by this TemplateEngineDataProvider.
      Specified by:
      evaluateCondition in interface TemplateEngineDataProvider
      Parameters:
      condition - the condition to check (which is a search expression)
      Returns:
      true if the condition holds
    • getParentRelations

      public static Set<Relation> getParentRelations(Collection<? extends OsmPrimitive> primitives)
      Replies the set of referring relations
      Parameters:
      primitives - primitives to fetch relations from
      Returns:
      the set of referring relations
    • hasAreaTags

      public final boolean hasAreaTags()
      Determines if this primitive has tags denoting an area.
      Returns:
      true if this primitive has tags denoting an area, false otherwise.
      Since:
      6491
    • concernsArea

      public abstract boolean concernsArea()
      Determines if this primitive semantically concerns an area.
      Returns:
      true if this primitive semantically concerns an area, according to its type, geometry and tags, false otherwise.
      Since:
      6491
    • isOutsideDownloadArea

      public abstract boolean isOutsideDownloadArea()
      Tests if this primitive lies outside of the downloaded area of its DataSet.
      Returns:
      true if this primitive lies outside of the downloaded area
    • addToBBox

      protected abstract void addToBBox(BBox box, Set<PrimitiveId> visited)
      If necessary, extend the bbox to contain this primitive
      Parameters:
      box - a bbox instance
      visited - a set of visited members or null
      Since:
      11269