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

Commit da2f67c

Browse files
committed
Removes 'Tag' and moves its utility methods to a 'StringSanitization' class.
1 parent 4bd2de1 commit da2f67c

File tree

9 files changed

+63
-59
lines changed

9 files changed

+63
-59
lines changed

BUILD

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ java_test(
162162
)
163163

164164
java_test(
165-
name = "TagKeyTest",
166-
srcs = ["core/javatests/com/google/census/TagKeyTest.java"],
165+
name = "StringSanitizationTest",
166+
srcs = ["core/javatests/com/google/census/StringSanitizationTest.java"],
167167
deps = [
168168
":census-core",
169169
":census-core_native",
@@ -176,8 +176,8 @@ java_test(
176176
)
177177

178178
java_test(
179-
name = "TagTest",
180-
srcs = ["core/javatests/com/google/census/TagTest.java"],
179+
name = "TagKeyTest",
180+
srcs = ["core/javatests/com/google/census/TagKeyTest.java"],
181181
deps = [
182182
":census-core",
183183
":census-core_native",

core/java/com/google/census/MetricName.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
* MetricName's are {@link String}s with enforced restrictions.
2020
*/
2121
public final class MetricName {
22-
public static final int MAX_LENGTH = Tag.MAX_LENGTH;
22+
public static final int MAX_LENGTH = StringSanitization.MAX_LENGTH;
2323

2424
public MetricName(String name) {
25-
this.name = Tag.sanitize(name);
25+
this.name = StringSanitization.sanitize(name);
2626
}
2727

2828
@Override

core/java/com/google/census/Tag.java renamed to core/java/com/google/census/StringSanitization.java

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,10 @@
1313

1414
package com.google.census;
1515

16-
import java.util.Map.Entry;
17-
1816
/**
19-
* Immutable representation of a Tag.
17+
* Utility methods for sanitizing tag keys, tag values, and metric names.
2018
*/
21-
public class Tag implements Entry<String, String> {
22-
23-
public Tag(TagKey key, TagValue value) {
24-
this.key = key.toString();
25-
this.value = value.toString();
26-
}
27-
28-
@Override
29-
public String getKey() {
30-
return key;
31-
}
32-
33-
@Override
34-
public String getValue() {
35-
return value;
36-
}
37-
38-
@Override
39-
public String setValue(String value) {
40-
throw new UnsupportedOperationException();
41-
}
42-
43-
@Override
44-
public boolean equals(Object obj) {
45-
if (!(obj instanceof Tag)) {
46-
return false;
47-
}
48-
Tag that = (Tag) obj;
49-
return key.equals(that.key) && value.equals(that.value);
50-
}
51-
52-
@Override
53-
public int hashCode() {
54-
return key.hashCode() * 31 + value.hashCode();
55-
}
56-
57-
// Assumes key and value have already been sanitized.
58-
Tag(String key, String value) {
59-
this.key = key;
60-
this.value = value;
61-
}
19+
final class StringSanitization {
6220

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

94-
private final String key;
95-
private final String value;
52+
//Visible for testing
53+
StringSanitization() {
54+
throw new AssertionError();
55+
}
9656
}

core/java/com/google/census/TagKey.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
* TagKey's are {@link String}s with enforced restrictions.
2020
*/
2121
public final class TagKey {
22-
public static final int MAX_LENGTH = Tag.MAX_LENGTH;
22+
public static final int MAX_LENGTH = StringSanitization.MAX_LENGTH;
2323

2424
public TagKey(String key) {
25-
this.key = Tag.sanitize(key);
25+
this.key = StringSanitization.sanitize(key);
2626
}
2727

2828
@Override

core/java/com/google/census/TagValue.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
* TagValue's are {@link String}s with enforced restrictions.
2020
*/
2121
public final class TagValue {
22-
public static final int MAX_LENGTH = Tag.MAX_LENGTH;
22+
public static final int MAX_LENGTH = StringSanitization.MAX_LENGTH;
2323

2424
public TagValue(String value) {
25-
this.value = Tag.sanitize(value);
25+
this.value = StringSanitization.sanitize(value);
2626
}
2727

2828
@Override

core/javatests/com/google/census/MetricNameTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public void testNameMaxLength() {
3939
@Test
4040
public void testNameBadChar() {
4141
assertThat(new MetricName("\2ab\3cd").toString())
42-
.isEqualTo(Tag.UNPRINTABLE_CHAR_SUBSTITUTE + "ab" + Tag.UNPRINTABLE_CHAR_SUBSTITUTE + "cd");
42+
.isEqualTo(StringSanitization.UNPRINTABLE_CHAR_SUBSTITUTE + "ab"
43+
+ StringSanitization.UNPRINTABLE_CHAR_SUBSTITUTE + "cd");
4344
}
4445

4546
@Test
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2016, Google Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*/
13+
14+
package com.google.census;
15+
16+
import static com.google.common.truth.Truth.assertThat;
17+
18+
import java.util.Arrays;
19+
import org.junit.Test;
20+
21+
public class StringSanitizationTest {
22+
@Test
23+
public void testMaxLength() {
24+
char[] string = new char[StringSanitization.MAX_LENGTH];
25+
char[] truncString = new char[StringSanitization.MAX_LENGTH + 10];
26+
Arrays.fill(string, 'v');
27+
Arrays.fill(truncString, 'v');
28+
assertThat(StringSanitization.sanitize(new String(truncString))).isEqualTo(new String(string));
29+
}
30+
31+
@Test
32+
public void testBadChar() {
33+
String string = "\2ab\3cd";
34+
assertThat(StringSanitization.sanitize(string).toString()).isEqualTo("_ab_cd");
35+
}
36+
37+
@Test(expected=AssertionError.class)
38+
public void testConstructor() {
39+
new StringSanitization();
40+
}
41+
}

core/javatests/com/google/census/TagKeyTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ public void testKeyMaxLength() {
4141
public void testKeyBadChar() {
4242
String key = "\2ab\3cd";
4343
assertThat(new TagKey(key).toString())
44-
.isEqualTo(Tag.UNPRINTABLE_CHAR_SUBSTITUTE + "ab" + Tag.UNPRINTABLE_CHAR_SUBSTITUTE + "cd");
44+
.isEqualTo(StringSanitization.UNPRINTABLE_CHAR_SUBSTITUTE + "ab"
45+
+ StringSanitization.UNPRINTABLE_CHAR_SUBSTITUTE + "cd");
4546
}
4647

4748
@Test

core/javatests/com/google/census/TagValueTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public void testValueMaxLength() {
4040
public void testValueBadChar() {
4141
String value = "\2ab\3cd";
4242
assertThat(new TagValue(value).toString())
43-
.isEqualTo(Tag.UNPRINTABLE_CHAR_SUBSTITUTE + "ab" + Tag.UNPRINTABLE_CHAR_SUBSTITUTE + "cd");
43+
.isEqualTo(StringSanitization.UNPRINTABLE_CHAR_SUBSTITUTE + "ab"
44+
+ StringSanitization.UNPRINTABLE_CHAR_SUBSTITUTE + "cd");
4445
}
4546

4647
@Test

0 commit comments

Comments
 (0)