Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6871151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:46:14+00:00 2026-05-27T03:46:14+00:00

I like guava preconditions, but what I really need from it is one more

  • 0

I like guava preconditions, but what I really need from it is one more method – check that the number is in range. Smt like this

//probably there should be checkStateInRange also
public static void checkArgumentInRange(double value, int min, int max) {
    if (value < min || value > max) {
        throw new IllegalArgumentException(String.format("%s must be in range [%s, %s]", value, min, max));
    }
}

I believe I’m not alone and it’s a pretty common case. But such method doesn’t exist. Is there any reasons to not put such methods in com.google.common.base.Preconditions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:46:14+00:00Added an answer on May 27, 2026 at 3:46 am

    There are quite a few reasons I’d say. Here are the main ones:

    • There’s no standard Java exception type for “value out of range”. Note that each of the Preconditions methods throws a specific exception type for what it checks: NullPointerException, IllegalArgumentException, IllegalStateException or IndexOutOfBoundsException. A generalized range check would have no exception more specific than IllegalArgumentException to throw.
    • checkArgument and checkState do everything you need. You can just write checkArgument(value >= min && value <= max, ...). It’s simple and obvious what you’re checking.

    Additionally:

    • There are too many different combinations you might want here. Exclusive/inclusive bounds as @rsp mentions, etc.
    • It’s limiting to only allow ints for the bounds, so you would really like to allow any Comparable there.
    • At this point you notice you’re just checking if the value is contained in a Range.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I love using Guava's Iterables filters. But one common code snippet looks like: List
I'm using MultiMap from Google Guava 12 like this: Multimap<Integer, OccupancyType> pkgPOP = HashMultimap.create();
I really like the Multimap class of the google guava library. It is a
I'd like to load dynamically a Class object defined by the Guava library from
Using Guava, I would like to have a map that has the following properties:
Coming from Java I really like the flexibility afforded by the rich collection of
I have a service that I would like to implement as a Google Guava
I have a multiset in guava and I would like to retrieve the number
I could not find a method in guava that converts a Collection (or Iterator/Iterable)
I have read many people really like the MapMaker of Google Guava (Collections), however

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.