Package org.openstreetmap.josm.data.osm
Class TagCollection
- java.lang.Object
-
- org.openstreetmap.josm.data.osm.TagCollection
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Iterable<Tag>
public class TagCollection extends java.lang.Object implements java.lang.Iterable<Tag>, java.io.Serializable
TagCollection is a collection of tags which can be used to manipulate tags managed byOsmPrimitives. A TagCollection can be created:- from the tags managed by a specific
OsmPrimitivewithfrom(org.openstreetmap.josm.data.osm.Tagged) - from the union of all tags managed by a collection of
OsmPrimitives withunionOfAllPrimitives(java.util.Collection) - from the union of all tags managed by a
DataSetwithunionOfAllPrimitives(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 longserialVersionUIDprivate static java.util.regex.PatternSPLIT_VALUES_PATTERNprivate java.util.Map<Tag,java.lang.Integer>tags
-
Constructor Summary
Constructors Constructor Description TagCollection()Creates an empty tag collection.TagCollection(java.util.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 voidadd(java.util.Collection<Tag> tags)Adds a collection of tags to the tag collection.voidadd(Tag tag)Adds a tag to the tag collection.voidadd(TagCollection tags)Adds the tags of another tag collection to this collection.voidapplyTo(java.util.Collection<? extends Tagged> primitives)Applies this tag collection to a collection ofOsmPrimitives.voidapplyTo(Tagged primitive)Applies this tag collection to anOsmPrimitive.java.util.List<Tag>asList()Replies the tags of this tag collection as list.java.util.Set<Tag>asSet()Replies the tags of this tag collection as setstatic TagCollectioncommonToAllPrimitives(java.util.Collection<? extends Tagged> primitives)Replies a tag collection with the tags which are common to all primitives in inprimitives.booleancontains(Tag tag)Replies true if the this tag collection containstag.booleancontainsAll(java.util.Collection<Tag> tags)Replies true if this tag collection contains all tags intags.booleancontainsAllKeys(java.util.Collection<java.lang.String> keys)Replies true if this tag collection at least one tag for every key inkeys.TagCollectionemptyTagsForKeysMissingIn(TagCollection other)private voidensureApplicableToPrimitive()static TagCollectionfrom(java.util.Map<java.lang.String,java.lang.String> tags)Creates a tag collection from a map of key/value-pairs.static TagCollectionfrom(Tagged primitive)Creates a tag collection from the tags managed by a specificOsmPrimitive.private java.util.stream.Stream<java.lang.String>generateKeyStream()private java.util.stream.Stream<Tag>generateStreamForKey(java.lang.String key)Get a stream for the given key.java.lang.StringgetJoinedValues(java.lang.String key)Replies the concatenation of all tag values (concatenated by a semicolon)java.util.Set<java.lang.String>getKeys()Replies the set of keys of this tag collection.java.util.Set<java.lang.String>getKeysWithMultipleValues()Replies the set of keys which have at least 2 matching tags.intgetNumTagsFor(java.lang.String key)Replies the number of tags with keykeyjava.lang.StringgetSummedValues(java.lang.String key)Replies the sum of all numeric tag values.intgetTagOccurrence(Tag tag)Gets the number of times this tag was added to the collection.TagCollectiongetTagsFor(java.lang.String key)Replies a tag collection with the tags for a given key.TagCollectiongetTagsFor(java.util.Collection<java.lang.String> keys)Replies a tag collection with all tags whose key is equal to one of the keys inkeys.java.util.Set<java.lang.String>getValues()Replies the set of values in this tag collectionjava.util.Set<java.lang.String>getValues(java.lang.String key)Replies the set of values for a given key.booleanhasEmptyValue(java.lang.String key)Replies true if there is a tag with an empty value forkey.booleanhasTagsFor(java.lang.String key)Replies true if there is at least one tag for the given key.booleanhasUniqueEmptyValue(java.lang.String key)Replies true if there is exactly one tag forkeyand if the value for this tag is empty.booleanhasUniqueNonEmptyValue(java.lang.String key)Replies true if there is exactly one tag forkeyand if the value of this tag is not empty.booleanhasValuesFor(java.lang.String key)Replies true it there is at least one tag with a non empty value for key.TagCollectionintersect(TagCollection other)Builds the intersection of this tag collection and another tag collectionbooleanisApplicableToPrimitive()Replies true if for every key there is one tag only, i.e.booleanisEmpty()Replies true if this tag collection is emptyjava.util.Iterator<Tag>iterator()Replies an iterator to iterate over the tags in this collectionTagCollectionminus(TagCollection other)Replies the difference of this tag collection and another tag collectionvoidremove(java.util.Collection<Tag> tags)Removes a collection of tags from the tag collection.voidremove(Tag tag)Removes a specific tag from the tag collection.voidremove(TagCollection tags)Removes all tags in the tag collectiontagsfrom the current tag collection.voidremoveByKey(java.lang.String key)Removes all tags whose keys are equal tokey.voidremoveByKey(java.util.Collection<java.lang.String> keys)Removes all tags whose key is in the collectionkeys.voidreplaceTagsOf(java.util.Collection<? extends Tagged> primitives)Replaces the tags of a collection ofOsmPrimitives by the tags in this collection.voidreplaceTagsOf(Tagged primitive)Replaces the tags of anOsmPrimitiveby the tags in this collection .voidsetUniqueForKey(java.lang.String key, java.lang.String value)Sets a unique tag for the key of this tag.voidsetUniqueForKey(Tag tag)Sets a unique tag for the key of this tag.intsize()Replies the number of tags in this tag collectionjava.lang.StringtoString()TagCollectionunion(TagCollection other)Replies the union of this tag collection and another tag collectionstatic TagCollectionunionOfAllPrimitives(java.util.Collection<? extends Tagged> primitives)Creates a tag collection from the union of the tags managed by a collection of primitives.static TagCollectionunionOfAllPrimitives(DataSet ds)Replies a tag collection with the union of the tags which are common to all primitives in the datasetds.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
SPLIT_VALUES_PATTERN
private static final java.util.regex.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 ifotheris null.- Parameters:
other- the other collection
-
TagCollection
public TagCollection(java.util.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. Ifprimitiveis 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(java.util.Map<java.lang.String,java.lang.String> tags)
Creates a tag collection from a map of key/value-pairs. Replies an empty tag collection iftagsis null.- Parameters:
tags- the key/value-pairs- Returns:
- the tag collection
-
unionOfAllPrimitives
public static TagCollection unionOfAllPrimitives(java.util.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, ifprimitivesis 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(java.util.Collection<? extends Tagged> primitives)
Replies a tag collection with the tags which are common to all primitives in inprimitives. Replies an empty tag collection ofprimitivesis 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 ofdsis 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. Iftagis 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(java.util.Collection<Tag> tags)
Adds a collection of tags to the tag collection. Iftagsis 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, iftagsis null.- Parameters:
tags- the other tag collection
-
remove
public void remove(Tag tag)
Removes a specific tag from the tag collection. Does nothing iftagis null.- Parameters:
tag- the tag to be removed
-
remove
public void remove(java.util.Collection<Tag> tags)
Removes a collection of tags from the tag collection. Does nothing iftagsis null.- Parameters:
tags- the tags to be removed
-
remove
public void remove(TagCollection tags)
Removes all tags in the tag collectiontagsfrom the current tag collection. Does nothing iftagsis null.- Parameters:
tags- the tag collection to be removed.
-
removeByKey
public void removeByKey(java.lang.String key)
Removes all tags whose keys are equal tokey. Does nothing ifkeyis null.- Parameters:
key- the key to be removed
-
removeByKey
public void removeByKey(java.util.Collection<java.lang.String> keys)
Removes all tags whose key is in the collectionkeys. Does nothing ifkeysis 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(java.util.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(java.util.Collection<java.lang.String> keys)
Replies true if this tag collection at least one tag for every key inkeys. Replies false, ifkeysis null. null values inkeysare 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(java.lang.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(java.lang.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(java.lang.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(java.lang.String key)
Replies true if there is exactly one tag forkeyand 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
keyand if the value of this tag is not empty
-
hasEmptyValue
public boolean hasEmptyValue(java.lang.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(java.lang.String key)
Replies true if there is exactly one tag forkeyand 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
keyand if the value for this tag is empty
-
getTagsFor
public TagCollection getTagsFor(java.lang.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(java.util.Collection<java.lang.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 java.util.Set<Tag> asSet()
Replies the tags of this tag collection as set- Returns:
- the tags of this tag collection as set
-
asList
public java.util.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 java.util.Iterator<Tag> iterator()
Replies an iterator to iterate over the tags in this collection- Specified by:
iteratorin interfacejava.lang.Iterable<Tag>- Returns:
- the iterator
-
getKeys
public java.util.Set<java.lang.String> getKeys()
Replies the set of keys of this tag collection.- Returns:
- the set of keys of this tag collection
-
getKeysWithMultipleValues
public java.util.Set<java.lang.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(java.lang.String key, java.lang.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 java.util.Set<java.lang.String> getValues()
Replies the set of values in this tag collection- Returns:
- the set of values
-
getValues
public java.util.Set<java.lang.String> getValues(java.lang.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:
trueif 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:
java.lang.IllegalStateException- if this tag collection can't be applied because there are keys with multiple values
-
applyTo
public void applyTo(java.util.Collection<? extends Tagged> primitives)
Applies this tag collection to a collection ofOsmPrimitives. Does nothing if primitives is null- Parameters:
primitives- the collection of primitives- Throws:
java.lang.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 anOsmPrimitiveby the tags in this collection . Does nothing if primitive is null- Parameters:
primitive- the primitive- Throws:
java.lang.IllegalStateException- if this tag collection can't be applied because there are keys with multiple values
-
replaceTagsOf
public void replaceTagsOf(java.util.Collection<? extends Tagged> primitives)
Replaces the tags of a collection ofOsmPrimitives by the tags in this collection. Does nothing if primitives is null- Parameters:
primitives- the collection of primitives- Throws:
java.lang.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 java.lang.String getJoinedValues(java.lang.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 java.lang.String getSummedValues(java.lang.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 java.util.stream.Stream<java.lang.String> generateKeyStream()
-
generateStreamForKey
private java.util.stream.Stream<Tag> generateStreamForKey(java.lang.String key)
Get a stream for the given key.- Parameters:
key- The key- Returns:
- The stream. An empty stream if key is
null
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-