Make Measurement an opaque interface in the API. (#216)
This commit is contained in:
parent
96a34987e3
commit
ecf24c1a92
|
|
@ -16,105 +16,12 @@
|
|||
|
||||
package openconsensus.stats;
|
||||
|
||||
import com.google.auto.value.AutoValue;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* Immutable representation of a Measurement.
|
||||
* Immutable representation of a {@code Measurement}.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
@Immutable
|
||||
public abstract class Measurement {
|
||||
|
||||
/**
|
||||
* Extracts the measured {@link Measure}.
|
||||
*
|
||||
* @return the {@link Measure} if this measurement.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public abstract Measure getMeasure();
|
||||
|
||||
/**
|
||||
* Returns the double value for the {@link Measurement}.
|
||||
*
|
||||
* <p>This method should only be called with {@link MeasurementDouble}.
|
||||
*
|
||||
* @return the double value.
|
||||
* @throws UnsupportedOperationException if the {@code Measure} type is not {@link
|
||||
* Measure.Type#DOUBLE}.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public double getDoubleValue() {
|
||||
throw new UnsupportedOperationException("This type can only return double data");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the long value for the {@link Measurement}.
|
||||
*
|
||||
* <p>This method should only be called with {@link MeasurementLong}.
|
||||
*
|
||||
* @return the long value.
|
||||
* @throws UnsupportedOperationException if the {@code Measure} type is not {@link
|
||||
* Measure.Type#LONG}.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public long getLongValue() {
|
||||
throw new UnsupportedOperationException("This type can only return long data");
|
||||
}
|
||||
|
||||
// Prevents this class from being subclassed anywhere else.
|
||||
private Measurement() {}
|
||||
|
||||
/**
|
||||
* {@code double} typed {@link Measurement}.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
@Immutable
|
||||
@AutoValue
|
||||
public abstract static class MeasurementDouble extends Measurement {
|
||||
MeasurementDouble() {}
|
||||
|
||||
/**
|
||||
* Constructs a new {@link MeasurementDouble}.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static MeasurementDouble create(Measure measure, double value) {
|
||||
return new AutoValue_Measurement_MeasurementDouble(measure, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract Measure getMeasure();
|
||||
|
||||
@Override
|
||||
public abstract double getDoubleValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code long} typed {@link Measurement}.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
@Immutable
|
||||
@AutoValue
|
||||
public abstract static class MeasurementLong extends Measurement {
|
||||
MeasurementLong() {}
|
||||
|
||||
/**
|
||||
* Constructs a new {@link MeasurementLong}.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
public static MeasurementLong create(Measure measure, long value) {
|
||||
return new AutoValue_Measurement_MeasurementLong(measure, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract Measure getMeasure();
|
||||
|
||||
@Override
|
||||
public abstract long getLongValue();
|
||||
}
|
||||
}
|
||||
public interface Measurement {}
|
||||
|
|
|
|||
|
|
@ -21,8 +21,6 @@ import javax.annotation.concurrent.Immutable;
|
|||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import openconsensus.internal.StringUtils;
|
||||
import openconsensus.internal.Utils;
|
||||
import openconsensus.stats.Measurement.MeasurementDouble;
|
||||
import openconsensus.stats.Measurement.MeasurementLong;
|
||||
import openconsensus.tags.TagMap;
|
||||
import openconsensus.trace.SpanContext;
|
||||
|
||||
|
|
@ -89,21 +87,21 @@ public final class NoopStats {
|
|||
}
|
||||
|
||||
@Override
|
||||
public MeasurementDouble createDoubleMeasurement(double value) {
|
||||
public Measurement createDoubleMeasurement(double value) {
|
||||
if (type != Type.DOUBLE) {
|
||||
throw new UnsupportedOperationException("This type can only create double measurement");
|
||||
}
|
||||
Utils.checkArgument(value >= 0.0, "Unsupported negative values.");
|
||||
return MeasurementDouble.create(this, value);
|
||||
return NoopMeasurement.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MeasurementLong createLongMeasurement(long value) {
|
||||
public Measurement createLongMeasurement(long value) {
|
||||
if (type != Type.LONG) {
|
||||
throw new UnsupportedOperationException("This type can only create long measurement");
|
||||
}
|
||||
Utils.checkArgument(value >= 0, "Unsupported negative values.");
|
||||
return MeasurementLong.create(this, value);
|
||||
return NoopMeasurement.INSTANCE;
|
||||
}
|
||||
|
||||
private static final class NoopBuilder implements Measure.Builder {
|
||||
|
|
@ -133,4 +131,9 @@ public final class NoopStats {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Immutable
|
||||
private static final class NoopMeasurement implements Measurement {
|
||||
private static final Measurement INSTANCE = new NoopMeasurement();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,115 @@
|
|||
/*
|
||||
* Copyright 2019, OpenConsensus Authors
|
||||
*
|
||||
* 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 openconsensus.sdk.stats.impl;
|
||||
|
||||
import com.google.auto.value.AutoValue;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import openconsensus.stats.Measure;
|
||||
import openconsensus.stats.Measurement;
|
||||
|
||||
abstract class MeasurementImpl implements Measurement {
|
||||
/**
|
||||
* Extracts the measured {@link Measure}.
|
||||
*
|
||||
* @return the {@link Measure} if this measurement.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
abstract Measure getMeasure();
|
||||
|
||||
/**
|
||||
* Returns the double value for the {@link openconsensus.stats.Measurement}.
|
||||
*
|
||||
* <p>This method should only be called with {@link MeasurementDouble}.
|
||||
*
|
||||
* @return the double value.
|
||||
* @throws UnsupportedOperationException if the {@code Measure} type is not {@link
|
||||
* Measure.Type#DOUBLE}.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
double getDoubleValue() {
|
||||
throw new UnsupportedOperationException("This type can only return double data");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the long value for the {@link openconsensus.stats.Measurement}.
|
||||
*
|
||||
* <p>This method should only be called with {@link MeasurementLong}.
|
||||
*
|
||||
* @return the long value.
|
||||
* @throws UnsupportedOperationException if the {@code Measure} type is not {@link
|
||||
* Measure.Type#LONG}.
|
||||
* @since 0.1.0
|
||||
*/
|
||||
long getLongValue() {
|
||||
throw new UnsupportedOperationException("This type can only return long data");
|
||||
}
|
||||
|
||||
// Prevents this class from being subclassed anywhere else.
|
||||
private MeasurementImpl() {}
|
||||
|
||||
/**
|
||||
* {@code double} typed {@link openconsensus.stats.Measurement}.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
@Immutable
|
||||
@AutoValue
|
||||
abstract static class MeasurementDouble extends MeasurementImpl {
|
||||
MeasurementDouble() {}
|
||||
|
||||
/**
|
||||
* Constructs a new {@link MeasurementDouble}.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
static MeasurementDouble create(Measure measure, double value) {
|
||||
return new AutoValue_MeasurementImpl_MeasurementDouble(measure, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
abstract Measure getMeasure();
|
||||
|
||||
@Override
|
||||
abstract double getDoubleValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code long} typed {@link openconsensus.stats.Measurement}.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
@Immutable
|
||||
@AutoValue
|
||||
abstract static class MeasurementLong extends MeasurementImpl {
|
||||
MeasurementLong() {}
|
||||
|
||||
/**
|
||||
* Constructs a new {@link MeasurementLong}.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
static MeasurementLong create(Measure measure, long value) {
|
||||
return new AutoValue_MeasurementImpl_MeasurementLong(measure, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
abstract Measure getMeasure();
|
||||
|
||||
@Override
|
||||
abstract long getLongValue();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue