Greater than and less than in one statement

134,408

Solution 1

Simple utility method:

public static boolean isBetween(int value, int min, int max)
{
  return((value > min) && (value < max));
}

Solution 2

Several third-party libraries have classes encapsulating the concept of a range, such as Apache commons-lang's Range (and subclasses).

Using classes such as this you could express your constraint similar to:

if (new IntRange(0, 5).contains(orderBean.getFiles().size())
// (though actually Apache's Range is INclusive, so it'd be new Range(1, 4) - meh

with the added bonus that the range object could be defined as a constant value elsewhere in the class.

However, without pulling in other libraries and using their classes, Java's strong syntax means you can't massage the language itself to provide this feature nicely. And (in my own opinion), pulling in a third party library just for this small amount of syntactic sugar isn't worth it.

Solution 3

If getFiles() returns a java.util.Collection, !getFiles().isEmpty() && size<5 can be OK.

On the other hand, unless you encapsulate the container which provides method such as boolean sizeBetween(int min, int max).

Solution 4

This is one ugly way to do this. I would just use a local variable.

EDIT: If size() > 0 as well.

if (orderBean.getFiles().size() + Integer.MIN_VALUE-1 < Integer.MIN_VALUE + 5-1)

Solution 5

If this is really bothering you, why not write your own method isBetween(orderBean.getFiles().size(),0,5)?

Another option is to use isEmpty as it is a tad clearer:

if(!orderBean.getFiles().isEmpty() && orderBean.getFiles().size() < 5)
Share:
134,408
lisak
Author by

lisak

Github

Updated on November 01, 2022

Comments

  • lisak
    lisak over 1 year

    I was wondering, do you have a neat way of doing this ?

    if(orderBean.getFiles().size() > 0  && orderBean.getFiles().size() < 5)
    

    without declaring a variable that is not needed anywhere else ?

    int filesCount = orderBean.getFiles().size();
    if(filesCount > 0  && filesCount < 5) {
    

    I mean, in for loop we are "declaring conditions" for the actual iteration, one can declare a variable and then specify the conditions. Here one can't do it, and neither can do something like

    if(5 > orderBean.getFiles().size() > 0)
    
  • lisak
    lisak over 13 years
    This is it :-) I had seen this a long time ago but I haven't used it so that I lost it. But this is exactly what I meant :-) Thank you
  • 卢声远 Shengyuan Lu
    卢声远 Shengyuan Lu over 13 years
    You mean orderBean.getFiles().size() -5 < 0 ?
  • Gareth Davis
    Gareth Davis over 13 years
    cleaver yes, but this is isn't going to help any body understand your code in six months time
  • lisak
    lisak over 13 years
    that's right, fortunately I'm not a team player, doing my own business :-)
  • Gursel Koca
    Gursel Koca over 13 years
    not valid for orderBean.getFiles().size() == 0 .. It does not enforce that number of files should be greater than zero
  • Nishant
    Nishant over 13 years
    @Peter Lawrey tell me what I am missing when I say, orderBean.getFiles().size() + Integer.MIN_VALUE-1 < Integer.MIN_VALUE + 5-1 is same as orderBean.getFiles().size() < 5
  • lisak
    lisak over 13 years
    ok I think I end this, people want it to be a static method. I think it's more convenient that using apache commons IntRange object
  • Vishy
    Vishy over 13 years
    if the size is less than 1 it will underflow to a large positive number and so will not be less than MIN_VALUE+4
  • Krzysztof Atłasik
    Krzysztof Atłasik almost 8 years
    Question was about Java, not Javascript :).
  • qqilihq
    qqilihq almost 8 years
    Question is about Java. There are Java-ports of _, but you should at least link to them and give your code in Java syntax.