Skip to content

Commit

Permalink
Issue Netflix#39 Created a Sorted tag list and unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
gorzell committed Apr 13, 2012
1 parent 04da626 commit 6765b07
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 0 deletions.
118 changes: 118 additions & 0 deletions servo-core/src/main/java/com/netflix/servo/tag/SortedTagList.java
Original file line number Diff line number Diff line change
@@ -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<String, Tag> tagSortedMap;
private final int size;

public static final class Builder {
private final ImmutableSortedMap.Builder<String, Tag> mapBuilder =
new ImmutableSortedMap.Builder<String, Tag>(Ordering.natural());

public Builder withTags(Collection<Tag> 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<Tag> iterator() {
return tagSortedMap.values().iterator();
}

/**
* Returns a map containing a copy of the tags in this list.
*/
@Override
public Map<String, String> asMap() {
Map<String, String> stringMap = new HashMap<String, String>(size());
for (Tag t : tagSortedMap.values()) {
stringMap.put(t.getKey(), t.getValue());
}
return stringMap;
}
}
133 changes: 133 additions & 0 deletions servo-core/src/test/java/com/netflix/servo/tag/SortedTagListTest.java
Original file line number Diff line number Diff line change
@@ -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<Tag> 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<Tag>();
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<Tag> t = testListFromStrings.iterator(); t.hasNext();){
assertTrue(t.next().equals(tagArray[i]));
i++;
}

i = 0;
for(Iterator<Tag> t = testListFromTags.iterator(); t.hasNext();){
assertTrue(t.next().equals(tagArray[i]));
i++;
}

i = 0;
for(Iterator<Tag> t = testListFromCollection.iterator(); t.hasNext();){
assertTrue(t.next().equals(tagArray[i]));
i++;
}
}

@Test
public void testAsMap() throws Exception {
Map<String, String> 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());
}
}
}

0 comments on commit 6765b07

Please sign in to comment.