From ecf24c1a92394e4e0f35dda916e14f7a997769cf Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Fri, 26 Apr 2019 11:22:51 -0700 Subject: [PATCH] Make Measurement an opaque interface in the API. (#216) --- .../java/openconsensus/stats/Measurement.java | 97 +-------------- .../java/openconsensus/stats/NoopStats.java | 15 ++- .../sdk/stats/impl/MeasurementImpl.java | 115 ++++++++++++++++++ 3 files changed, 126 insertions(+), 101 deletions(-) create mode 100644 sdk/src/main/java/openconsensus/sdk/stats/impl/MeasurementImpl.java diff --git a/api/src/main/java/openconsensus/stats/Measurement.java b/api/src/main/java/openconsensus/stats/Measurement.java index 64f2fa27ac..3646412729 100644 --- a/api/src/main/java/openconsensus/stats/Measurement.java +++ b/api/src/main/java/openconsensus/stats/Measurement.java @@ -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}. - * - *

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}. - * - *

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 {} diff --git a/api/src/main/java/openconsensus/stats/NoopStats.java b/api/src/main/java/openconsensus/stats/NoopStats.java index a80a31b636..e98d83b1b6 100644 --- a/api/src/main/java/openconsensus/stats/NoopStats.java +++ b/api/src/main/java/openconsensus/stats/NoopStats.java @@ -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(); + } } diff --git a/sdk/src/main/java/openconsensus/sdk/stats/impl/MeasurementImpl.java b/sdk/src/main/java/openconsensus/sdk/stats/impl/MeasurementImpl.java new file mode 100644 index 0000000000..e2a63662db --- /dev/null +++ b/sdk/src/main/java/openconsensus/sdk/stats/impl/MeasurementImpl.java @@ -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}. + * + *

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}. + * + *

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(); + } +}