Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Histogram class uses unsafe methods for returning min/max #1657

Open
rickymagner opened this issue Feb 23, 2023 · 0 comments
Open

Histogram class uses unsafe methods for returning min/max #1657

rickymagner opened this issue Feb 23, 2023 · 0 comments

Comments

@rickymagner
Copy link
Contributor

Description of the issue:

Using the Histogram methods to get values when the histogram is empty are implemented in an unsafe way, leading to unhelpful error messages in some edge cases. This is related to this issue in Picard and is inspired by stumbling on that edge case in a ticket.

Your environment:

  • version of htsjdk: 3.0.1
  • version of java: Java 19
  • which OS: MacOS

Steps to reproduce

Call getMin() or getMax() on any Histogram without any values in it.

Expected behaviour

Some helpful error should be thrown in this case so users can track down what went wrong better.

Actual behaviour

A NullPointerException is thrown.

More Details

The getMin method is implemented in one line:

public double getMin() {
        return map.firstEntry().getValue().getIdValue();
    }

However, the .firstEntry method will return null in the case mentioned above. Instead, something like

public double getMin() throws HistogramEmptyException {
        final first = map.firstEntry();
        if (first != null) {
            return first.getValue().getIdValue();
        } else {
            throw new HistogramEmtpyException;
        }
    }

Or any other paradigm that could add some safety rails/assurances the tools depending on it would better handle this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant