Skip to content

Commit 69e1b77

Browse files
committed
fix: raise exception in generic
1 parent 7b229f3 commit 69e1b77

3 files changed

Lines changed: 103 additions & 6 deletions

File tree

src/main/java/org/apache/datasketches/quantilescommon/GenericSortedView.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public interface GenericSortedView<T> extends PartitioningFeature<T>, SketchPar
6767
* </blockquote>
6868
* @param searchCrit the desired search criteria.
6969
* @return a discrete CDF array of m+1 double ranks (or cumulative probabilities) on the interval [0.0, 1.0].
70-
* @throws IllegalArgumentException if sketch is empty.
70+
* @throws SketchesArgumentException if sketch is empty.
7171
*/
7272
default double[] getCDF(final T[] splitPoints, final QuantileSearchCriteria searchCrit) {
7373
if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); }
@@ -92,7 +92,7 @@ default double[] getCDF(final T[] splitPoints, final QuantileSearchCriteria sear
9292
* sketch algorithm.
9393
*
9494
* @return the maximum item of the stream
95-
* @throws IllegalArgumentException if sketch is empty.
95+
* @throws SketchesArgumentException if sketch is empty.
9696
*/
9797
T getMaxItem();
9898

@@ -101,7 +101,7 @@ default double[] getCDF(final T[] splitPoints, final QuantileSearchCriteria sear
101101
* sketch algorithm.
102102
*
103103
* @return the minimum item of the stream
104-
* @throws IllegalArgumentException if sketch is empty.
104+
* @throws SketchesArgumentException if sketch is empty.
105105
*/
106106
T getMinItem();
107107

@@ -143,7 +143,7 @@ default double[] getCDF(final T[] splitPoints, final QuantileSearchCriteria sear
143143
* </blockquote>
144144
* @param searchCrit the desired search criteria.
145145
* @return a PMF array of m+1 probability masses as doubles on the interval [0.0, 1.0].
146-
* @throws IllegalArgumentException if sketch is empty.
146+
* @throws SketchesArgumentException if sketch is empty.
147147
*/
148148
default double[] getPMF(final T[] splitPoints, final QuantileSearchCriteria searchCrit) {
149149
if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); }
@@ -164,7 +164,7 @@ default double[] getPMF(final T[] splitPoints, final QuantileSearchCriteria sear
164164
* If EXCLUSIVE, he given rank includes all quantiles &lt;
165165
* the quantile directly corresponding to the given rank.
166166
* @return the approximate quantile given the normalized rank.
167-
* @throws IllegalArgumentException if sketch is empty.
167+
* @throws SketchesArgumentException if sketch is empty.
168168
* @see org.apache.datasketches.quantilescommon.QuantileSearchCriteria
169169
*/
170170
T getQuantile(double rank, QuantileSearchCriteria searchCrit);
@@ -181,7 +181,7 @@ default double[] getPMF(final T[] splitPoints, final QuantileSearchCriteria sear
181181
* @param quantile the given quantile
182182
* @param searchCrit if INCLUSIVE the given quantile is included into the rank.
183183
* @return the normalized rank corresponding to the given quantile.
184-
* @throws IllegalArgumentException if sketch is empty.
184+
* @throws SketchesArgumentException if sketch is empty.
185185
* @see org.apache.datasketches.quantilescommon.QuantileSearchCriteria
186186
*/
187187
double getRank(T quantile, QuantileSearchCriteria searchCrit);

src/main/java/org/apache/datasketches/quantilescommon/ItemsSketchSortedView.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,14 @@ public long[] getCumulativeWeights() {
104104

105105
@Override
106106
public T getMaxItem() {
107+
if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); }
107108
final int top = quantiles.length - 1;
108109
return quantiles[top];
109110
}
110111

111112
@Override
112113
public T getMinItem() {
114+
if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); }
113115
return quantiles[0];
114116
}
115117

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
20+
package org.apache.datasketches.quantilescommon;
21+
22+
import static org.testng.Assert.fail;
23+
24+
import java.util.Comparator;
25+
26+
import org.apache.datasketches.common.SketchesArgumentException;
27+
import org.testng.annotations.Test;
28+
29+
public final class SortedViewEmptyAccessorTest {
30+
31+
@Test
32+
public void checkFloatsSortedViewEmptyAccessors() {
33+
final FloatsSketchSortedView sv = new FloatsSketchSortedView(
34+
new float[] {1F},
35+
new long[] {1},
36+
0,
37+
1F,
38+
1F);
39+
40+
assertEmptyAccessorThrows(() -> sv.getMaxItem());
41+
assertEmptyAccessorThrows(() -> sv.getMinItem());
42+
}
43+
44+
@Test
45+
public void checkDoublesSortedViewEmptyAccessors() {
46+
final DoublesSketchSortedView sv = new DoublesSketchSortedView(
47+
new double[] {1.0},
48+
new long[] {1},
49+
0,
50+
1.0,
51+
1.0);
52+
53+
assertEmptyAccessorThrows(() -> sv.getMaxItem());
54+
assertEmptyAccessorThrows(() -> sv.getMinItem());
55+
}
56+
57+
@Test
58+
public void checkLongsSortedViewEmptyAccessors() {
59+
final LongsSketchSortedView sv = new LongsSketchSortedView(
60+
new long[] {1},
61+
new long[] {1},
62+
0,
63+
1,
64+
1);
65+
66+
assertEmptyAccessorThrows(() -> sv.getMaxItem());
67+
assertEmptyAccessorThrows(() -> sv.getMinItem());
68+
}
69+
70+
@Test
71+
public void checkItemsSortedViewEmptyAccessors() {
72+
final ItemsSketchSortedView<String> sv = new ItemsSketchSortedView<>(
73+
new String[] {"1"},
74+
new long[] {1},
75+
0,
76+
Comparator.naturalOrder(),
77+
"1",
78+
"1",
79+
String.class,
80+
.01,
81+
1);
82+
83+
assertEmptyAccessorThrows(() -> sv.getMaxItem());
84+
assertEmptyAccessorThrows(() -> sv.getMinItem());
85+
}
86+
87+
private static void assertEmptyAccessorThrows(final Runnable accessor) {
88+
try {
89+
accessor.run();
90+
fail();
91+
} catch (final SketchesArgumentException e) {
92+
// expected
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)