ripper234
ripper234

Reputation: 230048

Are there helper classes that implement logical operations on boolean collections in any of the standard libraries?

I concocted this little helper class, and wanted to know if there's anywhere I can steal it from instead of re-implementing the wheel:

public class Booleans3 {
    private Booleans3(){}

    public static boolean and(Iterable<Boolean> booleans) {
        boolean result = true;
        for (Boolean boolRef : booleans) {
            boolean bool = boolRef;
            result &= bool;
        }
        return result;
    }

    public static boolean or(Iterable<Boolean> booleans) {
        boolean result = false;
        for (Boolean boolRef : booleans) {
            boolean bool = boolRef;
            result |= bool;
        }
        return result;
    }
}

I looked at com.google.common.primitives.Booleans, and it doesn't seem to contain what I need.

Upvotes: 4

Views: 1756

Answers (4)

MohsenIT
MohsenIT

Reputation: 394

An Improvement on @Eng.Fouad answer:

The following code return false if boolean collection is null or empty, otherwise do logical operation on elements:

public static boolean and(Collection<Boolean> booleans) {
    return booleans != null && !booleans.isEmpty() && !booleans.contains(Boolean.FALSE);
}

public static boolean or(Collection<Boolean> booleans) {
    return booleans != null && !booleans.isEmpty() && booleans.contains(Boolean.TRUE);
}

Upvotes: 0

Slava Semushin
Slava Semushin

Reputation: 15204

While @eng-fouad answer is good enough I still suggest another one which utilizes Iterables.all() and Iterables.any() with equalTo predicate:

import java.util.Arrays;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

import static com.google.common.collect.Iterables.all;
import static com.google.common.collect.Iterables.any;
import static com.google.common.base.Predicates.equalTo;

...

Iterable<Boolean> booleans = Arrays.asList(TRUE, TRUE, FALSE);

System.out.println(all(booleans, equalTo(TRUE)));
System.out.println(any(booleans, equalTo(TRUE)));

will print

false
true

As pluses I see:

  • Uses Guava library
  • No need to write own class with methods
  • Better readability (IMHO)

Upvotes: 2

Eng.Fouad
Eng.Fouad

Reputation: 117597

How about this:

public static boolean and(Collection<Boolean> booleans)
{
    return !booleans.contains(Boolean.FALSE);
}

public static boolean or(Collection<Boolean> booleans)
{
    return booleans.contains(Boolean.TRUE);
}

Upvotes: 12

templatetypedef
templatetypedef

Reputation: 372814

I don't believe that there is any part of the Java Standard Libraries that provides exactly this functionality.

In some languages, these are provided as functions called anyOf (for or) or allOf (for and). You may have some luck searching for Java libraries implementing those functions.

A note-, the code you have here can be optimized quite a bit. Note that if you're computing the AND of many boolean values, as soon as you find a false value you can stop and say that the answer is false. Similarly, if you're computing the OR of boolean values, you can stop as soon as you find a true value, since the answer will be true. This is essentially a generalization of short-circuit evaluation to lists of objects, and is the behavior of some versions of and and or in languages like Scheme. This gives the following:

public class ModifiedBooleans3 {
    private ModifiedBooleans3(){}

    public static boolean and(Iterable<Boolean> booleans) {
        for (Boolean boolRef : booleans)
            if (!boolRef)
                return false;
        return true;
    }

    public static boolean or(Iterable<Boolean> booleans) {
        for (Boolean boolRef : booleans)
            if (boolRef)
                return true;
        return false;
    }
}

Hope this helps!

Upvotes: 1

Related Questions