1 /* 2 * Copyright 2016-17, OpenCensus Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.opencensus.stats; 18 19 import com.google.auto.value.AutoValue; 20 import io.opencensus.common.Function; 21 import io.opencensus.stats.Measure.MeasureDouble; 22 import io.opencensus.stats.Measure.MeasureLong; 23 import javax.annotation.concurrent.Immutable; 24 25 /** 26 * Immutable representation of a Measurement. 27 * 28 * @since 0.8 29 */ 30 @Immutable 31 public abstract class Measurement { 32 33 /** 34 * Applies the given match function to the underlying data type. 35 * 36 * @since 0.8 37 */ match( Function<? super MeasurementDouble, T> p0, Function<? super MeasurementLong, T> p1, Function<? super Measurement, T> defaultFunction)38 public abstract <T> T match( 39 Function<? super MeasurementDouble, T> p0, 40 Function<? super MeasurementLong, T> p1, 41 Function<? super Measurement, T> defaultFunction); 42 43 /** 44 * Extracts the measured {@link Measure}. 45 * 46 * @since 0.8 47 */ getMeasure()48 public abstract Measure getMeasure(); 49 50 // Prevents this class from being subclassed anywhere else. Measurement()51 private Measurement() {} 52 53 /** 54 * {@code Double} typed {@link Measurement}. 55 * 56 * @since 0.8 57 */ 58 @Immutable 59 @AutoValue 60 public abstract static class MeasurementDouble extends Measurement { MeasurementDouble()61 MeasurementDouble() {} 62 63 /** 64 * Constructs a new {@link MeasurementDouble}. 65 * 66 * @since 0.8 67 */ create(MeasureDouble measure, double value)68 public static MeasurementDouble create(MeasureDouble measure, double value) { 69 return new AutoValue_Measurement_MeasurementDouble(measure, value); 70 } 71 72 @Override getMeasure()73 public abstract MeasureDouble getMeasure(); 74 75 /** 76 * Returns the value for the measure. 77 * 78 * @return the value for the measure. 79 * @since 0.8 80 */ getValue()81 public abstract double getValue(); 82 83 @Override match( Function<? super MeasurementDouble, T> p0, Function<? super MeasurementLong, T> p1, Function<? super Measurement, T> defaultFunction)84 public <T> T match( 85 Function<? super MeasurementDouble, T> p0, 86 Function<? super MeasurementLong, T> p1, 87 Function<? super Measurement, T> defaultFunction) { 88 return p0.apply(this); 89 } 90 } 91 92 /** 93 * {@code Long} typed {@link Measurement}. 94 * 95 * @since 0.8 96 */ 97 @Immutable 98 @AutoValue 99 public abstract static class MeasurementLong extends Measurement { MeasurementLong()100 MeasurementLong() {} 101 102 /** 103 * Constructs a new {@link MeasurementLong}. 104 * 105 * @since 0.8 106 */ create(MeasureLong measure, long value)107 public static MeasurementLong create(MeasureLong measure, long value) { 108 return new AutoValue_Measurement_MeasurementLong(measure, value); 109 } 110 111 @Override getMeasure()112 public abstract MeasureLong getMeasure(); 113 114 /** 115 * Returns the value for the measure. 116 * 117 * @return the value for the measure. 118 * @since 0.8 119 */ getValue()120 public abstract long getValue(); 121 122 @Override match( Function<? super MeasurementDouble, T> p0, Function<? super MeasurementLong, T> p1, Function<? super Measurement, T> defaultFunction)123 public <T> T match( 124 Function<? super MeasurementDouble, T> p0, 125 Function<? super MeasurementLong, T> p1, 126 Function<? super Measurement, T> defaultFunction) { 127 return p1.apply(this); 128 } 129 } 130 } 131