org.openstreetmap.josm.data.osm
Class AbstractPrimitive

java.lang.Object
  extended by org.openstreetmap.josm.data.osm.AbstractPrimitive
All Implemented Interfaces:
IPrimitive, PrimitiveId, Tagged
Direct Known Subclasses:
OsmPrimitive, PrimitiveData

public abstract class AbstractPrimitive
extends java.lang.Object
implements IPrimitive

Abstract class to represent common features of the datatypes primitives.

Since:
4099

Field Summary
protected  int changesetId
          The id of the changeset this primitive was last uploaded to.
protected static int FLAG_DELETED
          An object that was deleted by the user.
protected static int FLAG_INCOMPLETE
          A primitive is incomplete if we know its id and type, but nothing more.
protected static int FLAG_MODIFIED
          This flag shows, that the properties have been changed by the user and on upload the object will be send to the server.
protected static int FLAG_VISIBLE
          This flag is false, if the object is marked as deleted on the server.
protected  short flags
          Put several boolean flags to one short int field to save memory.
protected  long id
          Unique identifier in OSM.
private static java.util.concurrent.atomic.AtomicLong idCounter
           
protected  java.lang.String[] keys
          The key/value list for this primitive.
protected  int timestamp
           
protected  User user
          User that last modified this primitive, as specified by the server.
protected  int version
          Contains the version number as returned by the API.
 
Constructor Summary
AbstractPrimitive()
           
 
Method Summary
 void clearOsmId()
          Clears the id and version known to the OSM API.
 void cloneFrom(AbstractPrimitive other)
          Get and write all attributes from the parameter.
(package private) static long generateUniqueId()
           
 java.lang.String get(java.lang.String key)
          Replies the value for key key.
 int getChangesetId()
          Replies the id of the changeset this primitive was last uploaded to.
 OsmPrimitiveType getDisplayType()
           
protected  java.lang.String getFlagsAsString()
           
 long getId()
          Replies the id of this primitive.
 java.lang.String getIgnoreCase(java.lang.String key)
           
 java.util.Map<java.lang.String,java.lang.String> getKeys()
          Replies the map of key/value pairs.
 java.lang.String getLocalName()
          Replies the a localized name for this primitive given by the value of the tags (in this order) name:lang_COUNTRY_Variant of the current locale name:lang_COUNTRY of the current locale name:lang of the current locale name of the current locale null, if no such tag exists
 java.lang.String getName()
          Replies the name of this primitive.
 PrimitiveId getPrimitiveId()
          Replies the unique primitive id for this primitive
 java.util.Date getTimestamp()
          Time of last modification to this object.
 long getUniqueId()
          Gets a unique id representing this object.
 User getUser()
          Replies the user who has last touched this object.
 int getVersion()
          Replies the version number as returned by the API.
 boolean hasKey(java.lang.String key)
          Replies true if this primitive has a tag with key key.
 boolean hasKeys()
          Replies true, if the map of key/value pairs of this primitive is not empty.
 boolean hasSameTags(OsmPrimitive other)
          Replies true if other isn't null and has the same tags (key/value-pairs) as this.
 boolean hasTag(java.lang.String key, java.util.Collection<java.lang.String> values)
          Tests whether this primitive contains a tag consisting of key and any of values.
 boolean hasTag(java.lang.String key, java.lang.String... values)
          Tests whether this primitive contains a tag consisting of key and any of values.
 boolean isDeleted()
          Replies true, if the object has been deleted.
 boolean isIncomplete()
           
 boolean isModified()
          Replies true if the object has been modified since it was loaded from the server.
 boolean isNew()
          Replies true if this id represents a new primitive.
 boolean isNewOrUndeleted()
           
 boolean isTimestampEmpty()
           
 boolean isUndeleted()
          Replies true if the object has been deleted on the server and was undeleted by the user.
 boolean isUsable()
          Replies true, if the object is usable (i.e.
 boolean isVisible()
          Checks if object is known to the server.
protected abstract  void keysChangedImpl(java.util.Map<java.lang.String,java.lang.String> originalKeys)
          What to do, when the tags have changed by one of the tag-changing methods.
 java.util.Collection<java.lang.String> keySet()
          Replies the set of keys
 void put(java.lang.String key, java.lang.String value)
          Set the given value to the given key.
 void remove(java.lang.String key)
          Remove the given key from the list
 void removeAll()
          Removes all keys from this primitive.
 void setChangesetId(int changesetId)
          Sets the changeset id of this primitive.
 void setDeleted(boolean deleted)
          Sets whether this primitive is deleted or not.
protected  void setIncomplete(boolean incomplete)
          If set to true, this object is incomplete, which means only the id and type is known (type is the objects instance class)
 void setKeys(java.util.Map<java.lang.String,java.lang.String> keys)
          Sets the keys of this primitives to the key/value pairs in keys.
 void setModified(boolean modified)
          Marks this primitive as being modified.
 void setOsmId(long id, int version)
          Sets the id and the version of this primitive if it is known to the OSM API.
 void setTimestamp(java.util.Date timestamp)
           
 void setUser(User user)
          Sets the user who has last touched this object.
 void setVisible(boolean visible)
          Sets whether this primitive is visible, i.e.
protected  void updateFlags(int flag, boolean value)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.openstreetmap.josm.data.osm.IPrimitive
visit
 
Methods inherited from interface org.openstreetmap.josm.data.osm.PrimitiveId
getType
 

Field Detail

idCounter

private static final java.util.concurrent.atomic.AtomicLong idCounter

FLAG_MODIFIED

protected static final int FLAG_MODIFIED
This flag shows, that the properties have been changed by the user and on upload the object will be send to the server.

See Also:
Constant Field Values

FLAG_VISIBLE

protected static final int FLAG_VISIBLE
This flag is false, if the object is marked as deleted on the server.

See Also:
Constant Field Values

FLAG_DELETED

protected static final int FLAG_DELETED
An object that was deleted by the user. Deleted objects are usually hidden on the map and a request for deletion will be send to the server on upload. An object usually cannot be deleted if it has non-deleted objects still referring to it.

See Also:
Constant Field Values

FLAG_INCOMPLETE

protected static final int FLAG_INCOMPLETE
A primitive is incomplete if we know its id and type, but nothing more. Typically some members of a relation are incomplete until they are fetched from the server.

See Also:
Constant Field Values

flags

protected volatile short flags
Put several boolean flags to one short int field to save memory. Other bits of this field are used in subclasses.


id

protected long id
Unique identifier in OSM. This is used to identify objects on the server. An id of 0 means an unknown id. The object has not been uploaded yet to know what id it will get.


user

protected User user
User that last modified this primitive, as specified by the server. Never changed by JOSM.


version

protected int version
Contains the version number as returned by the API. Needed to ensure update consistency


changesetId

protected int changesetId
The id of the changeset this primitive was last uploaded to. 0 if it wasn't uploaded to a changeset yet of if the changeset id isn't known.


timestamp

protected int timestamp

keys

protected java.lang.String[] keys
The key/value list for this primitive.

Constructor Detail

AbstractPrimitive

public AbstractPrimitive()
Method Detail

generateUniqueId

static long generateUniqueId()

cloneFrom

public void cloneFrom(AbstractPrimitive 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 - the primitive to clone data from

getVersion

public int getVersion()
Replies the version number as returned by the API. The version is 0 if the id is 0 or if this primitive is incomplete.

Specified by:
getVersion in interface IPrimitive
See Also:
PrimitiveData.setVersion(int)

getId

public long getId()
Replies the id of this primitive.

Specified by:
getId in interface IPrimitive
Returns:
the id of this primitive.

getUniqueId

public long getUniqueId()
Gets a unique id representing this object.

Specified by:
getUniqueId in interface PrimitiveId
Returns:
Osm id if primitive already exists on the server. Unique negative value if primitive is new

isNew

public boolean isNew()
Description copied from interface: PrimitiveId
Replies true if this id represents a new primitive.

Specified by:
isNew in interface PrimitiveId
Returns:
True if primitive is new (not yet uploaded the server, id <= 0)

isNewOrUndeleted

public boolean isNewOrUndeleted()
Specified by:
isNewOrUndeleted in interface IPrimitive
Returns:
True if primitive is new or undeleted
See Also:
isNew(), isUndeleted()

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
Parameters:
id - the id. > 0 required
version - the version > 0 required
Throws:
java.lang.IllegalArgumentException - thrown if id <= 0
java.lang.IllegalArgumentException - thrown if version <= 0
DataIntegrityProblemException - If id is changed and primitive was already added to the dataset

clearOsmId

public void clearOsmId()
Clears the id and version known to the OSM API. The id and the version is set to 0. incomplete is set to false. It's preferred to use copy constructor with clearId set to true instead of calling this method.


getUser

public User getUser()
Replies the user who has last touched this object. May be null.

Specified by:
getUser in interface IPrimitive
Returns:
the user who has last touched this object. May be null.

setUser

public void setUser(User user)
Sets the user who has last touched this object.

Specified by:
setUser in interface IPrimitive
Parameters:
user - the user

getChangesetId

public int getChangesetId()
Replies the id of the changeset this primitive was last uploaded to. 0 if this primitive wasn't uploaded to a changeset yet or if the changeset isn't known.

Specified by:
getChangesetId in interface IPrimitive
Returns:
the id of the changeset this primitive was last uploaded to.

setChangesetId

public void setChangesetId(int changesetId)
                    throws java.lang.IllegalStateException,
                           java.lang.IllegalArgumentException
Sets the changeset id of this primitive. Can't be set on a new primitive.

Specified by:
setChangesetId in interface IPrimitive
Parameters:
changesetId - the id. >= 0 required.
Throws:
java.lang.IllegalStateException - thrown if this primitive is new.
java.lang.IllegalArgumentException - thrown if id < 0

getPrimitiveId

public PrimitiveId getPrimitiveId()
Replies the unique primitive id for this primitive

Specified by:
getPrimitiveId in interface IPrimitive
Returns:
the unique primitive id for this primitive

getDisplayType

public OsmPrimitiveType getDisplayType()

setTimestamp

public void setTimestamp(java.util.Date timestamp)
Specified by:
setTimestamp in interface IPrimitive

getTimestamp

public java.util.Date getTimestamp()
Time of last modification to this object. This is not set by JOSM but read from the server and delivered back to the server unmodified. It is used to check against edit conflicts.

Specified by:
getTimestamp in interface IPrimitive
Returns:
date of last modification

isTimestampEmpty

public boolean isTimestampEmpty()
Specified by:
isTimestampEmpty in interface IPrimitive

updateFlags

protected void updateFlags(int flag,
                           boolean value)

setModified

public void setModified(boolean modified)
Marks this primitive as being modified.

Specified by:
setModified in interface IPrimitive
Parameters:
modified - true, if this primitive is to be modified

isModified

public boolean isModified()
Replies true if the object has been modified since it was loaded from the server. In this case, on next upload, this object will be updated. Deleted objects are deleted from the server. If the objects are added (id=0), the modified is ignored and the object is added to the server.

Specified by:
isModified in interface IPrimitive
Returns:
true if the object has been modified since it was loaded from the server

isDeleted

public boolean isDeleted()
Replies true, if the object has been deleted.

Specified by:
isDeleted in interface IPrimitive
Returns:
true, if the object has been deleted.
See Also:
setDeleted(boolean)

isUndeleted

public boolean isUndeleted()
Replies true if the object has been deleted on the server and was undeleted by the user.

Returns:
true if the object has been undeleted

isUsable

public boolean isUsable()
Replies true, if the object is usable (i.e. complete and not deleted).

Returns:
true, if the object is usable.
See Also:
setDeleted(boolean)

isVisible

public boolean isVisible()
Checks if object is known to the server. Replies true if this primitive is either unknown to the server (i.e. its id is 0) or it is known to the server and it hasn't be deleted on the server. Replies false, if this primitive is known on the server and has been deleted on the server.

Specified by:
isVisible in interface IPrimitive
Returns:
true, if the object is visible on server.
See Also:
setVisible(boolean)

setVisible

public void setVisible(boolean visible)
                throws java.lang.IllegalStateException
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
Throws:
java.lang.IllegalStateException - thrown if visible is set to false on an primitive with id==0
See Also:
isVisible()

setDeleted

public void setDeleted(boolean deleted)
Sets whether this primitive is deleted or not. Also marks this primitive as modified if deleted is true.

Specified by:
setDeleted in interface IPrimitive
Parameters:
deleted - true, if this primitive is deleted; false, otherwise

setIncomplete

protected void setIncomplete(boolean incomplete)
If set to true, this object is incomplete, which means only the id and type is known (type is the objects instance class)


isIncomplete

public boolean isIncomplete()
Specified by:
isIncomplete in interface IPrimitive

getFlagsAsString

protected java.lang.String getFlagsAsString()

getKeys

public java.util.Map<java.lang.String,java.lang.String> getKeys()
Replies the map of key/value pairs. Never replies null. The map can be empty, though.

Specified by:
getKeys in interface Tagged
Returns:
tags of this primitive. Changes made in returned map are not mapped back to the primitive, use setKeys() to modify the keys

setKeys

public void setKeys(java.util.Map<java.lang.String,java.lang.String> keys)
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.

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

put

public void put(java.lang.String key,
                java.lang.String value)
Set the given value to the given key. If key is null, does nothing. If value is null, removes the key and behaves like remove(String).

Specified by:
put in interface Tagged
Parameters:
key - The key, for which the value is to be set. Can be null, does nothing in this case.
value - The value for the key. If null, removes the respective key/value pair.
See Also:
remove(String)

remove

public void remove(java.lang.String key)
Remove the given key from the list

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

removeAll

public void removeAll()
Removes all keys from this primitive.

Specified by:
removeAll in interface Tagged

get

public final java.lang.String get(java.lang.String key)
Replies the value for key key. Replies null, if key is null. Replies null, if there is no value for the given key.

Specified by:
get in interface Tagged
Parameters:
key - the key. Can be null, replies null in this case.
Returns:
the value for key key.

getIgnoreCase

public final java.lang.String getIgnoreCase(java.lang.String key)

keySet

public final java.util.Collection<java.lang.String> keySet()
Description copied from interface: Tagged
Replies the set of keys

Specified by:
keySet in interface Tagged
Returns:
the set of keys

hasKeys

public final boolean hasKeys()
Replies true, if the map of key/value pairs of this primitive is not empty.

Specified by:
hasKeys in interface Tagged
Returns:
true, if the map of key/value pairs of this primitive is not empty; false otherwise

hasKey

public boolean hasKey(java.lang.String key)
Replies true if this primitive has a tag with key key.

Parameters:
key - the key
Returns:
true, if his primitive has a tag with key key

hasSameTags

public boolean hasSameTags(OsmPrimitive other)
Replies true if other isn't null and has the same tags (key/value-pairs) as this.

Parameters:
other - the other object primitive
Returns:
true if other isn't null and has the same tags (key/value-pairs) as this.

keysChangedImpl

protected abstract void keysChangedImpl(java.util.Map<java.lang.String,java.lang.String> originalKeys)
What to do, when the tags have changed by one of the tag-changing methods.


getName

public java.lang.String getName()
Replies the name of this primitive. The default implementation replies the value of the tag name or null, if this tag is not present.

Specified by:
getName in interface IPrimitive
Returns:
the name of this primitive

getLocalName

public java.lang.String getLocalName()
Replies the a localized name for this primitive given by the value of the tags (in this order) null, if no such tag exists

Specified by:
getLocalName in interface IPrimitive
Returns:
the name of this primitive

hasTag

public boolean hasTag(java.lang.String key,
                      java.lang.String... values)
Tests whether this primitive contains a tag consisting of key and any of values.

Parameters:
key - the key forming the tag.
values - one or many values forming the tag.
Returns:
true iff primitive contains a tag consisting of key and any of values.

hasTag

public boolean hasTag(java.lang.String key,
                      java.util.Collection<java.lang.String> values)
Tests whether this primitive contains a tag consisting of key and any of values.

Parameters:
key - the key forming the tag.
values - one or many values forming the tag.
Returns:
true iff primitive contains a tag consisting of key and any of values.


JOSM