Skip to content

Commit f2b1fc2

Browse files
committed
Remove UniqueId.uniqueIdFormat field
The `UniqueIdFormat` is a singleton instance and not part of the public API, as such is effectively constant. Tracking the format in every `UniqueId` is unnecessary and makes the larger than it needs to be.
1 parent 67e977a commit f2b1fc2

File tree

2 files changed

+9
-16
lines changed

2 files changed

+9
-16
lines changed

junit-platform-engine/src/main/java/org/junit/platform/engine/UniqueId.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
public final class UniqueId implements Cloneable, Serializable {
3939

4040
@Serial
41-
private static final long serialVersionUID = 1L;
41+
private static final long serialVersionUID = 2L;
4242

4343
private static final String ENGINE_SEGMENT_TYPE = "engine";
4444

@@ -79,11 +79,9 @@ public static UniqueId forEngine(String engineId) {
7979
* @see #forEngine(String)
8080
*/
8181
public static UniqueId root(String segmentType, String value) {
82-
return new UniqueId(UniqueIdFormat.getDefault(), new Segment(segmentType, value));
82+
return new UniqueId(new Segment(segmentType, value));
8383
}
8484

85-
private final UniqueIdFormat uniqueIdFormat;
86-
8785
@SuppressWarnings({ "serial", "RedundantSuppression" }) // always used with serializable implementation (List.copyOf())
8886
private final List<Segment> segments;
8987

@@ -93,19 +91,14 @@ public static UniqueId root(String segmentType, String value) {
9391
// lazily computed
9492
private transient @Nullable SoftReference<String> toString;
9593

96-
private UniqueId(UniqueIdFormat uniqueIdFormat, Segment segment) {
97-
this(uniqueIdFormat, List.of(segment));
94+
private UniqueId(Segment segment) {
95+
this(List.of(segment));
9896
}
9997

10098
/**
10199
* Initialize a {@code UniqueId} instance.
102-
*
103-
* @implNote A defensive copy of the segment list is <b>not</b> created by
104-
* this implementation. All callers should immediately drop the reference
105-
* to the list instance that they pass into this constructor.
106100
*/
107-
UniqueId(UniqueIdFormat uniqueIdFormat, List<Segment> segments) {
108-
this.uniqueIdFormat = uniqueIdFormat;
101+
UniqueId(List<Segment> segments) {
109102
this.segments = List.copyOf(segments);
110103
}
111104

@@ -164,7 +157,7 @@ public UniqueId append(Segment segment) {
164157
List<Segment> baseSegments = new ArrayList<>(this.segments.size() + 1);
165158
baseSegments.addAll(this.segments);
166159
baseSegments.add(segment);
167-
return new UniqueId(this.uniqueIdFormat, baseSegments);
160+
return new UniqueId(baseSegments);
168161
}
169162

170163
/**
@@ -215,7 +208,7 @@ public boolean hasPrefix(UniqueId potentialPrefix) {
215208
@API(status = STABLE, since = "1.5")
216209
public UniqueId removeLastSegment() {
217210
Preconditions.condition(this.segments.size() > 1, "Cannot remove last remaining segment");
218-
return new UniqueId(uniqueIdFormat, List.copyOf(this.segments.subList(0, this.segments.size() - 1)));
211+
return new UniqueId(this.segments.subList(0, this.segments.size() - 1));
219212
}
220213

221214
/**
@@ -277,7 +270,7 @@ public String toString() {
277270
SoftReference<String> s = this.toString;
278271
String value = s == null ? null : s.get();
279272
if (value == null) {
280-
value = this.uniqueIdFormat.format(this);
273+
value = UniqueIdFormat.getDefault().format(this);
281274
// this is a benign race like String#hash
282275
// we potentially read and write values from multiple threads
283276
// without a happens-before relationship

junit-platform-engine/src/main/java/org/junit/platform/engine/UniqueIdFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private static String encode(char c) {
8888
UniqueId parse(String source) throws JUnitException {
8989
String[] parts = source.split(String.valueOf(this.segmentDelimiter));
9090
List<Segment> segments = Arrays.stream(parts).map(this::createSegment).toList();
91-
return new UniqueId(this, segments);
91+
return new UniqueId(segments);
9292
}
9393

9494
private Segment createSegment(String segmentString) throws JUnitException {

0 commit comments

Comments
 (0)