com.swirlds.common.metrics.FunctionGauge Maven / Gradle / Ivy
Show all versions of swirlds-common Show documentation
/*
* Copyright (C) 2016-2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.swirlds.common.metrics;
import static com.swirlds.metrics.api.Metric.ValueType.VALUE;
import com.swirlds.base.utility.ToStringBuilder;
import com.swirlds.metrics.api.Metric;
import com.swirlds.metrics.api.MetricType;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.EnumSet;
import java.util.Objects;
import java.util.function.Supplier;
/**
* A {@code FunctionGauge} maintains a single value.
*
* Unlike the other gauges, the value of a {@code FunctionGauge} is not explicitly set. Instead,
* a {@link java.util.function.Supplier} has to be specified, which reads the current value of this gauge.
*
* Only the current value is stored, no history or distribution is kept.
*
* @param the type of the contained value
*/
public interface FunctionGauge extends Metric {
/**
* {@inheritDoc}
*/
@NonNull
@Override
default MetricType getMetricType() {
return MetricType.GAUGE;
}
/**
* {@inheritDoc}
*/
@NonNull
@Override
default EnumSet getValueTypes() {
return EnumSet.of(VALUE);
}
/**
* {@inheritDoc}
*/
@NonNull
@Override
default T get(@NonNull final ValueType valueType) {
Objects.requireNonNull(valueType, "valueType must not be null");
if (valueType == VALUE) {
return get();
}
throw new IllegalArgumentException("Unsupported ValueType: " + valueType);
}
/**
* Get the current value
*
* @return the current value
*/
T get();
/**
* Configuration of a {@link FunctionGauge}
*
* @param the type of the value that will be contained in the {@code FunctionGauge}
*/
final class Config extends PlatformMetricConfig, Config> {
private final Class type;
private final Supplier supplier;
/**
* 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 type
* the type of the values this {@code FunctionGauge} returns
* @param supplier
* the {@code Supplier} of the value of this {@code Gauge}
* @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 type,
@NonNull final Supplier supplier) {
super(category, name, "%s");
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,
@NonNull final String description,
@NonNull final String unit,
@NonNull final String format,
@NonNull final Class type,
@NonNull final Supplier supplier) {
super(category, name, description, unit, format);
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<>(
getCategory(), getName(), description, getUnit(), getFormat(), getType(), getSupplier());
}
/**
* {@inheritDoc}
*/
@NonNull
@Override
public FunctionGauge.Config withUnit(@NonNull final String unit) {
return new FunctionGauge.Config<>(
getCategory(), getName(), getDescription(), unit, getFormat(), getType(), getSupplier());
}
/**
* Sets the {@link Metric#getFormat() Metric.format} in fluent style.
*
* @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
*/
@NonNull
public FunctionGauge.Config withFormat(@NonNull final String format) {
return new FunctionGauge.Config<>(
getCategory(), getName(), getDescription(), getUnit(), format, getType(), getSupplier());
}
/**
* Getter of the type of the returned values
*
* @return the type of the returned values
*/
@NonNull
public Class getType() {
return type;
}
/**
* Getter of the {@code supplier}
*
* @return the {@code supplier}
*/
@NonNull
public Supplier getSupplier() {
return supplier;
}
/**
* {@inheritDoc}
*/
@NonNull
@SuppressWarnings("unchecked")
@Override
public Class> getResultClass() {
return (Class>) (Class>) FunctionGauge.class;
}
/**
* {@inheritDoc}
*/
@Override
@NonNull
public FunctionGauge create(@NonNull final PlatformMetricsFactory factory) {
return factory.createFunctionGauge(this);
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return new ToStringBuilder(this)
.appendSuper(super.toString())
.append("type", type.getName())
.toString();
}
}
}