Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions internal-api/src/main/java/datadog/trace/api/TagMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ public boolean isNumber() {
return _isNumericPrimitive(curType) || (this.rawObj instanceof Number);
}

private static boolean _isNumericPrimitive(byte type) {
static boolean _isNumericPrimitive(byte type) {
return (type >= BYTE);
}

Expand Down Expand Up @@ -1080,7 +1080,7 @@ abstract class TagMapFactory<MapT extends TagMap> {
createFactory(Config.get().isOptimizedMapEnabled());

static final TagMapFactory<?> createFactory(boolean useOptimized) {
return useOptimized ? new OptimizedTagMapFactory() : new LegacyTagMapFactory();
return useOptimized ? OptimizedTagMapFactory.INSTANCE : LegacyTagMapFactory.INSTANCE;
}

public abstract MapT create();
Expand All @@ -1091,6 +1091,10 @@ static final TagMapFactory<?> createFactory(boolean useOptimized) {
}

final class OptimizedTagMapFactory extends TagMapFactory<OptimizedTagMap> {
static final OptimizedTagMapFactory INSTANCE = new OptimizedTagMapFactory();

private OptimizedTagMapFactory() {}

@Override
public OptimizedTagMap create() {
return new OptimizedTagMap();
Expand All @@ -1108,6 +1112,10 @@ public OptimizedTagMap empty() {
}

final class LegacyTagMapFactory extends TagMapFactory<LegacyTagMap> {
static final LegacyTagMapFactory INSTANCE = new LegacyTagMapFactory();

private LegacyTagMapFactory() {}

@Override
public LegacyTagMap create() {
return new LegacyTagMap();
Expand Down
80 changes: 67 additions & 13 deletions internal-api/src/test/java/datadog/trace/api/TagMapEntryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@
* @author dougqh
*/
public class TagMapEntryTest {
@Test
public void isNumericPrimitive() {
assertFalse(TagMap.Entry._isNumericPrimitive(TagMap.Entry.ANY));
assertFalse(TagMap.Entry._isNumericPrimitive(TagMap.Entry.BOOLEAN));
assertFalse(TagMap.Entry._isNumericPrimitive(TagMap.Entry.CHAR));
assertFalse(TagMap.Entry._isNumericPrimitive(TagMap.Entry.OBJECT));

assertTrue(TagMap.Entry._isNumericPrimitive(TagMap.Entry.BYTE));
assertTrue(TagMap.Entry._isNumericPrimitive(TagMap.Entry.SHORT));
assertTrue(TagMap.Entry._isNumericPrimitive(TagMap.Entry.INT));
assertTrue(TagMap.Entry._isNumericPrimitive(TagMap.Entry.LONG));
assertTrue(TagMap.Entry._isNumericPrimitive(TagMap.Entry.FLOAT));
assertTrue(TagMap.Entry._isNumericPrimitive(TagMap.Entry.DOUBLE));
}

@Test
public void objectEntry() {
test(
Expand All @@ -42,7 +57,8 @@ public void objectEntry() {
checkKey("foo", entry),
checkValue("bar", entry),
checkEquals("bar", entry::stringValue),
checkTrue(entry::isObject)));
checkTrue(entry::isObject),
checkFalse(entry::isNumber)));
}

@Test
Expand All @@ -55,6 +71,7 @@ public void anyEntry_object() {
checkKey("foo", entry),
checkValue("bar", entry),
checkTrue(entry::isObject),
checkFalse(entry::isNumber),
checkKey("foo", entry),
checkValue("bar", entry)));
}
Expand All @@ -70,6 +87,7 @@ public void booleanEntry(boolean value) {
checkKey("foo", entry),
checkValue(value, entry),
checkFalse(entry::isNumericPrimitive),
checkFalse(entry::isNumber),
checkType(TagMap.Entry.BOOLEAN, entry)));
}

Expand All @@ -84,6 +102,7 @@ public void booleanEntry_boxed(boolean value) {
checkKey("foo", entry),
checkValue(value, entry),
checkFalse(entry::isNumericPrimitive),
checkFalse(entry::isNumber),
checkType(TagMap.Entry.BOOLEAN, entry)));
}

Expand All @@ -98,6 +117,7 @@ public void anyEntry_boolean(boolean value) {
checkKey("foo", entry),
checkValue(value, entry),
checkFalse(entry::isNumericPrimitive),
checkFalse(entry::isNumber),
checkType(TagMap.Entry.BOOLEAN, entry),
checkValue(value, entry)));
}
Expand All @@ -112,7 +132,8 @@ public void intEntry(int value) {
multiCheck(
checkKey("foo", entry),
checkValue(value, entry),
checkTrue(entry::isNumericPrimitive),
checkIsNumericPrimitive(entry),
checkInstanceOf(Number.class, entry),
checkType(TagMap.Entry.INT, entry)));
}

Expand All @@ -126,7 +147,8 @@ public void intEntry_boxed(int value) {
multiCheck(
checkKey("foo", entry),
checkValue(value, entry),
checkTrue(entry::isNumericPrimitive),
checkIsNumericPrimitive(entry),
checkInstanceOf(Number.class, entry),
checkType(TagMap.Entry.INT, entry)));
}

Expand All @@ -140,7 +162,8 @@ public void anyEntry_int(int value) {
multiCheck(
checkKey("foo", entry),
checkValue(value, entry),
checkTrue(entry::isNumericPrimitive),
checkIsNumericPrimitive(entry),
checkInstanceOf(Number.class, entry),
checkType(TagMap.Entry.INT, entry),
checkValue(value, entry)));
}
Expand Down Expand Up @@ -199,7 +222,7 @@ public void longEntry_boxed(long value) {
multiCheck(
checkKey("foo", entry),
checkValue(value, entry),
checkTrue(entry::isNumericPrimitive),
checkIsNumericPrimitive(entry),
checkType(TagMap.Entry.LONG, entry)));
}

Expand Down Expand Up @@ -228,7 +251,7 @@ public void anyEntry_long(long value) {
multiCheck(
checkKey("foo", entry),
checkValue(value, entry),
checkTrue(entry::isNumericPrimitive),
checkIsNumericPrimitive(entry),
checkTrue(() -> entry.is(TagMap.Entry.LONG)),
checkValue(value, entry)));
}
Expand Down Expand Up @@ -257,7 +280,7 @@ public void floatEntry_boxed(float value) {
multiCheck(
checkKey("foo", entry),
checkValue(value, entry),
checkTrue(entry::isNumericPrimitive),
checkIsNumericPrimitive(entry),
checkType(TagMap.Entry.FLOAT, entry)));
}

Expand Down Expand Up @@ -286,7 +309,7 @@ public void doubleEntry(double value) {
multiCheck(
checkKey("foo", entry),
checkValue(value, entry),
checkTrue(entry::isNumericPrimitive),
checkIsNumericPrimitive(entry),
checkType(TagMap.Entry.DOUBLE, entry)));
}

Expand Down Expand Up @@ -417,6 +440,20 @@ static final Check checkKey(String expected, TagMap.Entry entry) {
return multiCheck(checkEquals(expected, entry::tag), checkEquals(expected, entry::getKey));
}

static final Check checkIsNumericPrimitive(TagMap.Entry entry) {
return multiCheck(
checkTrue(entry::isNumericPrimitive),
checkTrue(entry::isNumber),
checkInstanceOf(Number.class, entry));
}

static final Check checkIsBigNumber(TagMap.Entry entry) {
return multiCheck(
checkFalse(entry::isNumericPrimitive),
checkTrue(entry::isNumber),
checkInstanceOf(Number.class, entry));
}

static final Check checkValue(Object expected, TagMap.Entry entry) {
return multiCheck(
checkEquals(expected, entry::objectValue),
Expand Down Expand Up @@ -468,6 +505,16 @@ static final Check checkValue(double expected, TagMap.Entry entry) {
checkEquals(Double.toString(expected), entry::stringValue));
}

public static Check checkNumber(Number number, TagMap.Entry entry) {
return multiCheck(
checkEquals(number, entry::objectValue),
checkEquals(number.intValue(), entry::intValue),
checkEquals(number.longValue(), entry::longValue),
checkEquals(number.floatValue(), entry::floatValue),
checkEquals(number.doubleValue(), entry::doubleValue),
checkEquals(number.toString(), entry::stringValue));
}

static final Check checkValue(float expected, TagMap.Entry entry) {
return multiCheck(
checkEquals(expected, entry::floatValue),
Expand All @@ -479,6 +526,13 @@ static final Check checkValue(float expected, TagMap.Entry entry) {
checkEquals(Float.toString(expected), entry::stringValue));
}

static final Check checkInstanceOf(Class<?> klass, TagMap.Entry entry) {
return () ->
assertTrue(
klass.isAssignableFrom(entry.objectValue().getClass()),
"instanceof " + klass.getSimpleName());
}

static final Check checkType(byte entryType, TagMap.Entry entry) {
return () -> assertTrue(entry.is(entryType), "type is " + entryType);
}
Expand All @@ -496,23 +550,23 @@ static final Check checkTrue(Supplier<Boolean> actual) {
}

static final Check checkEquals(float expected, Supplier<Float> actual) {
return () -> assertEquals(expected, actual.get(), actual.toString());
return () -> assertEquals(expected, actual.get().floatValue(), actual.toString());
}

static final Check checkEquals(int expected, Supplier<Integer> actual) {
return () -> assertEquals(expected, actual.get(), actual.toString());
return () -> assertEquals(expected, actual.get().intValue(), actual.toString());
}

static final Check checkEquals(double expected, Supplier<Double> actual) {
return () -> assertEquals(expected, actual.get(), actual.toString());
return () -> assertEquals(expected, actual.get().doubleValue(), actual.toString());
}

static final Check checkEquals(long expected, Supplier<Long> actual) {
return () -> assertEquals(expected, actual.get(), actual.toString());
return () -> assertEquals(expected, actual.get().longValue(), actual.toString());
}

static final Check checkEquals(boolean expected, Supplier<Boolean> actual) {
return () -> assertEquals(expected, actual.get(), actual.toString());
return () -> assertEquals(expected, actual.get().booleanValue(), actual.toString());
}

static final Check checkEquals(Object expected, Supplier<Object> actual) {
Expand Down
Loading