Steve
Steve

Reputation: 2818

Questions about this bit of Java code, why it is written as it is

I came across this source code and wanted to make sure I understood why it is written as it is ( or if it should be ):

boolean modified = false;
Set<String> possibleSites = settings.getPossibleSites();
Set<String> visibleSites = settings.getVisibleSites();
modified = someMysteriousMethod();

// Remove sites from visibleSites that are not in possibleSites
modified |= visibleSites.retainAll(possibleSites);

My questions about the LAST statement:

  1. Why use the bitwise operator? retainAll() will return a boolean as to what happened?

  2. What is the operator/statement saying? If modified equals the return value leave the value of modified alone OR if the return value is different set modified to that new value?

Upvotes: 1

Views: 70

Answers (5)

user10762593
user10762593

Reputation:

After this:

modified = someMysteriousMethod();

then modified may be true, or not. The effect of the following:

modified |= visibleSites.retainAll(possibleSites);

is to leave modified set true if it was already true, and to change it from false to true if some other condition holds (I say effect intentionally, I am not describing actual instruction operation).

The second statement could be written as

if (visibleSites.retainAll(possibleSites)) {
      modified = true;
}

with the same outcome. To some extent this is a matter of taste. I find the original (using the |= operator) to be clearer to read, since it's a simple straight-line logical expression rather than a control-flow statement

By the way, it's not called a 'bitwise' operator. Java has two '|' and two corresponding '|=' operators; between integers it's bitwise-or, between booleans it's logical-or.

Upvotes: 0

GBlodgett
GBlodgett

Reputation: 12819

It is meant to say that if either modified or retainAll() is true, then modified should be true. (Which makes sense given the name. retainAll only returns true if the Set was changed, and someMysteriousMethod probably returns true or false depending if the state of the Set was changed.)

The code breaks down to:

modified = modified | visibleSites.retainAll(possibleSites);

Look at the following code to see the results of the | on booleans:

System.out.println(true|true);
System.out.println(false|true);
System.out.println(true|false);
System.out.println(false|false);

Output:

true
true
true
false

(Only false|false will return false, so in the code, both modified and retainAll will have to return false for modified to be false)

Upvotes: 1

Madhu Bhat
Madhu Bhat

Reputation: 15213

boolean retainAll(Collection<?> c)

returns true if the set changed as a result of the call

modified |= visibleSites.retainAll(possibleSites);

So the above statement means, if modified is true OR retainAll returns true, then modified is set to true, otherwise the value of modified is false

Upvotes: 1

Oli
Oli

Reputation: 10406

According to the javadoc,

retainAll retuns true if this set changed as a result of the call

a |= b is an operation that affects the result of "a or b" to a. Therefore, in your situation, the idea is to affect to modified the result of the statement "visibleSites was modified before OR visibleSites was modified by retainAll"

Upvotes: 0

rgettman
rgettman

Reputation: 178293

The return value of retainAll method in Set indicates whether the set was changed by the call.

Returns:

true if this set changed as a result of the call

Here, true indicates that the set was modified. The |= compound operator performs an "or" on the arguments and assigns it back to the left side. With your case that means that if retainAll returns true, then assign true back to modified, or else leave modified unchanged.

Upvotes: 0

Related Questions