File tree Expand file tree Collapse file tree 2 files changed +30
-6
lines changed
micrometer-commons/src/main/java/io/micrometer/common
micrometer-core/src/main/java/io/micrometer/core/instrument Expand file tree Collapse file tree 2 files changed +30
-6
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,11 @@ public final class KeyValues implements Iterable<KeyValue> {
5151 */
5252 private final int length ;
5353
54+ /**
55+ * Cache the hash code of the {@code KeyValues}, 0 value means hash was not computed.
56+ */
57+ private int hash ;
58+
5459 /**
5560 * A constructor that initializes a {@code KeyValues} object with a sorted set of
5661 * key-values and its length.
@@ -291,9 +296,16 @@ public Stream<KeyValue> stream() {
291296
292297 @ Override
293298 public int hashCode () {
294- int result = 1 ;
295- for (int i = 0 ; i < length ; i ++) {
296- result = 31 * result + sortedSet [i ].hashCode ();
299+ int result = hash ;
300+ if (result == 0 ) {
301+ result = 1 ;
302+ for (int i = 0 ; i < length ; i ++) {
303+ result = 31 * result + sortedSet [i ].hashCode ();
304+ }
305+ if (result == 0 ) { // Re-map 0 hash code
306+ result = 1 ;
307+ }
308+ hash = result ;
297309 }
298310 return result ;
299311 }
Original file line number Diff line number Diff line change @@ -48,6 +48,11 @@ public final class Tags implements Iterable<Tag> {
4848 */
4949 private final int length ;
5050
51+ /**
52+ * Cache the hash code of the {@code Tags}, 0 value means hash was not computed.
53+ */
54+ private int hash ;
55+
5156 /**
5257 * A constructor that initializes a {@code Tags} object with a sorted set of tags and
5358 * its length.
@@ -267,9 +272,16 @@ public Stream<Tag> stream() {
267272
268273 @ Override
269274 public int hashCode () {
270- int result = 1 ;
271- for (int i = 0 ; i < length ; i ++) {
272- result = 31 * result + sortedSet [i ].hashCode ();
275+ int result = hash ;
276+ if (result == 0 ) {
277+ result = 1 ;
278+ for (int i = 0 ; i < length ; i ++) {
279+ result = 31 * result + sortedSet [i ].hashCode ();
280+ }
281+ if (result == 0 ) { // Re-map 0 hash code
282+ result = 1 ;
283+ }
284+ hash = result ;
273285 }
274286 return result ;
275287 }
You can’t perform that action at this time.
0 commit comments