Skip to content

Commit 2658f25

Browse files
KAFKA-19305 Make ClientQuotaImage and TopicImage immutable (#19847)
- Updated `ClientQuotaImage` and `TopicImage` by using `Collections.unmodifiableMap` or `ImmutableMap` to prevent accidental or intentional mutations after construction. Reviewers: Alyssa Huang <[email protected]>, Chia-Ping Tsai <[email protected]>
1 parent daece61 commit 2658f25

File tree

3 files changed

+9
-66
lines changed

3 files changed

+9
-66
lines changed

metadata/src/main/java/org/apache/kafka/image/ClientQuotaImage.java

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,18 @@
3131
import java.util.List;
3232
import java.util.Map;
3333
import java.util.Map.Entry;
34-
import java.util.Objects;
3534

3635

3736
/**
3837
* Represents a quota for a client entity in the metadata image.
3938
*
4039
* This class is thread-safe.
4140
*/
42-
public final class ClientQuotaImage {
41+
public record ClientQuotaImage(Map<String, Double> quotas) {
4342
public static final ClientQuotaImage EMPTY = new ClientQuotaImage(Map.of());
4443

45-
private final Map<String, Double> quotas;
46-
47-
public ClientQuotaImage(Map<String, Double> quotas) {
48-
this.quotas = quotas;
49-
}
50-
51-
Map<String, Double> quotas() {
52-
return quotas;
53-
}
54-
55-
public Map<String, Double> quotaMap() {
56-
return Collections.unmodifiableMap(quotas);
44+
public ClientQuotaImage {
45+
quotas = Collections.unmodifiableMap(quotas);
5746
}
5847

5948
public void write(
@@ -100,17 +89,6 @@ public boolean isEmpty() {
10089
return quotas.isEmpty();
10190
}
10291

103-
@Override
104-
public boolean equals(Object o) {
105-
if (!(o instanceof ClientQuotaImage other)) return false;
106-
return quotas.equals(other.quotas);
107-
}
108-
109-
@Override
110-
public int hashCode() {
111-
return Objects.hash(quotas);
112-
}
113-
11492
@Override
11593
public String toString() {
11694
return new ClientQuotaImageNode(this).stringify();

metadata/src/main/java/org/apache/kafka/image/TopicImage.java

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,19 @@
2424
import org.apache.kafka.image.writer.ImageWriterOptions;
2525
import org.apache.kafka.metadata.PartitionRegistration;
2626

27+
import java.util.Collections;
2728
import java.util.Map;
2829
import java.util.Map.Entry;
29-
import java.util.Objects;
3030

3131

3232
/**
3333
* Represents a topic in the metadata image.
3434
*
3535
* This class is thread-safe.
3636
*/
37-
public final class TopicImage {
38-
private final String name;
39-
40-
private final Uuid id;
41-
42-
private final Map<Integer, PartitionRegistration> partitions;
43-
44-
public TopicImage(String name,
45-
Uuid id,
46-
Map<Integer, PartitionRegistration> partitions) {
47-
this.name = name;
48-
this.id = id;
49-
this.partitions = partitions;
50-
}
51-
52-
public String name() {
53-
return name;
54-
}
55-
56-
public Uuid id() {
57-
return id;
58-
}
59-
60-
public Map<Integer, PartitionRegistration> partitions() {
61-
return partitions;
37+
public record TopicImage(String name, Uuid id, Map<Integer, PartitionRegistration> partitions) {
38+
public TopicImage {
39+
partitions = Collections.unmodifiableMap(partitions);
6240
}
6341

6442
public void write(ImageWriter writer, ImageWriterOptions options) {
@@ -72,19 +50,6 @@ public void write(ImageWriter writer, ImageWriterOptions options) {
7250
}
7351
}
7452

75-
@Override
76-
public boolean equals(Object o) {
77-
if (!(o instanceof TopicImage other)) return false;
78-
return name.equals(other.name) &&
79-
id.equals(other.id) &&
80-
partitions.equals(other.partitions);
81-
}
82-
83-
@Override
84-
public int hashCode() {
85-
return Objects.hash(name, id, partitions);
86-
}
87-
8853
@Override
8954
public String toString() {
9055
return new TopicImageNode(this).stringify();

metadata/src/main/java/org/apache/kafka/image/node/ClientQuotaImageNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public ClientQuotaImageNode(ClientQuotaImage image) {
3434

3535
@Override
3636
public Collection<String> childNames() {
37-
return image.quotaMap().keySet();
37+
return image.quotas().keySet();
3838
}
3939

4040
@Override
4141
public MetadataNode child(String name) {
42-
Double result = image.quotaMap().get(name);
42+
Double result = image.quotas().get(name);
4343
if (result == null) return null;
4444
return new MetadataLeafNode(result + "");
4545
}

0 commit comments

Comments
 (0)