diff --git a/src/main/java/org/apache/datasketches/quantilescommon/DoublesSketchSortedView.java b/src/main/java/org/apache/datasketches/quantilescommon/DoublesSketchSortedView.java index 2ad3ecdd0..42b19d983 100644 --- a/src/main/java/org/apache/datasketches/quantilescommon/DoublesSketchSortedView.java +++ b/src/main/java/org/apache/datasketches/quantilescommon/DoublesSketchSortedView.java @@ -74,12 +74,14 @@ public long[] getCumulativeWeights() { @Override public double getMaxItem() { + if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); } final int top = quantiles.length - 1; return quantiles[top]; } @Override public double getMinItem() { + if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); } return quantiles[0]; } diff --git a/src/main/java/org/apache/datasketches/quantilescommon/DoublesSortedView.java b/src/main/java/org/apache/datasketches/quantilescommon/DoublesSortedView.java index 47bad1c67..80bbcdbcd 100644 --- a/src/main/java/org/apache/datasketches/quantilescommon/DoublesSortedView.java +++ b/src/main/java/org/apache/datasketches/quantilescommon/DoublesSortedView.java @@ -19,6 +19,8 @@ package org.apache.datasketches.quantilescommon; +import org.apache.datasketches.common.SketchesArgumentException; + /** * The Sorted View for quantile sketches of primitive type double. * @see SortedView @@ -58,7 +60,7 @@ public interface DoublesSortedView extends SortedView { * * @param searchCrit the desired search criteria. * @return a discrete CDF array of m+1 double ranks (or cumulative probabilities) on the interval [0.0, 1.0]. - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. */ default double[] getCDF(final double[] splitPoints, final QuantileSearchCriteria searchCrit) { QuantilesUtil.checkDoublesSplitPointsOrder(splitPoints); @@ -76,7 +78,7 @@ default double[] getCDF(final double[] splitPoints, final QuantileSearchCriteria * sketch algorithm. * * @return the maximum item of the stream - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. */ double getMaxItem(); @@ -85,7 +87,7 @@ default double[] getCDF(final double[] splitPoints, final QuantileSearchCriteria * sketch algorithm. * * @return the minimum item of the stream - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. */ double getMinItem(); @@ -127,7 +129,7 @@ default double[] getCDF(final double[] splitPoints, final QuantileSearchCriteria * * @param searchCrit the desired search criteria. * @return a PMF array of m+1 probability masses as doubles on the interval [0.0, 1.0]. - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. */ default double[] getPMF(final double[] splitPoints, final QuantileSearchCriteria searchCrit) { final double[] buckets = getCDF(splitPoints, searchCrit); @@ -147,7 +149,7 @@ default double[] getPMF(final double[] splitPoints, final QuantileSearchCriteri * If EXCLUSIVE, he given rank includes all quantiles < * the quantile directly corresponding to the given rank. * @return the approximate quantile given the normalized rank. - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. * @see QuantileSearchCriteria */ double getQuantile(double rank, QuantileSearchCriteria searchCrit); @@ -164,7 +166,7 @@ default double[] getPMF(final double[] splitPoints, final QuantileSearchCriteri * @param quantile the given quantile * @param searchCrit if INCLUSIVE the given quantile is included into the rank. * @return the normalized rank corresponding to the given quantile. - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. * @see QuantileSearchCriteria */ double getRank(double quantile, QuantileSearchCriteria searchCrit); diff --git a/src/main/java/org/apache/datasketches/quantilescommon/FloatsSketchSortedView.java b/src/main/java/org/apache/datasketches/quantilescommon/FloatsSketchSortedView.java index 4c2276f0e..0260d1b3b 100644 --- a/src/main/java/org/apache/datasketches/quantilescommon/FloatsSketchSortedView.java +++ b/src/main/java/org/apache/datasketches/quantilescommon/FloatsSketchSortedView.java @@ -74,12 +74,14 @@ public long[] getCumulativeWeights() { @Override public float getMaxItem() { + if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); } final int top = quantiles.length - 1; return quantiles[top]; } @Override public float getMinItem() { + if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); } return quantiles[0]; } diff --git a/src/main/java/org/apache/datasketches/quantilescommon/FloatsSortedView.java b/src/main/java/org/apache/datasketches/quantilescommon/FloatsSortedView.java index 0667a6748..81e4c9cc2 100644 --- a/src/main/java/org/apache/datasketches/quantilescommon/FloatsSortedView.java +++ b/src/main/java/org/apache/datasketches/quantilescommon/FloatsSortedView.java @@ -19,6 +19,8 @@ package org.apache.datasketches.quantilescommon; +import org.apache.datasketches.common.SketchesArgumentException; + /** * The Sorted View for quantiles of primitive type float. * @see SortedView @@ -58,7 +60,7 @@ public interface FloatsSortedView extends SortedView { * * @param searchCrit the desired search criteria. * @return a discrete CDF array of m+1 double ranks (or cumulative probabilities) on the interval [0.0, 1.0]. - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. */ default double[] getCDF(final float[] splitPoints, final QuantileSearchCriteria searchCrit) { QuantilesUtil.checkFloatsSplitPointsOrder(splitPoints); @@ -76,7 +78,7 @@ default double[] getCDF(final float[] splitPoints, final QuantileSearchCriteria * sketch algorithm. * * @return the maximum item of the stream - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. */ float getMaxItem(); @@ -85,7 +87,7 @@ default double[] getCDF(final float[] splitPoints, final QuantileSearchCriteria * sketch algorithm. * * @return the minimum item of the stream - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. */ float getMinItem(); @@ -127,7 +129,7 @@ default double[] getCDF(final float[] splitPoints, final QuantileSearchCriteria * * @param searchCrit the desired search criteria. * @return a PMF array of m+1 probability masses as doubles on the interval [0.0, 1.0]. - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. */ default double[] getPMF(final float[] splitPoints, final QuantileSearchCriteria searchCrit) { final double[] buckets = getCDF(splitPoints, searchCrit); @@ -147,7 +149,7 @@ default double[] getPMF(final float[] splitPoints, final QuantileSearchCriteria * If EXCLUSIVE, he given rank includes all quantiles < * the quantile directly corresponding to the given rank. * @return the approximate quantile given the normalized rank. - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. * @see org.apache.datasketches.quantilescommon.QuantileSearchCriteria */ float getQuantile(double rank, QuantileSearchCriteria searchCrit); @@ -164,7 +166,7 @@ default double[] getPMF(final float[] splitPoints, final QuantileSearchCriteria * @param quantile the given quantile * @param searchCrit if INCLUSIVE the given quantile is included into the rank. * @return the normalized rank corresponding to the given quantile. - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. * @see org.apache.datasketches.quantilescommon.QuantileSearchCriteria */ double getRank(float quantile, QuantileSearchCriteria searchCrit); @@ -173,4 +175,3 @@ default double[] getPMF(final float[] splitPoints, final QuantileSearchCriteria FloatsSortedViewIterator iterator(); } - diff --git a/src/main/java/org/apache/datasketches/quantilescommon/GenericSortedView.java b/src/main/java/org/apache/datasketches/quantilescommon/GenericSortedView.java index f0dc81151..294d60c9f 100644 --- a/src/main/java/org/apache/datasketches/quantilescommon/GenericSortedView.java +++ b/src/main/java/org/apache/datasketches/quantilescommon/GenericSortedView.java @@ -67,7 +67,7 @@ public interface GenericSortedView extends PartitioningFeature, SketchPar * * @param searchCrit the desired search criteria. * @return a discrete CDF array of m+1 double ranks (or cumulative probabilities) on the interval [0.0, 1.0]. - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. */ default double[] getCDF(final T[] splitPoints, final QuantileSearchCriteria searchCrit) { if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); } @@ -92,7 +92,7 @@ default double[] getCDF(final T[] splitPoints, final QuantileSearchCriteria sear * sketch algorithm. * * @return the maximum item of the stream - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. */ T getMaxItem(); @@ -101,7 +101,7 @@ default double[] getCDF(final T[] splitPoints, final QuantileSearchCriteria sear * sketch algorithm. * * @return the minimum item of the stream - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. */ T getMinItem(); @@ -143,7 +143,7 @@ default double[] getCDF(final T[] splitPoints, final QuantileSearchCriteria sear * * @param searchCrit the desired search criteria. * @return a PMF array of m+1 probability masses as doubles on the interval [0.0, 1.0]. - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. */ default double[] getPMF(final T[] splitPoints, final QuantileSearchCriteria searchCrit) { if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); } @@ -164,7 +164,7 @@ default double[] getPMF(final T[] splitPoints, final QuantileSearchCriteria sear * If EXCLUSIVE, he given rank includes all quantiles < * the quantile directly corresponding to the given rank. * @return the approximate quantile given the normalized rank. - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. * @see org.apache.datasketches.quantilescommon.QuantileSearchCriteria */ T getQuantile(double rank, QuantileSearchCriteria searchCrit); @@ -181,7 +181,7 @@ default double[] getPMF(final T[] splitPoints, final QuantileSearchCriteria sear * @param quantile the given quantile * @param searchCrit if INCLUSIVE the given quantile is included into the rank. * @return the normalized rank corresponding to the given quantile. - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. * @see org.apache.datasketches.quantilescommon.QuantileSearchCriteria */ double getRank(T quantile, QuantileSearchCriteria searchCrit); diff --git a/src/main/java/org/apache/datasketches/quantilescommon/ItemsSketchSortedView.java b/src/main/java/org/apache/datasketches/quantilescommon/ItemsSketchSortedView.java index ebbed228d..4b0f8dd24 100644 --- a/src/main/java/org/apache/datasketches/quantilescommon/ItemsSketchSortedView.java +++ b/src/main/java/org/apache/datasketches/quantilescommon/ItemsSketchSortedView.java @@ -104,12 +104,14 @@ public long[] getCumulativeWeights() { @Override public T getMaxItem() { + if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); } final int top = quantiles.length - 1; return quantiles[top]; } @Override public T getMinItem() { + if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); } return quantiles[0]; } diff --git a/src/main/java/org/apache/datasketches/quantilescommon/LongsSketchSortedView.java b/src/main/java/org/apache/datasketches/quantilescommon/LongsSketchSortedView.java index efb4006f6..32735938d 100644 --- a/src/main/java/org/apache/datasketches/quantilescommon/LongsSketchSortedView.java +++ b/src/main/java/org/apache/datasketches/quantilescommon/LongsSketchSortedView.java @@ -74,12 +74,14 @@ public long[] getCumulativeWeights() { @Override public long getMaxItem() { + if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); } final int top = quantiles.length - 1; return quantiles[top]; } @Override public long getMinItem() { + if (isEmpty()) { throw new SketchesArgumentException(EMPTY_MSG); } return quantiles[0]; } diff --git a/src/main/java/org/apache/datasketches/quantilescommon/LongsSortedView.java b/src/main/java/org/apache/datasketches/quantilescommon/LongsSortedView.java index eaeceeb92..59819b851 100644 --- a/src/main/java/org/apache/datasketches/quantilescommon/LongsSortedView.java +++ b/src/main/java/org/apache/datasketches/quantilescommon/LongsSortedView.java @@ -19,6 +19,8 @@ package org.apache.datasketches.quantilescommon; +import org.apache.datasketches.common.SketchesArgumentException; + /** * The Sorted View for quantile sketches of primitive type long. * @see SortedView @@ -58,7 +60,7 @@ public interface LongsSortedView extends SortedView { * * @param searchCrit the desired search criteria. * @return a discrete CDF array of m+1 double ranks (or cumulative probabilities) on the interval [0.0, 1.0]. - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. */ default double[] getCDF(final long[] splitPoints, final QuantileSearchCriteria searchCrit) { QuantilesUtil.checkLongsSplitPointsOrder(splitPoints); @@ -76,7 +78,7 @@ default double[] getCDF(final long[] splitPoints, final QuantileSearchCriteria s * sketch algorithm. * * @return the maximum item of the stream - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. */ long getMaxItem(); @@ -85,7 +87,7 @@ default double[] getCDF(final long[] splitPoints, final QuantileSearchCriteria s * sketch algorithm. * * @return the minimum item of the stream - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. */ long getMinItem(); @@ -127,7 +129,7 @@ default double[] getCDF(final long[] splitPoints, final QuantileSearchCriteria s * * @param searchCrit the desired search criteria. * @return a PMF array of m+1 probability masses as doubles on the interval [0.0, 1.0]. - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. */ default double[] getPMF(final long[] splitPoints, final QuantileSearchCriteria searchCrit) { final double[] buckets = getCDF(splitPoints, searchCrit); @@ -147,7 +149,7 @@ default double[] getPMF(final long[] splitPoints, final QuantileSearchCriteria s * If EXCLUSIVE, he given rank includes all quantiles < * the quantile directly corresponding to the given rank. * @return the approximate quantile given the normalized rank. - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. * @see QuantileSearchCriteria */ long getQuantile(double rank, QuantileSearchCriteria searchCrit); @@ -164,7 +166,7 @@ default double[] getPMF(final long[] splitPoints, final QuantileSearchCriteria s * @param quantile the given quantile * @param searchCrit if INCLUSIVE the given quantile is included into the rank. * @return the normalized rank corresponding to the given quantile. - * @throws IllegalArgumentException if sketch is empty. + * @throws SketchesArgumentException if sketch is empty. * @see QuantileSearchCriteria */ double getRank(long quantile, QuantileSearchCriteria searchCrit); diff --git a/src/test/java/org/apache/datasketches/quantilescommon/SortedViewEmptyAccessorTest.java b/src/test/java/org/apache/datasketches/quantilescommon/SortedViewEmptyAccessorTest.java new file mode 100644 index 000000000..e983272d3 --- /dev/null +++ b/src/test/java/org/apache/datasketches/quantilescommon/SortedViewEmptyAccessorTest.java @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.datasketches.quantilescommon; + +import static org.testng.Assert.fail; + +import java.util.Comparator; + +import org.apache.datasketches.common.SketchesArgumentException; +import org.testng.annotations.Test; + +public final class SortedViewEmptyAccessorTest { + + @Test + public void checkFloatsSortedViewEmptyAccessors() { + final FloatsSketchSortedView sv = new FloatsSketchSortedView( + new float[] {1F}, + new long[] {1}, + 0, + 1F, + 1F); + + assertEmptyAccessorThrows(() -> sv.getMaxItem()); + assertEmptyAccessorThrows(() -> sv.getMinItem()); + } + + @Test + public void checkDoublesSortedViewEmptyAccessors() { + final DoublesSketchSortedView sv = new DoublesSketchSortedView( + new double[] {1.0}, + new long[] {1}, + 0, + 1.0, + 1.0); + + assertEmptyAccessorThrows(() -> sv.getMaxItem()); + assertEmptyAccessorThrows(() -> sv.getMinItem()); + } + + @Test + public void checkLongsSortedViewEmptyAccessors() { + final LongsSketchSortedView sv = new LongsSketchSortedView( + new long[] {1}, + new long[] {1}, + 0, + 1, + 1); + + assertEmptyAccessorThrows(() -> sv.getMaxItem()); + assertEmptyAccessorThrows(() -> sv.getMinItem()); + } + + @Test + public void checkItemsSortedViewEmptyAccessors() { + final ItemsSketchSortedView sv = new ItemsSketchSortedView<>( + new String[] {"1"}, + new long[] {1}, + 0, + Comparator.naturalOrder(), + "1", + "1", + String.class, + .01, + 1); + + assertEmptyAccessorThrows(() -> sv.getMaxItem()); + assertEmptyAccessorThrows(() -> sv.getMinItem()); + } + + private static void assertEmptyAccessorThrows(final Runnable accessor) { + try { + accessor.run(); + fail(); + } catch (final SketchesArgumentException e) { + // expected + } + } +}