user4391669
user4391669

Reputation:

How to write short logical expressions in Java?

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

Answers (4)

user2101106
user2101106

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

Grzegorz Żur
Grzegorz Żur

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

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

You can do it in a couple of ways:

  1. By introducing a temporary variable - make a variable, assign the status to it, and use the value of the variable in the expressions instead of making the call multiple times
  2. By writing a helper method - write a method that takes the current status and a variable argument list of statuses, and call this method in the conditional.

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

Dave Newton
Dave Newton

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 enums, which arguably they should be anyway:

switch (currentStatus) {
case PAUSED:
case STOPPED:
    animation.playFromStart();
    break;
case RUNNING:
    animation.stop();
}

Upvotes: 6

Related Questions