Reputation:
Is there a way to make consecutive logical operations on the same variable shorter?
Example:
if (animation.getStatus() == Animation.Status.PAUSED || animation.getStatus() == Animation.Status.STOPPED) {
animation.playFromStart();
} else if (animation.getStatus() == Animation.Status.RUNNING) {
animation.stop();
}
You see in the if-clause that there I check the animation.getStatus()
twice, once for paused and once for stopped.
Is there a way to make it like animation.getStatus() == Animation.Status.PAUSED || Animation.Status.STOPPED
?
I'm sure this question has been asked already but I really don't know what to search for, so I'm sorry if this is a duplicate.
Upvotes: 2
Views: 98
Reputation:
If your code is multithreaded, which it probably should be if you're running animations and want to be able to stop/start/pause them, then you should consider synchronization.
So to take Dave Newtons code snippet and make it thread-safe:
synchronized(this){
if (animation.shouldReplay()) {
animation.playFromStart();
} else if (animation.shouldStop() {
animation.stop();
}
}
Thus, there is no danger the first condition will return false, then the current thread is no longer Running, another thread modifies the status of the animation, then the current thread again becomes Runnable and tries to stop the animation.
Upvotes: 0
Reputation: 49171
In this case switch
statement would look nice
switch (animation.getStatus()) {
case Animation.Status.PAUSED:
case Animation.Status.STOPPED:
animation.playFromStart();
break;
case Animation.Status.RUNNING:
animation.stop();
break;
}
Upvotes: 1
Reputation: 726499
You can do it in a couple of ways:
Here is the illustration to the first way of doing it:
Animation.Status status = animation.getStatus();
if (status == Animation.Status.PAUSED || status == Animation.Status.STOPPED) {
animation.playFromStart();
} else if (status == Animation.Status.RUNNING) {
animation.stop();
}
Here is an illustration to the second way of doing it:
private static boolean checkStatus(Animation.Status status, Animation.Status... expected) {
for (Animation.Status e : expected) {
if (e == status) {
return true;
}
}
return false;
}
...
if (checkStatus(animation.getStatus(), Animation.Status.PAUSED, Animation.Status.STOPPED)) {
...
}
Upvotes: 0
Reputation: 160181
No; Java syntax is immutable.
There are several options, but the easiest is to refactor, which as a bonus makes it legible, e.g.,
if (animation.shouldReplay()) {
animation.playFromStart();
} else if (animation.shouldStop() {
animation.stop();
}
Or a level deeper, e.g.,
animation.controlFromCurrentStatus();
If you're unwilling to encapsulate, simply importing the statuses helps:
Animation.Status currentStatus = animation.getStatus();
if (currentStatus == PAUSED || currentStatus == STOPPED) {
animation.playFromStart();
} else if (currentStatus == RUNNING) {
animation.stop();
}
Or make them enum
s, which arguably they should be anyway:
switch (currentStatus) {
case PAUSED:
case STOPPED:
animation.playFromStart();
break;
case RUNNING:
animation.stop();
}
Upvotes: 6