|
| 1 | +// Copyright 2023 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.firebase.firestore; |
| 16 | + |
| 17 | +import androidx.annotation.NonNull; |
| 18 | +import androidx.annotation.Nullable; |
| 19 | +import androidx.annotation.RestrictTo; |
| 20 | +import java.util.Objects; |
| 21 | + |
| 22 | +// TODO(sumavg): Remove the `hide` and scope annotations. |
| 23 | +/** @hide */ |
| 24 | +@RestrictTo(RestrictTo.Scope.LIBRARY) |
| 25 | +public abstract class AggregateField { |
| 26 | + @Nullable private final FieldPath fieldPath; |
| 27 | + |
| 28 | + @NonNull private final String operator; |
| 29 | + |
| 30 | + @NonNull private final String alias; |
| 31 | + |
| 32 | + private AggregateField(@Nullable FieldPath fieldPath, @NonNull String operator) { |
| 33 | + this.fieldPath = fieldPath; |
| 34 | + this.operator = operator; |
| 35 | + |
| 36 | + // Use $operator_$field format if it's an aggregation of a specific field. For example: sum_foo. |
| 37 | + // Use $operator format if there's no field. For example: count. |
| 38 | + this.alias = operator + (fieldPath == null ? "" : "_" + fieldPath); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Returns the field on which the aggregation takes place. Returns an empty string if there's no |
| 43 | + * field (e.g. for count). |
| 44 | + */ |
| 45 | + @RestrictTo(RestrictTo.Scope.LIBRARY) |
| 46 | + @NonNull |
| 47 | + public String getFieldPath() { |
| 48 | + return fieldPath == null ? "" : fieldPath.toString(); |
| 49 | + } |
| 50 | + |
| 51 | + /** Returns the alias used internally for this aggregate field. */ |
| 52 | + @RestrictTo(RestrictTo.Scope.LIBRARY) |
| 53 | + @NonNull |
| 54 | + public String getAlias() { |
| 55 | + return alias; |
| 56 | + } |
| 57 | + |
| 58 | + /** Returns a string representation of this aggregation's operator. For example: "sum" */ |
| 59 | + @RestrictTo(RestrictTo.Scope.LIBRARY) |
| 60 | + @NonNull |
| 61 | + public String getOperator() { |
| 62 | + return operator; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Returns true if the given object is equal to this object. Two `AggregateField` objects are |
| 67 | + * considered equal if they have the same operator and operate on the same field. |
| 68 | + */ |
| 69 | + @Override |
| 70 | + public boolean equals(Object other) { |
| 71 | + if (this == other) { |
| 72 | + return true; |
| 73 | + } |
| 74 | + if (!(other instanceof AggregateField)) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + AggregateField otherAggregateField = (AggregateField) other; |
| 79 | + if (fieldPath == null || otherAggregateField.fieldPath == null) { |
| 80 | + return fieldPath == null && otherAggregateField.fieldPath == null; |
| 81 | + } |
| 82 | + return operator.equals(otherAggregateField.getOperator()) |
| 83 | + && getFieldPath().equals(otherAggregateField.getFieldPath()); |
| 84 | + } |
| 85 | + |
| 86 | + /** Calculates and returns the hash code for this object. */ |
| 87 | + @Override |
| 88 | + public int hashCode() { |
| 89 | + return Objects.hash(getOperator(), getFieldPath()); |
| 90 | + } |
| 91 | + |
| 92 | + @NonNull |
| 93 | + public static CountAggregateField count() { |
| 94 | + return new CountAggregateField(); |
| 95 | + } |
| 96 | + |
| 97 | + @NonNull |
| 98 | + public static SumAggregateField sum(@NonNull String field) { |
| 99 | + return new SumAggregateField(FieldPath.fromDotSeparatedPath(field)); |
| 100 | + } |
| 101 | + |
| 102 | + @NonNull |
| 103 | + public static SumAggregateField sum(@NonNull FieldPath fieldPath) { |
| 104 | + return new SumAggregateField(fieldPath); |
| 105 | + } |
| 106 | + |
| 107 | + @NonNull |
| 108 | + public static AverageAggregateField average(@NonNull String field) { |
| 109 | + return new AverageAggregateField(FieldPath.fromDotSeparatedPath(field)); |
| 110 | + } |
| 111 | + |
| 112 | + @NonNull |
| 113 | + public static AverageAggregateField average(@NonNull FieldPath fieldPath) { |
| 114 | + return new AverageAggregateField(fieldPath); |
| 115 | + } |
| 116 | + |
| 117 | + public static class CountAggregateField extends AggregateField { |
| 118 | + private CountAggregateField() { |
| 119 | + super(null, "count"); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public static class SumAggregateField extends AggregateField { |
| 124 | + private SumAggregateField(@NonNull FieldPath fieldPath) { |
| 125 | + super(fieldPath, "sum"); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public static class AverageAggregateField extends AggregateField { |
| 130 | + private AverageAggregateField(@NonNull FieldPath fieldPath) { |
| 131 | + super(fieldPath, "average"); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments