1- // Copyright (c) 2020-2025 Broadcom. All Rights Reserved.
1+ // Copyright (c) 2020-2026 Broadcom. All Rights Reserved.
22// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
33//
44// This software, the RabbitMQ Stream Java client library, is dual-licensed under the
1414// info@rabbitmq.com.
1515package com .rabbitmq .stream .amqp ;
1616
17- public class Symbol {
17+ import java . util . concurrent . ConcurrentHashMap ;
1818
19- // FIXME maintain a cache
19+ public final class Symbol {
20+
21+ private static final int CACHE_MAX_SIZE = 2048 ;
22+ private static final ConcurrentHashMap <String , Symbol > CACHE = new ConcurrentHashMap <>(64 );
2023
2124 private final String value ;
2225
@@ -25,11 +28,40 @@ private Symbol(String value) {
2528 }
2629
2730 public static Symbol valueOf (String value ) {
28- return new Symbol (value );
31+ if (value == null ) {
32+ return null ;
33+ }
34+ Symbol symbol = CACHE .get (value );
35+ if (symbol == null ) {
36+ symbol = new Symbol (value );
37+ if (CACHE .size () < CACHE_MAX_SIZE ) {
38+ Symbol existing = CACHE .putIfAbsent (value , symbol );
39+ if (existing != null ) {
40+ symbol = existing ;
41+ }
42+ }
43+ }
44+ return symbol ;
2945 }
3046
3147 @ Override
3248 public String toString () {
3349 return value ;
3450 }
51+
52+ @ Override
53+ public int hashCode () {
54+ return value .hashCode ();
55+ }
56+
57+ @ Override
58+ public boolean equals (Object o ) {
59+ if (this == o ) {
60+ return true ;
61+ }
62+ if (o == null || getClass () != o .getClass ()) {
63+ return false ;
64+ }
65+ return value .equals (((Symbol ) o ).value );
66+ }
3567}
0 commit comments