Skip to content

Commit 8d735de

Browse files
dougqhclaude
andcommitted
Drop EmptyHolder, make factories final, simplify test-helper instanceof
Post-fold tidy, all TagMap-scoped: - Remove EmptyHolder: with one class there is no interface<->impl class-init cycle to break, and the private constructor reads no statics, so EMPTY is a direct `new TagMap(new Object[1], 0)` initializer. - Static factories (create/fromMap/ledger/...) are now `public static final` (not expressible on the old interface). - assertSize/assertNotEmpty/assertEmpty/checkIntegrity test helpers dropped their now-always-true `instanceof TagMap` guard + redundant cast. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 1e5db48 commit 8d735de

2 files changed

Lines changed: 15 additions & 31 deletions

File tree

internal-api/src/main/java/datadog/trace/api/TagMap.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,39 +46,43 @@
4646
*/
4747
public final class TagMap implements Map<String, Object>, Iterable<TagMap.EntryReader> {
4848
/** Immutable empty TagMap - similar to {@link Collections#emptyMap()} */
49-
public static final TagMap EMPTY = EmptyHolder.EMPTY;
49+
// Frozen view over a length-1 array: bucket masking needs a power-of-two array length (size 0
50+
// would fail with ArrayIndexOutOfBoundsException, size 1 works), and the private constructor
51+
// reads
52+
// no statics, so this is safe to build directly during TagMap's <clinit>.
53+
public static final TagMap EMPTY = new TagMap(new Object[1], 0);
5054

5155
/** Creates a new mutable TagMap that contains the contents of <code>map</code> */
52-
public static TagMap fromMap(Map<String, ?> map) {
56+
public static final TagMap fromMap(Map<String, ?> map) {
5357
TagMap tagMap = TagMap.create(map.size());
5458
tagMap.putAll(map);
5559
return tagMap;
5660
}
5761

5862
/** Creates a new immutable TagMap that contains the contents of <code>map</code> */
59-
public static TagMap fromMapImmutable(Map<String, ?> map) {
63+
public static final TagMap fromMapImmutable(Map<String, ?> map) {
6064
if (map.isEmpty()) {
6165
return TagMap.EMPTY;
6266
} else {
6367
return fromMap(map).freeze();
6468
}
6569
}
6670

67-
public static TagMap create() {
71+
public static final TagMap create() {
6872
return new TagMap();
6973
}
7074

71-
public static TagMap create(int size) {
75+
public static final TagMap create(int size) {
7276
return new TagMap();
7377
}
7478

7579
/** Creates a new TagMap.Ledger */
76-
public static Ledger ledger() {
80+
public static final Ledger ledger() {
7781
return new Ledger();
7882
}
7983

8084
/** Creates a new TagMap.Ledger which handles <code>size</code> modifications before expansion */
81-
public static Ledger ledger(int size) {
85+
public static final Ledger ledger(int size) {
8286
return new Ledger(size);
8387
}
8488

@@ -987,17 +991,6 @@ public EntryChange next() {
987991
* However as a precaution if a BucketGroup becomes completely empty, then that BucketGroup will be
988992
* removed from the collision chain.
989993
*/
990-
// Lazy holder for the shared empty map. EMPTY is initialized from EmptyHolder.EMPTY during
991-
// TagMap's <clinit>; isolating the instance in a holder means its construction is deferred to
992-
// first access and never observes a half-initialized TagMap static (the private constructor
993-
// reads no statics, so the empty view is safe to build during class init).
994-
static final class EmptyHolder {
995-
// Using special constructor that creates a frozen view of an existing array.
996-
// Bucket calculation requires that array length is a power of 2; size 0 fails with
997-
// ArrayIndexOutOfBoundsException, but size 1 works.
998-
static final TagMap EMPTY = new TagMap(new Object[1], 0);
999-
}
1000-
1001994
private final Object[] buckets;
1002995
private int size;
1003996
private boolean frozen;

internal-api/src/test/java/datadog/trace/api/TagMapTest.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,9 +1064,7 @@ static final void assertEntry(String key, String value, TagMap map) {
10641064
}
10651065

10661066
static final void assertSize(int size, TagMap map) {
1067-
if (map instanceof TagMap) {
1068-
assertEquals(size, ((TagMap) map).computeSize());
1069-
}
1067+
assertEquals(size, map.computeSize());
10701068
assertEquals(size, map.size());
10711069

10721070
assertEquals(size, count(map));
@@ -1094,16 +1092,12 @@ static void assertEmptiness(boolean expectEmpty, TagMap map) {
10941092
}
10951093

10961094
static void assertNotEmpty(TagMap map) {
1097-
if (map instanceof TagMap) {
1098-
assertFalse(((TagMap) map).checkIfEmpty());
1099-
}
1095+
assertFalse(map.checkIfEmpty());
11001096
assertFalse(map.isEmpty());
11011097
}
11021098

11031099
static void assertEmpty(TagMap map) {
1104-
if (map instanceof TagMap) {
1105-
assertTrue(((TagMap) map).checkIfEmpty());
1106-
}
1100+
assertTrue(map.checkIfEmpty());
11071101
assertTrue(map.isEmpty());
11081102
}
11091103

@@ -1128,9 +1122,6 @@ static void assertFrozen(Runnable runnable) {
11281122
}
11291123

11301124
static void checkIntegrity(TagMap map) {
1131-
if (map instanceof TagMap) {
1132-
TagMap optMap = (TagMap) map;
1133-
optMap.checkIntegrity();
1134-
}
1125+
map.checkIntegrity();
11351126
}
11361127
}

0 commit comments

Comments
 (0)