Skip to content

Commit 52df196

Browse files
Add NumericOverflowException, thrown if sum overflows.
1 parent 20bff6e commit 52df196

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2019 ObjectBox Ltd. All rights reserved.
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.objectbox.exception;
18+
19+
/**
20+
* Thrown if a property query aggregate function can not compute a result due to a number type overflowing.
21+
*/
22+
public class NumericOverflowException extends DbException {
23+
24+
public NumericOverflowException(String message) {
25+
super(message);
26+
}
27+
}

objectbox-java/src/main/java/io/objectbox/query/PropertyQuery.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,13 @@ public Double findDouble() {
382382
}
383383

384384

385-
/** Sums up all values for the given property over all Objects matching the query. */
385+
/**
386+
* Sums up all values for the given property over all Objects matching the query.
387+
* <p>
388+
* Note: throws {@link io.objectbox.exception.NumericOverflowException NumericOverflowException} if
389+
* the sum exceeds the numbers {@link Long} can represent. This is different from Java arithmetic
390+
* where it would "wrap around" (e.g. max. value + 1 = min. value).
391+
*/
386392
public long sum() {
387393
return (Long) query.callInReadTx(new Callable<Long>() {
388394
@Override

tests/objectbox-java-test/src/test/java/io/objectbox/query/PropertyQueryTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.objectbox.TestEntity;
2020
import io.objectbox.TestEntityCursor;
2121
import io.objectbox.exception.DbException;
22+
import io.objectbox.exception.NumericOverflowException;
2223
import io.objectbox.query.QueryBuilder.StringOrder;
2324
import org.junit.Rule;
2425
import org.junit.Test;
@@ -640,7 +641,7 @@ public void sum_byteShortIntOverflow() {
640641

641642
@Test
642643
public void sum_longOverflow_exception() {
643-
exceptionRule.expect(DbException.class);
644+
exceptionRule.expect(NumericOverflowException.class);
644645
exceptionRule.expectMessage("Numeric overflow");
645646

646647
putTestEntityInteger((byte) 0, (short) 0, 0, Long.MAX_VALUE);

0 commit comments

Comments
 (0)