Package org.openstreetmap.josm.data.osm
Class TagCollection
- java.lang.Object
-
- org.openstreetmap.josm.data.osm.TagCollection
-
- All Implemented Interfaces:
Serializable
,Iterable<Tag>
public class TagCollection extends Object implements Iterable<Tag>, Serializable
TagCollection is a collection of tags which can be used to manipulate tags managed byOsmPrimitive
s. A TagCollection can be created:- from the tags managed by a specific
OsmPrimitive
withfrom(org.openstreetmap.josm.data.osm.Tagged)
- from the union of all tags managed by a collection of
OsmPrimitive
s withunionOfAllPrimitives(java.util.Collection)
- from the union of all tags managed by a
DataSet
withunionOfAllPrimitives(org.openstreetmap.josm.data.osm.DataSet)
- from the intersection of all tags managed by a collection of primitives
with
commonToAllPrimitives(java.util.Collection)
size()
,hasTagsFor(String)
, etc. Basic set operations allow to create the union, the intersection and the difference of tag collections, seeunion(org.openstreetmap.josm.data.osm.TagCollection)
,intersect(org.openstreetmap.josm.data.osm.TagCollection)
, andminus(org.openstreetmap.josm.data.osm.TagCollection)
.- Since:
- 2008
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private static long
serialVersionUID
private static Pattern
SPLIT_VALUES_PATTERN
private Map<Tag,Integer>
tags
-
Constructor Summary
Constructors Constructor Description TagCollection()
Creates an empty tag collection.TagCollection(Collection<Tag> tags)
Creates a tag collection fromtags
.TagCollection(TagCollection other)
Creates a clone of the tag collectionother
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(Collection<Tag> tags)
Adds a collection of tags to the tag collection.void
add(Tag tag)
Adds a tag to the tag collection.void
add(TagCollection tags)
Adds the tags of another tag collection to this collection.void
applyTo(Collection<? extends Tagged> primitives)
Applies this tag collection to a collection ofOsmPrimitive
s.void
applyTo(Tagged primitive)
Applies this tag collection to anOsmPrimitive
.List<Tag>
asList()
Replies the tags of this tag collection as list.Set<Tag>
asSet()
Replies the tags of this tag collection as setstatic TagCollection
commonToAllPrimitives(Collection<? extends Tagged> primitives)
Replies a tag collection with the tags which are common to all primitives in inprimitives
.boolean
contains(Tag tag)
Replies true if the this tag collection containstag
.boolean
containsAll(Collection<Tag> tags)
Replies true if this tag collection contains all tags intags
.boolean
containsAllKeys(Collection<String> keys)
Replies true if this tag collection at least one tag for every key inkeys
.TagCollection
emptyTagsForKeysMissingIn(TagCollection other)
private void
ensureApplicableToPrimitive()
static TagCollection
from(Map<String,String> tags)
Creates a tag collection from a map of key/value-pairs.static TagCollection
from(Tagged primitive)
Creates a tag collection from the tags managed by a specificOsmPrimitive
.private Stream<String>
generateKeyStream()
private Stream<Tag>
generateStreamForKey(String key)
Get a stream for the given key.String
getJoinedValues(String key)
Replies the concatenation of all tag values (concatenated by a semicolon)Set<String>
getKeys()
Replies the set of keys of this tag collection.Set<String>
getKeysWithMultipleValues()
Replies the set of keys which have at least 2 matching tags.int
getNumTagsFor(String key)
Replies the number of tags with keykey
String
getSummedValues(String key)
Replies the sum of all numeric tag values.int
getTagOccurrence(Tag tag)
Gets the number of times this tag was added to the collection.TagCollection
getTagsFor(String key)
Replies a tag collection with the tags for a given key.TagCollection
getTagsFor(Collection<String> keys)
Replies a tag collection with all tags whose key is equal to one of the keys inkeys
.Set<String>
getValues()
Replies the set of values in this tag collectionSet<String>
getValues(String key)
Replies the set of values for a given key.boolean
hasEmptyValue(String key)
Replies true if there is a tag with an empty value forkey
.boolean
hasTagsFor(String key)
Replies true if there is at least one tag for the given key.boolean
hasUniqueEmptyValue(String key)
Replies true if there is exactly one tag forkey
and if the value for this tag is empty.boolean
hasUniqueNonEmptyValue(String key)
Replies true if there is exactly one tag forkey
and if the value of this tag is not empty.boolean
hasValuesFor(String key)
Replies true it there is at least one tag with a non empty value for key.TagCollection
intersect(TagCollection other)
Builds the intersection of this tag collection and another tag collectionboolean
isApplicableToPrimitive()
Replies true if for every key there is one tag only, i.e.boolean
isEmpty()
Replies true if this tag collection is emptyIterator<Tag>
iterator()
Replies an iterator to iterate over the tags in this collectionTagCollection
minus(TagCollection other)
Replies the difference of this tag collection and another tag collectionvoid
remove(Collection<Tag> tags)
Removes a collection of tags from the tag collection.void
remove(Tag tag)
Removes a specific tag from the tag collection.void
remove(TagCollection tags)
Removes all tags in the tag collectiontags
from the current tag collection.void
removeByKey(String key)
Removes all tags whose keys are equal tokey
.void
removeByKey(Collection<String> keys)
Removes all tags whose key is in the collectionkeys
.void
replaceTagsOf(Collection<? extends Tagged> primitives)
Replaces the tags of a collection ofOsmPrimitive
s by the tags in this collection.void
replaceTagsOf(Tagged primitive)
Replaces the tags of anOsmPrimitive
by the tags in this collection .void
setUniqueForKey(String key, String value)
Sets a unique tag for the key of this tag.void
setUniqueForKey(Tag tag)
Sets a unique tag for the key of this tag.int
size()
Replies the number of tags in this tag collectionString
toString()
TagCollection
union(TagCollection other)
Replies the union of this tag collection and another tag collectionstatic TagCollection
unionOfAllPrimitives(Collection<? extends Tagged> primitives)
Creates a tag collection from the union of the tags managed by a collection of primitives.static TagCollection
unionOfAllPrimitives(DataSet ds)
Replies a tag collection with the union of the tags which are common to all primitives in the datasetds
.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
SPLIT_VALUES_PATTERN
private static final Pattern SPLIT_VALUES_PATTERN
-
-
Constructor Detail
-
TagCollection
public TagCollection()
Creates an empty tag collection.
-
TagCollection
public TagCollection(TagCollection other)
Creates a clone of the tag collectionother
. Creates an empty tag collection ifother
is null.- Parameters:
other
- the other collection
-
TagCollection
public TagCollection(Collection<Tag> tags)
Creates a tag collection fromtags
.- Parameters:
tags
- the collection of tags- Since:
- 5724
-
-
Method Detail
-
from
public static TagCollection from(Tagged primitive)
Creates a tag collection from the tags managed by a specificOsmPrimitive
. Ifprimitive
is null, replies an empty tag collection.- Parameters:
primitive
- the primitive- Returns:
- a tag collection with the tags managed by a specific
OsmPrimitive
-
from
public static TagCollection from(Map<String,String> tags)
Creates a tag collection from a map of key/value-pairs. Replies an empty tag collection iftags
is null.- Parameters:
tags
- the key/value-pairs- Returns:
- the tag collection
-
unionOfAllPrimitives
public static TagCollection unionOfAllPrimitives(Collection<? extends Tagged> primitives)
Creates a tag collection from the union of the tags managed by a collection of primitives. Replies an empty tag collection, ifprimitives
is null.- Parameters:
primitives
- the primitives- Returns:
- a tag collection with the union of the tags managed by a collection of primitives
-
commonToAllPrimitives
public static TagCollection commonToAllPrimitives(Collection<? extends Tagged> primitives)
Replies a tag collection with the tags which are common to all primitives in inprimitives
. Replies an empty tag collection ofprimitives
is null.- Parameters:
primitives
- the primitives- Returns:
- a tag collection with the tags which are common to all primitives
-
unionOfAllPrimitives
public static TagCollection unionOfAllPrimitives(DataSet ds)
Replies a tag collection with the union of the tags which are common to all primitives in the datasetds
. Returns an empty tag collection ofds
is null.- Parameters:
ds
- the dataset- Returns:
- a tag collection with the union of the tags which are common to all primitives in
the dataset
ds
-
size
public int size()
Replies the number of tags in this tag collection- Returns:
- the number of tags in this tag collection
-
isEmpty
public boolean isEmpty()
Replies true if this tag collection is empty- Returns:
- true if this tag collection is empty; false, otherwise
-
add
public final void add(Tag tag)
Adds a tag to the tag collection. Iftag
is null, nothing is added.- Parameters:
tag
- the tag to add
-
getTagOccurrence
public int getTagOccurrence(Tag tag)
Gets the number of times this tag was added to the collection.- Parameters:
tag
- The tag- Returns:
- The number of times this tag is used in this collection.
- Since:
- 14302
-
add
public final void add(Collection<Tag> tags)
Adds a collection of tags to the tag collection. Iftags
is null, nothing is added. null values in the collection are ignored.- Parameters:
tags
- the collection of tags
-
add
public final void add(TagCollection tags)
Adds the tags of another tag collection to this collection. Adds nothing, iftags
is null.- Parameters:
tags
- the other tag collection
-
remove
public void remove(Tag tag)
Removes a specific tag from the tag collection. Does nothing iftag
is null.- Parameters:
tag
- the tag to be removed
-
remove
public void remove(Collection<Tag> tags)
Removes a collection of tags from the tag collection. Does nothing iftags
is null.- Parameters:
tags
- the tags to be removed
-
remove
public void remove(TagCollection tags)
Removes all tags in the tag collectiontags
from the current tag collection. Does nothing iftags
is null.- Parameters:
tags
- the tag collection to be removed.
-
removeByKey
public void removeByKey(String key)
Removes all tags whose keys are equal tokey
. Does nothing ifkey
is null.- Parameters:
key
- the key to be removed
-
removeByKey
public void removeByKey(Collection<String> keys)
Removes all tags whose key is in the collectionkeys
. Does nothing ifkeys
is null.- Parameters:
keys
- the collection of keys to be removed
-
contains
public boolean contains(Tag tag)
Replies true if the this tag collection containstag
.- Parameters:
tag
- the tag to look up- Returns:
- true if the this tag collection contains
tag
; false, otherwise
-
containsAll
public boolean containsAll(Collection<Tag> tags)
Replies true if this tag collection contains all tags intags
. Replies false, if tags is null.- Parameters:
tags
- the tags to look up- Returns:
- true if this tag collection contains all tags in
tags
. Replies false, if tags is null.
-
containsAllKeys
public boolean containsAllKeys(Collection<String> keys)
Replies true if this tag collection at least one tag for every key inkeys
. Replies false, ifkeys
is null. null values inkeys
are ignored.- Parameters:
keys
- the keys to lookup- Returns:
- true if this tag collection at least one tag for every key in
keys
.
-
getNumTagsFor
public int getNumTagsFor(String key)
Replies the number of tags with keykey
- Parameters:
key
- the key to look up- Returns:
- the number of tags with key
key
, including the empty "" value. 0, if key is null.
-
hasTagsFor
public boolean hasTagsFor(String key)
Replies true if there is at least one tag for the given key.- Parameters:
key
- the key to look up- Returns:
- true if there is at least one tag for the given key. false, if key is null.
-
hasValuesFor
public boolean hasValuesFor(String key)
Replies true it there is at least one tag with a non empty value for key. Replies false if key is null.- Parameters:
key
- the key- Returns:
- true it there is at least one tag with a non empty value for key.
-
hasUniqueNonEmptyValue
public boolean hasUniqueNonEmptyValue(String key)
Replies true if there is exactly one tag forkey
and if the value of this tag is not empty. Replies false if key is null.- Parameters:
key
- the key- Returns:
- true if there is exactly one tag for
key
and if the value of this tag is not empty
-
hasEmptyValue
public boolean hasEmptyValue(String key)
Replies true if there is a tag with an empty value forkey
. Replies false, if key is null.- Parameters:
key
- the key- Returns:
- true if there is a tag with an empty value for
key
-
hasUniqueEmptyValue
public boolean hasUniqueEmptyValue(String key)
Replies true if there is exactly one tag forkey
and if the value for this tag is empty. Replies false if key is null.- Parameters:
key
- the key- Returns:
- true if there is exactly one tag for
key
and if the value for this tag is empty
-
getTagsFor
public TagCollection getTagsFor(String key)
Replies a tag collection with the tags for a given key. Replies an empty collection if key is null.- Parameters:
key
- the key to look up- Returns:
- a tag collection with the tags for a given key. Replies an empty collection if key is null.
-
getTagsFor
public TagCollection getTagsFor(Collection<String> keys)
Replies a tag collection with all tags whose key is equal to one of the keys inkeys
. Replies an empty collection if keys is null.- Parameters:
keys
- the keys to look up- Returns:
- a tag collection with all tags whose key is equal to one of the keys in
keys
-
asSet
public Set<Tag> asSet()
Replies the tags of this tag collection as set- Returns:
- the tags of this tag collection as set
-
asList
public List<Tag> asList()
Replies the tags of this tag collection as list. Note that the order of the list is not preserved between method invocations.- Returns:
- the tags of this tag collection as list. There are no duplicate values.
-
iterator
public Iterator<Tag> iterator()
Replies an iterator to iterate over the tags in this collection
-
getKeys
public Set<String> getKeys()
Replies the set of keys of this tag collection.- Returns:
- the set of keys of this tag collection
-
getKeysWithMultipleValues
public Set<String> getKeysWithMultipleValues()
Replies the set of keys which have at least 2 matching tags.- Returns:
- the set of keys which have at least 2 matching tags.
-
setUniqueForKey
public void setUniqueForKey(Tag tag)
Sets a unique tag for the key of this tag. All other tags with the same key are removed from the collection. Does nothing if tag is null.- Parameters:
tag
- the tag to set
-
setUniqueForKey
public void setUniqueForKey(String key, String value)
Sets a unique tag for the key of this tag. All other tags with the same key are removed from the collection. Assume the empty string for key and value if either key or value is null.- Parameters:
key
- the keyvalue
- the value
-
getValues
public Set<String> getValues()
Replies the set of values in this tag collection- Returns:
- the set of values
-
getValues
public Set<String> getValues(String key)
Replies the set of values for a given key. Replies an empty collection if there are no values for the given key.- Parameters:
key
- the key to look up- Returns:
- the set of values for a given key. Replies an empty collection if there are no values for the given key
-
isApplicableToPrimitive
public boolean isApplicableToPrimitive()
Replies true if for every key there is one tag only, i.e. exactly one value.- Returns:
true
if for every key there is one tag only
-
applyTo
public void applyTo(Tagged primitive)
Applies this tag collection to anOsmPrimitive
. Does nothing if primitive is null- Parameters:
primitive
- the primitive- Throws:
IllegalStateException
- if this tag collection can't be applied because there are keys with multiple values
-
applyTo
public void applyTo(Collection<? extends Tagged> primitives)
Applies this tag collection to a collection ofOsmPrimitive
s. Does nothing if primitives is null- Parameters:
primitives
- the collection of primitives- Throws:
IllegalStateException
- if this tag collection can't be applied because there are keys with multiple values
-
replaceTagsOf
public void replaceTagsOf(Tagged primitive)
Replaces the tags of anOsmPrimitive
by the tags in this collection . Does nothing if primitive is null- Parameters:
primitive
- the primitive- Throws:
IllegalStateException
- if this tag collection can't be applied because there are keys with multiple values
-
replaceTagsOf
public void replaceTagsOf(Collection<? extends Tagged> primitives)
Replaces the tags of a collection ofOsmPrimitive
s by the tags in this collection. Does nothing if primitives is null- Parameters:
primitives
- the collection of primitives- Throws:
IllegalStateException
- if this tag collection can't be applied because there are keys with multiple values
-
ensureApplicableToPrimitive
private void ensureApplicableToPrimitive()
-
intersect
public TagCollection intersect(TagCollection other)
Builds the intersection of this tag collection and another tag collection- Parameters:
other
- the other tag collection. If null, replies an empty tag collection.- Returns:
- the intersection of this tag collection and another tag collection. All counts are set to 1.
-
minus
public TagCollection minus(TagCollection other)
Replies the difference of this tag collection and another tag collection- Parameters:
other
- the other tag collection. May be null.- Returns:
- the difference of this tag collection and another tag collection
-
union
public TagCollection union(TagCollection other)
Replies the union of this tag collection and another tag collection- Parameters:
other
- the other tag collection. May be null.- Returns:
- the union of this tag collection and another tag collection. The tag count is summed.
-
emptyTagsForKeysMissingIn
public TagCollection emptyTagsForKeysMissingIn(TagCollection other)
-
getJoinedValues
public String getJoinedValues(String key)
Replies the concatenation of all tag values (concatenated by a semicolon)- Parameters:
key
- the key to look up- Returns:
- the concatenation of all tag values
-
getSummedValues
public String getSummedValues(String key)
Replies the sum of all numeric tag values. Ignores duplicates.- Parameters:
key
- the key to look up- Returns:
- the sum of all numeric tag values, as string.
- Since:
- 7743
-
generateKeyStream
private Stream<String> generateKeyStream()
-
generateStreamForKey
private Stream<Tag> generateStreamForKey(String key)
Get a stream for the given key.- Parameters:
key
- The key- Returns:
- The stream. An empty stream if key is
null
-
-