2222
2323import java .util .Properties ;
2424
25- /** A properties class with added utility method to extract primitives. */
25+ /**
26+ * A properties class with added utility methods to extract primitives.
27+ *
28+ * <p>Values may be stored as strings via {@link #setProperty(String, String)} or as native Java
29+ * types via {@link #put(Object, Object)} (e.g., when Flink's YAML configuration parser stores
30+ * Integer, Long, or Boolean values directly). The getter methods handle both representations
31+ * transparently.
32+ */
2633@ Public
2734public class MetricConfig extends Properties {
2835
@@ -31,72 +38,108 @@ public String getString(String key, String defaultValue) {
3138 }
3239
3340 /**
34- * Searches for the property with the specified key in this property list. If the key is not
35- * found in this property list, the default property list, and its defaults, recursively, are
36- * then checked. The method returns the default value argument if the property is not found.
41+ * Returns the value associated with the given key as an {@code int}.
42+ *
43+ * <p>If the value is a {@link Number}, its {@code intValue()} is returned directly. Otherwise,
44+ * the value's string representation is parsed via {@link Integer#parseInt(String)}.
3745 *
3846 * @param key the hashtable key.
3947 * @param defaultValue a default value.
40- * @return the value in this property list with the specified key value parsed as an int.
48+ * @return the value in this property list with the specified key value as an int.
4149 */
4250 public int getInteger (String key , int defaultValue ) {
43- String argument = getProperty (key , null );
44- return argument == null ? defaultValue : Integer .parseInt (argument );
51+ final Object value = get (key );
52+ if (value == null ) {
53+ return defaultValue ;
54+ }
55+ if (value instanceof Number ) {
56+ return ((Number ) value ).intValue ();
57+ }
58+ return Integer .parseInt (value .toString ());
4559 }
4660
4761 /**
48- * Searches for the property with the specified key in this property list. If the key is not
49- * found in this property list, the default property list, and its defaults, recursively, are
50- * then checked. The method returns the default value argument if the property is not found.
62+ * Returns the value associated with the given key as a {@code long}.
63+ *
64+ * <p>If the value is a {@link Number}, its {@code longValue()} is returned directly. Otherwise,
65+ * the value's string representation is parsed via {@link Long#parseLong(String)}.
5166 *
5267 * @param key the hashtable key.
5368 * @param defaultValue a default value.
54- * @return the value in this property list with the specified key value parsed as a long.
69+ * @return the value in this property list with the specified key value as a long.
5570 */
5671 public long getLong (String key , long defaultValue ) {
57- String argument = getProperty (key , null );
58- return argument == null ? defaultValue : Long .parseLong (argument );
72+ final Object value = get (key );
73+ if (value == null ) {
74+ return defaultValue ;
75+ }
76+ if (value instanceof Number ) {
77+ return ((Number ) value ).longValue ();
78+ }
79+ return Long .parseLong (value .toString ());
5980 }
6081
6182 /**
62- * Searches for the property with the specified key in this property list. If the key is not
63- * found in this property list, the default property list, and its defaults, recursively, are
64- * then checked. The method returns the default value argument if the property is not found.
83+ * Returns the value associated with the given key as a {@code float}.
84+ *
85+ * <p>If the value is a {@link Number}, its {@code floatValue()} is returned directly.
86+ * Otherwise, the value's string representation is parsed via {@link Float#parseFloat(String)}.
6587 *
6688 * @param key the hashtable key.
6789 * @param defaultValue a default value.
68- * @return the value in this property list with the specified key value parsed as a float.
90+ * @return the value in this property list with the specified key value as a float.
6991 */
7092 public float getFloat (String key , float defaultValue ) {
71- String argument = getProperty (key , null );
72- return argument == null ? defaultValue : Float .parseFloat (argument );
93+ final Object value = get (key );
94+ if (value == null ) {
95+ return defaultValue ;
96+ }
97+ if (value instanceof Number ) {
98+ return ((Number ) value ).floatValue ();
99+ }
100+ return Float .parseFloat (value .toString ());
73101 }
74102
75103 /**
76- * Searches for the property with the specified key in this property list. If the key is not
77- * found in this property list, the default property list, and its defaults, recursively, are
78- * then checked. The method returns the default value argument if the property is not found.
104+ * Returns the value associated with the given key as a {@code double}.
105+ *
106+ * <p>If the value is a {@link Number}, its {@code doubleValue()} is returned directly.
107+ * Otherwise, the value's string representation is parsed via {@link
108+ * Double#parseDouble(String)}.
79109 *
80110 * @param key the hashtable key.
81111 * @param defaultValue a default value.
82- * @return the value in this property list with the specified key value parsed as a double.
112+ * @return the value in this property list with the specified key value as a double.
83113 */
84114 public double getDouble (String key , double defaultValue ) {
85- String argument = getProperty (key , null );
86- return argument == null ? defaultValue : Double .parseDouble (argument );
115+ final Object value = get (key );
116+ if (value == null ) {
117+ return defaultValue ;
118+ }
119+ if (value instanceof Number ) {
120+ return ((Number ) value ).doubleValue ();
121+ }
122+ return Double .parseDouble (value .toString ());
87123 }
88124
89125 /**
90- * Searches for the property with the specified key in this property list. If the key is not
91- * found in this property list, the default property list, and its defaults, recursively, are
92- * then checked. The method returns the default value argument if the property is not found.
126+ * Returns the value associated with the given key as a {@code boolean}.
127+ *
128+ * <p>If the value is a {@link Boolean}, it is returned directly. Otherwise, the value's string
129+ * representation is parsed via {@link Boolean#parseBoolean(String)}.
93130 *
94131 * @param key the hashtable key.
95132 * @param defaultValue a default value.
96- * @return the value in this property list with the specified key value parsed as a boolean.
133+ * @return the value in this property list with the specified key value as a boolean.
97134 */
98135 public boolean getBoolean (String key , boolean defaultValue ) {
99- String argument = getProperty (key , null );
100- return argument == null ? defaultValue : Boolean .parseBoolean (argument );
136+ final Object value = get (key );
137+ if (value == null ) {
138+ return defaultValue ;
139+ }
140+ if (value instanceof Boolean ) {
141+ return (Boolean ) value ;
142+ }
143+ return Boolean .parseBoolean (value .toString ());
101144 }
102145}
0 commit comments