From 6765b079eace1fa789d201dd69448b52a67f5cc6 Mon Sep 17 00:00:00 2001 From: Greg Orzell Date: Fri, 13 Apr 2012 16:57:57 -0700 Subject: [PATCH] Issue #39 Created a Sorted tag list and unit test. --- .../com/netflix/servo/tag/SortedTagList.java | 118 ++++++++++++++++ .../netflix/servo/tag/SortedTagListTest.java | 133 ++++++++++++++++++ 2 files changed, 251 insertions(+) create mode 100644 servo-core/src/main/java/com/netflix/servo/tag/SortedTagList.java create mode 100644 servo-core/src/test/java/com/netflix/servo/tag/SortedTagListTest.java diff --git a/servo-core/src/main/java/com/netflix/servo/tag/SortedTagList.java b/servo-core/src/main/java/com/netflix/servo/tag/SortedTagList.java new file mode 100644 index 00000000..ac1ddc8f --- /dev/null +++ b/servo-core/src/main/java/com/netflix/servo/tag/SortedTagList.java @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2012. Netflix, Inc. + * + * 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 com.netflix.servo.tag; + +import com.google.common.collect.ImmutableSortedMap; +import com.google.common.collect.Ordering; + +import java.util.*; + +/** + * User: gorzell + * Date: 4/13/12 + * Time: 1:55 PM + */ + +public final class SortedTagList implements TagList { + + public static final SortedTagList EMPTY = new Builder().build(); + + private final SortedMap tagSortedMap; + private final int size; + + public static final class Builder { + private final ImmutableSortedMap.Builder mapBuilder = + new ImmutableSortedMap.Builder(Ordering.natural()); + + public Builder withTags(Collection tagsCollection) { + for (Tag t : tagsCollection) { + mapBuilder.put(t.getKey(), t); + } + return this; + } + + public Builder withTag(Tag t) { + mapBuilder.put(t.getKey(), t); + return this; + } + + public Builder withTag(String key, String value) { + return withTag(new BasicTag(key, value)); + } + + public SortedTagList build() { + return new SortedTagList(this); + } + } + + private SortedTagList(Builder builder) { + this.tagSortedMap = builder.mapBuilder.build(); + this.size = tagSortedMap.size(); + } + + /** + * Returns the tag matching a given key or null if not match is found. + */ + @Override + public Tag getTag(String key) { + return tagSortedMap.get(key); + } + + /** + * Returns true if this list has a tag with the given key. + */ + @Override + public boolean containsKey(String key) { + return tagSortedMap.containsKey(key); + } + + /** + * Returns true if this list is emtpy. + */ + @Override + public boolean isEmpty() { + return tagSortedMap.isEmpty(); + } + + /** + * Returns the number of tags in this list. + */ + @Override + public int size() { + return size; + } + + /** + * {@inheritDoc} + */ + @Override + public Iterator iterator() { + return tagSortedMap.values().iterator(); + } + + /** + * Returns a map containing a copy of the tags in this list. + */ + @Override + public Map asMap() { + Map stringMap = new HashMap(size()); + for (Tag t : tagSortedMap.values()) { + stringMap.put(t.getKey(), t.getValue()); + } + return stringMap; + } +} diff --git a/servo-core/src/test/java/com/netflix/servo/tag/SortedTagListTest.java b/servo-core/src/test/java/com/netflix/servo/tag/SortedTagListTest.java new file mode 100644 index 00000000..7d194de3 --- /dev/null +++ b/servo-core/src/test/java/com/netflix/servo/tag/SortedTagListTest.java @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2012. Netflix, Inc. + * + * 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 com.netflix.servo.tag; + +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; + +import static org.testng.Assert.assertTrue; + +/** + * User: gorzell + * Date: 4/13/12 + * Time: 4:01 PM + */ +public class SortedTagListTest { + + public static Tag a; + public static Tag b; + public static Tag c; + + public static Tag[] tagArray = new Tag[3]; + + public TagList testListFromStrings; + public TagList testListFromCollection; + public TagList testListFromTags; + public Collection collection; + + @BeforeClass + public void setup() throws Exception { + a = new BasicTag("a", "a"); + b = new BasicTag("b", "b"); + c = new BasicTag("c", "c"); + + tagArray[0] = a; + tagArray[1] = b; + tagArray[2] = c; + + testListFromStrings = new SortedTagList.Builder().withTag("a", "a").withTag("b", "b").withTag("c", "c").build(); + + collection = new ArrayList(); + collection.add(a); + collection.add(c); + collection.add(b); + + testListFromCollection = new SortedTagList.Builder().withTags(collection).build(); + testListFromTags = new SortedTagList.Builder().withTag(c).withTag(a).withTag(b).build(); + } + + @Test + public void testGetTag() throws Exception { + assertTrue(testListFromCollection.getTag("a").equals(a)); + assertTrue(testListFromStrings.getTag("b").equals(b)); + assertTrue(testListFromTags.getTag("c").equals(c)); + } + + @Test + public void testContainsKey() throws Exception { + assertTrue(testListFromCollection.containsKey("b")); + assertTrue(testListFromStrings.containsKey("c")); + assertTrue(testListFromTags.containsKey("a")); + } + + @Test + public void testIsEmpty() throws Exception { + assertTrue(SortedTagList.EMPTY.isEmpty()); + assertTrue(!testListFromCollection.isEmpty()); + assertTrue(!testListFromStrings.isEmpty()); + assertTrue(!testListFromTags.isEmpty()); + } + + @Test + public void testSize() throws Exception { + assertTrue(SortedTagList.EMPTY.isEmpty()); + assertTrue(!testListFromCollection.isEmpty()); + assertTrue(!testListFromStrings.isEmpty()); + assertTrue(!testListFromTags.isEmpty()); + } + + @Test + public void testOrder() throws Exception { + int i = 0; + for(Iterator t = testListFromStrings.iterator(); t.hasNext();){ + assertTrue(t.next().equals(tagArray[i])); + i++; + } + + i = 0; + for(Iterator t = testListFromTags.iterator(); t.hasNext();){ + assertTrue(t.next().equals(tagArray[i])); + i++; + } + + i = 0; + for(Iterator t = testListFromCollection.iterator(); t.hasNext();){ + assertTrue(t.next().equals(tagArray[i])); + i++; + } + } + + @Test + public void testAsMap() throws Exception { + Map stringMap = testListFromCollection.asMap(); + + int i = 0; + for(String s : stringMap.keySet()){ + s.equals(tagArray[i].getKey()); + } + + i = 0; + for(String s : stringMap.values()){ + s.equals(tagArray[i].getValue()); + } + } +}