diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/DurationGauge.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/DurationGauge.java index 4b217b742a04..a8e4f80679bd 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/DurationGauge.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/DurationGauge.java @@ -36,6 +36,7 @@ public interface DurationGauge extends Metric { /** * {@inheritDoc} */ + @NonNull @Override default MetricType getMetricType() { return MetricType.GAUGE; @@ -44,6 +45,7 @@ default MetricType getMetricType() { /** * {@inheritDoc} */ + @NonNull @Override default DataType getDataType() { return DataType.FLOAT; @@ -52,6 +54,7 @@ default DataType getDataType() { /** * {@inheritDoc} */ + @NonNull @Override default EnumSet getValueTypes() { return EnumSet.of(VALUE); @@ -60,9 +63,10 @@ default EnumSet getValueTypes() { /** * {@inheritDoc} */ + @NonNull @Override default Double get(@NonNull final ValueType valueType) { - Objects.requireNonNull(valueType, "valueType"); + Objects.requireNonNull(valueType, "valueType must not be null"); if (valueType == VALUE) { return get(); } @@ -93,10 +97,9 @@ default Double get(@NonNull final ValueType valueType) { */ final class Config extends PlatformMetricConfig { - private static final String TIME_UNIT = "timeUnit"; private static final String UNSUPPORTED_TIME_UNIT = "Unsupported time unit: "; - private final ChronoUnit timeUnit; + private final @NonNull ChronoUnit timeUnit; /** * Constructor of {@code DoubleGauge.Config} @@ -104,18 +107,29 @@ final class Config extends PlatformMetricConfig { * @param category the kind of metric (metrics are grouped or filtered by this) * @param name a short name for the metric * @param timeUnit the time unit in which to display this duration - * @throws IllegalArgumentException if one of the parameters is {@code null} or consists only of whitespaces + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces */ - public Config(@NonNull final String category, @NonNull final String name, final ChronoUnit timeUnit) { + public Config(@NonNull final String category, @NonNull final String name, final @NonNull ChronoUnit timeUnit) { super(category, name, name, getUnit(timeUnit), getFormat(timeUnit)); this.timeUnit = timeUnit; } + /** + * Constructor of {@code DoubleGauge.Config} + * + * @param category the kind of metric (metrics are grouped or filtered by this) + * @param name a short name for the metric + * @param description metric description + * @param timeUnit the time unit in which to display this duration + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces + */ private Config( @NonNull final String category, @NonNull final String name, @NonNull final String description, - final ChronoUnit timeUnit) { + final @NonNull ChronoUnit timeUnit) { super(category, name, description, getUnit(timeUnit), getFormat(timeUnit)); // at this point, timeUnit was checked for null in getUnit() and getFormat() this.timeUnit = timeUnit; @@ -124,6 +138,7 @@ private Config( /** * {@inheritDoc} */ + @NonNull @Override public DurationGauge.Config withDescription(@NonNull final String description) { return new DurationGauge.Config(getCategory(), getName(), description, getTimeUnit()); @@ -133,6 +148,7 @@ public DurationGauge.Config withDescription(@NonNull final String description) { * The unit of a {@link DurationGauge} depends on the configured {@link ChronoUnit}. Therefore, it is not * possible to specify the unit and this method throws an {@link UnsupportedOperationException} */ + @NonNull @Override public DurationGauge.Config withUnit(@NonNull final String unit) { throw new UnsupportedOperationException("a String unit is not compatible with this class"); @@ -143,6 +159,7 @@ public DurationGauge.Config withUnit(@NonNull final String unit) { * * @return the {@code timeUnit} */ + @NonNull public ChronoUnit getTimeUnit() { return timeUnit; } @@ -150,6 +167,7 @@ public ChronoUnit getTimeUnit() { /** * {@inheritDoc} */ + @NonNull @Override public Class getResultClass() { return DurationGauge.class; @@ -166,7 +184,7 @@ public DurationGauge create(@NonNull final PlatformMetricsFactory factory) { @NonNull private static String getFormat(@NonNull final ChronoUnit timeUnit) { - Objects.requireNonNull(timeUnit, TIME_UNIT); + Objects.requireNonNull(timeUnit, "timeUnit must not be null"); return switch (timeUnit) { case NANOS, MICROS -> FloatFormats.FORMAT_DECIMAL_0; case MILLIS, SECONDS -> FloatFormats.FORMAT_DECIMAL_3; @@ -176,7 +194,7 @@ private static String getFormat(@NonNull final ChronoUnit timeUnit) { @NonNull private static String getUnit(final ChronoUnit timeUnit) { - Objects.requireNonNull(timeUnit, TIME_UNIT); + Objects.requireNonNull(timeUnit, "timeUnit must not be null"); return switch (timeUnit) { case NANOS -> UnitConstants.NANOSECOND_UNIT; case MICROS -> UnitConstants.MICROSECOND_UNIT; diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/FunctionGauge.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/FunctionGauge.java index 724390b3f0ee..4cb90d4b3b9d 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/FunctionGauge.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/FunctionGauge.java @@ -41,6 +41,7 @@ public interface FunctionGauge extends Metric { /** * {@inheritDoc} */ + @NonNull @Override default MetricType getMetricType() { return MetricType.GAUGE; @@ -49,6 +50,7 @@ default MetricType getMetricType() { /** * {@inheritDoc} */ + @NonNull @Override default EnumSet getValueTypes() { return EnumSet.of(VALUE); @@ -57,9 +59,10 @@ default EnumSet getValueTypes() { /** * {@inheritDoc} */ + @NonNull @Override - default T get(final ValueType valueType) { - Objects.requireNonNull(valueType, "valueType"); + default T get(@NonNull final ValueType valueType) { + Objects.requireNonNull(valueType, "valueType must not be null"); if (valueType == VALUE) { return get(); } @@ -94,8 +97,8 @@ final class Config extends PlatformMetricConfig, Config> * the type of the values this {@code FunctionGauge} returns * @param supplier * the {@code Supplier} of the value of this {@code Gauge} - * @throws IllegalArgumentException - * if one of the parameters is {@code null} or consists only of whitespaces + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces */ public Config( @NonNull final String category, @@ -103,10 +106,26 @@ public Config( @NonNull final Class type, @NonNull final Supplier supplier) { super(category, name, "%s"); - this.type = Objects.requireNonNull(type, "type"); - this.supplier = Objects.requireNonNull(supplier, "supplier"); + this.type = Objects.requireNonNull(type, "type must not be null"); + this.supplier = Objects.requireNonNull(supplier, "supplier must not be null"); } + /** + * Constructor of {@code FunctionGauge.Config} + * + * @param category + * the kind of metric (metrics are grouped or filtered by this) + * @param name + * a short name for the metric + * @param description metric description + * @param unit the unit for metric + * @param format the format for metric + * @param type + * the type of the values this {@code FunctionGauge} returns + * @param supplier the format for metric + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces + */ private Config( @NonNull final String category, @NonNull final String name, @@ -116,13 +135,14 @@ private Config( @NonNull final Class type, @NonNull final Supplier supplier) { super(category, name, description, unit, format); - this.type = Objects.requireNonNull(type, "type"); - this.supplier = Objects.requireNonNull(supplier, "supplier"); + this.type = Objects.requireNonNull(type, "type must not be null"); + this.supplier = Objects.requireNonNull(supplier, "supplier must not be null"); } /** * {@inheritDoc} */ + @NonNull @Override public FunctionGauge.Config withDescription(@NonNull final String description) { return new FunctionGauge.Config<>( @@ -132,6 +152,7 @@ public FunctionGauge.Config withDescription(@NonNull final String description /** * {@inheritDoc} */ + @NonNull @Override public FunctionGauge.Config withUnit(@NonNull final String unit) { return new FunctionGauge.Config<>( @@ -176,6 +197,7 @@ public Supplier getSupplier() { /** * {@inheritDoc} */ + @NonNull @SuppressWarnings("unchecked") @Override public Class> getResultClass() { diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/IntegerPairAccumulator.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/IntegerPairAccumulator.java index ea7462d7801d..639bba26706d 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/IntegerPairAccumulator.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/IntegerPairAccumulator.java @@ -47,6 +47,7 @@ public interface IntegerPairAccumulator extends Metric { return ((double) sum) / count; }; + @NonNull @Override default MetricType getMetricType() { return MetricType.ACCUMULATOR; @@ -55,6 +56,7 @@ default MetricType getMetricType() { /** * {@inheritDoc} */ + @NonNull @Override default EnumSet getValueTypes() { return EnumSet.of(VALUE); @@ -63,9 +65,10 @@ default EnumSet getValueTypes() { /** * {@inheritDoc} */ + @NonNull @Override default T get(@NonNull final ValueType valueType) { - Objects.requireNonNull(valueType, "valueType"); + Objects.requireNonNull(valueType, "valueType must not be null"); if (valueType == VALUE) { return get(); } @@ -77,6 +80,7 @@ default T get(@NonNull final ValueType valueType) { * * @return the current value */ + @NonNull T get(); /** @@ -110,15 +114,15 @@ default T get(@NonNull final ValueType valueType) { */ final class Config extends PlatformMetricConfig, Config> { - private final Class type; + private final @NonNull Class type; - private final BiFunction resultFunction; + private final @NonNull BiFunction resultFunction; - private final IntBinaryOperator leftAccumulator; - private final IntBinaryOperator rightAccumulator; + private final @NonNull IntBinaryOperator leftAccumulator; + private final @NonNull IntBinaryOperator rightAccumulator; - private final IntSupplier leftInitializer; - private final IntSupplier rightInitializer; + private final @NonNull IntSupplier leftInitializer; + private final @NonNull IntSupplier rightInitializer; private static final IntSupplier DEFAULT_INITIALIZER = () -> 0; @@ -140,14 +144,33 @@ public Config( @NonNull final BiFunction resultFunction) { super(category, name, "%s"); - this.type = Objects.requireNonNull(type, "type"); - this.resultFunction = Objects.requireNonNull(resultFunction, "resultFunction"); + this.type = Objects.requireNonNull(type, "type must not be null"); + this.resultFunction = Objects.requireNonNull(resultFunction, "resultFunction must not be null"); this.leftAccumulator = Integer::sum; this.rightAccumulator = Integer::sum; this.leftInitializer = DEFAULT_INITIALIZER; this.rightInitializer = DEFAULT_INITIALIZER; } + /** + * Constructor of {@code IntegerPairAccumulator.Config} + *

+ * The accumulators are by default set to {@code Integer::sum}. + * + * @param category the kind of metric (metrics are grouped or filtered by this) + * @param name a short name for the metric + * @param description metric description + * @param unit the unit for metric + * @param format the format for metric + * @param type the type of the values this {@code IntegerPairAccumulator} returns + * @param resultFunction the function that is used to calculate the {@code IntegerAccumulator}'s value. + * @param leftAccumulator the leftAccumulator for metric + * @param rightAccumulator the rightAccumulator for metric + * @param leftInitializer the leftInitializer for metric + * @param rightInitializer the rightInitializer for metric + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces + */ private Config( @NonNull final String category, @NonNull final String name, @@ -163,16 +186,17 @@ private Config( super(category, name, description, unit, format); this.type = Objects.requireNonNull(type, "type"); - this.resultFunction = Objects.requireNonNull(resultFunction, "resultFunction"); - this.leftAccumulator = Objects.requireNonNull(leftAccumulator, "leftAccumulator"); - this.rightAccumulator = Objects.requireNonNull(rightAccumulator, "rightAccumulator"); - this.leftInitializer = Objects.requireNonNull(leftInitializer, "leftInitializer"); - this.rightInitializer = Objects.requireNonNull(rightInitializer, "rightInitializer"); + this.resultFunction = Objects.requireNonNull(resultFunction, "resultFunction must not be null"); + this.leftAccumulator = Objects.requireNonNull(leftAccumulator, "leftAccumulator must not be null"); + this.rightAccumulator = Objects.requireNonNull(rightAccumulator, "rightAccumulator must not be null"); + this.leftInitializer = Objects.requireNonNull(leftInitializer, "leftInitializer must not be null"); + this.rightInitializer = Objects.requireNonNull(rightInitializer, "rightInitializer must not be null"); } /** * {@inheritDoc} */ + @NonNull @Override public IntegerPairAccumulator.Config withDescription(@NonNull final String description) { return new IntegerPairAccumulator.Config<>( @@ -192,6 +216,7 @@ public IntegerPairAccumulator.Config withDescription(@NonNull final String de /** * {@inheritDoc} */ + @NonNull @Override public IntegerPairAccumulator.Config withUnit(@NonNull final String unit) { return new IntegerPairAccumulator.Config<>( @@ -213,7 +238,9 @@ public IntegerPairAccumulator.Config withUnit(@NonNull final String unit) { * * @param format the format-string * @return a new configuration-object with updated {@code format} - * @throws IllegalArgumentException if {@code format} is {@code null} or consists only of whitespaces + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces + * */ @NonNull public IntegerPairAccumulator.Config withFormat(@NonNull final String format) { @@ -427,6 +454,7 @@ public IntegerPairAccumulator.Config withRightInitialValue(final int rightIni /** * {@inheritDoc} */ + @NonNull @SuppressWarnings("unchecked") @Override public Class> getResultClass() { diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/PlatformMetric.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/PlatformMetric.java index 0f230ba0dff7..aba547388c84 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/PlatformMetric.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/PlatformMetric.java @@ -18,6 +18,7 @@ import com.swirlds.common.metrics.statistics.StatsBuffered; import com.swirlds.metrics.api.Metric; +import edu.umd.cs.findbugs.annotations.Nullable; /** * This class is only used to simplify the migration and will be removed afterwards. @@ -35,5 +36,6 @@ public interface PlatformMetric extends Metric { * @return the {@code StatsBuffered}, if there is one, {@code null} otherwise * @deprecated This method is only temporary and will be removed during the Metric overhaul. */ + @Nullable StatsBuffered getStatsBuffered(); } diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/PlatformMetricConfig.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/PlatformMetricConfig.java index 908d31c30873..ab76101c7d32 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/PlatformMetricConfig.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/PlatformMetricConfig.java @@ -68,6 +68,7 @@ protected PlatformMetricConfig( */ public abstract T create(@NonNull final PlatformMetricsFactory factory); + @NonNull @Override public final T create(@NonNull MetricsFactory factory) { if (factory instanceof PlatformMetricsFactory) { diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/PlatformMetricsProvider.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/PlatformMetricsProvider.java index cfb5bd2a475f..7bce251b2323 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/PlatformMetricsProvider.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/PlatformMetricsProvider.java @@ -18,6 +18,7 @@ import com.swirlds.common.platform.NodeId; import com.swirlds.metrics.api.Metrics; +import edu.umd.cs.findbugs.annotations.NonNull; /** * An implementation of this class is responsible for creating {@link Metrics}-implementations. @@ -35,6 +36,7 @@ public interface PlatformMetricsProvider { * * @return the new instance of {@code Metrics} */ + @NonNull Metrics createGlobalMetrics(); /** @@ -44,5 +46,6 @@ public interface PlatformMetricsProvider { * the {@link NodeId} of the platform * @return the new instance of {@code Metrics} */ - Metrics createPlatformMetrics(NodeId selfId); + @NonNull + Metrics createPlatformMetrics(final @NonNull NodeId selfId); } diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/RunningAverageMetric.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/RunningAverageMetric.java index 15f3199669fe..65b75192cb62 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/RunningAverageMetric.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/RunningAverageMetric.java @@ -39,6 +39,7 @@ public interface RunningAverageMetric extends Metric { /** * {@inheritDoc} */ + @NonNull @Override default MetricType getMetricType() { return MetricType.RUNNING_AVERAGE; @@ -47,6 +48,7 @@ default MetricType getMetricType() { /** * {@inheritDoc} */ + @NonNull @Override default DataType getDataType() { return DataType.FLOAT; @@ -55,6 +57,7 @@ default DataType getDataType() { /** * {@inheritDoc} */ + @NonNull default EnumSet getValueTypes() { return EnumSet.of(VALUE, MAX, MIN, STD_DEV); } @@ -62,8 +65,9 @@ default EnumSet getValueTypes() { /** * {@inheritDoc} */ + @NonNull @Override - Double get(final ValueType valueType); + Double get(@NonNull final ValueType valueType); /** * Getter of the {@code halfLife} @@ -116,8 +120,8 @@ final class Config extends PlatformMetricConfig { * the kind of metric (stats are grouped or filtered by this) * @param name * a short name for the statistic - * @throws IllegalArgumentException - * if one of the parameters is {@code null} or consists only of whitespaces + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces */ public Config(@NonNull final String category, @NonNull final String name) { super(category, name, FloatFormats.FORMAT_11_3); @@ -125,6 +129,26 @@ public Config(@NonNull final String category, @NonNull final String name) { this.useDefaultHalfLife = true; } + /** + * Constructor of {@code RunningAverageMetric.Config} + * + * The {@code useDefaultHalfLife} determines whether the default {@code halfLife} value + * (see {@link MetricsConfig#halfLife()}) should be used during the creation of a metric based on + * this configuration. If set to {@code false}, the specific {@code halfLife} defined in this configuration will + * be used instead. + * + * @param category + * the kind of metric (stats are grouped or filtered by this) + * @param name + * a short name for the statistic + * @param description metric description + * @param unit metric unit + * @param format metric format + * @param halfLife metric halfLife + * @param useDefaultHalfLife if a default should be used + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces + */ private Config( @NonNull final String category, @NonNull final String name, @@ -142,6 +166,7 @@ private Config( /** * {@inheritDoc} */ + @NonNull @Override public RunningAverageMetric.Config withDescription(@NonNull final String description) { return new RunningAverageMetric.Config( @@ -157,6 +182,7 @@ public RunningAverageMetric.Config withDescription(@NonNull final String descrip /** * {@inheritDoc} */ + @NonNull @Override public RunningAverageMetric.Config withUnit(@NonNull final String unit) { return new RunningAverageMetric.Config( @@ -175,8 +201,8 @@ public RunningAverageMetric.Config withUnit(@NonNull final String unit) { * @param format * the format-string * @return a new configuration-object with updated {@code format} - * @throws IllegalArgumentException - * if {@code format} is {@code null} or consists only of whitespaces + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces */ @NonNull public RunningAverageMetric.Config withFormat(@NonNull final String format) { @@ -224,6 +250,7 @@ public RunningAverageMetric.Config withHalfLife(final double halfLife) { /** * {@inheritDoc} */ + @NonNull @Override public Class getResultClass() { return RunningAverageMetric.class; diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/SpeedometerMetric.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/SpeedometerMetric.java index 50ccabe79c6d..4c8e9404961d 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/SpeedometerMetric.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/SpeedometerMetric.java @@ -41,6 +41,7 @@ public interface SpeedometerMetric extends Metric { /** * {@inheritDoc} */ + @NonNull @Override default MetricType getMetricType() { return MetricType.SPEEDOMETER; @@ -49,6 +50,7 @@ default MetricType getMetricType() { /** * {@inheritDoc} */ + @NonNull @Override default DataType getDataType() { return DataType.FLOAT; @@ -57,6 +59,7 @@ default DataType getDataType() { /** * {@inheritDoc} */ + @NonNull default EnumSet getValueTypes() { return EnumSet.of(VALUE, MAX, MIN, STD_DEV); } @@ -64,8 +67,9 @@ default EnumSet getValueTypes() { /** * {@inheritDoc} */ + @NonNull @Override - Double get(final ValueType valueType); + Double get(@NonNull final ValueType valueType); /** * Getter of the {@code halfLife} @@ -150,6 +154,7 @@ private Config( /** * {@inheritDoc} */ + @NonNull @Override public SpeedometerMetric.Config withDescription(@NonNull final String description) { return new SpeedometerMetric.Config( @@ -165,6 +170,7 @@ public SpeedometerMetric.Config withDescription(@NonNull final String descriptio /** * {@inheritDoc} */ + @NonNull @Override public SpeedometerMetric.Config withUnit(@NonNull final String unit) { return new SpeedometerMetric.Config( @@ -232,6 +238,7 @@ public SpeedometerMetric.Config withHalfLife(final double halfLife) { /** * {@inheritDoc} */ + @NonNull @Override public Class getResultClass() { return SpeedometerMetric.class; diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/StatEntry.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/StatEntry.java index 754ec6f683c4..273f77b84e5c 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/StatEntry.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/StatEntry.java @@ -45,6 +45,7 @@ public interface StatEntry extends Metric { /** * {@inheritDoc} */ + @NonNull @Override default MetricType getMetricType() { return MetricType.STAT_ENTRY; @@ -53,6 +54,7 @@ default MetricType getMetricType() { /** * {@inheritDoc} */ + @NonNull @Override default EnumSet getValueTypes() { return getBuffered() == null ? EnumSet.of(VALUE) : EnumSet.of(VALUE, MAX, MIN, STD_DEV); @@ -61,9 +63,10 @@ default EnumSet getValueTypes() { /** * {@inheritDoc} */ + @NonNull @Override default Object get(@NonNull final ValueType valueType) { - Objects.requireNonNull(valueType, "valueType"); + Objects.requireNonNull(valueType, "valueType must not be null"); if (getBuffered() == null) { if (valueType == VALUE) { return getStatsStringSupplier().get(); @@ -85,6 +88,7 @@ default Object get(@NonNull final ValueType valueType) { * * @return the {@link StatsBuffered}, if available, otherwise {@code null} */ + @Nullable StatsBuffered getBuffered(); /** @@ -92,6 +96,7 @@ default Object get(@NonNull final ValueType valueType) { * * @return the reset-lambda, if available, otherwise {@code null} */ + @Nullable Consumer getReset(); /** @@ -99,6 +104,7 @@ default Object get(@NonNull final ValueType valueType) { * * @return the lambda */ + @NonNull Supplier getStatsStringSupplier(); /** @@ -107,6 +113,7 @@ default Object get(@NonNull final ValueType valueType) { * * @return the lambda */ + @NonNull Supplier getResetStatsStringSupplier(); /** @@ -116,12 +123,12 @@ default Object get(@NonNull final ValueType valueType) { */ final class Config extends PlatformMetricConfig> { - private final Class type; - private final StatsBuffered buffered; - private final Function init; - private final Consumer reset; - private final Supplier statsStringSupplier; - private final Supplier resetStatsStringSupplier; + private final @NonNull Class type; + private final @Nullable StatsBuffered buffered; + private final @Nullable Function init; + private final @Nullable Consumer reset; + private final @NonNull Supplier statsStringSupplier; + private final @NonNull Supplier resetStatsStringSupplier; private final double halfLife; /** @@ -145,15 +152,30 @@ public Config( @NonNull final Supplier statsStringSupplier) { super(category, name, FloatFormats.FORMAT_11_3); - this.type = Objects.requireNonNull(type, "type"); + this.type = Objects.requireNonNull(type, "type must not be null"); this.buffered = null; this.init = null; this.reset = null; - this.statsStringSupplier = Objects.requireNonNull(statsStringSupplier, "statsStringSupplier"); + this.statsStringSupplier = + Objects.requireNonNull(statsStringSupplier, "statsStringSupplier must not be null"); this.resetStatsStringSupplier = statsStringSupplier; this.halfLife = -1; } + /** + * stores all the parameters, which can be accessed directly + * + * @param category + * the kind of metric (metrics are grouped or filtered by this) + * @param name + * a short name for the metric + * @param type + * the type of the values this {@code StatEntry} returns + * @param statsStringSupplier + * a lambda that returns the metric string + * @throws IllegalArgumentException + * if one of the parameters is {@code null} or consists only of whitespaces + */ @SuppressWarnings("java:S107") private Config( @NonNull final String category, @@ -169,19 +191,21 @@ private Config( @NonNull final Supplier resetStatsStringSupplier, final double halfLife) { super(category, name, description, unit, format); - this.type = Objects.requireNonNull(type, "type"); + this.type = Objects.requireNonNull(type, "type must not be null"); this.buffered = buffered; this.init = init; this.reset = reset; - this.statsStringSupplier = Objects.requireNonNull(statsStringSupplier, "statsStringSupplier"); + this.statsStringSupplier = + Objects.requireNonNull(statsStringSupplier, "statsStringSupplier must not be null"); this.resetStatsStringSupplier = - Objects.requireNonNull(resetStatsStringSupplier, "resetStatsStringSupplier"); + Objects.requireNonNull(resetStatsStringSupplier, "resetStatsStringSupplier must not be null"); this.halfLife = halfLife; } /** * {@inheritDoc} */ + @NonNull @Override public StatEntry.Config withDescription(@NonNull final String description) { return new StatEntry.Config<>( @@ -202,6 +226,7 @@ public StatEntry.Config withDescription(@NonNull final String description) { /** * {@inheritDoc} */ + @NonNull @Override public StatEntry.Config withUnit(@NonNull final String unit) { return new StatEntry.Config<>( @@ -404,6 +429,7 @@ public StatEntry.Config withResetStatsStringSupplier(@NonNull final Supplier< /** * {@inheritDoc} */ + @NonNull @Override public Class getResultClass() { return StatEntry.class; diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/NoOpMetrics.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/NoOpMetrics.java index fab767de4ad0..bc46eb90980d 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/NoOpMetrics.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/NoOpMetrics.java @@ -22,11 +22,14 @@ import com.swirlds.metrics.api.Metric; import com.swirlds.metrics.api.MetricConfig; import com.swirlds.metrics.api.Metrics; +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; /** * A no-op {@link Metrics} implementation. @@ -39,7 +42,7 @@ public class NoOpMetrics implements PlatformMetrics { private final Map> metrics = new HashMap<>(); - private static final NoOpMetricsFactory factory = new NoOpMetricsFactory(); + private static final NoOpMetricsFactory FACTORY = new NoOpMetricsFactory(); @Override public NodeId getNodeId() { @@ -60,7 +63,7 @@ public boolean isPlatformMetrics() { * {@inheritDoc} */ @Override - public synchronized Metric getMetric(final String category, final String name) { + public synchronized @Nullable Metric getMetric(@NonNull final String category, @NonNull final String name) { final Map metricsInCategory = metrics.get(category); if (metricsInCategory == null) { return null; @@ -71,8 +74,9 @@ public synchronized Metric getMetric(final String category, final String name) { /** * {@inheritDoc} */ + @NonNull @Override - public synchronized Collection findMetricsByCategory(final String category) { + public synchronized Collection findMetricsByCategory(@NonNull final String category) { final Map metricsInCategory = metrics.get(category); if (metricsInCategory == null) { return List.of(); @@ -83,6 +87,7 @@ public synchronized Collection findMetricsByCategory(final String catego /** * {@inheritDoc} */ + @NonNull @Override public synchronized Collection getAll() { // Not very efficient, but the no-op metrics doesn't do snapshotting, so this should rarely (if ever) be called. @@ -96,22 +101,25 @@ public synchronized Collection getAll() { /** * {@inheritDoc} */ + @NonNull @SuppressWarnings("unchecked") @Override - public synchronized T getOrCreate(final MetricConfig config) { - + public synchronized T getOrCreate(@NonNull final MetricConfig config) { + Objects.requireNonNull(config, "config must not be null"); final String category = config.getCategory(); final String name = config.getName(); final Map metricsInCategory = metrics.computeIfAbsent(category, k -> new HashMap<>()); - return (T) metricsInCategory.computeIfAbsent(name, k -> factory.createMetric(config)); + return (T) metricsInCategory.computeIfAbsent(name, k -> FACTORY.createMetric(config)); } /** * {@inheritDoc} */ @Override - public synchronized void remove(final String category, final String name) { + public synchronized void remove(@NonNull final String category, @NonNull final String name) { + Objects.requireNonNull(category, "category must not be null"); + Objects.requireNonNull(name, "name must not be null"); final Map metricsInCategory = metrics.get(category); if (metricsInCategory == null) { @@ -129,7 +137,8 @@ public synchronized void remove(final String category, final String name) { * {@inheritDoc} */ @Override - public void remove(final Metric metric) { + public void remove(@NonNull final Metric metric) { + Objects.requireNonNull(metric, "metric must not be null"); remove(metric.getCategory(), metric.getName()); } @@ -137,7 +146,8 @@ public void remove(final Metric metric) { * {@inheritDoc} */ @Override - public void remove(final MetricConfig config) { + public void remove(@NonNull final MetricConfig config) { + Objects.requireNonNull(config, "config must not be null"); remove(config.getCategory(), config.getName()); } @@ -145,7 +155,8 @@ public void remove(final MetricConfig config) { * {@inheritDoc} */ @Override - public void addUpdater(final Runnable updater) { + public void addUpdater(@NonNull final Runnable updater) { + Objects.requireNonNull(updater, "updater must not be null"); // Intentional no-op } @@ -153,7 +164,8 @@ public void addUpdater(final Runnable updater) { * {@inheritDoc} */ @Override - public void removeUpdater(final Runnable updater) { + public void removeUpdater(@NonNull final Runnable updater) { + Objects.requireNonNull(updater, "updater must not be null"); // Intentional no-op } diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/AbstractNoOpMetric.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/AbstractNoOpMetric.java index 4bbb576fe733..b14223b0f4fc 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/AbstractNoOpMetric.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/AbstractNoOpMetric.java @@ -20,6 +20,7 @@ import com.swirlds.common.metrics.statistics.StatsBuffered; import com.swirlds.metrics.api.Metric; import com.swirlds.metrics.api.MetricConfig; +import edu.umd.cs.findbugs.annotations.NonNull; import java.util.EnumSet; /** @@ -36,6 +37,7 @@ protected AbstractNoOpMetric(final MetricConfig config) { /** * {@inheritDoc} */ + @NonNull @Override public String getCategory() { return config.getCategory(); @@ -44,6 +46,7 @@ public String getCategory() { /** * {@inheritDoc} */ + @NonNull @Override public String getName() { return config.getName(); @@ -52,6 +55,7 @@ public String getName() { /** * {@inheritDoc} */ + @NonNull @Override public String getDescription() { return config.getDescription(); @@ -60,6 +64,7 @@ public String getDescription() { /** * {@inheritDoc} */ + @NonNull @Override public String getUnit() { return config.getUnit(); @@ -68,6 +73,7 @@ public String getUnit() { /** * {@inheritDoc} */ + @NonNull @Override public String getFormat() { return config.getFormat(); @@ -76,6 +82,7 @@ public String getFormat() { /** * {@inheritDoc} */ + @NonNull @Override public EnumSet getValueTypes() { return EnumSet.noneOf(ValueType.class); @@ -92,6 +99,7 @@ public void reset() { /** * {@inheritDoc} */ + @NonNull @Override public StatsBuffered getStatsBuffered() { return new NoOpStatsBuffered(); diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpCounter.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpCounter.java index cea48cfce62f..2df6f6c896f8 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpCounter.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpCounter.java @@ -18,13 +18,14 @@ import com.swirlds.metrics.api.Counter; import com.swirlds.metrics.api.MetricConfig; +import edu.umd.cs.findbugs.annotations.NonNull; /** * A no-op counter. */ public class NoOpCounter extends AbstractNoOpMetric implements Counter { - public NoOpCounter(final MetricConfig config) { + public NoOpCounter(final @NonNull MetricConfig config) { super(config); } diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpDoubleAccumulator.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpDoubleAccumulator.java index 993545d164a9..707554344f27 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpDoubleAccumulator.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpDoubleAccumulator.java @@ -18,13 +18,14 @@ import com.swirlds.metrics.api.DoubleAccumulator; import com.swirlds.metrics.api.MetricConfig; +import edu.umd.cs.findbugs.annotations.NonNull; /** * A no-op double accumulator. */ public class NoOpDoubleAccumulator extends AbstractNoOpMetric implements DoubleAccumulator { - public NoOpDoubleAccumulator(final MetricConfig config) { + public NoOpDoubleAccumulator(final @NonNull MetricConfig config) { super(config); } diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpDoubleGauge.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpDoubleGauge.java index 769f45ee5d6e..ea892ab7aa8e 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpDoubleGauge.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpDoubleGauge.java @@ -18,13 +18,14 @@ import com.swirlds.metrics.api.DoubleGauge; import com.swirlds.metrics.api.MetricConfig; +import edu.umd.cs.findbugs.annotations.NonNull; /** * A no-op implementation of a double gauge. */ public class NoOpDoubleGauge extends AbstractNoOpMetric implements DoubleGauge { - public NoOpDoubleGauge(final MetricConfig config) { + public NoOpDoubleGauge(final @NonNull MetricConfig config) { super(config); } diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpDurationGauge.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpDurationGauge.java index 2f9e63253e95..1897845c3b11 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpDurationGauge.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpDurationGauge.java @@ -18,6 +18,7 @@ import com.swirlds.common.metrics.DurationGauge; import com.swirlds.metrics.api.MetricConfig; +import edu.umd.cs.findbugs.annotations.NonNull; import java.time.Duration; /** @@ -25,7 +26,7 @@ */ public class NoOpDurationGauge extends AbstractNoOpMetric implements DurationGauge { - public NoOpDurationGauge(final MetricConfig config) { + public NoOpDurationGauge(final @NonNull MetricConfig config) { super(config); } diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpFunctionGauge.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpFunctionGauge.java index 6c2cac4f40a5..27249adc3b66 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpFunctionGauge.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpFunctionGauge.java @@ -18,6 +18,7 @@ import com.swirlds.common.metrics.FunctionGauge; import com.swirlds.metrics.api.MetricConfig; +import edu.umd.cs.findbugs.annotations.NonNull; /** * A no-op implementation of a function gauge. @@ -29,7 +30,7 @@ public class NoOpFunctionGauge extends AbstractNoOpMetric implements Function private final T value; - public NoOpFunctionGauge(final MetricConfig config, final T value) { + public NoOpFunctionGauge(final @NonNull MetricConfig config, final @NonNull T value) { super(config); this.value = value; } @@ -38,6 +39,7 @@ public NoOpFunctionGauge(final MetricConfig config, final T value) { * {@inheritDoc} */ @Override + @NonNull public T get() { return value; } @@ -45,6 +47,7 @@ public T get() { /** * {@inheritDoc} */ + @NonNull @Override public DataType getDataType() { return DataType.INT; diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpIntegerPairAccumulator.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpIntegerPairAccumulator.java index 1f68064a96a9..7686c0cf596a 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpIntegerPairAccumulator.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpIntegerPairAccumulator.java @@ -18,15 +18,16 @@ import com.swirlds.common.metrics.IntegerPairAccumulator; import com.swirlds.metrics.api.MetricConfig; +import edu.umd.cs.findbugs.annotations.NonNull; /** * A no-op implementation of an integer pair accumulator. */ public class NoOpIntegerPairAccumulator extends AbstractNoOpMetric implements IntegerPairAccumulator { - private final T value; + private final @NonNull T value; - public NoOpIntegerPairAccumulator(final MetricConfig config, final T value) { + public NoOpIntegerPairAccumulator(final @NonNull MetricConfig config, final @NonNull T value) { super(config); this.value = value; } @@ -34,6 +35,7 @@ public NoOpIntegerPairAccumulator(final MetricConfig config, final T value /** * {@inheritDoc} */ + @NonNull @Override public T get() { return value; @@ -64,6 +66,7 @@ public void update(final int leftValue, final int rightValue) {} /** * {@inheritDoc} */ + @NonNull @Override public DataType getDataType() { return DataType.INT; diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpMetricsFactory.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpMetricsFactory.java index f5574dc7d1db..43dcc261c915 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpMetricsFactory.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpMetricsFactory.java @@ -30,6 +30,7 @@ import com.swirlds.metrics.api.IntegerGauge; import com.swirlds.metrics.api.LongAccumulator; import com.swirlds.metrics.api.LongGauge; +import edu.umd.cs.findbugs.annotations.NonNull; /** * Builds no-op metrics. @@ -40,7 +41,7 @@ public class NoOpMetricsFactory implements PlatformMetricsFactory { * {@inheritDoc} */ @Override - public Counter createCounter(final Counter.Config config) { + public @NonNull Counter createCounter(final @NonNull Counter.Config config) { return new NoOpCounter(config); } @@ -48,7 +49,7 @@ public Counter createCounter(final Counter.Config config) { * {@inheritDoc} */ @Override - public DoubleAccumulator createDoubleAccumulator(final DoubleAccumulator.Config config) { + public @NonNull DoubleAccumulator createDoubleAccumulator(final @NonNull DoubleAccumulator.Config config) { return new NoOpDoubleAccumulator(config); } @@ -56,7 +57,7 @@ public DoubleAccumulator createDoubleAccumulator(final DoubleAccumulator.Config * {@inheritDoc} */ @Override - public DoubleGauge createDoubleGauge(final DoubleGauge.Config config) { + public @NonNull DoubleGauge createDoubleGauge(final @NonNull DoubleGauge.Config config) { return new NoOpDoubleGauge(config); } @@ -64,7 +65,7 @@ public DoubleGauge createDoubleGauge(final DoubleGauge.Config config) { * {@inheritDoc} */ @Override - public DurationGauge createDurationGauge(final DurationGauge.Config config) { + public @NonNull DurationGauge createDurationGauge(final @NonNull DurationGauge.Config config) { return new NoOpDurationGauge(config); } @@ -72,7 +73,7 @@ public DurationGauge createDurationGauge(final DurationGauge.Config config) { * {@inheritDoc} */ @Override - public FunctionGauge createFunctionGauge(final FunctionGauge.Config config) { + public @NonNull FunctionGauge createFunctionGauge(final @NonNull FunctionGauge.Config config) { return new NoOpFunctionGauge<>(config, config.getSupplier().get()); } @@ -80,7 +81,7 @@ public FunctionGauge createFunctionGauge(final FunctionGauge.Config co * {@inheritDoc} */ @Override - public IntegerAccumulator createIntegerAccumulator(final IntegerAccumulator.Config config) { + public @NonNull IntegerAccumulator createIntegerAccumulator(final @NonNull IntegerAccumulator.Config config) { return new NoOpIntegerAccumulator(config); } @@ -88,7 +89,7 @@ public IntegerAccumulator createIntegerAccumulator(final IntegerAccumulator.Conf * {@inheritDoc} */ @Override - public IntegerGauge createIntegerGauge(final IntegerGauge.Config config) { + public @NonNull IntegerGauge createIntegerGauge(final @NonNull IntegerGauge.Config config) { return new NoOpIntegerGauge(config); } @@ -96,7 +97,8 @@ public IntegerGauge createIntegerGauge(final IntegerGauge.Config config) { * {@inheritDoc} */ @Override - public IntegerPairAccumulator createIntegerPairAccumulator(final IntegerPairAccumulator.Config config) { + public @NonNull IntegerPairAccumulator createIntegerPairAccumulator( + final @NonNull IntegerPairAccumulator.Config config) { return new NoOpIntegerPairAccumulator<>( config, config.getResultFunction().apply(0, 0)); } @@ -105,7 +107,7 @@ public IntegerPairAccumulator createIntegerPairAccumulator(final IntegerP * {@inheritDoc} */ @Override - public LongAccumulator createLongAccumulator(final LongAccumulator.Config config) { + public @NonNull LongAccumulator createLongAccumulator(final @NonNull LongAccumulator.Config config) { return new NoOpLongAccumulator(config); } @@ -113,7 +115,7 @@ public LongAccumulator createLongAccumulator(final LongAccumulator.Config config * {@inheritDoc} */ @Override - public LongGauge createLongGauge(final LongGauge.Config config) { + public @NonNull LongGauge createLongGauge(final @NonNull LongGauge.Config config) { return new NoOpLongGauge(config); } @@ -121,7 +123,7 @@ public LongGauge createLongGauge(final LongGauge.Config config) { * {@inheritDoc} */ @Override - public RunningAverageMetric createRunningAverageMetric(final RunningAverageMetric.Config config) { + public @NonNull RunningAverageMetric createRunningAverageMetric(final @NonNull RunningAverageMetric.Config config) { return new NoOpRunningAverageMetric(config); } @@ -129,7 +131,7 @@ public RunningAverageMetric createRunningAverageMetric(final RunningAverageMetri * {@inheritDoc} */ @Override - public SpeedometerMetric createSpeedometerMetric(final SpeedometerMetric.Config config) { + public @NonNull SpeedometerMetric createSpeedometerMetric(final @NonNull SpeedometerMetric.Config config) { return new NoOpSpeedometerMetric(config); } @@ -137,7 +139,7 @@ public SpeedometerMetric createSpeedometerMetric(final SpeedometerMetric.Config * {@inheritDoc} */ @Override - public StatEntry createStatEntry(final StatEntry.Config config) { + public @NonNull StatEntry createStatEntry(final @NonNull StatEntry.Config config) { return new NoOpStatEntry(config); } } diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpRunningAverageMetric.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpRunningAverageMetric.java index 9b181561fdde..5c23e1115963 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpRunningAverageMetric.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpRunningAverageMetric.java @@ -18,21 +18,23 @@ import com.swirlds.common.metrics.RunningAverageMetric; import com.swirlds.metrics.api.MetricConfig; +import edu.umd.cs.findbugs.annotations.NonNull; /** * A no-op implementation of a running average metric. */ public class NoOpRunningAverageMetric extends AbstractNoOpMetric implements RunningAverageMetric { - public NoOpRunningAverageMetric(final MetricConfig config) { + public NoOpRunningAverageMetric(final @NonNull MetricConfig config) { super(config); } /** * {@inheritDoc} */ + @NonNull @Override - public Double get(final ValueType valueType) { + public Double get(@NonNull final ValueType valueType) { return 0.0; } diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpSpeedometerMetric.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpSpeedometerMetric.java index 298f36e46470..fba84fdfdbb5 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpSpeedometerMetric.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpSpeedometerMetric.java @@ -18,21 +18,23 @@ import com.swirlds.common.metrics.SpeedometerMetric; import com.swirlds.metrics.api.MetricConfig; +import edu.umd.cs.findbugs.annotations.NonNull; /** * A no-op implementation of a speedometer metric. */ public class NoOpSpeedometerMetric extends AbstractNoOpMetric implements SpeedometerMetric { - public NoOpSpeedometerMetric(final MetricConfig config) { + public NoOpSpeedometerMetric(final @NonNull MetricConfig config) { super(config); } /** * {@inheritDoc} */ + @NonNull @Override - public Double get(final ValueType valueType) { + public Double get(final @NonNull ValueType valueType) { return 0.0; } diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpStatEntry.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpStatEntry.java index bf0574e9e94f..0dc81f516acd 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpStatEntry.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/noop/internal/NoOpStatEntry.java @@ -21,6 +21,7 @@ import com.swirlds.common.metrics.StatEntry; import com.swirlds.common.metrics.statistics.StatsBuffered; import com.swirlds.metrics.api.MetricConfig; +import edu.umd.cs.findbugs.annotations.NonNull; import java.util.function.Consumer; import java.util.function.Supplier; @@ -29,13 +30,14 @@ */ public class NoOpStatEntry extends AbstractNoOpMetric implements StatEntry { - public NoOpStatEntry(final MetricConfig config) { + public NoOpStatEntry(final @NonNull MetricConfig config) { super(config); } /** * {@inheritDoc} */ + @NonNull @Override public DataType getDataType() { return INT; @@ -45,6 +47,7 @@ public DataType getDataType() { * {@inheritDoc} */ @Override + @NonNull public StatsBuffered getBuffered() { return new NoOpStatsBuffered(); } @@ -53,6 +56,7 @@ public StatsBuffered getBuffered() { * {@inheritDoc} */ @Override + @NonNull public Consumer getReset() { return x -> {}; } @@ -60,6 +64,7 @@ public Consumer getReset() { /** * {@inheritDoc} */ + @NonNull @Override public Supplier getStatsStringSupplier() { return () -> ""; @@ -68,6 +73,7 @@ public Supplier getStatsStringSupplier() { /** * {@inheritDoc} */ + @NonNull @Override public Supplier getResetStatsStringSupplier() { return () -> ""; diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/AbstractDistributionMetric.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/AbstractDistributionMetric.java index a8beea5dc6ac..562a7d2efdfd 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/AbstractDistributionMetric.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/AbstractDistributionMetric.java @@ -19,6 +19,7 @@ import com.swirlds.common.metrics.platform.Snapshot.SnapshotEntry; import com.swirlds.common.metrics.statistics.StatsBuffered; import com.swirlds.metrics.api.MetricConfig; +import edu.umd.cs.findbugs.annotations.NonNull; import java.util.List; import java.util.Objects; @@ -56,8 +57,9 @@ public double getHalfLife() { /** * {@inheritDoc} */ + @NonNull @Override - public Double get(final ValueType valueType) { + public Double get(@NonNull final ValueType valueType) { Objects.requireNonNull(valueType, "valueType must not be null"); return switch (valueType) { case VALUE -> get(); @@ -71,6 +73,7 @@ public Double get(final ValueType valueType) { /** * {@inheritDoc} */ + @NonNull @Override public List takeSnapshot() { final StatsBuffered statsBuffered = getStatsBuffered(); diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultCounter.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultCounter.java index 2ce1f0c9728a..d5fe0a754440 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultCounter.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultCounter.java @@ -21,6 +21,7 @@ import com.swirlds.base.utility.ToStringBuilder; import com.swirlds.common.metrics.platform.Snapshot.SnapshotEntry; import com.swirlds.metrics.api.Counter; +import edu.umd.cs.findbugs.annotations.NonNull; import java.util.List; import java.util.concurrent.atomic.LongAdder; @@ -40,6 +41,7 @@ public DefaultCounter(final Counter.Config config) { /** * {@inheritDoc} */ + @NonNull @Override public List takeSnapshot() { return List.of(new SnapshotEntry(VALUE, get())); diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultDoubleAccumulator.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultDoubleAccumulator.java index 3d504cf6c5d9..ec78040c7afb 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultDoubleAccumulator.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultDoubleAccumulator.java @@ -32,9 +32,9 @@ */ public class DefaultDoubleAccumulator extends DefaultMetric implements DoubleAccumulator { - private final AtomicDouble container; - private final DoubleBinaryOperator accumulator; - private final DoubleSupplier initializer; + private final @NonNull AtomicDouble container; + private final @NonNull DoubleBinaryOperator accumulator; + private final @NonNull DoubleSupplier initializer; public DefaultDoubleAccumulator(@NonNull final Config config) { super(config); @@ -58,6 +58,7 @@ public double getInitialValue() { * {@inheritDoc} */ @Override + @NonNull public List takeSnapshot() { return List.of(new SnapshotEntry(VALUE, container.getAndSet(initializer.getAsDouble()))); } diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultDoubleGauge.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultDoubleGauge.java index 6b259ff06a04..52b2cdda9e39 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultDoubleGauge.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultDoubleGauge.java @@ -22,6 +22,7 @@ import com.swirlds.common.metrics.platform.Snapshot.SnapshotEntry; import com.swirlds.common.threading.atomic.AtomicDouble; import com.swirlds.metrics.api.DoubleGauge; +import edu.umd.cs.findbugs.annotations.NonNull; import java.util.List; /** @@ -39,6 +40,7 @@ public DefaultDoubleGauge(final DoubleGauge.Config config) { /** * {@inheritDoc} */ + @NonNull @Override public List takeSnapshot() { return List.of(new SnapshotEntry(VALUE, get())); diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultDurationGauge.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultDurationGauge.java index 4d5b27038c81..dfc26f154f55 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultDurationGauge.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultDurationGauge.java @@ -19,6 +19,7 @@ import static com.swirlds.metrics.api.Metric.ValueType.VALUE; import com.swirlds.common.metrics.DurationGauge; +import edu.umd.cs.findbugs.annotations.NonNull; import java.time.Duration; import java.time.temporal.ChronoUnit; import java.util.List; @@ -40,6 +41,7 @@ public DefaultDurationGauge(final DurationGauge.Config config) { /** * {@inheritDoc} */ + @NonNull @Override public List takeSnapshot() { return List.of(new Snapshot.SnapshotEntry(VALUE, get())); diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultFunctionGauge.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultFunctionGauge.java index b6e2bc7568df..be70cda11c9e 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultFunctionGauge.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultFunctionGauge.java @@ -22,6 +22,7 @@ import com.swirlds.common.metrics.FunctionGauge; import com.swirlds.common.metrics.platform.Snapshot.SnapshotEntry; import com.swirlds.metrics.api.MetricConfig; +import edu.umd.cs.findbugs.annotations.NonNull; import java.util.List; import java.util.function.Supplier; @@ -42,6 +43,7 @@ public DefaultFunctionGauge(final FunctionGauge.Config config) { /** * {@inheritDoc} */ + @NonNull @Override public DataType getDataType() { return dataType; @@ -50,6 +52,7 @@ public DataType getDataType() { /** * {@inheritDoc} */ + @NonNull @Override public List takeSnapshot() { return List.of(new SnapshotEntry(VALUE, get())); diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultIntegerAccumulator.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultIntegerAccumulator.java index 8c306ecdd458..da7e81f64b61 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultIntegerAccumulator.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultIntegerAccumulator.java @@ -21,6 +21,7 @@ import com.swirlds.base.utility.ToStringBuilder; import com.swirlds.common.metrics.platform.Snapshot.SnapshotEntry; import com.swirlds.metrics.api.IntegerAccumulator; +import edu.umd.cs.findbugs.annotations.NonNull; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.IntBinaryOperator; @@ -54,6 +55,7 @@ public int getInitialValue() { /** * {@inheritDoc} */ + @NonNull @Override public List takeSnapshot() { return List.of(new SnapshotEntry(VALUE, container.getAndSet(initializer.getAsInt()))); diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultIntegerGauge.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultIntegerGauge.java index a6a379a71b2a..d06189eac45c 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultIntegerGauge.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultIntegerGauge.java @@ -21,6 +21,7 @@ import com.swirlds.base.utility.ToStringBuilder; import com.swirlds.common.metrics.platform.Snapshot.SnapshotEntry; import com.swirlds.metrics.api.IntegerGauge; +import edu.umd.cs.findbugs.annotations.NonNull; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; @@ -39,6 +40,7 @@ public DefaultIntegerGauge(final IntegerGauge.Config config) { /** * {@inheritDoc} */ + @NonNull @Override public List takeSnapshot() { return List.of(new SnapshotEntry(VALUE, get())); diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultIntegerPairAccumulator.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultIntegerPairAccumulator.java index 2fd40e5a6f91..bd84d7208cee 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultIntegerPairAccumulator.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultIntegerPairAccumulator.java @@ -23,6 +23,7 @@ import com.swirlds.common.metrics.platform.Snapshot.SnapshotEntry; import com.swirlds.common.threading.atomic.AtomicIntPair; import com.swirlds.metrics.api.MetricConfig; +import edu.umd.cs.findbugs.annotations.NonNull; import java.util.List; import java.util.function.BiFunction; import java.util.function.IntSupplier; @@ -52,6 +53,7 @@ public DefaultIntegerPairAccumulator(final Config config) { /** * {@inheritDoc} */ + @NonNull @Override public DataType getDataType() { return dataType; @@ -60,6 +62,7 @@ public DataType getDataType() { /** * {@inheritDoc} */ + @NonNull @Override public List takeSnapshot() { final T result = @@ -70,6 +73,7 @@ public List takeSnapshot() { /** * {@inheritDoc} */ + @NonNull @Override public T get() { return container.compute(resultFunction); diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultLongAccumulator.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultLongAccumulator.java index 348f43bc57d0..66e37bd4b725 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultLongAccumulator.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultLongAccumulator.java @@ -21,6 +21,7 @@ import com.swirlds.base.utility.ToStringBuilder; import com.swirlds.common.metrics.platform.Snapshot.SnapshotEntry; import com.swirlds.metrics.api.LongAccumulator; +import edu.umd.cs.findbugs.annotations.NonNull; import java.util.List; import java.util.concurrent.atomic.AtomicLong; import java.util.function.LongBinaryOperator; @@ -56,6 +57,7 @@ public long getInitialValue() { /** * {@inheritDoc} */ + @NonNull @Override public List takeSnapshot() { return List.of(new SnapshotEntry(VALUE, container.getAndSet(initializer.getAsLong()))); diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultLongGauge.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultLongGauge.java index 869de86e1ccc..e0cfd48b62ff 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultLongGauge.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultLongGauge.java @@ -21,6 +21,7 @@ import com.swirlds.base.utility.ToStringBuilder; import com.swirlds.common.metrics.platform.Snapshot.SnapshotEntry; import com.swirlds.metrics.api.LongGauge; +import edu.umd.cs.findbugs.annotations.NonNull; import java.util.List; import java.util.concurrent.atomic.AtomicLong; @@ -39,6 +40,7 @@ public DefaultLongGauge(final Config config) { /** * {@inheritDoc} */ + @NonNull @Override public List takeSnapshot() { return List.of(new SnapshotEntry(VALUE, get())); diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultMetric.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultMetric.java index 1aa7c664a75b..821660ead4c1 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultMetric.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultMetric.java @@ -22,6 +22,8 @@ import com.swirlds.common.metrics.statistics.StatsBuffered; import com.swirlds.metrics.api.Metric; import com.swirlds.metrics.api.MetricConfig; +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; import java.util.List; import java.util.Objects; @@ -48,6 +50,7 @@ public abstract class DefaultMetric implements Metric, PlatformMetric { /** * {@inheritDoc} */ + @NonNull @Override public String getCategory() { return category; @@ -56,6 +59,7 @@ public String getCategory() { /** * {@inheritDoc} */ + @NonNull @Override public String getName() { return name; @@ -65,11 +69,13 @@ public String getName() { * {@inheritDoc} */ @Override + @NonNull public String getDescription() { return description; } @Override + @NonNull public String getUnit() { return unit; } @@ -78,6 +84,7 @@ public String getUnit() { * {@inheritDoc} */ @Override + @NonNull public String getFormat() { return format; } @@ -89,7 +96,7 @@ public String getFormat() { * * @return the list of {@code ValueTypes} with their current values */ - public abstract List takeSnapshot(); + public abstract @NonNull List takeSnapshot(); /** * {@inheritDoc} @@ -102,6 +109,7 @@ public void reset() { /** * {@inheritDoc} */ + @Nullable @SuppressWarnings("removal") @Override public StatsBuffered getStatsBuffered() { diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultMetrics.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultMetrics.java index 2f096664c24b..214684d4016e 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultMetrics.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultMetrics.java @@ -26,6 +26,8 @@ import com.swirlds.metrics.api.Metric; import com.swirlds.metrics.api.MetricConfig; import com.swirlds.metrics.api.Metrics; +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; import java.util.Collection; import java.util.Collections; import java.util.Map; @@ -50,57 +52,52 @@ public class DefaultMetrics implements PlatformMetrics { public static final int EXCEPTION_RATE_THRESHOLD = 10; // A reference to the NodeId of the current node - private final NodeId selfId; + private final @Nullable NodeId selfId; // The MetricKeyRegistry ensures that no two conflicting metrics with the same key exist - private final MetricKeyRegistry metricKeyRegistry; + private final @NonNull MetricKeyRegistry metricKeyRegistry; // A map of metric-keys to metrics - private final NavigableMap metricMap = new ConcurrentSkipListMap<>(); + private final @NonNull NavigableMap metricMap = new ConcurrentSkipListMap<>(); // A read-only view of all registered metrics - private final Collection metricsView = Collections.unmodifiableCollection(metricMap.values()); + private final @NonNull Collection metricsView = Collections.unmodifiableCollection(metricMap.values()); // A map of all global metrics in the system (only used if this instance maintains platform metrics - private final Map globalMetricKeys = new ConcurrentHashMap<>(); + private final @NonNull Map globalMetricKeys = new ConcurrentHashMap<>(); // Factory that creates specific implementation of Metric - private final PlatformMetricsFactory factory; + private final @NonNull PlatformMetricsFactory factory; // Helper-class that implements the Observer-pattern for MetricsEvents - private final MetricsEventBus eventBus; + private final @NonNull MetricsEventBus eventBus; // Helper class that maintains a list of all metrics, which need to be updated in regular intervals - private final MetricsUpdateService updateService; + private final @Nullable MetricsUpdateService updateService; /** * Constructor of {@code DefaultMetrics} * - * @param selfId - * the {@link NodeId} of the platform, {@code null} if these are the global metrics - * @param metricKeyRegistry - * the {@link MetricKeyRegistry} that ensures no conflicting metrics are registered - * @param executor - * the {@link ScheduledExecutorService} that will be used by this {@code DefaultMetrics} - * @param factory - * the {@link PlatformMetricsFactory} that will be used to create new instances of {@link Metric} - * @param metricsConfig - * the {@link MetricsConfig} for metrics configuration + * @param selfId the {@link NodeId} of the platform, {@code null} if these are the global metrics + * @param metricKeyRegistry the {@link MetricKeyRegistry} that ensures no conflicting metrics are registered + * @param executor the {@link ScheduledExecutorService} that will be used by this {@code DefaultMetrics} + * @param factory the {@link PlatformMetricsFactory} that will be used to create new instances of + * {@link Metric} + * @param metricsConfig the {@link MetricsConfig} for metrics configuration * @throws NullPointerException if any of the following parameters are {@code null}. - *
    - *
  • {@code metricKeyRegistry}
  • - *
  • {@code executor}
  • - *
  • {@code factory}
  • - *
  • {@code metricsConfig}
  • - *
- * + *
    + *
  • {@code metricKeyRegistry}
  • + *
  • {@code executor}
  • + *
  • {@code factory}
  • + *
  • {@code metricsConfig}
  • + *
*/ public DefaultMetrics( - final NodeId selfId, - final MetricKeyRegistry metricKeyRegistry, - final ScheduledExecutorService executor, - final PlatformMetricsFactory factory, - final MetricsConfig metricsConfig) { + final @Nullable NodeId selfId, + final @NonNull MetricKeyRegistry metricKeyRegistry, + final @NonNull ScheduledExecutorService executor, + final @NonNull PlatformMetricsFactory factory, + final @NonNull MetricsConfig metricsConfig) { this.selfId = selfId; this.metricKeyRegistry = Objects.requireNonNull(metricKeyRegistry, "metricsKeyRegistry must not be null"); this.factory = Objects.requireNonNull(factory, "factory must not be null"); @@ -124,7 +121,7 @@ public NodeId getNodeId() { * {@inheritDoc} */ @Override - public Metric getMetric(final String category, final String name) { + public Metric getMetric(final @NonNull String category, final @NonNull String name) { Objects.requireNonNull(category, "category must not be null"); Objects.requireNonNull(name, "name must not be null"); return metricMap.get(calculateMetricKey(category, name)); @@ -133,8 +130,9 @@ public Metric getMetric(final String category, final String name) { /** * {@inheritDoc} */ + @NonNull @Override - public Collection findMetricsByCategory(final String category) { + public Collection findMetricsByCategory(final @NonNull String category) { Objects.requireNonNull(category, "category must not be null"); final String start = category + "."; // The character '/' is the successor of '.' in Unicode. We use it to define the first metric-key, @@ -146,6 +144,7 @@ public Collection findMetricsByCategory(final String category) { /** * {@inheritDoc} */ + @NonNull @Override public Collection getAll() { return metricsView; @@ -156,14 +155,13 @@ public Collection getAll() { *

* A new subscriber will immediately receive ADD-events for all metrics, that are already registered. *

- * If the list of metrics is modified while a new subscriber is added, it may happen, that the new subscriber - * gets two ADD-events for the same {@code Metric} or a REMOVE-event for a {@code Metric} that was not added before. + * If the list of metrics is modified while a new subscriber is added, it may happen, that the new subscriber gets + * two ADD-events for the same {@code Metric} or a REMOVE-event for a {@code Metric} that was not added before. * - * @param subscriber - * the new {@code subscriber} + * @param subscriber the new {@code subscriber} * @return a {@link Runnable} that, when called, unsubscribes the subscriber */ - public Runnable subscribe(final Consumer subscriber) { + public @NonNull Runnable subscribe(final @NonNull Consumer subscriber) { final Supplier> previousEventsSupplier = () -> metricMap.values().stream().map(metric -> new MetricsEvent(ADDED, selfId, metric)); return eventBus.subscribe(subscriber, previousEventsSupplier); @@ -172,8 +170,9 @@ public Runnable subscribe(final Consumer subscriber) { /** * {@inheritDoc} */ + @NonNull @Override - public T getOrCreate(final MetricConfig config) { + public T getOrCreate(final @NonNull MetricConfig config) { Objects.requireNonNull(config, "config must not be null"); // first we check the happy path, if the metric is already registered @@ -213,7 +212,7 @@ public T getOrCreate(final MetricConfig config) { * {@inheritDoc} */ @Override - public void remove(final String category, final String name) { + public void remove(final @NonNull String category, final @NonNull String name) { Objects.requireNonNull(category, "category must not be null"); Objects.requireNonNull(name, "name must not be null"); final String metricKey = calculateMetricKey(category, name); @@ -230,7 +229,7 @@ public void remove(final String category, final String name) { * {@inheritDoc} */ @Override - public void remove(final Metric metric) { + public void remove(final @NonNull Metric metric) { Objects.requireNonNull(metric, "metric must not be null"); final String metricKey = calculateMetricKey(metric); throwIfGlobal(metricKey); @@ -246,7 +245,7 @@ public void remove(final Metric metric) { * {@inheritDoc} */ @Override - public void remove(final MetricConfig config) { + public void remove(final @NonNull MetricConfig config) { Objects.requireNonNull(config, "config must not be null"); final String metricKey = calculateMetricKey(config); throwIfGlobal(metricKey); @@ -272,7 +271,7 @@ private void throwIfGlobal(final String metricKey) { * {@inheritDoc} */ @Override - public void addUpdater(final Runnable updater) { + public void addUpdater(final @NonNull Runnable updater) { Objects.requireNonNull(updater, "updater must not be null"); if (updateService != null) { updateService.addUpdater(updater); @@ -283,7 +282,7 @@ public void addUpdater(final Runnable updater) { * {@inheritDoc} */ @Override - public void removeUpdater(final Runnable updater) { + public void removeUpdater(final @NonNull Runnable updater) { Objects.requireNonNull(updater, "updater must not be null"); if (updateService != null) { updateService.removeUpdater(updater); @@ -304,8 +303,7 @@ public void start() { * Shuts down the service * * @return {@code true} if the shutdown finished on time, {@code false} if the call ran into a timeout - * @throws InterruptedException - * if the current thread was interrupted while waiting + * @throws InterruptedException if the current thread was interrupted while waiting */ public boolean shutdown() throws InterruptedException { metricMap.entrySet().stream() @@ -321,13 +319,11 @@ public boolean shutdown() throws InterruptedException { * The generated key is compatible with keys generated by {@link #calculateMetricKey(Metric)} and * {@link #calculateMetricKey(MetricConfig)}. * - * @param category - * the {@code category} used in the key - * @param name - * the {@code name} used in the key + * @param category the {@code category} used in the key + * @param name the {@code name} used in the key * @return the calculated key */ - public static String calculateMetricKey(final String category, final String name) { + public static String calculateMetricKey(final @NonNull String category, final @NonNull String name) { return category + "." + name; } @@ -337,11 +333,10 @@ public static String calculateMetricKey(final String category, final String name * The generated key is compatible with keys generated by {@link #calculateMetricKey(String, String)} and * {@link #calculateMetricKey(MetricConfig)}. * - * @param metric - * the {@code Metric} for which the key should be calculated + * @param metric the {@code Metric} for which the key should be calculated * @return the calculated key */ - public static String calculateMetricKey(final Metric metric) { + public static @NonNull String calculateMetricKey(final @NonNull Metric metric) { return calculateMetricKey(metric.getCategory(), metric.getName()); } @@ -351,21 +346,19 @@ public static String calculateMetricKey(final Metric metric) { * The generated key is compatible with keys generated by {@link #calculateMetricKey(String, String)} and * {@link #calculateMetricKey(Metric)}. * - * @param config - * the {@code MetricConfig} for which the key should be calculated + * @param config the {@code MetricConfig} for which the key should be calculated * @return the calculated key */ - public static String calculateMetricKey(final MetricConfig config) { + public static @NonNull String calculateMetricKey(final @NonNull MetricConfig config) { return calculateMetricKey(config.getCategory(), config.getName()); } /** * Handles new and removed global metrics. * - * @param event - * The {@link MetricsEvent} with information about the change + * @param event The {@link MetricsEvent} with information about the change */ - public void handleGlobalMetrics(final MetricsEvent event) { + public void handleGlobalMetrics(final @NonNull MetricsEvent event) { final Metric metric = event.metric(); final String metricKey = calculateMetricKey(metric); switch (event.type()) { diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultMetricsProvider.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultMetricsProvider.java index ad09d6fc2d82..368a4f0b266c 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultMetricsProvider.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultMetricsProvider.java @@ -32,6 +32,7 @@ import com.swirlds.config.api.Configuration; import com.swirlds.metrics.api.Metrics; import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.file.Path; @@ -50,25 +51,25 @@ public class DefaultMetricsProvider implements PlatformMetricsProvider, Lifecycl private static final Logger logger = LogManager.getLogger(DefaultMetricsProvider.class); - private final PlatformMetricsFactory factory; - private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor( + private final @NonNull PlatformMetricsFactory factory; + private final @NonNull ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor( getStaticThreadManager().createThreadFactory("platform-core", "MetricsThread")); - private final MetricKeyRegistry metricKeyRegistry = new MetricKeyRegistry(); - private final DefaultMetrics globalMetrics; - private final ConcurrentMap platformMetrics = new ConcurrentHashMap<>(); - private final PrometheusEndpoint prometheusEndpoint; - private final SnapshotService snapshotService; - private final MetricsConfig metricsConfig; - private final Configuration configuration; + private final @NonNull MetricKeyRegistry metricKeyRegistry = new MetricKeyRegistry(); + private final @NonNull DefaultMetrics globalMetrics; + private final @NonNull ConcurrentMap platformMetrics = new ConcurrentHashMap<>(); + private final @Nullable PrometheusEndpoint prometheusEndpoint; + private final @NonNull SnapshotService snapshotService; + private final @NonNull MetricsConfig metricsConfig; + private final @NonNull Configuration configuration; - private LifecyclePhase lifecyclePhase = LifecyclePhase.NOT_STARTED; + private @NonNull LifecyclePhase lifecyclePhase = LifecyclePhase.NOT_STARTED; /** * Constructor of {@code DefaultMetricsProvider} */ public DefaultMetricsProvider(@NonNull final Configuration configuration) { - this.configuration = Objects.requireNonNull(configuration, "configuration is null"); + this.configuration = Objects.requireNonNull(configuration, "configuration must not be null"); metricsConfig = configuration.getConfigData(MetricsConfig.class); final PrometheusConfig prometheusConfig = configuration.getConfigData(PrometheusConfig.class); @@ -100,16 +101,16 @@ public DefaultMetricsProvider(@NonNull final Configuration configuration) { * {@inheritDoc} */ @Override - public Metrics createGlobalMetrics() { - return globalMetrics; + public @NonNull Metrics createGlobalMetrics() { + return this.globalMetrics; } /** * {@inheritDoc} */ @Override - public Metrics createPlatformMetrics(@NonNull final NodeId nodeId) { - Objects.requireNonNull(nodeId, "selfId is null"); + public @NonNull Metrics createPlatformMetrics(@NonNull final NodeId nodeId) { + Objects.requireNonNull(nodeId, "selfId must not be null"); final DefaultMetrics newMetrics = new DefaultMetrics(nodeId, metricKeyRegistry, executor, factory, metricsConfig); @@ -145,7 +146,7 @@ public Metrics createPlatformMetrics(@NonNull final NodeId nodeId) { } @Override - public LifecyclePhase getLifecyclePhase() { + public @NonNull LifecyclePhase getLifecyclePhase() { return lifecyclePhase; } @@ -171,7 +172,9 @@ public void start() { public void stop() { if (lifecyclePhase == LifecyclePhase.STARTED) { snapshotService.shutdown(); - prometheusEndpoint.close(); + if (prometheusEndpoint != null) { + prometheusEndpoint.close(); + } lifecyclePhase = LifecyclePhase.STOPPED; } } diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultRunningAverageMetric.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultRunningAverageMetric.java index efd1ffc3f5d4..e408df1cae3e 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultRunningAverageMetric.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultRunningAverageMetric.java @@ -21,6 +21,7 @@ import com.swirlds.common.metrics.RunningAverageMetric; import com.swirlds.common.metrics.statistics.StatsBuffered; import com.swirlds.common.metrics.statistics.StatsRunningAverage; +import edu.umd.cs.findbugs.annotations.NonNull; /** * Platform-implementation of {@link RunningAverageMetric} @@ -29,7 +30,7 @@ public class DefaultRunningAverageMetric extends AbstractDistributionMetric implements RunningAverageMetric { @SuppressWarnings("removal") - private final StatsRunningAverage runningAverage; + private final @NonNull StatsRunningAverage runningAverage; public DefaultRunningAverageMetric(final RunningAverageMetric.Config config) { this(config, Time.getCurrent()); @@ -47,6 +48,7 @@ public DefaultRunningAverageMetric(final RunningAverageMetric.Config config, fin /** * {@inheritDoc} */ + @NonNull @SuppressWarnings("removal") @Override public StatsBuffered getStatsBuffered() { diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultSpeedometerMetric.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultSpeedometerMetric.java index 6b9c673d3f34..92e5d74491c8 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultSpeedometerMetric.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultSpeedometerMetric.java @@ -21,6 +21,7 @@ import com.swirlds.common.metrics.SpeedometerMetric; import com.swirlds.common.metrics.statistics.StatsBuffered; import com.swirlds.common.metrics.statistics.StatsSpeedometer; +import edu.umd.cs.findbugs.annotations.NonNull; /** * Platform-implementation of {@link SpeedometerMetric} @@ -46,6 +47,7 @@ public DefaultSpeedometerMetric(final SpeedometerMetric.Config config, final Tim /** * {@inheritDoc} */ + @NonNull @SuppressWarnings("removal") @Override public StatsBuffered getStatsBuffered() { diff --git a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultStatEntry.java b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultStatEntry.java index 0310c796a308..23dcfda3b78e 100644 --- a/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultStatEntry.java +++ b/platform-sdk/swirlds-common/src/main/java/com/swirlds/common/metrics/platform/DefaultStatEntry.java @@ -26,6 +26,8 @@ import com.swirlds.common.metrics.platform.Snapshot.SnapshotEntry; import com.swirlds.common.metrics.statistics.StatsBuffered; import com.swirlds.metrics.api.MetricConfig; +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; import java.util.List; import java.util.function.Consumer; import java.util.function.Supplier; @@ -36,23 +38,23 @@ @SuppressWarnings("removal") public class DefaultStatEntry extends DefaultMetric implements StatEntry { - private final DataType dataType; + private final @NonNull DataType dataType; /** * the statistics object (if it implements StatsBuffered), else null */ - private final StatsBuffered buffered; + private final @Nullable StatsBuffered buffered; /** * a lambda that resets the statistic, using the given half life */ - private final Consumer reset; + private final @Nullable Consumer reset; /** * a lambda that returns the statistic string */ - private final Supplier statsStringSupplier; + private final @NonNull Supplier statsStringSupplier; /** * a lambda that returns the statistic string and resets it at the same time */ - private final Supplier resetStatsStringSupplier; + private final @NonNull Supplier resetStatsStringSupplier; /** * the half life of the statistic, in seconds @@ -76,6 +78,7 @@ public DefaultStatEntry(final StatEntry.Config config) { /** * {@inheritDoc} */ + @NonNull @Override public DataType getDataType() { return dataType; @@ -85,6 +88,7 @@ public DataType getDataType() { * {@inheritDoc} */ @Override + @Nullable public StatsBuffered getBuffered() { return buffered; } @@ -92,6 +96,7 @@ public StatsBuffered getBuffered() { /** * {@inheritDoc} */ + @Nullable @Override public Consumer getReset() { return reset; @@ -100,6 +105,7 @@ public Consumer getReset() { /** * {@inheritDoc} */ + @NonNull @Override public Supplier getStatsStringSupplier() { return statsStringSupplier; @@ -108,6 +114,7 @@ public Supplier getStatsStringSupplier() { /** * {@inheritDoc} */ + @NonNull @Override public Supplier getResetStatsStringSupplier() { return resetStatsStringSupplier; @@ -116,6 +123,7 @@ public Supplier getResetStatsStringSupplier() { /** * {@inheritDoc} */ + @NonNull @Override public List takeSnapshot() { if (buffered == null) { @@ -148,6 +156,7 @@ public void reset() { /** * {@inheritDoc} */ + @Nullable @SuppressWarnings("removal") @Override public StatsBuffered getStatsBuffered() { diff --git a/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/DurationGaugeTest.java b/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/DurationGaugeTest.java index a6313c7808b7..61af91d46572 100644 --- a/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/DurationGaugeTest.java +++ b/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/DurationGaugeTest.java @@ -23,60 +23,29 @@ import static com.swirlds.metrics.api.Metric.ValueType.VALUE; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import com.swirlds.metrics.api.MetricType; -import java.time.Duration; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; @DisplayName("Testing DurationGauge") class DurationGaugeTest { - private final DurationGauge sut = new DurationGauge() { - @Override - public long getNanos() { - return 0; - } + private DurationGauge sut; - @Override - public void set(Duration duration) {} - - @Override - public double get() { - return 0; - } - - @Override - public String getCategory() { - return null; - } - - @Override - public String getName() { - return null; - } - - @Override - public String getDescription() { - return null; - } - - @Override - public String getUnit() { - return null; - } - - @Override - public String getFormat() { - return null; - } - - @Override - public void reset() {} - }; + @BeforeEach + void setup() { + sut = Mockito.mock(DurationGauge.class); + when(sut.get(Mockito.any())).thenCallRealMethod(); + when(sut.getMetricType()).thenCallRealMethod(); + when(sut.getDataType()).thenCallRealMethod(); + when(sut.getValueTypes()).thenCallRealMethod(); + } @Test void getMetricType() { @@ -95,12 +64,11 @@ void getValueTypes() { @Test void get_ShouldReturnValueByValueType() { - final DurationGauge gauge = spy(sut); - final Double value = gauge.get(VALUE); + final Double value = sut.get(VALUE); assertThat(value).isEqualTo(sut.get()); - verify(gauge, times(1)).get(); + verify(sut, times(2)).get(); } @Test diff --git a/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/FunctionGaugeTest.java b/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/FunctionGaugeTest.java index 0cc1214a56de..0e06993523b8 100644 --- a/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/FunctionGaugeTest.java +++ b/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/FunctionGaugeTest.java @@ -22,56 +22,28 @@ import static com.swirlds.metrics.api.Metric.ValueType.VALUE; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import com.swirlds.metrics.api.MetricType; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; @DisplayName("Testing FunctionGauge") class FunctionGaugeTest { - private final FunctionGauge sut = new FunctionGauge<>() { - @Override - public Double get() { - return null; - } + private FunctionGauge sut; - @Override - public String getCategory() { - return null; - } - - @Override - public String getName() { - return null; - } - - @Override - public String getDescription() { - return null; - } - - @Override - public DataType getDataType() { - return null; - } - - @Override - public String getUnit() { - return null; - } - - @Override - public String getFormat() { - return null; - } - - @Override - public void reset() {} - }; + @BeforeEach + void setup() { + sut = Mockito.mock(FunctionGauge.class); + when(sut.get(Mockito.any())).thenCallRealMethod(); + when(sut.getMetricType()).thenCallRealMethod(); + when(sut.getValueTypes()).thenCallRealMethod(); + } @Test void getMetricType() { @@ -85,12 +57,10 @@ void getValueTypes() { @Test void get_ShouldReturnValueByValueType() { - final FunctionGauge gauge = spy(sut); - - final Double value = gauge.get(VALUE); + final Double value = sut.get(VALUE); assertThat(value).isEqualTo(sut.get()); - verify(gauge, times(1)).get(); + verify(sut, times(2)).get(); } @Test diff --git a/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/IntegerPairAccumulatorTest.java b/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/IntegerPairAccumulatorTest.java index 48a5aefec935..762d7310ea70 100644 --- a/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/IntegerPairAccumulatorTest.java +++ b/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/IntegerPairAccumulatorTest.java @@ -22,74 +22,28 @@ import static com.swirlds.metrics.api.Metric.ValueType.VALUE; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import com.swirlds.metrics.api.MetricType; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; @DisplayName("Testing IntegerPairAccumulator") class IntegerPairAccumulatorTest { - private final IntegerPairAccumulator sut = new IntegerPairAccumulator<>() { - @Override - public Double get() { - return null; - } + private IntegerPairAccumulator sut; - @Override - public int getLeft() { - return 0; - } - - @Override - public int getRight() { - return 0; - } - - @Override - public void update(int leftValue, int rightValue) {} - - @Override - public String getCategory() { - return null; - } - - @Override - public String getName() { - return null; - } - - @Override - public String getDescription() { - return null; - } - - @Override - public MetricType getMetricType() { - return MetricType.ACCUMULATOR; - } - - @Override - public DataType getDataType() { - return null; - } - - @Override - public String getUnit() { - return null; - } - - @Override - public String getFormat() { - return null; - } - - @Override - public void reset() {} - }; + @BeforeEach + void setup() { + sut = Mockito.mock(IntegerPairAccumulator.class); + when(sut.get(Mockito.any())).thenCallRealMethod(); + when(sut.getMetricType()).thenCallRealMethod(); + when(sut.getValueTypes()).thenCallRealMethod(); + } @Test void getMetricType() { @@ -103,12 +57,10 @@ void getValueTypes() { @Test void get_ShouldReturnValueByValueType() { - final IntegerPairAccumulator accumulator = spy(sut); - - final Double value = accumulator.get(VALUE); + final Double value = sut.get(VALUE); assertThat(value).isEqualTo(sut.get()); - verify(accumulator, times(1)).get(); + verify(sut, times(2)).get(); } @Test diff --git a/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/RunningAverageMetricTest.java b/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/RunningAverageMetricTest.java index 00b08abec25c..9c225ba0a277 100644 --- a/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/RunningAverageMetricTest.java +++ b/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/RunningAverageMetricTest.java @@ -22,61 +22,26 @@ import static com.swirlds.metrics.api.Metric.ValueType.STD_DEV; import static com.swirlds.metrics.api.Metric.ValueType.VALUE; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; import com.swirlds.metrics.api.MetricType; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; @DisplayName("Testing RunningAverageMetric") class RunningAverageMetricTest { - private final RunningAverageMetric sut = new RunningAverageMetric() { - @Override - public Double get(ValueType valueType) { - return null; - } + private RunningAverageMetric sut; - @Override - public double getHalfLife() { - return 0; - } - - @Override - public void update(double value) {} - - @Override - public double get() { - return 0; - } - - @Override - public String getCategory() { - return null; - } - - @Override - public String getName() { - return null; - } - - @Override - public String getDescription() { - return null; - } - - @Override - public String getUnit() { - return null; - } - - @Override - public String getFormat() { - return null; - } - - @Override - public void reset() {} - }; + @BeforeEach + void setup() { + sut = Mockito.mock(RunningAverageMetric.class); + when(sut.getMetricType()).thenCallRealMethod(); + when(sut.getDataType()).thenCallRealMethod(); + when(sut.getValueTypes()).thenCallRealMethod(); + } @Test void getMetricType() { diff --git a/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/SpeedometerMetricTest.java b/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/SpeedometerMetricTest.java index db7b6120e061..a4b4072a42b4 100644 --- a/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/SpeedometerMetricTest.java +++ b/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/SpeedometerMetricTest.java @@ -22,64 +22,26 @@ import static com.swirlds.metrics.api.Metric.ValueType.STD_DEV; import static com.swirlds.metrics.api.Metric.ValueType.VALUE; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; import com.swirlds.metrics.api.MetricType; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; @DisplayName("Testing SpeedometerMetric") class SpeedometerMetricTest { - private final SpeedometerMetric sut = new SpeedometerMetric() { - @Override - public Double get(ValueType valueType) { - return null; - } + private SpeedometerMetric sut; - @Override - public double getHalfLife() { - return 0; - } - - @Override - public void update(double value) {} - - @Override - public void cycle() {} - - @Override - public double get() { - return 0; - } - - @Override - public String getCategory() { - return null; - } - - @Override - public String getName() { - return null; - } - - @Override - public String getDescription() { - return null; - } - - @Override - public String getUnit() { - return null; - } - - @Override - public String getFormat() { - return null; - } - - @Override - public void reset() {} - }; + @BeforeEach + void setup() { + sut = Mockito.mock(SpeedometerMetric.class); + when(sut.getMetricType()).thenCallRealMethod(); + when(sut.getDataType()).thenCallRealMethod(); + when(sut.getValueTypes()).thenCallRealMethod(); + } @Test void getMetricType() { diff --git a/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/StatEntryTest.java b/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/StatEntryTest.java index c634ca20c73c..1807983ad02c 100644 --- a/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/StatEntryTest.java +++ b/platform-sdk/swirlds-common/src/test/java/com/swirlds/common/metrics/StatEntryTest.java @@ -21,160 +21,32 @@ import static com.swirlds.metrics.api.Metric.ValueType.STD_DEV; import static com.swirlds.metrics.api.Metric.ValueType.VALUE; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import com.swirlds.common.metrics.statistics.StatsBuffered; -import com.swirlds.common.metrics.statistics.internal.StatsBuffer; import com.swirlds.metrics.api.MetricType; -import java.util.function.Consumer; -import java.util.function.Supplier; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; @DisplayName("Testing StatEntry") class StatEntryTest { - private final StatEntry sut = new StatEntry() { - @Override - public StatsBuffered getBuffered() { - return null; - } - - @Override - public Consumer getReset() { - return null; - } - - @Override - public Supplier getStatsStringSupplier() { - return null; - } - - @Override - public Supplier getResetStatsStringSupplier() { - return null; - } - - @Override - public String getCategory() { - return null; - } - - @Override - public String getName() { - return null; - } - - @Override - public String getDescription() { - return null; - } - - @Override - public DataType getDataType() { - return null; - } - - @Override - public String getUnit() { - return null; - } - - @Override - public String getFormat() { - return null; - } - - @Override - public void reset() {} - }; - - private final StatEntry sutWithStatsBuffered = new StatEntry() { - @Override - public StatsBuffered getBuffered() { - return new StatsBuffered() { - @Override - public StatsBuffer getAllHistory() { - return null; - } - - @Override - public StatsBuffer getRecentHistory() { - return null; - } - - @Override - public void reset(double halflife) {} - - @Override - public double getMean() { - return 0; - } - - @Override - public double getMax() { - return 0; - } - - @Override - public double getMin() { - return 0; - } - - @Override - public double getStdDev() { - return 0; - } - }; - } - - @Override - public Consumer getReset() { - return null; - } - - @Override - public Supplier getStatsStringSupplier() { - return null; - } - - @Override - public Supplier getResetStatsStringSupplier() { - return null; - } - - @Override - public String getCategory() { - return null; - } - - @Override - public String getName() { - return null; - } - - @Override - public String getDescription() { - return null; - } - - @Override - public DataType getDataType() { - return null; - } - - @Override - public String getUnit() { - return null; - } - - @Override - public String getFormat() { - return null; - } - - @Override - public void reset() {} - }; + private StatEntry sut; + private StatEntry sutWithStatsBuffered; + + @BeforeEach + void setup() { + sut = Mockito.mock(StatEntry.class); + when(sut.get(Mockito.any())).thenCallRealMethod(); + when(sut.getMetricType()).thenCallRealMethod(); + when(sut.getValueTypes()).thenCallRealMethod(); + sutWithStatsBuffered = Mockito.mock(StatEntry.class); + when(sutWithStatsBuffered.getBuffered()).thenReturn(mock(StatsBuffered.class)); + when(sutWithStatsBuffered.getValueTypes()).thenCallRealMethod(); + } @Test void getMetricType() { diff --git a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/Counter.java b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/Counter.java index 2034a914b480..442f63b73ad5 100644 --- a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/Counter.java +++ b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/Counter.java @@ -34,6 +34,7 @@ public interface Counter extends Metric { * {@inheritDoc} */ @Override + @NonNull default MetricType getMetricType() { return MetricType.COUNTER; } @@ -42,6 +43,7 @@ default MetricType getMetricType() { * {@inheritDoc} */ @Override + @NonNull default DataType getDataType() { return DataType.INT; } @@ -50,6 +52,7 @@ default DataType getDataType() { * {@inheritDoc} */ @Override + @NonNull default EnumSet getValueTypes() { return EnumSet.of(VALUE); } @@ -58,8 +61,9 @@ default EnumSet getValueTypes() { * {@inheritDoc} */ @Override + @NonNull default Long get(@NonNull final ValueType valueType) { - Objects.requireNonNull(valueType, "valueType"); + Objects.requireNonNull(valueType, "valueType must not be null"); if (valueType == VALUE) { return get(); } @@ -78,10 +82,8 @@ default Long get(@NonNull final ValueType valueType) { *

* The value of a {@code Counter} can only increase, thus only non-negative numbers can be added. * - * @param value - * the value that needs to be added - * @throws IllegalArgumentException - * if {@code value <= 0} + * @param value the value that needs to be added + * @throws IllegalArgumentException if {@code value <= 0} */ void add(final long value); @@ -98,17 +100,25 @@ final class Config extends MetricConfig { /** * Constructor of {@code Counter.Config} * - * @param category - * the kind of metric (metrics are grouped or filtered by this) - * @param name - * a short name for the metric - * @throws IllegalArgumentException - * if one of the parameters is {@code null} or consists only of whitespaces + * @param category the kind of metric (metrics are grouped or filtered by this) + * @param name a short name for the metric + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces */ public Config(@NonNull final String category, @NonNull final String name) { super(category, name, "%d"); } + /** + * Constructor of {@code Counter.Config} + * + * @param category the kind of metric (metrics are grouped or filtered by this) + * @param name a short name for the metric + * @param description metric description + * @param unit metric unit + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces + */ private Config( @NonNull final String category, @NonNull final String name, @@ -120,6 +130,7 @@ private Config( /** * {@inheritDoc} */ + @NonNull @Override public Counter.Config withDescription(@NonNull final String description) { return new Counter.Config(getCategory(), getName(), description, getUnit()); @@ -128,6 +139,7 @@ public Counter.Config withDescription(@NonNull final String description) { /** * {@inheritDoc} */ + @NonNull @Override public Counter.Config withUnit(@NonNull final String unit) { return new Counter.Config(getCategory(), getName(), getDescription(), unit); @@ -136,6 +148,7 @@ public Counter.Config withUnit(@NonNull final String unit) { /** * {@inheritDoc} */ + @NonNull @Override public Class getResultClass() { return Counter.class; diff --git a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/DoubleAccumulator.java b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/DoubleAccumulator.java index 50484cca293e..a6c27803571b 100644 --- a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/DoubleAccumulator.java +++ b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/DoubleAccumulator.java @@ -32,14 +32,15 @@ *

* It is reset in regular intervals. The exact timing depends on the implementation. *

- * A {@code DoubleAccumulator} is reset to the {@link #getInitialValue() initialValue}. - * If no {@code initialValue} was specified, the {@code DoubleAccumulator} is reset to {@code 0.0}. + * A {@code DoubleAccumulator} is reset to the {@link #getInitialValue() initialValue}. If no {@code initialValue} was + * specified, the {@code DoubleAccumulator} is reset to {@code 0.0}. */ public interface DoubleAccumulator extends Metric { /** * {@inheritDoc} */ + @NonNull @Override default MetricType getMetricType() { return MetricType.ACCUMULATOR; @@ -48,6 +49,7 @@ default MetricType getMetricType() { /** * {@inheritDoc} */ + @NonNull @Override default DataType getDataType() { return DataType.FLOAT; @@ -56,6 +58,7 @@ default DataType getDataType() { /** * {@inheritDoc} */ + @NonNull @Override default EnumSet getValueTypes() { return EnumSet.of(VALUE); @@ -64,9 +67,10 @@ default EnumSet getValueTypes() { /** * {@inheritDoc} */ + @NonNull @Override default Double get(@NonNull final ValueType valueType) { - Objects.requireNonNull(valueType, "valueType"); + Objects.requireNonNull(valueType, "valueType must not be null"); if (valueType == VALUE) { return get(); } @@ -94,8 +98,7 @@ default Double get(@NonNull final ValueType valueType) { * The function is applied with the current value as its first argument, and the provided {@code other} as the * second argument. * - * @param other - * the second parameter + * @param other the second parameter */ void update(final double other); @@ -104,23 +107,21 @@ default Double get(@NonNull final ValueType valueType) { */ final class Config extends MetricConfig { - private final DoubleBinaryOperator accumulator; - private final DoubleSupplier initializer; + private final @NonNull DoubleBinaryOperator accumulator; + private final @Nullable DoubleSupplier initializer; private final double initialValue; /** * Constructor of {@code DoubleAccumulator.Config} + *

+ * By default, the {@link #getAccumulator() accumulator} is set to {@code Double::max}, the + * {@link #getInitialValue() initialValue} is set to {@code 0.0}, and {@link #getFormat() format} is set to + * {@link FloatFormats#FORMAT_11_3}. * - * By default, the {@link #getAccumulator() accumulator} is set to {@code Double::max}, - * the {@link #getInitialValue() initialValue} is set to {@code 0.0}, - * and {@link #getFormat() format} is set to {@link FloatFormats#FORMAT_11_3}. - * - * @param category - * the kind of metric (metrics are grouped or filtered by this) - * @param name - * a short name for the metric - * @throws IllegalArgumentException - * if one of the parameters is {@code null} or consists only of whitespaces + * @param category the kind of metric (metrics are grouped or filtered by this) + * @param name a short name for the metric + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces */ public Config(@NonNull final String category, @NonNull final String name) { super(category, name, FORMAT_11_3); @@ -129,6 +130,21 @@ public Config(@NonNull final String category, @NonNull final String name) { this.initialValue = 0.0; } + /** + * Constructor of {@code DoubleAccumulator.Config} + *

+ * By default, the {@link #getAccumulator() accumulator} is set to {@code Double::max}, the + * {@link #getInitialValue() initialValue} is set to {@code 0.0}, and {@link #getFormat() format} is set to + * {@link FloatFormats#FORMAT_11_3}. + * + * @param category the kind of metric (metrics are grouped or filtered by this) + * @param name a short name for the metric + * @param description metric description + * @param unit metric unit + * @param accumulator accumulator for metric + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces + */ private Config( @NonNull final String category, @NonNull final String name, @@ -140,7 +156,7 @@ private Config( final double initialValue) { super(category, name, description, unit, format); - this.accumulator = Objects.requireNonNull(accumulator, "accumulator"); + this.accumulator = Objects.requireNonNull(accumulator, "accumulator must not be null"); this.initializer = initializer; this.initialValue = initialValue; } @@ -148,6 +164,7 @@ private Config( /** * {@inheritDoc} */ + @NonNull @Override public DoubleAccumulator.Config withDescription(@NonNull final String description) { return new DoubleAccumulator.Config( @@ -164,6 +181,7 @@ public DoubleAccumulator.Config withDescription(@NonNull final String descriptio /** * {@inheritDoc} */ + @NonNull @Override public DoubleAccumulator.Config withUnit(@NonNull final String unit) { return new DoubleAccumulator.Config( @@ -180,11 +198,10 @@ public DoubleAccumulator.Config withUnit(@NonNull final String unit) { /** * Sets the {@link Metric#getFormat() Metric.format} in fluent style. * - * @param format - * the format-string + * @param format the format-string * @return a new configuration-object with updated {@code format} - * @throws IllegalArgumentException - * if {@code format} is {@code null} or consists only of whitespaces + * @throws NullPointerException if {@code format} is {@code null} + * @throws IllegalArgumentException if {@code format} consists only of whitespaces */ public DoubleAccumulator.Config withFormat(@NonNull final String format) { return new DoubleAccumulator.Config( @@ -221,11 +238,10 @@ public DoubleSupplier getInitializer() { /** * Fluent-style setter of the accumulator. *

- * The accumulator should be side-effect-free, since it may be re-applied when attempted updates fail - * due to contention among threads. + * The accumulator should be side-effect-free, since it may be re-applied when attempted updates fail due to + * contention among threads. * - * @param accumulator - * The {@link DoubleBinaryOperator} that is used to accumulate the value. + * @param accumulator The {@link DoubleBinaryOperator} that is used to accumulate the value. * @return a new configuration-object with updated {@code initialValue} */ @NonNull @@ -253,8 +269,7 @@ public double getInitialValue() { /** * Fluent-style setter of the initial value. * - * @param initialValue - * the initial value + * @param initialValue the initial value * @return a new configuration-object with updated {@code initialValue} */ @NonNull @@ -275,8 +290,7 @@ public DoubleAccumulator.Config withInitialValue(final double initialValue) { *

* If both {@code initializer} and {@code initialValue} are set, the {@code initialValue} is ignored * - * @param initializer - * the initializer + * @param initializer the initializer * @return a new configuration-object with updated {@code initializer} */ @NonNull @@ -295,6 +309,7 @@ public DoubleAccumulator.Config withInitializer(@NonNull final DoubleSupplier in /** * {@inheritDoc} */ + @NonNull @Override public Class getResultClass() { return DoubleAccumulator.class; diff --git a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/DoubleGauge.java b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/DoubleGauge.java index 4e449ac18976..beb6b58226f9 100644 --- a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/DoubleGauge.java +++ b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/DoubleGauge.java @@ -34,6 +34,7 @@ public interface DoubleGauge extends Metric { /** * {@inheritDoc} */ + @NonNull @Override default MetricType getMetricType() { return MetricType.GAUGE; @@ -42,6 +43,7 @@ default MetricType getMetricType() { /** * {@inheritDoc} */ + @NonNull @Override default DataType getDataType() { return DataType.FLOAT; @@ -50,6 +52,7 @@ default DataType getDataType() { /** * {@inheritDoc} */ + @NonNull @Override default EnumSet getValueTypes() { return EnumSet.of(VALUE); @@ -59,8 +62,9 @@ default EnumSet getValueTypes() { * {@inheritDoc} */ @Override + @NonNull default Double get(@NonNull final ValueType valueType) { - Objects.requireNonNull(valueType, "valueType"); + Objects.requireNonNull(valueType, "valueType must not be null"); if (valueType == VALUE) { return get(); } @@ -79,8 +83,7 @@ default Double get(@NonNull final ValueType valueType) { *

* {@link Double#NaN}, {@link Double#POSITIVE_INFINITY}, {@link Double#NEGATIVE_INFINITY} are supported. * - * @param newValue - * the new value + * @param newValue the new value */ void set(final double newValue); @@ -93,23 +96,34 @@ final class Config extends MetricConfig { /** * Constructor of {@code DoubleGauge.Config} - * + *

* The {@link #getInitialValue() initialValue} is by default set to {@code 0.0} - * + *

* The initial value is set to {@code 0.0}. * - * @param category - * the kind of metric (metrics are grouped or filtered by this) - * @param name - * a short name for the metric - * @throws IllegalArgumentException - * if one of the parameters is {@code null} or consists only of whitespaces + * @param category the kind of metric (metrics are grouped or filtered by this) + * @param name a short name for the metric + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces */ public Config(@NonNull final String category, @NonNull final String name) { super(category, name, FloatFormats.FORMAT_11_3); this.initialValue = 0.0; } + /** + * Constructor of {@code DoubleGauge.Config} + * + * + * @param category the kind of metric (metrics are grouped or filtered by this) + * @param name a short name for the metric + * @param description metric description + * @param unit metric unit + * @param format format for metric + * @param initialValue initialValue for metric + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces + */ private Config( @NonNull final String category, @NonNull final String name, @@ -125,6 +139,7 @@ private Config( /** * {@inheritDoc} */ + @NonNull @Override public DoubleGauge.Config withDescription(@NonNull final String description) { return new DoubleGauge.Config( @@ -134,6 +149,7 @@ public DoubleGauge.Config withDescription(@NonNull final String description) { /** * {@inheritDoc} */ + @NonNull @Override public DoubleGauge.Config withUnit(@NonNull final String unit) { return new DoubleGauge.Config( @@ -143,11 +159,10 @@ public DoubleGauge.Config withUnit(@NonNull final String unit) { /** * Sets the {@link Metric#getFormat() Metric.format} in fluent style. * - * @param format - * the format-string + * @param format the format-string * @return a new configuration-object with updated {@code format} - * @throws IllegalArgumentException - * if {@code format} is {@code null} or consists only of whitespaces + * @throws NullPointerException if {@code format} is {@code null} + * @throws IllegalArgumentException if {@code format} consists only of whitespaces */ @NonNull public DoubleGauge.Config withFormat(@NonNull final String format) { @@ -167,8 +182,7 @@ public double getInitialValue() { /** * Fluent-style setter of the initial value. * - * @param initialValue - * the initial value + * @param initialValue the initial value * @return a new configuration-object with updated {@code initialValue} */ @NonNull @@ -180,6 +194,7 @@ public DoubleGauge.Config withInitialValue(final double initialValue) { /** * {@inheritDoc} */ + @NonNull @Override public Class getResultClass() { return DoubleGauge.class; diff --git a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/IntegerAccumulator.java b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/IntegerAccumulator.java index 30a773762904..3ea80c1d302b 100644 --- a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/IntegerAccumulator.java +++ b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/IntegerAccumulator.java @@ -39,6 +39,7 @@ public interface IntegerAccumulator extends Metric { /** * {@inheritDoc} */ + @NonNull @Override default MetricType getMetricType() { return MetricType.ACCUMULATOR; @@ -47,6 +48,7 @@ default MetricType getMetricType() { /** * {@inheritDoc} */ + @NonNull @Override default DataType getDataType() { return DataType.INT; @@ -55,6 +57,7 @@ default DataType getDataType() { /** * {@inheritDoc} */ + @NonNull @Override default EnumSet getValueTypes() { return EnumSet.of(VALUE); @@ -63,9 +66,10 @@ default EnumSet getValueTypes() { /** * {@inheritDoc} */ + @NonNull @Override - default Integer get(final ValueType valueType) { - Objects.requireNonNull(valueType, "valueType"); + default Integer get(@NonNull final ValueType valueType) { + Objects.requireNonNull(valueType, "valueType must not be null"); if (valueType == VALUE) { return get(); } @@ -103,8 +107,8 @@ default Integer get(final ValueType valueType) { */ final class Config extends MetricConfig { - private final IntBinaryOperator accumulator; - private final IntSupplier initializer; + private final @NonNull IntBinaryOperator accumulator; + private final @Nullable IntSupplier initializer; private final int initialValue; @@ -119,8 +123,8 @@ final class Config extends MetricConfig getResultClass() { return IntegerAccumulator.class; diff --git a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/IntegerGauge.java b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/IntegerGauge.java index 4df46a60aaf4..6497c874b23b 100644 --- a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/IntegerGauge.java +++ b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/IntegerGauge.java @@ -33,6 +33,7 @@ public interface IntegerGauge extends Metric { /** * {@inheritDoc} */ + @NonNull @Override default MetricType getMetricType() { return MetricType.GAUGE; @@ -41,6 +42,7 @@ default MetricType getMetricType() { /** * {@inheritDoc} */ + @NonNull @Override default DataType getDataType() { return DataType.INT; @@ -49,6 +51,7 @@ default DataType getDataType() { /** * {@inheritDoc} */ + @NonNull @Override default EnumSet getValueTypes() { return EnumSet.of(VALUE); @@ -57,9 +60,10 @@ default EnumSet getValueTypes() { /** * {@inheritDoc} */ + @NonNull @Override default Integer get(@NonNull final ValueType valueType) { - Objects.requireNonNull(valueType, "valueType"); + Objects.requireNonNull(valueType, "valueType must not be null"); if (valueType == VALUE) { return get(); } @@ -98,8 +102,8 @@ final class Config extends MetricConfig { * the kind of metric (metrics are grouped or filtered by this) * @param name * a short name for the metric - * @throws IllegalArgumentException - * if one of the parameters is {@code null} or consists only of whitespaces + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces */ public Config(@NonNull final String category, @NonNull final String name) { @@ -107,6 +111,22 @@ public Config(@NonNull final String category, @NonNull final String name) { this.initialValue = 0; } + /** + * Constructor of {@code IntegerGauge.Config} + * + * The {@link #getInitialValue() initialValue} is by default set to {@code 0}, + * the {@link #getFormat() format} is set to "%d". + * + * @param category + * the kind of metric (metrics are grouped or filtered by this) + * @param name + * a short name for the metric + * @param description metric description + * @param unit metric unit + * @param format format for metric + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces + */ private Config( @NonNull final String category, @NonNull final String name, @@ -122,6 +142,7 @@ private Config( /** * {@inheritDoc} */ + @NonNull @Override public IntegerGauge.Config withDescription(@NonNull final String description) { return new IntegerGauge.Config( @@ -131,6 +152,7 @@ public IntegerGauge.Config withDescription(@NonNull final String description) { /** * {@inheritDoc} */ + @NonNull @Override public IntegerGauge.Config withUnit(@NonNull final String unit) { return new IntegerGauge.Config( @@ -143,8 +165,8 @@ public IntegerGauge.Config withUnit(@NonNull final String unit) { * @param format * the format-string * @return a new configuration-object with updated {@code format} - * @throws IllegalArgumentException - * if {@code format} is {@code null} or consists only of whitespaces + * @throws NullPointerException if {@code format} is {@code null} + * @throws IllegalArgumentException if {@code format} consists only of whitespaces */ @NonNull public IntegerGauge.Config withFormat(@NonNull final String format) { @@ -177,6 +199,7 @@ public IntegerGauge.Config withInitialValue(final int initialValue) { /** * {@inheritDoc} */ + @NonNull @Override public Class getResultClass() { return IntegerGauge.class; diff --git a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/LongAccumulator.java b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/LongAccumulator.java index a43549c6fd91..0ef2f205bbab 100644 --- a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/LongAccumulator.java +++ b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/LongAccumulator.java @@ -39,6 +39,7 @@ public interface LongAccumulator extends Metric { /** * {@inheritDoc} */ + @NonNull @Override default MetricType getMetricType() { return MetricType.ACCUMULATOR; @@ -47,6 +48,7 @@ default MetricType getMetricType() { /** * {@inheritDoc} */ + @NonNull @Override default DataType getDataType() { return DataType.INT; @@ -55,6 +57,7 @@ default DataType getDataType() { /** * {@inheritDoc} */ + @NonNull @Override default EnumSet getValueTypes() { return EnumSet.of(VALUE); @@ -63,9 +66,10 @@ default EnumSet getValueTypes() { /** * {@inheritDoc} */ + @NonNull @Override - default Long get(final ValueType valueType) { - Objects.requireNonNull(valueType, "valueType"); + default Long get(@NonNull final ValueType valueType) { + Objects.requireNonNull(valueType, "valueType must not be null"); if (valueType == VALUE) { return get(); } @@ -103,8 +107,8 @@ default Long get(final ValueType valueType) { */ final class Config extends MetricConfig { - private final LongBinaryOperator accumulator; - private final LongSupplier initializer; + private final @NonNull LongBinaryOperator accumulator; + private final @Nullable LongSupplier initializer; private final long initialValue; @@ -129,6 +133,24 @@ public Config(@NonNull final String category, @NonNull final String name) { this.initialValue = 0L; } + /** + * Constructor of {@code LongAccumulator.Config} + * + * By default, the {@link #getAccumulator() accumulator} is set to {@code Long::max}, + * the {@link #getInitialValue() initialValue} is set to {@code 0L}, + * and {@link #getFormat() format} is set to {@code "%d"}. + * + * @param category + * the kind of metric (metrics are grouped or filtered by this) + * @param name + * a short name for the metric + * @param description metric description + * @param unit metric unit + * @param accumulator accumulator for metric + * @param initializer initializer for metric + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces + */ private Config( @NonNull final String category, @NonNull final String name, @@ -140,7 +162,7 @@ private Config( final long initialValue) { super(category, name, description, unit, format); - this.accumulator = Objects.requireNonNull(accumulator, "accumulator"); + this.accumulator = Objects.requireNonNull(accumulator, "accumulator must not be null"); this.initializer = initializer; this.initialValue = initialValue; } @@ -148,6 +170,7 @@ private Config( /** * {@inheritDoc} */ + @NonNull @Override public LongAccumulator.Config withDescription(@NonNull final String description) { return new LongAccumulator.Config( @@ -164,6 +187,7 @@ public LongAccumulator.Config withDescription(@NonNull final String description) /** * {@inheritDoc} */ + @NonNull @Override public LongAccumulator.Config withUnit(@NonNull final String unit) { return new LongAccumulator.Config( @@ -183,8 +207,8 @@ public LongAccumulator.Config withUnit(@NonNull final String unit) { * @param format * the format-string * @return a new configuration-object with updated {@code format} - * @throws IllegalArgumentException - * if {@code format} is {@code null} or consists only of whitespaces + * @throws NullPointerException if {@code format} is {@code null} + * @throws IllegalArgumentException if {@code format} consists only of whitespaces */ @NonNull public LongAccumulator.Config withFormat(@NonNull final String format) { @@ -296,6 +320,7 @@ public LongAccumulator.Config withInitialValue(final long initialValue) { /** * {@inheritDoc} */ + @NonNull @Override public Class getResultClass() { return LongAccumulator.class; diff --git a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/LongGauge.java b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/LongGauge.java index 9e91b7160ca4..c839b0a68139 100644 --- a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/LongGauge.java +++ b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/LongGauge.java @@ -33,6 +33,7 @@ public interface LongGauge extends Metric { /** * {@inheritDoc} */ + @NonNull @Override default MetricType getMetricType() { return MetricType.GAUGE; @@ -41,6 +42,7 @@ default MetricType getMetricType() { /** * {@inheritDoc} */ + @NonNull @Override default DataType getDataType() { return DataType.INT; @@ -49,6 +51,7 @@ default DataType getDataType() { /** * {@inheritDoc} */ + @NonNull @Override default EnumSet getValueTypes() { return EnumSet.of(VALUE); @@ -57,9 +60,10 @@ default EnumSet getValueTypes() { /** * {@inheritDoc} */ + @NonNull @Override default Long get(@NonNull final ValueType valueType) { - Objects.requireNonNull(valueType, "valueType"); + Objects.requireNonNull(valueType, "valueType must not be null"); if (valueType == VALUE) { return get(); } @@ -99,14 +103,32 @@ final class Config extends MetricConfig { * the kind of metric (metrics are grouped or filtered by this) * @param name * a short name for the metric - * @throws IllegalArgumentException - * if one of the parameters is {@code null} or consists only of whitespaces + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces */ public Config(@NonNull final String category, @NonNull final String name) { super(category, name, "%d"); this.initialValue = 0L; } + /** + * Constructor of {@code LongGauge.Config} + * + * + * The {@link #getInitialValue() initialValue} is by default set to {@code 0L}, + * the {@link #getFormat() format} is set to "%d". + * + * @param category + * the kind of metric (metrics are grouped or filtered by this) + * @param name + * a short name for the metric + * @param description metric description + * @param unit metric unit + * @param format format for metric + * @param initialValue initialValue for metric + * @throws NullPointerException if one of the parameters is {@code null} + * @throws IllegalArgumentException if one of the parameters consists only of whitespaces + */ private Config( @NonNull final String category, @NonNull final String name, @@ -122,6 +144,7 @@ private Config( /** * {@inheritDoc} */ + @NonNull @Override public LongGauge.Config withDescription(@NonNull final String description) { return new LongGauge.Config( @@ -131,6 +154,7 @@ public LongGauge.Config withDescription(@NonNull final String description) { /** * {@inheritDoc} */ + @NonNull @Override public LongGauge.Config withUnit(@NonNull final String unit) { return new LongGauge.Config( @@ -143,8 +167,8 @@ public LongGauge.Config withUnit(@NonNull final String unit) { * @param format * the format-string * @return a new configuration-object with updated {@code format} - * @throws IllegalArgumentException - * if {@code format} is {@code null} or consists only of whitespaces + * @throws NullPointerException if {@code format} is {@code null} + * @throws IllegalArgumentException if {@code format} consists only of whitespaces */ @NonNull public LongGauge.Config withFormat(@NonNull final String format) { @@ -177,6 +201,7 @@ public LongGauge.Config withInitialValue(final long initialValue) { /** * {@inheritDoc} */ + @NonNull @Override public Class getResultClass() { return LongGauge.class; diff --git a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/Metric.java b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/Metric.java index d75f2403e8d5..ab75c5bde0dc 100644 --- a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/Metric.java +++ b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/Metric.java @@ -48,7 +48,7 @@ enum DataType { * * @return the unique identifier */ - default String getIdentifier() { + default @NonNull String getIdentifier() { return getCategory() + "." + getName(); } @@ -57,6 +57,7 @@ default String getIdentifier() { * * @return the category */ + @NonNull String getCategory(); /** @@ -64,6 +65,7 @@ default String getIdentifier() { * * @return the name */ + @NonNull String getName(); /** @@ -71,6 +73,7 @@ default String getIdentifier() { * * @return the description */ + @NonNull String getDescription(); /** @@ -78,6 +81,7 @@ default String getIdentifier() { * * @return the metric type */ + @NonNull MetricType getMetricType(); /** @@ -85,6 +89,7 @@ default String getIdentifier() { * * @return the data-type */ + @NonNull DataType getDataType(); /** @@ -92,6 +97,7 @@ default String getIdentifier() { * * @return the unit */ + @NonNull String getUnit(); /** @@ -99,11 +105,12 @@ default String getIdentifier() { * * @return the format */ + @NonNull String getFormat(); /** - * Returns a list of {@link ValueType}s that are provided by this {@code Metric}. - * The list of {@code ValueTypes} will always be in the same order. + * Returns a list of {@link ValueType}s that are provided by this {@code Metric}. The list of {@code ValueTypes} + * will always be in the same order. *

* All values returned by this method have to be supported by an implementing class. * @@ -115,30 +122,29 @@ default String getIdentifier() { /** * Returns the current value of the given {@link ValueType}. *

- * The option {@link ValueType#VALUE} has a special meaning in this context. It is always supported and returns - * the "main" value of the {@code Metric}. I.e. even if you do not know the type of {@code Metric}, it is - * always safe to call this method with the parameter {@code ValueType.VALUE}. + * The option {@link ValueType#VALUE} has a special meaning in this context. It is always supported and returns the + * "main" value of the {@code Metric}. I.e. even if you do not know the type of {@code Metric}, it is always safe to + * call this method with the parameter {@code ValueType.VALUE}. * - * @param valueType - * The {@code ValueType} of the requested value + * @param valueType The {@code ValueType} of the requested value * @return the current value - * @throws IllegalArgumentException - * if the given {@code ValueType} is {@code null} or not supported by this {@code Metric} + * @throws NullPointerException if the given {@code ValueType} is {@code null} + * @throws IllegalArgumentException if the given {@code ValueType} is not supported by this {@code Metric} */ + @NonNull Object get(@NonNull final ValueType valueType); /** - * This method resets a {@code Metric}. It is for example called after startup to ensure that the - * startup time is not taken into consideration. + * This method resets a {@code Metric}. It is for example called after startup to ensure that the startup time is + * not taken into consideration. */ void reset(); /** - * Overwritten {@code equals}-method. Two {@code Metric}-instances are considered equal, if they - * have the same {@code Class}, {@link #getCategory() category}, and {@link #getName() name}. + * Overwritten {@code equals}-method. Two {@code Metric}-instances are considered equal, if they have the same + * {@code Class}, {@link #getCategory() category}, and {@link #getName() name}. * - * @param other - * the other {@code Object} + * @param other the other {@code Object} * @return {@code true}, if both {@code Metric}-instances are considered equal, {@code false} otherwise */ @Override diff --git a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/MetricConfig.java b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/MetricConfig.java index 1cea3c41109a..4b19c46d733d 100644 --- a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/MetricConfig.java +++ b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/MetricConfig.java @@ -38,31 +38,28 @@ public abstract class MetricConfig getResultClass(); + public abstract @NonNull Class getResultClass(); /** * Create a {@code Metric} using the given {@link MetricsFactory} + *

+ * Implementation note: we use the double-dispatch pattern when creating a {@link Metric}. More details can be found + * at {@link Metrics#getOrCreate(MetricConfig)}. * - * Implementation note: we use the double-dispatch pattern when creating a {@link Metric}. More details - * can be found at {@link Metrics#getOrCreate(MetricConfig)}. - * - * @param factory - * the {@code MetricFactory} + * @param factory the {@code MetricFactory} * @return the new {@code Metric}-instance */ @NonNull diff --git a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/Metrics.java b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/Metrics.java index 67c45dab2683..e747645fcc7d 100644 --- a/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/Metrics.java +++ b/platform-sdk/swirlds-metrics-api/src/main/java/com/swirlds/metrics/api/Metrics.java @@ -22,6 +22,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; import java.util.Collection; +import java.util.Objects; /** * Entry-point to the metrics-system. @@ -46,11 +47,11 @@ public interface Metrics extends Startable { * @param name * the name of the wanted category * @return the {@code Metric} if one is found, {@code null} otherwise - * @throws IllegalArgumentException + * @throws NullPointerException * if one of the parameters is {@code null} */ @Nullable - Metric getMetric(final String category, final String name); + Metric getMetric(final @NonNull String category, final @NonNull String name); /** * Get all metrics with the given category. @@ -66,11 +67,11 @@ public interface Metrics extends Startable { * @param category * the category of the wanted metrics * @return all metrics that have the category or a sub-category - * @throws IllegalArgumentException + * @throws NullPointerException * if {@code category} is {@code null} */ @NonNull - Collection findMetricsByCategory(final String category); + Collection findMetricsByCategory(final @NonNull String category); /** * Get a list of all metrics that are currently registered. @@ -94,11 +95,13 @@ public interface Metrics extends Startable { * @param name * the name of the wanted category * @return the {@code value} of the {@link Metric}, if one is found, {@code null} otherwise - * @throws IllegalArgumentException + * @throws NullPointerException * if one of the parameters is {@code null} */ @Nullable - default Object getValue(final String category, final String name) { + default Object getValue(final @NonNull String category, final @NonNull String name) { + Objects.requireNonNull(category, "category must not be null"); + Objects.requireNonNull(name, "name must not be null"); final Metric metric = getMetric(category, name); return metric != null ? metric.get(VALUE) : null; } @@ -121,12 +124,12 @@ default void resetAll() { * @param * class of the {@code Metric} that will be returned * @return the registered {@code Metric} (either existing or newly generated) - * @throws IllegalArgumentException + * @throws NullPointerException * if {@code config} is {@code null} * @throws IllegalStateException * if a {@code Metric} with the same category and name exists, but has a different type */ - T getOrCreate(final MetricConfig config); + @NonNull T getOrCreate(final @NonNull MetricConfig config); /** * Remove the {@link Metric} with the given category and name @@ -135,10 +138,10 @@ default void resetAll() { * the category of the {@code Metric}, that should be removed * @param name * the name of the {@code Metric}, that should be removed - * @throws IllegalArgumentException + * @throws NullPointerException * if one of the parameters is {@code null} */ - void remove(final String category, final String name); + void remove(final @NonNull String category, final @NonNull String name); /** * Remove the {@link Metric}. @@ -147,10 +150,10 @@ default void resetAll() { * * @param metric * the {@code Metric}, that should be removed - * @throws IllegalArgumentException + * @throws NullPointerException * if ({code metric} is {@code null} */ - void remove(final Metric metric); + void remove(final @NonNull Metric metric); /** * Remove the {@link Metric} with the given configuration. @@ -159,28 +162,28 @@ default void resetAll() { * * @param config * the {@link MetricConfig} of the {@code Metric}, that should be removed - * @throws IllegalArgumentException + * @throws NullPointerException * if {@code config} is {@code null} */ - void remove(final MetricConfig config); + void remove(final @NonNull MetricConfig config); /** * Add an updater that will be called once per second. An updater should only be used to update metrics regularly. * * @param updater * the updater - * @throws IllegalArgumentException + * @throws NullPointerException * if {@code updater} is {@code null} */ - void addUpdater(final Runnable updater); + void addUpdater(final @NonNull Runnable updater); /** * Remove an updater that was previously added. * * @param updater * the updater - * @throws IllegalArgumentException + * @throws NullPointerException * if {@code updater} is {@code null} */ - void removeUpdater(final Runnable updater); + void removeUpdater(final @NonNull Runnable updater); } diff --git a/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/CounterTest.java b/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/CounterTest.java index 0bb43d2f0666..f8afc86f89b6 100644 --- a/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/CounterTest.java +++ b/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/CounterTest.java @@ -23,56 +23,28 @@ import static com.swirlds.metrics.api.Metric.ValueType.VALUE; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; @DisplayName("Testing Counter") class CounterTest { - private final Counter sut = new Counter() { - @Override - public long get() { - return 0; - } + private Counter sut; - @Override - public void add(long value) {} - - @Override - public void increment() {} - - @Override - public String getCategory() { - return null; - } - - @Override - public String getName() { - return null; - } - - @Override - public String getDescription() { - return null; - } - - @Override - public String getUnit() { - return null; - } - - @Override - public String getFormat() { - return null; - } - - @Override - public void reset() {} - }; + @BeforeEach + void setup() { + sut = Mockito.mock(Counter.class); + when(sut.get(Mockito.any())).thenCallRealMethod(); + when(sut.getMetricType()).thenCallRealMethod(); + when(sut.getDataType()).thenCallRealMethod(); + when(sut.getValueTypes()).thenCallRealMethod(); + } @Test void getMetricType() { @@ -91,12 +63,12 @@ void getValueTypes() { @Test void get_ShouldReturnValueByValueType() { - final Counter counter = spy(sut); + final Counter counter = sut; final Long value = counter.get(VALUE); assertThat(value).isEqualTo(sut.get()); - verify(counter, times(1)).get(); + verify(counter, times(2)).get(); } @Test diff --git a/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/DoubleAccumulatorTest.java b/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/DoubleAccumulatorTest.java index c1f2160c4709..02d71ffe6aa4 100644 --- a/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/DoubleAccumulatorTest.java +++ b/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/DoubleAccumulatorTest.java @@ -23,58 +23,28 @@ import static com.swirlds.metrics.api.Metric.ValueType.VALUE; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; @DisplayName("Testing DoubleAccumulator") class DoubleAccumulatorTest { - private final DoubleAccumulator sut = new DoubleAccumulator() { - @Override - public double get() { - return 0; - } + private DoubleAccumulator sut; - @Override - public double getInitialValue() { - return 0; - } - - @Override - public void update(double other) {} - - @Override - public String getCategory() { - return null; - } - - @Override - public String getName() { - return null; - } - - @Override - public String getDescription() { - return null; - } - - @Override - public String getUnit() { - return null; - } - - @Override - public String getFormat() { - return null; - } - - @Override - public void reset() {} - }; + @BeforeEach + void setup() { + sut = Mockito.mock(DoubleAccumulator.class); + when(sut.get(Mockito.any())).thenCallRealMethod(); + when(sut.getMetricType()).thenCallRealMethod(); + when(sut.getDataType()).thenCallRealMethod(); + when(sut.getValueTypes()).thenCallRealMethod(); + } @Test void getMetricType() { @@ -93,12 +63,10 @@ void getValueTypes() { @Test void get_ShouldReturnValueByValueType() { - final DoubleAccumulator accumulator = spy(sut); - - final Double value = accumulator.get(VALUE); + final Double value = sut.get(VALUE); assertThat(value).isEqualTo(sut.get()); - verify(accumulator, times(1)).get(); + verify(sut, times(2)).get(); } @Test diff --git a/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/DoubleGaugeTest.java b/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/DoubleGaugeTest.java index 29687c4a6d26..5f304bd8c4d2 100644 --- a/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/DoubleGaugeTest.java +++ b/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/DoubleGaugeTest.java @@ -23,53 +23,28 @@ import static com.swirlds.metrics.api.Metric.ValueType.VALUE; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; @DisplayName("Testing DoubleGauge") class DoubleGaugeTest { - private final DoubleGauge sut = new DoubleGauge() { - @Override - public double get() { - return 0; - } + private DoubleGauge sut; - @Override - public void set(double newValue) {} - - @Override - public String getCategory() { - return null; - } - - @Override - public String getName() { - return null; - } - - @Override - public String getDescription() { - return null; - } - - @Override - public String getUnit() { - return null; - } - - @Override - public String getFormat() { - return null; - } - - @Override - public void reset() {} - }; + @BeforeEach + void setup() { + sut = Mockito.mock(DoubleGauge.class); + when(sut.get(Mockito.any())).thenCallRealMethod(); + when(sut.getMetricType()).thenCallRealMethod(); + when(sut.getDataType()).thenCallRealMethod(); + when(sut.getValueTypes()).thenCallRealMethod(); + } @Test void getMetricType() { @@ -88,12 +63,10 @@ void getValueTypes() { @Test void get_ShouldReturnValueByValueType() { - final DoubleGauge gauge = spy(sut); - - final Double value = gauge.get(VALUE); + final Double value = sut.get(VALUE); assertThat(value).isEqualTo(sut.get()); - verify(gauge, times(1)).get(); + verify(sut, times(2)).get(); } @Test diff --git a/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/IntegerAccumulatorTest.java b/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/IntegerAccumulatorTest.java index 31ce17702355..c84176965dfb 100644 --- a/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/IntegerAccumulatorTest.java +++ b/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/IntegerAccumulatorTest.java @@ -23,58 +23,28 @@ import static com.swirlds.metrics.api.Metric.ValueType.VALUE; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; @DisplayName("Testing IntegerAccumulator") class IntegerAccumulatorTest { - private final IntegerAccumulator sut = new IntegerAccumulator() { - @Override - public int get() { - return 0; - } + private IntegerAccumulator sut; - @Override - public int getInitialValue() { - return 0; - } - - @Override - public void update(int other) {} - - @Override - public String getCategory() { - return null; - } - - @Override - public String getName() { - return null; - } - - @Override - public String getDescription() { - return null; - } - - @Override - public String getUnit() { - return null; - } - - @Override - public String getFormat() { - return null; - } - - @Override - public void reset() {} - }; + @BeforeEach + void setup() { + sut = Mockito.mock(IntegerAccumulator.class); + when(sut.get(Mockito.any())).thenCallRealMethod(); + when(sut.getMetricType()).thenCallRealMethod(); + when(sut.getDataType()).thenCallRealMethod(); + when(sut.getValueTypes()).thenCallRealMethod(); + } @Test void getMetricType() { @@ -93,12 +63,10 @@ void getValueTypes() { @Test void get_ShouldReturnValueByValueType() { - final IntegerAccumulator accumulator = spy(sut); - - final Integer value = accumulator.get(VALUE); + final Integer value = sut.get(VALUE); assertThat(value).isEqualTo(sut.get()); - verify(accumulator, times(1)).get(); + verify(sut, times(2)).get(); } @Test diff --git a/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/IntegerGaugeTest.java b/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/IntegerGaugeTest.java index 167e30972ab5..c2da980f89d4 100644 --- a/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/IntegerGaugeTest.java +++ b/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/IntegerGaugeTest.java @@ -23,53 +23,28 @@ import static com.swirlds.metrics.api.Metric.ValueType.VALUE; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; @DisplayName("Testing IntegerGauge") class IntegerGaugeTest { - private final IntegerGauge sut = new IntegerGauge() { - @Override - public int get() { - return 0; - } + private IntegerGauge sut; - @Override - public void set(int newValue) {} - - @Override - public String getCategory() { - return null; - } - - @Override - public String getName() { - return null; - } - - @Override - public String getDescription() { - return null; - } - - @Override - public String getUnit() { - return null; - } - - @Override - public String getFormat() { - return null; - } - - @Override - public void reset() {} - }; + @BeforeEach + void setup() { + sut = Mockito.mock(IntegerGauge.class); + when(sut.get(Mockito.any())).thenCallRealMethod(); + when(sut.getMetricType()).thenCallRealMethod(); + when(sut.getDataType()).thenCallRealMethod(); + when(sut.getValueTypes()).thenCallRealMethod(); + } @Test void getMetricType() { @@ -88,12 +63,11 @@ void getValueTypes() { @Test void get_ShouldReturnValueByValueType() { - final IntegerGauge gauge = spy(sut); - final Integer value = gauge.get(VALUE); + final Integer value = sut.get(VALUE); assertThat(value).isEqualTo(sut.get()); - verify(gauge, times(1)).get(); + verify(sut, times(2)).get(); } @Test diff --git a/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/LongAccumulatorTest.java b/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/LongAccumulatorTest.java index 6a67a55df8fc..21bdaf897c8e 100644 --- a/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/LongAccumulatorTest.java +++ b/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/LongAccumulatorTest.java @@ -23,58 +23,28 @@ import static com.swirlds.metrics.api.Metric.ValueType.VALUE; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; @DisplayName("Testing LongAccumulator") class LongAccumulatorTest { - private final LongAccumulator sut = new LongAccumulator() { - @Override - public long get() { - return 0; - } + private LongAccumulator sut; - @Override - public long getInitialValue() { - return 0; - } - - @Override - public void update(long other) {} - - @Override - public String getCategory() { - return null; - } - - @Override - public String getName() { - return null; - } - - @Override - public String getDescription() { - return null; - } - - @Override - public String getUnit() { - return null; - } - - @Override - public String getFormat() { - return null; - } - - @Override - public void reset() {} - }; + @BeforeEach + void setup() { + sut = Mockito.mock(LongAccumulator.class); + when(sut.get(Mockito.any())).thenCallRealMethod(); + when(sut.getMetricType()).thenCallRealMethod(); + when(sut.getDataType()).thenCallRealMethod(); + when(sut.getValueTypes()).thenCallRealMethod(); + } @Test void getMetricType() { @@ -93,12 +63,10 @@ void getValueTypes() { @Test void get_ShouldReturnValueByValueType() { - final LongAccumulator accumulator = spy(sut); - - final Long value = accumulator.get(VALUE); + final Long value = sut.get(VALUE); assertThat(value).isEqualTo(sut.get()); - verify(accumulator, times(1)).get(); + verify(sut, times(2)).get(); } @Test diff --git a/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/LongGaugeTest.java b/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/LongGaugeTest.java index ed7f2a0c140f..db65a370384f 100644 --- a/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/LongGaugeTest.java +++ b/platform-sdk/swirlds-metrics-api/src/test/java/com/swirlds/metrics/api/LongGaugeTest.java @@ -23,53 +23,28 @@ import static com.swirlds.metrics.api.Metric.ValueType.VALUE; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; -import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; @DisplayName("Testing LongGauge") class LongGaugeTest { - private final LongGauge sut = new LongGauge() { - @Override - public long get() { - return 0; - } + private LongGauge sut; - @Override - public void set(long newValue) {} - - @Override - public String getCategory() { - return null; - } - - @Override - public String getName() { - return null; - } - - @Override - public String getDescription() { - return null; - } - - @Override - public String getUnit() { - return null; - } - - @Override - public String getFormat() { - return null; - } - - @Override - public void reset() {} - }; + @BeforeEach + void setup() { + sut = Mockito.mock(LongGauge.class); + when(sut.get(Mockito.any())).thenCallRealMethod(); + when(sut.getMetricType()).thenCallRealMethod(); + when(sut.getDataType()).thenCallRealMethod(); + when(sut.getValueTypes()).thenCallRealMethod(); + } @Test void getMetricType() { @@ -88,12 +63,10 @@ void getValueTypes() { @Test void get_ShouldReturnValueByValueType() { - final LongGauge gauge = spy(sut); - - final Long value = gauge.get(VALUE); + final Long value = sut.get(VALUE); assertThat(value).isEqualTo(sut.get()); - verify(gauge, times(1)).get(); + verify(sut, times(2)).get(); } @Test