001/* 002 * Copyright (C) 2008 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.primitives; 016 017import static com.google.common.base.Preconditions.checkArgument; 018import static com.google.common.base.Preconditions.checkElementIndex; 019import static com.google.common.base.Preconditions.checkNotNull; 020import static com.google.common.base.Preconditions.checkPositionIndexes; 021 022import com.google.common.annotations.Beta; 023import com.google.common.annotations.GwtCompatible; 024import java.io.Serializable; 025import java.util.AbstractList; 026import java.util.Arrays; 027import java.util.Collection; 028import java.util.Collections; 029import java.util.Comparator; 030import java.util.List; 031import java.util.RandomAccess; 032import javax.annotation.Nullable; 033 034/** 035 * Static utility methods pertaining to {@code boolean} primitives, that are not already found in 036 * either {@link Boolean} or {@link Arrays}. 037 * 038 * <p>See the Guava User Guide article on 039 * <a href="https://github.com/google/guava/wiki/PrimitivesExplained">primitive utilities</a>. 040 * 041 * @author Kevin Bourrillion 042 * @since 1.0 043 */ 044@GwtCompatible 045public final class Booleans { 046 private Booleans() {} 047 048 /** 049 * Returns a hash code for {@code value}; equal to the result of invoking 050 * {@code ((Boolean) value).hashCode()}. 051 * 052 * <p><b>Java 8 users:</b> use {@link Boolean#hashCode(boolean)} instead. 053 * 054 * @param value a primitive {@code boolean} value 055 * @return a hash code for the value 056 */ 057 public static int hashCode(boolean value) { 058 return value ? 1231 : 1237; 059 } 060 061 /** 062 * Compares the two specified {@code boolean} values in the standard way ({@code false} is 063 * considered less than {@code true}). The sign of the value returned is the same as that of 064 * {@code ((Boolean) a).compareTo(b)}. 065 * 066 * <p><b>Note for Java 7 and later:</b> this method should be treated as deprecated; use the 067 * equivalent {@link Boolean#compare} method instead. 068 * 069 * @param a the first {@code boolean} to compare 070 * @param b the second {@code boolean} to compare 071 * @return a positive number if only {@code a} is {@code true}, a negative number if only 072 * {@code b} is true, or zero if {@code a == b} 073 */ 074 public static int compare(boolean a, boolean b) { 075 return (a == b) ? 0 : (a ? 1 : -1); 076 } 077 078 /** 079 * Returns {@code true} if {@code target} is present as an element anywhere in {@code array}. 080 * 081 * <p><b>Note:</b> consider representing the array as a {@link java.util.BitSet} instead, 082 * replacing {@code Booleans.contains(array, true)} with {@code !bitSet.isEmpty()} and 083 * {@code Booleans.contains(array, false)} with {@code bitSet.nextClearBit(0) == sizeOfBitSet}. 084 * 085 * @param array an array of {@code boolean} values, possibly empty 086 * @param target a primitive {@code boolean} value 087 * @return {@code true} if {@code array[i] == target} for some value of {@code 088 * i} 089 */ 090 public static boolean contains(boolean[] array, boolean target) { 091 for (boolean value : array) { 092 if (value == target) { 093 return true; 094 } 095 } 096 return false; 097 } 098 099 /** 100 * Returns the index of the first appearance of the value {@code target} in {@code array}. 101 * 102 * <p><b>Note:</b> consider representing the array as a {@link java.util.BitSet} instead, and 103 * using {@link java.util.BitSet#nextSetBit(int)} or {@link java.util.BitSet#nextClearBit(int)}. 104 * 105 * @param array an array of {@code boolean} values, possibly empty 106 * @param target a primitive {@code boolean} value 107 * @return the least index {@code i} for which {@code array[i] == target}, or {@code -1} if no 108 * such index exists. 109 */ 110 public static int indexOf(boolean[] array, boolean target) { 111 return indexOf(array, target, 0, array.length); 112 } 113 114 // TODO(kevinb): consider making this public 115 private static int indexOf(boolean[] array, boolean target, int start, int end) { 116 for (int i = start; i < end; i++) { 117 if (array[i] == target) { 118 return i; 119 } 120 } 121 return -1; 122 } 123 124 /** 125 * Returns the start position of the first occurrence of the specified {@code 126 * target} within {@code array}, or {@code -1} if there is no such occurrence. 127 * 128 * <p>More formally, returns the lowest index {@code i} such that {@code 129 * Arrays.copyOfRange(array, i, i + target.length)} contains exactly the same elements as 130 * {@code target}. 131 * 132 * @param array the array to search for the sequence {@code target} 133 * @param target the array to search for as a sub-sequence of {@code array} 134 */ 135 public static int indexOf(boolean[] array, boolean[] target) { 136 checkNotNull(array, "array"); 137 checkNotNull(target, "target"); 138 if (target.length == 0) { 139 return 0; 140 } 141 142 outer: 143 for (int i = 0; i < array.length - target.length + 1; i++) { 144 for (int j = 0; j < target.length; j++) { 145 if (array[i + j] != target[j]) { 146 continue outer; 147 } 148 } 149 return i; 150 } 151 return -1; 152 } 153 154 /** 155 * Returns the index of the last appearance of the value {@code target} in {@code array}. 156 * 157 * @param array an array of {@code boolean} values, possibly empty 158 * @param target a primitive {@code boolean} value 159 * @return the greatest index {@code i} for which {@code array[i] == target}, or {@code -1} if no 160 * such index exists. 161 */ 162 public static int lastIndexOf(boolean[] array, boolean target) { 163 return lastIndexOf(array, target, 0, array.length); 164 } 165 166 // TODO(kevinb): consider making this public 167 private static int lastIndexOf(boolean[] array, boolean target, int start, int end) { 168 for (int i = end - 1; i >= start; i--) { 169 if (array[i] == target) { 170 return i; 171 } 172 } 173 return -1; 174 } 175 176 /** 177 * Returns the values from each provided array combined into a single array. For example, 178 * {@code concat(new boolean[] {a, b}, new boolean[] {}, new boolean[] {c}} returns the array 179 * {@code {a, b, c}}. 180 * 181 * @param arrays zero or more {@code boolean} arrays 182 * @return a single array containing all the values from the source arrays, in order 183 */ 184 public static boolean[] concat(boolean[]... arrays) { 185 int length = 0; 186 for (boolean[] array : arrays) { 187 length += array.length; 188 } 189 boolean[] result = new boolean[length]; 190 int pos = 0; 191 for (boolean[] array : arrays) { 192 System.arraycopy(array, 0, result, pos, array.length); 193 pos += array.length; 194 } 195 return result; 196 } 197 198 /** 199 * Returns an array containing the same values as {@code array}, but guaranteed to be of a 200 * specified minimum length. If {@code array} already has a length of at least {@code minLength}, 201 * it is returned directly. Otherwise, a new array of size {@code minLength + padding} is 202 * returned, containing the values of {@code array}, and zeroes in the remaining places. 203 * 204 * @param array the source array 205 * @param minLength the minimum length the returned array must guarantee 206 * @param padding an extra amount to "grow" the array by if growth is necessary 207 * @throws IllegalArgumentException if {@code minLength} or {@code padding} is negative 208 * @return an array containing the values of {@code array}, with guaranteed minimum length 209 * {@code minLength} 210 */ 211 public static boolean[] ensureCapacity(boolean[] array, int minLength, int padding) { 212 checkArgument(minLength >= 0, "Invalid minLength: %s", minLength); 213 checkArgument(padding >= 0, "Invalid padding: %s", padding); 214 return (array.length < minLength) ? Arrays.copyOf(array, minLength + padding) : array; 215 } 216 217 /** 218 * Returns a string containing the supplied {@code boolean} values separated by {@code separator}. 219 * For example, {@code join("-", false, true, false)} returns the string 220 * {@code "false-true-false"}. 221 * 222 * @param separator the text that should appear between consecutive values in the resulting string 223 * (but not at the start or end) 224 * @param array an array of {@code boolean} values, possibly empty 225 */ 226 public static String join(String separator, boolean... array) { 227 checkNotNull(separator); 228 if (array.length == 0) { 229 return ""; 230 } 231 232 // For pre-sizing a builder, just get the right order of magnitude 233 StringBuilder builder = new StringBuilder(array.length * 7); 234 builder.append(array[0]); 235 for (int i = 1; i < array.length; i++) { 236 builder.append(separator).append(array[i]); 237 } 238 return builder.toString(); 239 } 240 241 /** 242 * Returns a comparator that compares two {@code boolean} arrays <a 243 * href="http://en.wikipedia.org/wiki/Lexicographical_order">lexicographically</a>. That is, it 244 * compares, using {@link #compare(boolean, boolean)}), the first pair of values that follow any 245 * common prefix, or when one array is a prefix of the other, treats the shorter array as the 246 * lesser. For example, {@code [] < [false] < [false, true] < [true]}. 247 * 248 * <p>The returned comparator is inconsistent with {@link Object#equals(Object)} (since arrays 249 * support only identity equality), but it is consistent with 250 * {@link Arrays#equals(boolean[], boolean[])}. 251 * 252 * @since 2.0 253 */ 254 public static Comparator<boolean[]> lexicographicalComparator() { 255 return LexicographicalComparator.INSTANCE; 256 } 257 258 private enum LexicographicalComparator implements Comparator<boolean[]> { 259 INSTANCE; 260 261 @Override 262 public int compare(boolean[] left, boolean[] right) { 263 int minLength = Math.min(left.length, right.length); 264 for (int i = 0; i < minLength; i++) { 265 int result = Booleans.compare(left[i], right[i]); 266 if (result != 0) { 267 return result; 268 } 269 } 270 return left.length - right.length; 271 } 272 273 @Override 274 public String toString() { 275 return "Booleans.lexicographicalComparator()"; 276 } 277 } 278 279 /** 280 * Copies a collection of {@code Boolean} instances into a new array of primitive {@code boolean} 281 * values. 282 * 283 * <p>Elements are copied from the argument collection as if by {@code 284 * collection.toArray()}. Calling this method is as thread-safe as calling that method. 285 * 286 * <p><b>Note:</b> consider representing the collection as a {@link java.util.BitSet} instead. 287 * 288 * @param collection a collection of {@code Boolean} objects 289 * @return an array containing the same values as {@code collection}, in the same order, converted 290 * to primitives 291 * @throws NullPointerException if {@code collection} or any of its elements is null 292 */ 293 public static boolean[] toArray(Collection<Boolean> collection) { 294 if (collection instanceof BooleanArrayAsList) { 295 return ((BooleanArrayAsList) collection).toBooleanArray(); 296 } 297 298 Object[] boxedArray = collection.toArray(); 299 int len = boxedArray.length; 300 boolean[] array = new boolean[len]; 301 for (int i = 0; i < len; i++) { 302 // checkNotNull for GWT (do not optimize) 303 array[i] = (Boolean) checkNotNull(boxedArray[i]); 304 } 305 return array; 306 } 307 308 /** 309 * Returns a fixed-size list backed by the specified array, similar to 310 * {@link Arrays#asList(Object[])}. The list supports {@link List#set(int, Object)}, but any 311 * attempt to set a value to {@code null} will result in a {@link NullPointerException}. 312 * 313 * <p>The returned list maintains the values, but not the identities, of {@code Boolean} objects 314 * written to or read from it. For example, whether {@code list.get(0) == list.get(0)} is true for 315 * the returned list is unspecified. 316 * 317 * @param backingArray the array to back the list 318 * @return a list view of the array 319 */ 320 public static List<Boolean> asList(boolean... backingArray) { 321 if (backingArray.length == 0) { 322 return Collections.emptyList(); 323 } 324 return new BooleanArrayAsList(backingArray); 325 } 326 327 @GwtCompatible 328 private static class BooleanArrayAsList extends AbstractList<Boolean> 329 implements RandomAccess, Serializable { 330 final boolean[] array; 331 final int start; 332 final int end; 333 334 BooleanArrayAsList(boolean[] array) { 335 this(array, 0, array.length); 336 } 337 338 BooleanArrayAsList(boolean[] array, int start, int end) { 339 this.array = array; 340 this.start = start; 341 this.end = end; 342 } 343 344 @Override 345 public int size() { 346 return end - start; 347 } 348 349 @Override 350 public boolean isEmpty() { 351 return false; 352 } 353 354 @Override 355 public Boolean get(int index) { 356 checkElementIndex(index, size()); 357 return array[start + index]; 358 } 359 360 @Override 361 public boolean contains(Object target) { 362 // Overridden to prevent a ton of boxing 363 return (target instanceof Boolean) 364 && Booleans.indexOf(array, (Boolean) target, start, end) != -1; 365 } 366 367 @Override 368 public int indexOf(Object target) { 369 // Overridden to prevent a ton of boxing 370 if (target instanceof Boolean) { 371 int i = Booleans.indexOf(array, (Boolean) target, start, end); 372 if (i >= 0) { 373 return i - start; 374 } 375 } 376 return -1; 377 } 378 379 @Override 380 public int lastIndexOf(Object target) { 381 // Overridden to prevent a ton of boxing 382 if (target instanceof Boolean) { 383 int i = Booleans.lastIndexOf(array, (Boolean) target, start, end); 384 if (i >= 0) { 385 return i - start; 386 } 387 } 388 return -1; 389 } 390 391 @Override 392 public Boolean set(int index, Boolean element) { 393 checkElementIndex(index, size()); 394 boolean oldValue = array[start + index]; 395 // checkNotNull for GWT (do not optimize) 396 array[start + index] = checkNotNull(element); 397 return oldValue; 398 } 399 400 @Override 401 public List<Boolean> subList(int fromIndex, int toIndex) { 402 int size = size(); 403 checkPositionIndexes(fromIndex, toIndex, size); 404 if (fromIndex == toIndex) { 405 return Collections.emptyList(); 406 } 407 return new BooleanArrayAsList(array, start + fromIndex, start + toIndex); 408 } 409 410 @Override 411 public boolean equals(@Nullable Object object) { 412 if (object == this) { 413 return true; 414 } 415 if (object instanceof BooleanArrayAsList) { 416 BooleanArrayAsList that = (BooleanArrayAsList) object; 417 int size = size(); 418 if (that.size() != size) { 419 return false; 420 } 421 for (int i = 0; i < size; i++) { 422 if (array[start + i] != that.array[that.start + i]) { 423 return false; 424 } 425 } 426 return true; 427 } 428 return super.equals(object); 429 } 430 431 @Override 432 public int hashCode() { 433 int result = 1; 434 for (int i = start; i < end; i++) { 435 result = 31 * result + Booleans.hashCode(array[i]); 436 } 437 return result; 438 } 439 440 @Override 441 public String toString() { 442 StringBuilder builder = new StringBuilder(size() * 7); 443 builder.append(array[start] ? "[true" : "[false"); 444 for (int i = start + 1; i < end; i++) { 445 builder.append(array[i] ? ", true" : ", false"); 446 } 447 return builder.append(']').toString(); 448 } 449 450 boolean[] toBooleanArray() { 451 // Arrays.copyOfRange() is not available under GWT 452 int size = size(); 453 boolean[] result = new boolean[size]; 454 System.arraycopy(array, start, result, 0, size); 455 return result; 456 } 457 458 private static final long serialVersionUID = 0; 459 } 460 461 /** 462 * Returns the number of {@code values} that are {@code true}. 463 * 464 * @since 16.0 465 */ 466 @Beta 467 public static int countTrue(boolean... values) { 468 int count = 0; 469 for (boolean value : values) { 470 if (value) { 471 count++; 472 } 473 } 474 return count; 475 } 476}