Skip to content

Commit

Permalink
chore: Add NotNull/Nullable annotations to Metrics module (#11532)
Browse files Browse the repository at this point in the history
Signed-off-by: mxtartaglia <maxi@swirldslabs.com>
Signed-off-by: Timo Brandstätter <timo@swirldslabs.com>
  • Loading branch information
mxtartaglia-sl authored and timo0 committed Feb 16, 2024
1 parent e0c236b commit 6745028
Show file tree
Hide file tree
Showing 61 changed files with 875 additions and 1,030 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public interface DurationGauge extends Metric {
/**
* {@inheritDoc}
*/
@NonNull
@Override
default MetricType getMetricType() {
return MetricType.GAUGE;
Expand All @@ -44,6 +45,7 @@ default MetricType getMetricType() {
/**
* {@inheritDoc}
*/
@NonNull
@Override
default DataType getDataType() {
return DataType.FLOAT;
Expand All @@ -52,6 +54,7 @@ default DataType getDataType() {
/**
* {@inheritDoc}
*/
@NonNull
@Override
default EnumSet<ValueType> getValueTypes() {
return EnumSet.of(VALUE);
Expand All @@ -60,9 +63,10 @@ default EnumSet<ValueType> 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();
}
Expand Down Expand Up @@ -93,29 +97,39 @@ default Double get(@NonNull final ValueType valueType) {
*/
final class Config extends PlatformMetricConfig<DurationGauge, Config> {

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}
*
* @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;
Expand All @@ -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());
Expand All @@ -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");
Expand All @@ -143,13 +159,15 @@ public DurationGauge.Config withUnit(@NonNull final String unit) {
*
* @return the {@code timeUnit}
*/
@NonNull
public ChronoUnit getTimeUnit() {
return timeUnit;
}

/**
* {@inheritDoc}
*/
@NonNull
@Override
public Class<DurationGauge> getResultClass() {
return DurationGauge.class;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public interface FunctionGauge<T> extends Metric {
/**
* {@inheritDoc}
*/
@NonNull
@Override
default MetricType getMetricType() {
return MetricType.GAUGE;
Expand All @@ -49,6 +50,7 @@ default MetricType getMetricType() {
/**
* {@inheritDoc}
*/
@NonNull
@Override
default EnumSet<ValueType> getValueTypes() {
return EnumSet.of(VALUE);
Expand All @@ -57,9 +59,10 @@ default EnumSet<ValueType> 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();
}
Expand Down Expand Up @@ -94,19 +97,35 @@ final class Config<T> extends PlatformMetricConfig<FunctionGauge<T>, Config<T>>
* 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,
@NonNull final String name,
@NonNull final Class<T> type,
@NonNull final Supplier<T> 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,
Expand All @@ -116,13 +135,14 @@ private Config(
@NonNull final Class<T> type,
@NonNull final Supplier<T> 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<T> withDescription(@NonNull final String description) {
return new FunctionGauge.Config<>(
Expand All @@ -132,6 +152,7 @@ public FunctionGauge.Config<T> withDescription(@NonNull final String description
/**
* {@inheritDoc}
*/
@NonNull
@Override
public FunctionGauge.Config<T> withUnit(@NonNull final String unit) {
return new FunctionGauge.Config<>(
Expand Down Expand Up @@ -176,6 +197,7 @@ public Supplier<T> getSupplier() {
/**
* {@inheritDoc}
*/
@NonNull
@SuppressWarnings("unchecked")
@Override
public Class<FunctionGauge<T>> getResultClass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public interface IntegerPairAccumulator<T> extends Metric {
return ((double) sum) / count;
};

@NonNull
@Override
default MetricType getMetricType() {
return MetricType.ACCUMULATOR;
Expand All @@ -55,6 +56,7 @@ default MetricType getMetricType() {
/**
* {@inheritDoc}
*/
@NonNull
@Override
default EnumSet<ValueType> getValueTypes() {
return EnumSet.of(VALUE);
Expand All @@ -63,9 +65,10 @@ default EnumSet<ValueType> 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();
}
Expand All @@ -77,6 +80,7 @@ default T get(@NonNull final ValueType valueType) {
*
* @return the current value
*/
@NonNull
T get();

/**
Expand Down Expand Up @@ -110,15 +114,15 @@ default T get(@NonNull final ValueType valueType) {
*/
final class Config<T> extends PlatformMetricConfig<IntegerPairAccumulator<T>, Config<T>> {

private final Class<T> type;
private final @NonNull Class<T> type;

private final BiFunction<Integer, Integer, T> resultFunction;
private final @NonNull BiFunction<Integer, Integer, T> 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;

Expand All @@ -140,14 +144,33 @@ public Config(
@NonNull final BiFunction<Integer, Integer, T> 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}
* <p>
* 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,
Expand All @@ -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<T> withDescription(@NonNull final String description) {
return new IntegerPairAccumulator.Config<>(
Expand All @@ -192,6 +216,7 @@ public IntegerPairAccumulator.Config<T> withDescription(@NonNull final String de
/**
* {@inheritDoc}
*/
@NonNull
@Override
public IntegerPairAccumulator.Config<T> withUnit(@NonNull final String unit) {
return new IntegerPairAccumulator.Config<>(
Expand All @@ -213,7 +238,9 @@ public IntegerPairAccumulator.Config<T> 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<T> withFormat(@NonNull final String format) {
Expand Down Expand Up @@ -427,6 +454,7 @@ public IntegerPairAccumulator.Config<T> withRightInitialValue(final int rightIni
/**
* {@inheritDoc}
*/
@NonNull
@SuppressWarnings("unchecked")
@Override
public Class<IntegerPairAccumulator<T>> getResultClass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit 6745028

Please sign in to comment.