in spectator-api/src/main/java/com/netflix/spectator/api/ArrayTagSet.java [341:390]
static int merge(String[] dst, String[] srcA, int lengthA, String[] srcB, int lengthB) {
int i = 0;
int ai = 0;
int bi = 0;
while (ai < lengthA && bi < lengthB) {
final String ak = srcA[ai];
final String av = srcA[ai + 1];
String bk = srcB[bi];
String bv = srcB[bi + 1];
int cmp = ak.compareTo(bk);
if (cmp < 0) {
// srcA should already have been deduped as it comes from the tag list
dst[i++] = ak;
dst[i++] = av;
ai += 2;
} else if (cmp > 0) {
// Choose last value for a given key if there are duplicates. It is possible srcB
// will contain duplicates if the user supplied a type like a list.
int j = bi + 2;
for (; j < lengthB && bk.equals(srcB[j]); j += 2) {
bv = srcB[j + 1];
}
dst[i++] = bk;
dst[i++] = bv;
bi = j;
} else {
// Newer tags should override, use source B if there are duplicate keys.
// If source B has duplicates, then use the last value for the given key.
int j = bi + 2;
for (; j < lengthB && ak.equals(srcB[j]); j += 2) {
bk = srcB[j];
bv = srcB[j + 1];
}
dst[i++] = bk;
dst[i++] = bv;
bi = j;
ai += 2; // Ignore
}
}
if (ai < lengthA) {
System.arraycopy(srcA, ai, dst, i, lengthA - ai);
i += lengthA - ai;
} else if (bi < lengthB) {
i = dedup(srcB, bi, dst, i, lengthB - bi);
}
return i;
}