GeezerVison is showing github.com — Removes 'Tag' and moves its utility methods to a 'StringSanitization' class. by sebright2 · Pull Request #16 · census-instrumentation/opencensus-java
This repository was archived by the owner on Dec 23, 2023. It is now read-only.
Removes 'Tag' and moves its utility methods to a 'StringSanitization' class.
#16
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.java
(9)
No extension
(1)
All 2 file types selected
Deleted files
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
8 changes: 4 additions & 4 deletions
8
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
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();
}
}
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
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
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
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();
}
}
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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.