Skip to content

Commit ad56046

Browse files
committed
Missing files added
1 parent b7aaa52 commit ad56046

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.jmx.engine.unit;
7+
8+
import java.util.function.Function;
9+
10+
/** This class is responsible for converting a value using provided algorithm. */
11+
public class UnitConverter {
12+
private final Function<Number, Number> convertingFunction;
13+
private final boolean convertToDouble;
14+
15+
/**
16+
* Create an instance of converter
17+
*
18+
* @param convertingFunction an algorithm applied when converting value
19+
* @param convertToDouble indicates of algorithm will return floating point result. This must be
20+
* in-sync with algorithm implementation.
21+
*/
22+
UnitConverter(Function<Number, Number> convertingFunction, boolean convertToDouble) {
23+
this.convertingFunction = convertingFunction;
24+
this.convertToDouble = convertToDouble;
25+
}
26+
27+
public Number convert(Number value) {
28+
return convertingFunction.apply(value);
29+
}
30+
31+
public boolean isConvertingToDouble() {
32+
return convertToDouble;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.jmx.engine.unit;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
import java.util.concurrent.TimeUnit;
11+
import java.util.function.Function;
12+
import javax.annotation.Nullable;
13+
14+
public class UnitConverterFactory {
15+
16+
private static final Map<String, Map<String, UnitConverter>> conversionMappings = new HashMap<>();
17+
18+
static {
19+
registerConverter("ms", "s", value -> value.doubleValue() / TimeUnit.SECONDS.toMillis(1), true);
20+
registerConverter("ns", "s", value -> value.doubleValue() / TimeUnit.SECONDS.toNanos(1), true);
21+
}
22+
23+
private UnitConverterFactory() {}
24+
25+
public static UnitConverter getConverter(@Nullable String fromUnit, @Nullable String toUnit) {
26+
if (fromUnit == null || toUnit == null) {
27+
return null;
28+
}
29+
30+
Map<String, UnitConverter> converters = conversionMappings.get(fromUnit);
31+
if (converters == null) {
32+
return null;
33+
}
34+
35+
return converters.get(toUnit);
36+
}
37+
38+
public static void registerConverter(
39+
String sourceUnit,
40+
String targetUnit,
41+
Function<Number, Number> convertingFunction,
42+
boolean convertToDouble) {
43+
Map<String, UnitConverter> converters =
44+
conversionMappings.computeIfAbsent(sourceUnit, k -> new HashMap<>());
45+
46+
if (converters.containsKey(targetUnit)) {
47+
throw new IllegalArgumentException(
48+
"Converter from " + sourceUnit + " to " + targetUnit + " already registered");
49+
}
50+
converters.put(targetUnit, new UnitConverter(convertingFunction, convertToDouble));
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.jmx.engine.unit;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertFalse;
10+
import static org.junit.jupiter.api.Assertions.assertNull;
11+
import static org.junit.jupiter.api.Assertions.assertTrue;
12+
13+
import java.util.stream.Stream;
14+
import org.junit.jupiter.api.Test;
15+
import org.junit.jupiter.params.ParameterizedTest;
16+
import org.junit.jupiter.params.provider.Arguments;
17+
import org.junit.jupiter.params.provider.CsvSource;
18+
import org.junit.jupiter.params.provider.MethodSource;
19+
20+
class UnitConverterFactoryTest {
21+
22+
@ParameterizedTest
23+
@CsvSource({
24+
"1000000000,ns, 1.0",
25+
"25,ns, 0.000000025",
26+
"96614101945,ns, 96.614101945",
27+
"0,ns, 0",
28+
"1000,ms, 1.0",
29+
"25,ms, 0.025",
30+
"9661410,ms, 9661.41",
31+
"0,ms, 0",
32+
})
33+
void shouldSupportPredefined_to_s_Converters(
34+
Long originalValue, String originalUnit, Double expectedConvertedValue) {
35+
// Given
36+
String targetUnit = "s";
37+
38+
// When
39+
UnitConverter converter = UnitConverterFactory.getConverter(originalUnit, targetUnit);
40+
Number actualValue = converter.convert(originalValue);
41+
42+
// Then
43+
assertEquals(expectedConvertedValue, actualValue);
44+
assertTrue(converter.isConvertingToDouble());
45+
}
46+
47+
@Test
48+
void shouldSupportCustomConverter() {
49+
// Given
50+
String sourceUnit = "MB";
51+
String targetUnit = "By";
52+
53+
// When
54+
UnitConverterFactory.registerConverter(
55+
sourceUnit, targetUnit, (megaBytes) -> megaBytes.doubleValue() * 1024 * 1024, false);
56+
UnitConverter converter = UnitConverterFactory.getConverter(sourceUnit, targetUnit);
57+
Number actualValue = converter.convert(1.5);
58+
59+
// Then
60+
assertEquals(1572864, actualValue.longValue());
61+
assertFalse(converter.isConvertingToDouble());
62+
}
63+
64+
@ParameterizedTest
65+
@MethodSource("provideUnitsForMissingConverter")
66+
void shouldHandleMissingConverter(String sourceUnit, String targetUnit) {
67+
UnitConverter converter = UnitConverterFactory.getConverter(sourceUnit, targetUnit);
68+
assertNull(converter);
69+
}
70+
71+
private static Stream<Arguments> provideUnitsForMissingConverter() {
72+
return Stream.of(
73+
Arguments.of(null, null),
74+
Arguments.of("ms", null),
75+
Arguments.of("ms", ""),
76+
Arguments.of("ms", "--"),
77+
Arguments.of("--", "--"));
78+
}
79+
}

0 commit comments

Comments
 (0)