1111import org .agrona .concurrent .status .CountersManager ;
1212import org .agrona .concurrent .status .CountersReader ;
1313
14+ /**
15+ * Registry for storing typed properties as Agrona counters. Supports setting properties of any
16+ * primitive type and retrieving them via {@link CountersReader}.
17+ */
1418public class PropertiesRegistry {
1519
1620 public static final int PROPERTY_COUNTER_TYPE_ID = 2 ;
@@ -20,12 +24,24 @@ public class PropertiesRegistry {
2024 private final ThreadLocal <CounterAllocator > counterAllocatorHolder ;
2125 private final Map <String , AtomicCounter > counters = new ConcurrentHashMap <>();
2226
27+ /**
28+ * Creates new registry backed by given {@link CountersManager}.
29+ *
30+ * @param countersManager countersManager
31+ */
2332 public PropertiesRegistry (CountersManager countersManager ) {
2433 this .countersManager = countersManager ;
2534 this .counterAllocatorHolder =
2635 ThreadLocal .withInitial (() -> new CounterAllocator (this .countersManager ));
2736 }
2837
38+ /**
39+ * Stores or updates property. If property does not exist, new counter is allocated, otherwise,
40+ * counter is updated with the new value.
41+ *
42+ * @param name property name
43+ * @param value property value
44+ */
2945 public void put (String name , Object value ) {
3046 Objects .requireNonNull (name , "name" );
3147 Objects .requireNonNull (value , "value" );
@@ -45,41 +61,105 @@ public void put(String name, Object value) {
4561 });
4662 }
4763
64+ /**
65+ * Retrieves {@link Byte} property.
66+ *
67+ * @param countersReader countersReader
68+ * @param name property name
69+ * @return property value, or {@code null} if not found
70+ */
4871 public static Byte getByteProperty (CountersReader countersReader , String name ) {
4972 return getProperty (countersReader , name , Byte ::parseByte );
5073 }
5174
75+ /**
76+ * Retrieves {@link Short} property.
77+ *
78+ * @param countersReader countersReader
79+ * @param name property name
80+ * @return property value, or {@code null} if not found
81+ */
5282 public static Short getShortProperty (CountersReader countersReader , String name ) {
5383 return getProperty (countersReader , name , Short ::parseShort );
5484 }
5585
86+ /**
87+ * Retrieves {@link Integer} property.
88+ *
89+ * @param countersReader countersReader
90+ * @param name property name
91+ * @return property value, or {@code null} if not found
92+ */
5693 public static Integer getIntProperty (CountersReader countersReader , String name ) {
5794 return getProperty (countersReader , name , Integer ::parseInt );
5895 }
5996
97+ /**
98+ * Retrieves {@link Long} property.
99+ *
100+ * @param countersReader countersReader
101+ * @param name property name
102+ * @return property value, or {@code null} if not found
103+ */
60104 public static Long getLongProperty (CountersReader countersReader , String name ) {
61105 return getProperty (countersReader , name , Long ::parseLong );
62106 }
63107
108+ /**
109+ * Retrieves {@link Double} property.
110+ *
111+ * @param countersReader countersReader
112+ * @param name property name
113+ * @return property value, or {@code null} if not found
114+ */
64115 public static Double getDoubleProperty (CountersReader countersReader , String name ) {
65116 return getProperty (countersReader , name , Double ::parseDouble );
66117 }
67118
119+ /**
120+ * Retrieves enum property.
121+ *
122+ * @param countersReader countersReader
123+ * @param name property name
124+ * @return property value, or {@code null} if not found
125+ */
68126 public static <T extends Enum <T >> T getEnumProperty (
69127 CountersReader countersReader , String name , Function <String , T > enumFunc ) {
70128 return getProperty (countersReader , name , enumFunc );
71129 }
72130
131+ /**
132+ * Retrieves {@link String} property.
133+ *
134+ * @param countersReader countersReader
135+ * @param name property name
136+ * @return property value, or {@code null} if not found
137+ */
73138 public static String getProperty (CountersReader countersReader , String name ) {
74139 return getProperty (countersReader , name , s -> s );
75140 }
76141
142+ /**
143+ * Retrieves property from and converts it using provided function.
144+ *
145+ * @param countersReader countersReader
146+ * @param name property name
147+ * @param converter function to convert string value
148+ * @param <T> result type
149+ * @return converted property value, or {@code null} if not found
150+ */
77151 public static <T > T getProperty (
78152 CountersReader countersReader , String name , Function <String , T > converter ) {
79153 final var counter = CounterDescriptor .findFirstCounter (countersReader , byPropertyName (name ));
80154 return counter != null ? converter .apply (counter .label ().split ("=" )[1 ]) : null ;
81155 }
82156
157+ /**
158+ * Returns predicate that matches counters of property type, and with given property name.
159+ *
160+ * @param name property name
161+ * @return predicate for filtering property counters by property name
162+ */
83163 public static Predicate <CounterDescriptor > byPropertyName (String name ) {
84164 return byType (PropertiesRegistry .PROPERTY_COUNTER_TYPE_ID )
85165 .and (descriptor -> name .equals (descriptor .label ().split ("=" )[0 ]));
0 commit comments