diff options
author | Julien Desprez <jdesprez@google.com> | 2018-10-22 11:42:32 -0700 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2018-10-22 11:42:32 -0700 |
commit | 108d816db7cf4559963e6474a679af7a25d892ad (patch) | |
tree | ede84fcf0a9687d4907ae5f8a4788271d62e0922 /impl_core/src/main/java/io/opencensus/implcore/tags/TagContextImpl.java | |
parent | cfbefd32336596ea63784607e4106dc37ce0567f (diff) | |
parent | 13217871fefa43f6d16fbb31b04e9904996d87d5 (diff) | |
download | opencensus-java-108d816db7cf4559963e6474a679af7a25d892ad.tar.gz |
Merge remote-tracking branch 'aosp/upstream-master' into merge am: dd3cabeacc am: 6fbc3cf5a1
am: 13217871fe
Change-Id: Icfe12d6db2f81a6f5c65241919b1d63fb665ffd6
Diffstat (limited to 'impl_core/src/main/java/io/opencensus/implcore/tags/TagContextImpl.java')
-rw-r--r-- | impl_core/src/main/java/io/opencensus/implcore/tags/TagContextImpl.java | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/impl_core/src/main/java/io/opencensus/implcore/tags/TagContextImpl.java b/impl_core/src/main/java/io/opencensus/implcore/tags/TagContextImpl.java new file mode 100644 index 00000000..f7a8ff82 --- /dev/null +++ b/impl_core/src/main/java/io/opencensus/implcore/tags/TagContextImpl.java @@ -0,0 +1,85 @@ +/* + * Copyright 2017, OpenCensus Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opencensus.implcore.tags; + +import io.opencensus.tags.Tag; +import io.opencensus.tags.TagContext; +import io.opencensus.tags.TagKey; +import io.opencensus.tags.TagValue; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import javax.annotation.Nullable; +import javax.annotation.concurrent.Immutable; + +@Immutable +public final class TagContextImpl extends TagContext { + + public static final TagContextImpl EMPTY = + new TagContextImpl(Collections.<TagKey, TagValue>emptyMap()); + + // The types of the TagKey and value must match for each entry. + private final Map<TagKey, TagValue> tags; + + public TagContextImpl(Map<? extends TagKey, ? extends TagValue> tags) { + this.tags = Collections.unmodifiableMap(new HashMap<TagKey, TagValue>(tags)); + } + + public Map<TagKey, TagValue> getTags() { + return tags; + } + + @Override + protected Iterator<Tag> getIterator() { + return new TagIterator(tags); + } + + @Override + public boolean equals(@Nullable Object other) { + // Directly compare the tags when both objects are TagContextImpls, for efficiency. + if (other instanceof TagContextImpl) { + return getTags().equals(((TagContextImpl) other).getTags()); + } + return super.equals(other); + } + + private static final class TagIterator implements Iterator<Tag> { + Iterator<Map.Entry<TagKey, TagValue>> iterator; + + TagIterator(Map<TagKey, TagValue> tags) { + iterator = tags.entrySet().iterator(); + } + + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + + @Override + public Tag next() { + final Entry<TagKey, TagValue> next = iterator.next(); + return Tag.create(next.getKey(), next.getValue()); + } + + @Override + public void remove() { + throw new UnsupportedOperationException("TagIterator.remove()"); + } + } +} |