Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MetricName TagMap supports replacement of existing entries #1537

Merged
merged 5 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1537.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: MetricName TagMap supports replacement of existing entries
links:
- https://github.com/palantir/tritium/pull/1537
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Ordering;
import com.palantir.logsafe.exceptions.SafeIllegalArgumentException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -114,15 +113,20 @@ private static String[] toArray(Map<String, String> data) {
return values;
}

/** Returns a new {@link TagMap} with on additional entry. */
/** Returns a new {@link TagMap} with on additional or updated entry. */
carterkozak marked this conversation as resolved.
Show resolved Hide resolved
TagMap withEntry(String key, String value) {
String[] local = this.values;
int newPosition = 0;
for (; newPosition < local.length; newPosition += 2) {
String current = local[newPosition];
int comparisonResult = current.compareTo(key);
if (comparisonResult == 0) {
throw new SafeIllegalArgumentException("Base must not contain the extra key that is to be added");
if (Objects.equals(local[newPosition + 1], value)) {
return this;
}
String[] newArray = local.clone();
newArray[newPosition + 1] = value;
return new TagMap(newArray);
}
if (comparisonResult > 0) {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package com.palantir.tritium.metrics.registry;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Ordering;
import java.util.AbstractMap.SimpleImmutableEntry;
Expand Down Expand Up @@ -53,12 +53,22 @@ void testSingleton() {

@Test
void testExtraEntryKeyAlreadyExists() {
TagMap map = TagMap.of(Collections.singletonMap("foo", "bar"));
assertThatThrownBy(() -> map.withEntry("foo", "baz")).isInstanceOf(IllegalArgumentException.class);
TagMap original = TagMap.of(Collections.singletonMap("foo", "bar"));
TagMap updated = original.withEntry("foo", "baz");
assertThat(updated)
.isEqualTo(ImmutableMap.of("foo", "baz"))
.as("Original must not be mutated")
.isNotSameAs(original);
carterkozak marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
void testUpdateWithExisting() {
TagMap map = TagMap.EMPTY.withEntry("foo", "bar");
assertThat(map.withEntry("foo", "bar")).isSameAs(map);
}

@Test
public void testNaturalOrder() {
void testNaturalOrder() {
assertThat(TagMap.isNaturalOrder(Ordering.natural())).isTrue();
assertThat(TagMap.isNaturalOrder(Comparator.naturalOrder())).isTrue();

Expand Down