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 com.google.common.base.Converter; 025import java.io.Serializable; 026import java.util.AbstractList; 027import java.util.Arrays; 028import java.util.Collection; 029import java.util.Collections; 030import java.util.Comparator; 031import java.util.List; 032import java.util.RandomAccess; 033import javax.annotation.CheckForNull; 034import javax.annotation.Nullable; 035 036/** 037 * Static utility methods pertaining to {@code long} primitives, that are not already found in 038 * either {@link Long} or {@link Arrays}. 039 * 040 * <p>See the Guava User Guide article on 041 * <a href="https://github.com/google/guava/wiki/PrimitivesExplained">primitive utilities</a>. 042 * 043 * @author Kevin Bourrillion 044 * @since 1.0 045 */ 046@GwtCompatible 047public final class Longs { 048 private Longs() {} 049 050 /** 051 * The number of bytes required to represent a primitive {@code long} value. 052 * 053 * <p><b>Java 8 users:</b> use {@link Long#BYTES} instead. 054 */ 055 public static final int BYTES = Long.SIZE / Byte.SIZE; 056 057 /** 058 * The largest power of two that can be represented as a {@code long}. 059 * 060 * @since 10.0 061 */ 062 public static final long MAX_POWER_OF_TWO = 1L << (Long.SIZE - 2); 063 064 /** 065 * Returns a hash code for {@code value}; equal to the result of invoking 066 * {@code ((Long) value).hashCode()}. 067 * 068 * <p>This method always return the value specified by {@link Long#hashCode()} in java, which 069 * might be different from {@code ((Long) value).hashCode()} in GWT because 070 * {@link Long#hashCode()} in GWT does not obey the JRE contract. 071 * 072 * <p><b>Java 8 users:</b> use {@link Long#hashCode(long)} instead. 073 * 074 * @param value a primitive {@code long} value 075 * @return a hash code for the value 076 */ 077 public static int hashCode(long value) { 078 return (int) (value ^ (value >>> 32)); 079 } 080 081 /** 082 * Compares the two specified {@code long} values. The sign of the value returned is the same as 083 * that of {@code ((Long) a).compareTo(b)}. 084 * 085 * <p><b>Note for Java 7 and later:</b> this method should be treated as deprecated; use the 086 * equivalent {@link Long#compare} method instead. 087 * 088 * @param a the first {@code long} to compare 089 * @param b the second {@code long} to compare 090 * @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is 091 * greater than {@code b}; or zero if they are equal 092 */ 093 public static int compare(long a, long b) { 094 return (a < b) ? -1 : ((a > b) ? 1 : 0); 095 } 096 097 /** 098 * Returns {@code true} if {@code target} is present as an element anywhere in {@code array}. 099 * 100 * @param array an array of {@code long} values, possibly empty 101 * @param target a primitive {@code long} value 102 * @return {@code true} if {@code array[i] == target} for some value of {@code 103 * i} 104 */ 105 public static boolean contains(long[] array, long target) { 106 for (long value : array) { 107 if (value == target) { 108 return true; 109 } 110 } 111 return false; 112 } 113 114 /** 115 * Returns the index of the first appearance of the value {@code target} in {@code array}. 116 * 117 * @param array an array of {@code long} values, possibly empty 118 * @param target a primitive {@code long} value 119 * @return the least index {@code i} for which {@code array[i] == target}, or {@code -1} if no 120 * such index exists. 121 */ 122 public static int indexOf(long[] array, long target) { 123 return indexOf(array, target, 0, array.length); 124 } 125 126 // TODO(kevinb): consider making this public 127 private static int indexOf(long[] array, long target, int start, int end) { 128 for (int i = start; i < end; i++) { 129 if (array[i] == target) { 130 return i; 131 } 132 } 133 return -1; 134 } 135 136 /** 137 * Returns the start position of the first occurrence of the specified {@code 138 * target} within {@code array}, or {@code -1} if there is no such occurrence. 139 * 140 * <p>More formally, returns the lowest index {@code i} such that 141 * {@code Arrays.copyOfRange(array, i, i + target.length)} contains exactly the same elements as 142 * {@code target}. 143 * 144 * @param array the array to search for the sequence {@code target} 145 * @param target the array to search for as a sub-sequence of {@code array} 146 */ 147 public static int indexOf(long[] array, long[] target) { 148 checkNotNull(array, "array"); 149 checkNotNull(target, "target"); 150 if (target.length == 0) { 151 return 0; 152 } 153 154 outer: 155 for (int i = 0; i < array.length - target.length + 1; i++) { 156 for (int j = 0; j < target.length; j++) { 157 if (array[i + j] != target[j]) { 158 continue outer; 159 } 160 } 161 return i; 162 } 163 return -1; 164 } 165 166 /** 167 * Returns the index of the last appearance of the value {@code target} in {@code array}. 168 * 169 * @param array an array of {@code long} values, possibly empty 170 * @param target a primitive {@code long} value 171 * @return the greatest index {@code i} for which {@code array[i] == target}, or {@code -1} if no 172 * such index exists. 173 */ 174 public static int lastIndexOf(long[] array, long target) { 175 return lastIndexOf(array, target, 0, array.length); 176 } 177 178 // TODO(kevinb): consider making this public 179 private static int lastIndexOf(long[] array, long target, int start, int end) { 180 for (int i = end - 1; i >= start; i--) { 181 if (array[i] == target) { 182 return i; 183 } 184 } 185 return -1; 186 } 187 188 /** 189 * Returns the least value present in {@code array}. 190 * 191 * @param array a <i>nonempty</i> array of {@code long} values 192 * @return the value present in {@code array} that is less than or equal to every other value in 193 * the array 194 * @throws IllegalArgumentException if {@code array} is empty 195 */ 196 public static long min(long... array) { 197 checkArgument(array.length > 0); 198 long min = array[0]; 199 for (int i = 1; i < array.length; i++) { 200 if (array[i] < min) { 201 min = array[i]; 202 } 203 } 204 return min; 205 } 206 207 /** 208 * Returns the greatest value present in {@code array}. 209 * 210 * @param array a <i>nonempty</i> array of {@code long} values 211 * @return the value present in {@code array} that is greater than or equal to every other value 212 * in the array 213 * @throws IllegalArgumentException if {@code array} is empty 214 */ 215 public static long max(long... array) { 216 checkArgument(array.length > 0); 217 long max = array[0]; 218 for (int i = 1; i < array.length; i++) { 219 if (array[i] > max) { 220 max = array[i]; 221 } 222 } 223 return max; 224 } 225 226 /** 227 * Returns the values from each provided array combined into a single array. For example, 228 * {@code concat(new long[] {a, b}, new long[] {}, new long[] {c}} returns the array 229 * {@code {a, b, c}}. 230 * 231 * @param arrays zero or more {@code long} arrays 232 * @return a single array containing all the values from the source arrays, in order 233 */ 234 public static long[] concat(long[]... arrays) { 235 int length = 0; 236 for (long[] array : arrays) { 237 length += array.length; 238 } 239 long[] result = new long[length]; 240 int pos = 0; 241 for (long[] array : arrays) { 242 System.arraycopy(array, 0, result, pos, array.length); 243 pos += array.length; 244 } 245 return result; 246 } 247 248 /** 249 * Returns a big-endian representation of {@code value} in an 8-element byte array; equivalent to 250 * {@code ByteBuffer.allocate(8).putLong(value).array()}. For example, the input value 251 * {@code 0x1213141516171819L} would yield the byte array {@code {0x12, 0x13, 0x14, 0x15, 0x16, 252 * 0x17, 0x18, 0x19}}. 253 * 254 * <p>If you need to convert and concatenate several values (possibly even of different types), 255 * use a shared {@link java.nio.ByteBuffer} instance, or use 256 * {@link com.google.common.io.ByteStreams#newDataOutput()} to get a growable buffer. 257 */ 258 public static byte[] toByteArray(long value) { 259 // Note that this code needs to stay compatible with GWT, which has known 260 // bugs when narrowing byte casts of long values occur. 261 byte[] result = new byte[8]; 262 for (int i = 7; i >= 0; i--) { 263 result[i] = (byte) (value & 0xffL); 264 value >>= 8; 265 } 266 return result; 267 } 268 269 /** 270 * Returns the {@code long} value whose big-endian representation is stored in the first 8 bytes 271 * of {@code bytes}; equivalent to {@code ByteBuffer.wrap(bytes).getLong()}. For example, the 272 * input byte array {@code {0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19}} would yield the 273 * {@code long} value {@code 0x1213141516171819L}. 274 * 275 * <p>Arguably, it's preferable to use {@link java.nio.ByteBuffer}; that library exposes much more 276 * flexibility at little cost in readability. 277 * 278 * @throws IllegalArgumentException if {@code bytes} has fewer than 8 elements 279 */ 280 public static long fromByteArray(byte[] bytes) { 281 checkArgument(bytes.length >= BYTES, "array too small: %s < %s", bytes.length, BYTES); 282 return fromBytes( 283 bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]); 284 } 285 286 /** 287 * Returns the {@code long} value whose byte representation is the given 8 bytes, in big-endian 288 * order; equivalent to {@code Longs.fromByteArray(new byte[] {b1, b2, b3, b4, b5, b6, b7, b8})}. 289 * 290 * @since 7.0 291 */ 292 public static long fromBytes( 293 byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, byte b8) { 294 return (b1 & 0xFFL) << 56 295 | (b2 & 0xFFL) << 48 296 | (b3 & 0xFFL) << 40 297 | (b4 & 0xFFL) << 32 298 | (b5 & 0xFFL) << 24 299 | (b6 & 0xFFL) << 16 300 | (b7 & 0xFFL) << 8 301 | (b8 & 0xFFL); 302 } 303 304 private static final byte[] asciiDigits = createAsciiDigits(); 305 306 private static byte[] createAsciiDigits() { 307 byte[] result = new byte[128]; 308 Arrays.fill(result, (byte) -1); 309 for (int i = 0; i <= 9; i++) { 310 result['0' + i] = (byte) i; 311 } 312 for (int i = 0; i <= 26; i++) { 313 result['A' + i] = (byte) (10 + i); 314 result['a' + i] = (byte) (10 + i); 315 } 316 return result; 317 } 318 319 private static int digit(char c) { 320 return (c < 128) ? asciiDigits[c] : -1; 321 } 322 323 /** 324 * Parses the specified string as a signed decimal long value. The ASCII character {@code '-'} 325 * (<code>'\u002D'</code>) is recognized as the minus sign. 326 * 327 * <p>Unlike {@link Long#parseLong(String)}, this method returns {@code null} instead of throwing 328 * an exception if parsing fails. Additionally, this method only accepts ASCII digits, and returns 329 * {@code null} if non-ASCII digits are present in the string. 330 * 331 * <p>Note that strings prefixed with ASCII {@code '+'} are rejected, even under JDK 7, despite 332 * the change to {@link Long#parseLong(String)} for that version. 333 * 334 * @param string the string representation of a long value 335 * @return the long value represented by {@code string}, or {@code null} if {@code string} has a 336 * length of zero or cannot be parsed as a long value 337 * @since 14.0 338 */ 339 @Beta 340 @Nullable 341 @CheckForNull 342 public static Long tryParse(String string) { 343 return tryParse(string, 10); 344 } 345 346 /** 347 * Parses the specified string as a signed long value using the specified radix. The ASCII 348 * character {@code '-'} (<code>'\u002D'</code>) is recognized as the minus sign. 349 * 350 * <p>Unlike {@link Long#parseLong(String, int)}, this method returns {@code null} instead of 351 * throwing an exception if parsing fails. Additionally, this method only accepts ASCII digits, 352 * and returns {@code null} if non-ASCII digits are present in the string. 353 * 354 * <p>Note that strings prefixed with ASCII {@code '+'} are rejected, even under JDK 7, despite 355 * the change to {@link Long#parseLong(String, int)} for that version. 356 * 357 * @param string the string representation of an long value 358 * @param radix the radix to use when parsing 359 * @return the long value represented by {@code string} using {@code radix}, or {@code null} if 360 * {@code string} has a length of zero or cannot be parsed as a long value 361 * @throws IllegalArgumentException if {@code radix < Character.MIN_RADIX} or 362 * {@code radix > Character.MAX_RADIX} 363 * @since 19.0 364 */ 365 @Beta 366 @Nullable 367 @CheckForNull 368 public static Long tryParse(String string, int radix) { 369 if (checkNotNull(string).isEmpty()) { 370 return null; 371 } 372 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) { 373 throw new IllegalArgumentException( 374 "radix must be between MIN_RADIX and MAX_RADIX but was " + radix); 375 } 376 boolean negative = string.charAt(0) == '-'; 377 int index = negative ? 1 : 0; 378 if (index == string.length()) { 379 return null; 380 } 381 int digit = digit(string.charAt(index++)); 382 if (digit < 0 || digit >= radix) { 383 return null; 384 } 385 long accum = -digit; 386 387 long cap = Long.MIN_VALUE / radix; 388 389 while (index < string.length()) { 390 digit = digit(string.charAt(index++)); 391 if (digit < 0 || digit >= radix || accum < cap) { 392 return null; 393 } 394 accum *= radix; 395 if (accum < Long.MIN_VALUE + digit) { 396 return null; 397 } 398 accum -= digit; 399 } 400 401 if (negative) { 402 return accum; 403 } else if (accum == Long.MIN_VALUE) { 404 return null; 405 } else { 406 return -accum; 407 } 408 } 409 410 private static final class LongConverter extends Converter<String, Long> implements Serializable { 411 static final LongConverter INSTANCE = new LongConverter(); 412 413 @Override 414 protected Long doForward(String value) { 415 return Long.decode(value); 416 } 417 418 @Override 419 protected String doBackward(Long value) { 420 return value.toString(); 421 } 422 423 @Override 424 public String toString() { 425 return "Longs.stringConverter()"; 426 } 427 428 private Object readResolve() { 429 return INSTANCE; 430 } 431 432 private static final long serialVersionUID = 1; 433 } 434 435 /** 436 * Returns a serializable converter object that converts between strings and longs using 437 * {@link Long#decode} and {@link Long#toString()}. The returned converter throws 438 * {@link NumberFormatException} if the input string is invalid. 439 * 440 * <p><b>Warning:</b> please see {@link Long#decode} to understand exactly how strings are parsed. 441 * For example, the string {@code "0123"} is treated as <i>octal</i> and converted to the value 442 * {@code 83L}. 443 * 444 * @since 16.0 445 */ 446 @Beta 447 public static Converter<String, Long> stringConverter() { 448 return LongConverter.INSTANCE; 449 } 450 451 /** 452 * Returns an array containing the same values as {@code array}, but guaranteed to be of a 453 * specified minimum length. If {@code array} already has a length of at least {@code minLength}, 454 * it is returned directly. Otherwise, a new array of size {@code minLength + padding} is 455 * returned, containing the values of {@code array}, and zeroes in the remaining places. 456 * 457 * @param array the source array 458 * @param minLength the minimum length the returned array must guarantee 459 * @param padding an extra amount to "grow" the array by if growth is necessary 460 * @throws IllegalArgumentException if {@code minLength} or {@code padding} is negative 461 * @return an array containing the values of {@code array}, with guaranteed minimum length 462 * {@code minLength} 463 */ 464 public static long[] ensureCapacity(long[] array, int minLength, int padding) { 465 checkArgument(minLength >= 0, "Invalid minLength: %s", minLength); 466 checkArgument(padding >= 0, "Invalid padding: %s", padding); 467 return (array.length < minLength) ? Arrays.copyOf(array, minLength + padding) : array; 468 } 469 470 /** 471 * Returns a string containing the supplied {@code long} values separated by {@code separator}. 472 * For example, {@code join("-", 1L, 2L, 3L)} returns the string {@code "1-2-3"}. 473 * 474 * @param separator the text that should appear between consecutive values in the resulting string 475 * (but not at the start or end) 476 * @param array an array of {@code long} values, possibly empty 477 */ 478 public static String join(String separator, long... array) { 479 checkNotNull(separator); 480 if (array.length == 0) { 481 return ""; 482 } 483 484 // For pre-sizing a builder, just get the right order of magnitude 485 StringBuilder builder = new StringBuilder(array.length * 10); 486 builder.append(array[0]); 487 for (int i = 1; i < array.length; i++) { 488 builder.append(separator).append(array[i]); 489 } 490 return builder.toString(); 491 } 492 493 /** 494 * Returns a comparator that compares two {@code long} arrays <a 495 * href="http://en.wikipedia.org/wiki/Lexicographical_order">lexicographically</a>. That is, it 496 * compares, using {@link #compare(long, long)}), the first pair of values that follow any common 497 * prefix, or when one array is a prefix of the other, treats the shorter array as the lesser. For 498 * example, {@code [] < [1L] < [1L, 2L] < [2L]}. 499 * 500 * <p>The returned comparator is inconsistent with {@link Object#equals(Object)} (since arrays 501 * support only identity equality), but it is consistent with 502 * {@link Arrays#equals(long[], long[])}. 503 * 504 * @since 2.0 505 */ 506 public static Comparator<long[]> lexicographicalComparator() { 507 return LexicographicalComparator.INSTANCE; 508 } 509 510 private enum LexicographicalComparator implements Comparator<long[]> { 511 INSTANCE; 512 513 @Override 514 public int compare(long[] left, long[] right) { 515 int minLength = Math.min(left.length, right.length); 516 for (int i = 0; i < minLength; i++) { 517 int result = Longs.compare(left[i], right[i]); 518 if (result != 0) { 519 return result; 520 } 521 } 522 return left.length - right.length; 523 } 524 525 @Override 526 public String toString() { 527 return "Longs.lexicographicalComparator()"; 528 } 529 } 530 531 /** 532 * Returns an array containing each value of {@code collection}, converted to a {@code long} value 533 * in the manner of {@link Number#longValue}. 534 * 535 * <p>Elements are copied from the argument collection as if by {@code 536 * collection.toArray()}. Calling this method is as thread-safe as calling that method. 537 * 538 * @param collection a collection of {@code Number} instances 539 * @return an array containing the same values as {@code collection}, in the same order, converted 540 * to primitives 541 * @throws NullPointerException if {@code collection} or any of its elements is null 542 * @since 1.0 (parameter was {@code Collection<Long>} before 12.0) 543 */ 544 public static long[] toArray(Collection<? extends Number> collection) { 545 if (collection instanceof LongArrayAsList) { 546 return ((LongArrayAsList) collection).toLongArray(); 547 } 548 549 Object[] boxedArray = collection.toArray(); 550 int len = boxedArray.length; 551 long[] array = new long[len]; 552 for (int i = 0; i < len; i++) { 553 // checkNotNull for GWT (do not optimize) 554 array[i] = ((Number) checkNotNull(boxedArray[i])).longValue(); 555 } 556 return array; 557 } 558 559 /** 560 * Returns a fixed-size list backed by the specified array, similar to 561 * {@link Arrays#asList(Object[])}. The list supports {@link List#set(int, Object)}, but any 562 * attempt to set a value to {@code null} will result in a {@link NullPointerException}. 563 * 564 * <p>The returned list maintains the values, but not the identities, of {@code Long} objects 565 * written to or read from it. For example, whether {@code list.get(0) == list.get(0)} is true for 566 * the returned list is unspecified. 567 * 568 * @param backingArray the array to back the list 569 * @return a list view of the array 570 */ 571 public static List<Long> asList(long... backingArray) { 572 if (backingArray.length == 0) { 573 return Collections.emptyList(); 574 } 575 return new LongArrayAsList(backingArray); 576 } 577 578 @GwtCompatible 579 private static class LongArrayAsList extends AbstractList<Long> 580 implements RandomAccess, Serializable { 581 final long[] array; 582 final int start; 583 final int end; 584 585 LongArrayAsList(long[] array) { 586 this(array, 0, array.length); 587 } 588 589 LongArrayAsList(long[] array, int start, int end) { 590 this.array = array; 591 this.start = start; 592 this.end = end; 593 } 594 595 @Override 596 public int size() { 597 return end - start; 598 } 599 600 @Override 601 public boolean isEmpty() { 602 return false; 603 } 604 605 @Override 606 public Long get(int index) { 607 checkElementIndex(index, size()); 608 return array[start + index]; 609 } 610 611 @Override 612 public boolean contains(Object target) { 613 // Overridden to prevent a ton of boxing 614 return (target instanceof Long) && Longs.indexOf(array, (Long) target, start, end) != -1; 615 } 616 617 @Override 618 public int indexOf(Object target) { 619 // Overridden to prevent a ton of boxing 620 if (target instanceof Long) { 621 int i = Longs.indexOf(array, (Long) target, start, end); 622 if (i >= 0) { 623 return i - start; 624 } 625 } 626 return -1; 627 } 628 629 @Override 630 public int lastIndexOf(Object target) { 631 // Overridden to prevent a ton of boxing 632 if (target instanceof Long) { 633 int i = Longs.lastIndexOf(array, (Long) target, start, end); 634 if (i >= 0) { 635 return i - start; 636 } 637 } 638 return -1; 639 } 640 641 @Override 642 public Long set(int index, Long element) { 643 checkElementIndex(index, size()); 644 long oldValue = array[start + index]; 645 // checkNotNull for GWT (do not optimize) 646 array[start + index] = checkNotNull(element); 647 return oldValue; 648 } 649 650 @Override 651 public List<Long> subList(int fromIndex, int toIndex) { 652 int size = size(); 653 checkPositionIndexes(fromIndex, toIndex, size); 654 if (fromIndex == toIndex) { 655 return Collections.emptyList(); 656 } 657 return new LongArrayAsList(array, start + fromIndex, start + toIndex); 658 } 659 660 @Override 661 public boolean equals(@Nullable Object object) { 662 if (object == this) { 663 return true; 664 } 665 if (object instanceof LongArrayAsList) { 666 LongArrayAsList that = (LongArrayAsList) object; 667 int size = size(); 668 if (that.size() != size) { 669 return false; 670 } 671 for (int i = 0; i < size; i++) { 672 if (array[start + i] != that.array[that.start + i]) { 673 return false; 674 } 675 } 676 return true; 677 } 678 return super.equals(object); 679 } 680 681 @Override 682 public int hashCode() { 683 int result = 1; 684 for (int i = start; i < end; i++) { 685 result = 31 * result + Longs.hashCode(array[i]); 686 } 687 return result; 688 } 689 690 @Override 691 public String toString() { 692 StringBuilder builder = new StringBuilder(size() * 10); 693 builder.append('[').append(array[start]); 694 for (int i = start + 1; i < end; i++) { 695 builder.append(", ").append(array[i]); 696 } 697 return builder.append(']').toString(); 698 } 699 700 long[] toLongArray() { 701 // Arrays.copyOfRange() is not available under GWT 702 int size = size(); 703 long[] result = new long[size]; 704 System.arraycopy(array, start, result, 0, size); 705 return result; 706 } 707 708 private static final long serialVersionUID = 0; 709 } 710}