This repository was archived by the owner on Feb 24, 2026. It is now read-only.
File tree
Expand file treeCollapse file tree
Open diff view settings
google-cloud-datastore/src main/java/com/google/cloud/datastore test/java/com/google/cloud/datastore
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: google-cloud-datastore/src/main/java/com/google/cloud/datastore/AggregationResult.java +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -84,6 +84,26 @@ public Double getDouble(String alias) { |
84 | 84 | } |
85 | 85 | } |
86 | 86 |
|
| 87 | + /** |
| 88 | + * Returns a result value for the given alias. |
| 89 | + * |
| 90 | + * @param alias A custom alias provided in the query or an autogenerated alias in the form of |
| 91 | + * 'property_\d' |
| 92 | + * @return An aggregation result value as a {@link Number} for the given alias. |
| 93 | + */ |
| 94 | + public Number getNumber(String alias) { |
| 95 | + Value<?> value = properties.get(alias); |
| 96 | + switch (value.getType()) { |
| 97 | + case LONG: |
| 98 | + return (Long) value.get(); |
| 99 | + case DOUBLE: |
| 100 | + return (Double) value.get(); |
| 101 | + default: |
| 102 | + throw new RuntimeException( |
| 103 | + String.format("Unsupported type %s received for alias '%s'.", value.getType(), alias)); |
| 104 | + } |
| 105 | + } |
| 106 | + |
87 | 107 | @Override |
88 | 108 | public boolean equals(Object o) { |
89 | 109 | if (this == o) { |
|
Collapse file
Copy file name to clipboardExpand all lines: google-cloud-datastore/src/test/java/com/google/cloud/datastore/AggregationResultTest.java +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -77,5 +77,23 @@ public void shouldThrowRuntimeExceptionOnUnknownTypes() { |
77 | 77 | RuntimeException e2 = |
78 | 78 | assertThrows(RuntimeException.class, () -> aggregationResult.getDouble("qty_avg")); |
79 | 79 | assertThat(e2.getMessage()).isEqualTo("Unsupported type BOOLEAN received for alias 'qty_avg'."); |
| 80 | + |
| 81 | + RuntimeException e3 = |
| 82 | + assertThrows(RuntimeException.class, () -> aggregationResult.getDouble("qty_avg")); |
| 83 | + assertThat(e3.getMessage()).isEqualTo("Unsupported type BOOLEAN received for alias 'qty_avg'."); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void shouldGetDoubleAggregatedResultValueAsNumber() { |
| 88 | + AggregationResult aggregationResult = |
| 89 | + new AggregationResult(ImmutableMap.of("qty_avg", DoubleValue.of(45.9322))); |
| 90 | + assertThat(aggregationResult.getNumber("qty_avg")).isEqualTo(45.9322); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void shouldGetLongAggregatedResultValueAsNumber() { |
| 95 | + AggregationResult aggregationResult = |
| 96 | + new AggregationResult(ImmutableMap.of("count", DoubleValue.of(50))); |
| 97 | + assertThat(aggregationResult.getNumber("count")).isEqualTo(50); |
80 | 98 | } |
81 | 99 | } |