|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.gravitino.stats; |
| 20 | + |
| 21 | +import com.google.common.base.Preconditions; |
| 22 | +import org.apache.gravitino.rel.types.Type; |
| 23 | +import org.apache.gravitino.rel.types.Types; |
| 24 | + |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +/** |
| 29 | + * A class representing a collection of statistic values. |
| 30 | + */ |
| 31 | +public class StatisticValues { |
| 32 | + |
| 33 | + /** |
| 34 | + * A statistic value that holds a Boolean value. |
| 35 | + */ |
| 36 | + public static class BooleanValue implements StatisticValue<Boolean> { |
| 37 | + private final Boolean value; |
| 38 | + |
| 39 | + public BooleanValue(Boolean value) { |
| 40 | + this.value = value; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public Boolean value() { |
| 45 | + return value; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public Type dataType() { |
| 50 | + return Types.BooleanType.get(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * A statistic value that holds a Long value. |
| 56 | + */ |
| 57 | + public static class LongValue implements StatisticValue<Long> { |
| 58 | + private final Long value; |
| 59 | + |
| 60 | + public LongValue(Long value) { |
| 61 | + this.value = value; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Long value() { |
| 66 | + return value; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public Type dataType() { |
| 71 | + return Types.LongType.get(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * A statistic value that holds a Double value. |
| 77 | + */ |
| 78 | + public static class DoubleValue implements StatisticValue<Double> { |
| 79 | + private final Double value; |
| 80 | + |
| 81 | + /** |
| 82 | + * Creates a DoubleValue with the given double value. |
| 83 | + * @param value The double value to be held by this statistic value. |
| 84 | + */ |
| 85 | + public DoubleValue(Double value) { |
| 86 | + this.value = value; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public Double value() { |
| 91 | + return value; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public Type dataType() { |
| 96 | + return Types.DoubleType.get(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * A statistic value that holds a String value. |
| 102 | + */ |
| 103 | + public static class StringValue implements StatisticValue<String> { |
| 104 | + private final String value; |
| 105 | + |
| 106 | + /** |
| 107 | + * Creates a StringValue with the given string value. |
| 108 | + * @param value The string value to be held by this statistic value. |
| 109 | + */ |
| 110 | + public StringValue(String value) { |
| 111 | + this.value = value; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public String value() { |
| 116 | + return value; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public Type dataType() { |
| 121 | + return Types.StringType.get(); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * A statistic value that holds a List of other statistic values. |
| 127 | + */ |
| 128 | + public static class ListValue implements StatisticValue<List<StatisticValue>> { |
| 129 | + private final List<StatisticValue> values; |
| 130 | + |
| 131 | + /** |
| 132 | + * Creates a ListValue with the given list of statistic values. |
| 133 | + * @param values A list of StatisticValue instances. |
| 134 | + */ |
| 135 | + public ListValue(List<StatisticValue> values) { |
| 136 | + Preconditions.checkArgument(values != null && values.isEmpty(), "Values cannot be null or empty"); |
| 137 | + this.values = values; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public List<StatisticValue> value() { |
| 142 | + return values; |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public Type dataType() { |
| 147 | + return Types.ListType.notNull(values.get(0).dataType()); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * A statistic value that holds a Map of String keys to other statistic values. |
| 153 | + */ |
| 154 | + public static class ObjectValue implements StatisticValue<Map<String, StatisticValue>> { |
| 155 | + private final Map<String, StatisticValue> values; |
| 156 | + |
| 157 | + /** |
| 158 | + * Creates an ObjectValue with the given map of statistic values. |
| 159 | + * @param values A map where keys are String identifiers and values are StatisticValue instances. |
| 160 | + */ |
| 161 | + public ObjectValue(Map<String, StatisticValue> values) { |
| 162 | + Preconditions.checkArgument(values != null && !values.isEmpty(), "Values cannot be null or empty"); |
| 163 | + this.values = values; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public Map<String, StatisticValue> value() { |
| 168 | + return values; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public Type dataType() { |
| 173 | + return Types.StructType.of( |
| 174 | + values.entrySet().stream() |
| 175 | + .map( |
| 176 | + entry -> |
| 177 | + Types.StructType.Field.nullableField( |
| 178 | + entry.getKey(), entry.getValue().dataType())) |
| 179 | + .toArray(Types.StructType.Field[]::new)); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments