Make the function common for List <Boolean> and boolean []
I have two functions that check if all elements of an array or list are true . I have problems with this. How can I turn functions into one common Java function.
public static boolean allTrue(boolean[] booleans) { if (booleans == null) { return false; } for (boolean bool : booleans) { if (!bool) { return false; } } return true; } public static boolean allTrue(List<Boolean> booleans) { if (booleans == null) { return false; } for (boolean bool : booleans) { if (!bool) { return false; } } return true; } If you use Guava, you can wrap the boolean array in Booleans.asList() and pass it as a list:
public static boolean allTrue(boolean[] booleans) { return booleans != null && allTrue(Booleans.asList(booleans)); } According to fooobar.com/questions/198779 / ...
You can just accept the object
public static boolean allTrue(Object booleans) { and then check instanceof boolean[] or instanceof List<Boolean> , and then execute another code inside the method.
Again, this is not an improvement, but a bit closer to code unification
I think the answer given by @Joel was good, except for the question mentioned in the comment. If we just convert boolean[] to boolean[] , we can try the following:
public static boolean allTrue(List<Boolean> booleans) { if (booleans == null) { return false; } for (boolean bool : booleans) { if (!bool) { return false; } } return true; } public static boolean allTrue(boolean[] booleans) { Boolean[] newArray = new Boolean[booleans.length]; int i = 0; for (boolean value : booleans) { newArray[i++] = Boolean.valueOf(value); } return Arrays.asList(newArray); } The common ancestor for List<Boolean> and boolean[] is Object , so if you are not OK with allTrue(Object booleans) , you cannot do this with one method.
If you change the method signature to allTrue(Iterable<Boolean> booleans) , all you have to do is create a special Iterator<Boolean> to move the Boolean array.
import java.util.Iterator; import java.util.NoSuchElementException; public class BooleanAllTrue { public static boolean allTrue(Iterable<Boolean> booleans) { if (booleans == null) return false; for (Boolean bool : booleans) { if (!bool) return false; } return true; } public static Iterable<Boolean> asIterable(final boolean[] booleens) { return new Iterable<Boolean>() { public Iterator<Boolean> iterator() { final boolean[] booleans = booleens; return new Iterator<Boolean>() { private int i = 0; public boolean hasNext() { return i < booleans.length; } public Boolean next() { if (!hasNext()) throw new NoSuchElementException(); return booleans[i++]; } public void remove() {throw new UnsupportedOperationException("remove");} }; } }; } public static void main(String [] args) { System.out.println(allTrue(asIterable(new boolean[]{true, true}))); System.out.println(allTrue(asIterable(new boolean[]{true, false}))); try { asIterable(new boolean[0]).iterator().next(); } catch (NoSuchElementException e) { // expected } } } And finally, the allTrue(boolean[] booleans) method allTrue(boolean[] booleans) .
public static boolean allTrue(boolean[] booleans) { return allTrue(asIterable(booleans)); }