org.openstreetmap.josm.data.osm
Class TagCollection

java.lang.Object
  extended by org.openstreetmap.josm.data.osm.TagCollection
All Implemented Interfaces:
java.lang.Iterable<Tag>

public class TagCollection
extends java.lang.Object
implements java.lang.Iterable<Tag>

TagCollection is a collection of tags which can be used to manipulate tags managed by OsmPrimitives. A TagCollection can be created:

It provides methods to query the collection, like size(), hasTagsFor(String), etc. Basic set operations allow to create the union, the intersection and the difference of tag collections, see union(TagCollection), intersect(TagCollection), and minus(TagCollection).


Field Summary
private  java.util.HashSet<Tag> tags
           
 
Constructor Summary
TagCollection()
          Creates an empty tag collection
TagCollection(TagCollection other)
          Creates a clone of the tag collection other.
 
Method Summary
 void add(java.util.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(java.util.Collection<? extends Tagged> primitives)
          Applies this tag collection to a collection of OsmPrimitives.
 void applyTo(Tagged primitive)
          Applies this tag collection to an OsmPrimitive.
 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 set
static TagCollection commonToAllPrimitives(java.util.Collection<? extends Tagged> primitives)
          Replies a tag collection with the tags which are common to all primitives in in primitives.
 boolean contains(Tag tag)
          Replies true if the this tag collection contains tag.
 boolean containsAll(java.util.Collection<Tag> tags)
          Replies true if this tag collection contains all tags in tags.
 boolean containsAllKeys(java.util.Collection<java.lang.String> keys)
          Replies true if this tag collection at least one tag for every key in keys.
 boolean containsKey(java.lang.String key)
          Replies true if this tag collection contains at least one tag with key key.
 TagCollection emptyTagsForKeysMissingIn(TagCollection other)
           
static TagCollection from(java.util.Map<java.lang.String,java.lang.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 specific OsmPrimitive.
 java.lang.String getJoinedValues(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.
 int getNumTagsFor(java.lang.String key)
          Replies the number of tags with key key
 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 in keys.
 TagCollection getTagsFor(java.lang.String key)
          Replies a tag collection with the tags for a given key.
 java.util.Set<java.lang.String> getValues()
          Replies the set of values in this tag collection
 java.util.Set<java.lang.String> getValues(java.lang.String key)
          Replies the set of values for a given key.
 boolean hasEmptyValue(java.lang.String key)
          Replies true if there is a tag with an empty value for key.
 boolean hasTagsFor(java.lang.String key)
          Replies true if there is at least one tag for the given key.
 boolean hasUniqueEmptyValue(java.lang.String key)
          Replies true if there is exactly one tag for key and if the value for this tag is empty.
 boolean hasUniqueNonEmptyValue(java.lang.String key)
          Replies true if there is exactly one tag for key and if the value of this tag is not empty.
 boolean hasValuesFor(java.lang.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 collection
 boolean isApplicableToPrimitive()
          Replies true if for every key there is one tag only, i.e.
 boolean isEmpty()
          Replies true if this tag collection is empty
 java.util.Iterator<Tag> iterator()
          Replies an iterator to iterate over the tags in this collection
 TagCollection minus(TagCollection other)
          Replies the difference of this tag collection and another tag collection
 void remove(java.util.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 collection tags from the current tag collection.
 void removeByKey(java.util.Collection<java.lang.String> keys)
          Removes all tags whose key is in the collection keys.
 void removeByKey(java.lang.String key)
          Removes all tags whose keys are equal to key.
 void replaceTagsOf(java.util.Collection<? extends Tagged> primitives)
          Replaces the tags of a collection ofOsmPrimitives by the tags in this collection.
 void replaceTagsOf(Tagged primitive)
          Replaces the tags of an OsmPrimitive by the tags in this collection .
 void setUniqueForKey(java.lang.String key, java.lang.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 collection
 java.lang.String toString()
           
 TagCollection union(TagCollection other)
          Replies the union of this tag collection and another tag collection
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.
static TagCollection unionOfAllPrimitives(DataSet ds)
          Replies a tag collection with the union of the tags which are common to all primitives in the dataset ds.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

tags

private final java.util.HashSet<Tag> tags
Constructor Detail

TagCollection

public TagCollection()
Creates an empty tag collection


TagCollection

public TagCollection(TagCollection other)
Creates a clone of the tag collection other. Creats an empty tag collection if other is null.

Parameters:
other - the other collection
Method Detail

from

public static TagCollection from(Tagged primitive)
Creates a tag collection from the tags managed by a specific OsmPrimitive. If primitive 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(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 if tags is 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, if primitives 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(java.util.Collection<? extends Tagged> primitives)
Replies a tag collection with the tags which are common to all primitives in in primitives. Replies an empty tag collection of primitives 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 dataset ds. Returns an empty tag collection of ds 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 void add(Tag tag)
Adds a tag to the tag collection. If tag is null, nothing is added.

Parameters:
tag - the tag to add

add

public void add(java.util.Collection<Tag> tags)
Adds a collection of tags to the tag collection. If tags is null, nothing is added. null values in the collection are ignored.

Parameters:
tags - the collection of tags

add

public void add(TagCollection tags)
Adds the tags of another tag collection to this collection. Adds nothing, if tags is null.

Parameters:
tags - the other tag collection

remove

public void remove(Tag tag)
Removes a specific tag from the tag collection. Does nothing if tag is 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 if tags is null.

Parameters:
tags - the tags to be removed

remove

public void remove(TagCollection tags)
Removes all tags in the tag collection tags from the current tag collection. Does nothing if tags is null.

Parameters:
tags - the tag collection to be removed.

removeByKey

public void removeByKey(java.lang.String key)
Removes all tags whose keys are equal to key. Does nothing if key is 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 collection keys. Does nothing if keys is null.

Parameters:
keys - the collection of keys to be removed

contains

public boolean contains(Tag tag)
Replies true if the this tag collection contains tag.

Parameters:
tag - the tag to look up
Returns:
true if the this tag collection contains tag; false, otherwise

containsKey

public boolean containsKey(java.lang.String key)
Replies true if this tag collection contains at least one tag with key key.

Parameters:
key - the key to look up
Returns:
true if this tag collection contains at least one tag with key key; false, otherwise

containsAll

public boolean containsAll(java.util.Collection<Tag> tags)
Replies true if this tag collection contains all tags in tags. 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 in keys. Replies false, if keys is null. null values in keys 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(java.lang.String key)
Replies the number of tags with key key

Parameters:
key - the key to look up
Returns:
the number of tags with key key. 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 for key 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(java.lang.String key)
Replies true if there is a tag with an empty value for key. 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 for key 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(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 in keys. 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.

iterator

public java.util.Iterator<Tag> iterator()
Replies an iterator to iterate over the tags in this collection

Specified by:
iterator in interface java.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 key
value - 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:

applyTo

public void applyTo(Tagged primitive)
             throws java.lang.IllegalStateException
Applies this tag collection to an OsmPrimitive. Does nothing if primitive is null

Parameters:
primitive - the primitive
Throws:
java.lang.IllegalStateException - thrown 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)
             throws java.lang.IllegalStateException
Applies this tag collection to a collection of OsmPrimitives. Does nothing if primitives is null

Parameters:
primitives - the collection of primitives
Throws:
java.lang.IllegalStateException - thrown if this tag collection can't be applied because there are keys with multiple values

replaceTagsOf

public void replaceTagsOf(Tagged primitive)
                   throws java.lang.IllegalStateException
Replaces the tags of an OsmPrimitive by the tags in this collection . Does nothing if primitive is null

Parameters:
primitive - the primitive
Throws:
java.lang.IllegalStateException - thrown 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)
                   throws java.lang.IllegalStateException
Replaces the tags of a collection ofOsmPrimitives by the tags in this collection. Does nothing if primitives is null

Parameters:
primitive - the collection of primitives
Throws:
java.lang.IllegalStateException - thrown if this tag collection can't be applied because there are keys with multiple values

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

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

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)

Returns:
the concatenation of all tag values

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object


JOSM