Skip to content
This repository has been archived by the owner on Dec 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #16 from sebright/remove-tag-class
Browse files Browse the repository at this point in the history
Removes 'Tag' and moves its utility methods to a 'StringSanitization' class.
  • Loading branch information
dinooliva committed Sep 27, 2016
2 parents 4bd2de1 + 4f56d46 commit d1fa8a0
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 118 deletions.
8 changes: 4 additions & 4 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ java_test(
)

java_test(
name = "TagKeyTest",
srcs = ["core/javatests/com/google/census/TagKeyTest.java"],
name = "StringUtilTest",
srcs = ["core/javatests/com/google/census/StringUtilTest.java"],
deps = [
":census-core",
":census-core_native",
Expand All @@ -176,8 +176,8 @@ java_test(
)

java_test(
name = "TagTest",
srcs = ["core/javatests/com/google/census/TagTest.java"],
name = "TagKeyTest",
srcs = ["core/javatests/com/google/census/TagKeyTest.java"],
deps = [
":census-core",
":census-core_native",
Expand Down
4 changes: 2 additions & 2 deletions core/java/com/google/census/MetricName.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
* MetricName's are {@link String}s with enforced restrictions.
*/
public final class MetricName {
public static final int MAX_LENGTH = Tag.MAX_LENGTH;
public static final int MAX_LENGTH = StringUtil.MAX_LENGTH;

public MetricName(String name) {
this.name = Tag.sanitize(name);
this.name = StringUtil.sanitize(name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,10 @@

package com.google.census;

import java.util.Map.Entry;

/**
* Immutable representation of a Tag.
* Utility methods for working with tag keys, tag values, and metric names.
*/
public class Tag implements Entry<String, String> {

public Tag(TagKey key, TagValue value) {
this.key = key.toString();
this.value = value.toString();
}

@Override
public String getKey() {
return key;
}

@Override
public String getValue() {
return value;
}

@Override
public String setValue(String value) {
throw new UnsupportedOperationException();
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof Tag)) {
return false;
}
Tag that = (Tag) obj;
return key.equals(that.key) && value.equals(that.value);
}

@Override
public int hashCode() {
return key.hashCode() * 31 + value.hashCode();
}

// Assumes key and value have already been sanitized.
Tag(String key, String value) {
this.key = key;
this.value = value;
}
final class StringUtil {

static final int MAX_LENGTH = 255;
static final char UNPRINTABLE_CHAR_SUBSTITUTE = '_';
Expand Down Expand Up @@ -91,6 +49,8 @@ private static boolean isPrintableChar(char c) {
return c >= ' ' && c <= '~';
}

private final String key;
private final String value;
//Visible for testing
StringUtil() {
throw new AssertionError();
}
}
4 changes: 2 additions & 2 deletions core/java/com/google/census/TagKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
* TagKey's are {@link String}s with enforced restrictions.
*/
public final class TagKey {
public static final int MAX_LENGTH = Tag.MAX_LENGTH;
public static final int MAX_LENGTH = StringUtil.MAX_LENGTH;

public TagKey(String key) {
this.key = Tag.sanitize(key);
this.key = StringUtil.sanitize(key);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions core/java/com/google/census/TagValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
* TagValue's are {@link String}s with enforced restrictions.
*/
public final class TagValue {
public static final int MAX_LENGTH = Tag.MAX_LENGTH;
public static final int MAX_LENGTH = StringUtil.MAX_LENGTH;

public TagValue(String value) {
this.value = Tag.sanitize(value);
this.value = StringUtil.sanitize(value);
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion core/javatests/com/google/census/MetricNameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public void testNameMaxLength() {
@Test
public void testNameBadChar() {
assertThat(new MetricName("\2ab\3cd").toString())
.isEqualTo(Tag.UNPRINTABLE_CHAR_SUBSTITUTE + "ab" + Tag.UNPRINTABLE_CHAR_SUBSTITUTE + "cd");
.isEqualTo(StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "ab"
+ StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "cd");
}

@Test
Expand Down
47 changes: 47 additions & 0 deletions core/javatests/com/google/census/StringUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2016, Google 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.google.census;

import static com.google.common.truth.Truth.assertThat;

import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
* Tests for {@link StringUtil}.
*/
@RunWith(JUnit4.class)
public final class StringUtilTest {
@Test
public void testMaxLength() {
char[] string = new char[StringUtil.MAX_LENGTH];
char[] truncString = new char[StringUtil.MAX_LENGTH + 10];
Arrays.fill(string, 'v');
Arrays.fill(truncString, 'v');
assertThat(StringUtil.sanitize(new String(truncString))).isEqualTo(new String(string));
}

@Test
public void testBadChar() {
String string = "\2ab\3cd";
assertThat(StringUtil.sanitize(string).toString()).isEqualTo("_ab_cd");
}

@Test(expected=AssertionError.class)
public void testConstructor() {
new StringUtil();
}
}
3 changes: 2 additions & 1 deletion core/javatests/com/google/census/TagKeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public void testKeyMaxLength() {
public void testKeyBadChar() {
String key = "\2ab\3cd";
assertThat(new TagKey(key).toString())
.isEqualTo(Tag.UNPRINTABLE_CHAR_SUBSTITUTE + "ab" + Tag.UNPRINTABLE_CHAR_SUBSTITUTE + "cd");
.isEqualTo(StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "ab"
+ StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "cd");
}

@Test
Expand Down
59 changes: 0 additions & 59 deletions core/javatests/com/google/census/TagTest.java

This file was deleted.

3 changes: 2 additions & 1 deletion core/javatests/com/google/census/TagValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public void testValueMaxLength() {
public void testValueBadChar() {
String value = "\2ab\3cd";
assertThat(new TagValue(value).toString())
.isEqualTo(Tag.UNPRINTABLE_CHAR_SUBSTITUTE + "ab" + Tag.UNPRINTABLE_CHAR_SUBSTITUTE + "cd");
.isEqualTo(StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "ab"
+ StringUtil.UNPRINTABLE_CHAR_SUBSTITUTE + "cd");
}

@Test
Expand Down

0 comments on commit d1fa8a0

Please sign in to comment.