1 /* 2 * Copyright 2018, 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.metrics.export; 18 19 import com.google.auto.value.AutoValue; 20 import io.opencensus.common.ExperimentalApi; 21 import io.opencensus.common.Function; 22 import javax.annotation.concurrent.Immutable; 23 24 /** 25 * The actual point value for a {@link Point}. 26 * 27 * <p>Currently there are three types of {@link Value}: 28 * 29 * <ul> 30 * <li>{@code double} 31 * <li>{@code long} 32 * <li>{@link Distribution} 33 * </ul> 34 * 35 * <p>Each {@link Point} contains exactly one of the three {@link Value} types. 36 * 37 * @since 0.17 38 */ 39 @ExperimentalApi 40 @Immutable 41 public abstract class Value { 42 Value()43 Value() {} 44 45 /** 46 * Returns a double {@link Value}. 47 * 48 * @param value value in double. 49 * @return a double {@code Value}. 50 * @since 0.17 51 */ doubleValue(double value)52 public static Value doubleValue(double value) { 53 return ValueDouble.create(value); 54 } 55 56 /** 57 * Returns a long {@link Value}. 58 * 59 * @param value value in long. 60 * @return a long {@code Value}. 61 * @since 0.17 62 */ longValue(long value)63 public static Value longValue(long value) { 64 return ValueLong.create(value); 65 } 66 67 /** 68 * Returns a {@link Distribution} {@link Value}. 69 * 70 * @param value value in {@link Distribution}. 71 * @return a {@code Distribution} {@code Value}. 72 * @since 0.17 73 */ distributionValue(Distribution value)74 public static Value distributionValue(Distribution value) { 75 return ValueDistribution.create(value); 76 } 77 78 /** 79 * Returns a {@link Summary} {@link Value}. 80 * 81 * @param value value in {@link Summary}. 82 * @return a {@code Summary} {@code Value}. 83 * @since 0.17 84 */ summaryValue(Summary value)85 public static Value summaryValue(Summary value) { 86 return ValueSummary.create(value); 87 } 88 89 /** 90 * Applies the given match function to the underlying data type. 91 * 92 * @since 0.17 93 */ match( Function<? super Double, T> doubleFunction, Function<? super Long, T> longFunction, Function<? super Distribution, T> distributionFunction, Function<? super Summary, T> summaryFunction, Function<? super Value, T> defaultFunction)94 public abstract <T> T match( 95 Function<? super Double, T> doubleFunction, 96 Function<? super Long, T> longFunction, 97 Function<? super Distribution, T> distributionFunction, 98 Function<? super Summary, T> summaryFunction, 99 Function<? super Value, T> defaultFunction); 100 101 /** A 64-bit double-precision floating-point {@link Value}. */ 102 @AutoValue 103 @Immutable 104 abstract static class ValueDouble extends Value { 105 ValueDouble()106 ValueDouble() {} 107 108 @Override match( Function<? super Double, T> doubleFunction, Function<? super Long, T> longFunction, Function<? super Distribution, T> distributionFunction, Function<? super Summary, T> summaryFunction, Function<? super Value, T> defaultFunction)109 public final <T> T match( 110 Function<? super Double, T> doubleFunction, 111 Function<? super Long, T> longFunction, 112 Function<? super Distribution, T> distributionFunction, 113 Function<? super Summary, T> summaryFunction, 114 Function<? super Value, T> defaultFunction) { 115 return doubleFunction.apply(getValue()); 116 } 117 118 /** 119 * Creates a {@link ValueDouble}. 120 * 121 * @param value the value in double. 122 * @return a {@code ValueDouble}. 123 */ create(double value)124 static ValueDouble create(double value) { 125 return new AutoValue_Value_ValueDouble(value); 126 } 127 128 /** 129 * Returns the double value. 130 * 131 * @return the double value. 132 */ getValue()133 abstract double getValue(); 134 } 135 136 /** A 64-bit integer {@link Value}. */ 137 @AutoValue 138 @Immutable 139 abstract static class ValueLong extends Value { 140 ValueLong()141 ValueLong() {} 142 143 @Override match( Function<? super Double, T> doubleFunction, Function<? super Long, T> longFunction, Function<? super Distribution, T> distributionFunction, Function<? super Summary, T> summaryFunction, Function<? super Value, T> defaultFunction)144 public final <T> T match( 145 Function<? super Double, T> doubleFunction, 146 Function<? super Long, T> longFunction, 147 Function<? super Distribution, T> distributionFunction, 148 Function<? super Summary, T> summaryFunction, 149 Function<? super Value, T> defaultFunction) { 150 return longFunction.apply(getValue()); 151 } 152 153 /** 154 * Creates a {@link ValueLong}. 155 * 156 * @param value the value in long. 157 * @return a {@code ValueLong}. 158 */ create(long value)159 static ValueLong create(long value) { 160 return new AutoValue_Value_ValueLong(value); 161 } 162 163 /** 164 * Returns the long value. 165 * 166 * @return the long value. 167 */ getValue()168 abstract long getValue(); 169 } 170 171 /** 172 * {@link ValueDistribution} contains summary statistics for a population of values. It optionally 173 * contains a histogram representing the distribution of those values across a set of buckets. 174 */ 175 @AutoValue 176 @Immutable 177 abstract static class ValueDistribution extends Value { 178 ValueDistribution()179 ValueDistribution() {} 180 181 @Override match( Function<? super Double, T> doubleFunction, Function<? super Long, T> longFunction, Function<? super Distribution, T> distributionFunction, Function<? super Summary, T> summaryFunction, Function<? super Value, T> defaultFunction)182 public final <T> T match( 183 Function<? super Double, T> doubleFunction, 184 Function<? super Long, T> longFunction, 185 Function<? super Distribution, T> distributionFunction, 186 Function<? super Summary, T> summaryFunction, 187 Function<? super Value, T> defaultFunction) { 188 return distributionFunction.apply(getValue()); 189 } 190 191 /** 192 * Creates a {@link ValueDistribution}. 193 * 194 * @param value the {@link Distribution} value. 195 * @return a {@code ValueDistribution}. 196 */ create(Distribution value)197 static ValueDistribution create(Distribution value) { 198 return new AutoValue_Value_ValueDistribution(value); 199 } 200 201 /** 202 * Returns the {@link Distribution} value. 203 * 204 * @return the {@code Distribution} value. 205 */ getValue()206 abstract Distribution getValue(); 207 } 208 209 /** 210 * {@link ValueSummary} contains a snapshot representing values calculated over an arbitrary time 211 * window. 212 */ 213 @AutoValue 214 @Immutable 215 abstract static class ValueSummary extends Value { 216 ValueSummary()217 ValueSummary() {} 218 219 @Override match( Function<? super Double, T> doubleFunction, Function<? super Long, T> longFunction, Function<? super Distribution, T> distributionFunction, Function<? super Summary, T> summaryFunction, Function<? super Value, T> defaultFunction)220 public final <T> T match( 221 Function<? super Double, T> doubleFunction, 222 Function<? super Long, T> longFunction, 223 Function<? super Distribution, T> distributionFunction, 224 Function<? super Summary, T> summaryFunction, 225 Function<? super Value, T> defaultFunction) { 226 return summaryFunction.apply(getValue()); 227 } 228 229 /** 230 * Creates a {@link ValueSummary}. 231 * 232 * @param value the {@link Summary} value. 233 * @return a {@code ValueSummary}. 234 */ create(Summary value)235 static ValueSummary create(Summary value) { 236 return new AutoValue_Value_ValueSummary(value); 237 } 238 239 /** 240 * Returns the {@link Summary} value. 241 * 242 * @return the {@code Summary} value. 243 */ getValue()244 abstract Summary getValue(); 245 } 246 } 247