Greater than and less than in one statement
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)
Comments
-
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 over 13 yearsThis 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 over 13 yearsYou mean orderBean.getFiles().size() -5 < 0 ?
-
Gareth Davis over 13 yearscleaver yes, but this is isn't going to help any body understand your code in six months time
-
lisak over 13 yearsthat's right, fortunately I'm not a team player, doing my own business :-)
-
Gursel Koca over 13 yearsnot valid for orderBean.getFiles().size() == 0 .. It does not enforce that number of files should be greater than zero
-
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 asorderBean.getFiles().size() < 5
-
lisak over 13 yearsok 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 over 13 yearsif 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 almost 8 yearsQuestion was about Java, not Javascript :).
-
qqilihq almost 8 yearsQuestion is about Java. There are Java-ports of _, but you should at least link to them and give your code in Java syntax.