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 value nearest to {@code value} which is within the closed range {@code [min..max]}. 228 * 229 * <p>If {@code value} is within the range {@code [min..max]}, {@code value} is returned 230 * unchanged. If {@code value} is less than {@code min}, {@code min} is returned, and if 231 * {@code value} is greater than {@code max}, {@code max} is returned. 232 * 233 * @param value the {@code long} value to constrain 234 * @param min the lower bound (inclusive) of the range to constrain {@code value} to 235 * @param max the upper bound (inclusive) of the range to constrain {@code value} to 236 * @throws IllegalArgumentException if {@code min > max} 237 * @since 21.0 238 */ 239 @Beta 240 public static long constrainToRange(long value, long min, long max) { 241 checkArgument(min <= max, "min (%s) must be less than or equal to max (%s)", min, max); 242 return Math.min(Math.max(value, min), max); 243 } 244 245 /** 246 * Returns the values from each provided array combined into a single array. For example, 247 * {@code concat(new long[] {a, b}, new long[] {}, new long[] {c}} returns the array 248 * {@code {a, b, c}}. 249 * 250 * @param arrays zero or more {@code long} arrays 251 * @return a single array containing all the values from the source arrays, in order 252 */ 253 public static long[] concat(long[]... arrays) { 254 int length = 0; 255 for (long[] array : arrays) { 256 length += array.length; 257 } 258 long[] result = new long[length]; 259 int pos = 0; 260 for (long[] array : arrays) { 261 System.arraycopy(array, 0, result, pos, array.length); 262 pos += array.length; 263 } 264 return result; 265 } 266 267 /** 268 * Returns a big-endian representation of {@code value} in an 8-element byte array; equivalent to 269 * {@code ByteBuffer.allocate(8).putLong(value).array()}. For example, the input value 270 * {@code 0x1213141516171819L} would yield the byte array {@code {0x12, 0x13, 0x14, 0x15, 0x16, 271 * 0x17, 0x18, 0x19}}. 272 * 273 * <p>If you need to convert and concatenate several values (possibly even of different types), 274 * use a shared {@link java.nio.ByteBuffer} instance, or use 275 * {@link com.google.common.io.ByteStreams#newDataOutput()} to get a growable buffer. 276 */ 277 public static byte[] toByteArray(long value) { 278 // Note that this code needs to stay compatible with GWT, which has known 279 // bugs when narrowing byte casts of long values occur. 280 byte[] result = new byte[8]; 281 for (int i = 7; i >= 0; i--) { 282 result[i] = (byte) (value & 0xffL); 283 value >>= 8; 284 } 285 return result; 286 } 287 288 /** 289 * Returns the {@code long} value whose big-endian representation is stored in the first 8 bytes 290 * of {@code bytes}; equivalent to {@code ByteBuffer.wrap(bytes).getLong()}. For example, the 291 * input byte array {@code {0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19}} would yield the 292 * {@code long} value {@code 0x1213141516171819L}. 293 * 294 * <p>Arguably, it's preferable to use {@link java.nio.ByteBuffer}; that library exposes much more 295 * flexibility at little cost in readability. 296 * 297 * @throws IllegalArgumentException if {@code bytes} has fewer than 8 elements 298 */ 299 public static long fromByteArray(byte[] bytes) { 300 checkArgument(bytes.length >= BYTES, "array too small: %s < %s", bytes.length, BYTES); 301 return fromBytes( 302 bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]); 303 } 304 305 /** 306 * Returns the {@code long} value whose byte representation is the given 8 bytes, in big-endian 307 * order; equivalent to {@code Longs.fromByteArray(new byte[] {b1, b2, b3, b4, b5, b6, b7, b8})}. 308 * 309 * @since 7.0 310 */ 311 public static long fromBytes( 312 byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, byte b8) { 313 return (b1 & 0xFFL) << 56 314 | (b2 & 0xFFL) << 48 315 | (b3 & 0xFFL) << 40 316 | (b4 & 0xFFL) << 32 317 | (b5 & 0xFFL) << 24 318 | (b6 & 0xFFL) << 16 319 | (b7 & 0xFFL) << 8 320 | (b8 & 0xFFL); 321 } 322 323 /* 324 * Moving asciiDigits into this static holder class lets ProGuard eliminate and inline the Longs 325 * class. 326 */ 327 static final class AsciiDigits { 328 private AsciiDigits() {} 329 330 private static final byte[] asciiDigits; 331 332 static { 333 byte[] result = new byte[128]; 334 Arrays.fill(result, (byte) -1); 335 for (int i = 0; i <= 9; i++) { 336 result['0' + i] = (byte) i; 337 } 338 for (int i = 0; i <= 26; i++) { 339 result['A' + i] = (byte) (10 + i); 340 result['a' + i] = (byte) (10 + i); 341 } 342 asciiDigits = result; 343 } 344 345 static int digit(char c) { 346 return (c < 128) ? asciiDigits[c] : -1; 347 } 348 } 349 350 /** 351 * Parses the specified string as a signed decimal long value. The ASCII character {@code '-'} 352 * (<code>'\u002D'</code>) is recognized as the minus sign. 353 * 354 * <p>Unlike {@link Long#parseLong(String)}, this method returns {@code null} instead of throwing 355 * an exception if parsing fails. Additionally, this method only accepts ASCII digits, and returns 356 * {@code null} if non-ASCII digits are present in the string. 357 * 358 * <p>Note that strings prefixed with ASCII {@code '+'} are rejected, even under JDK 7, despite 359 * the change to {@link Long#parseLong(String)} for that version. 360 * 361 * @param string the string representation of a long value 362 * @return the long value represented by {@code string}, or {@code null} if {@code string} has a 363 * length of zero or cannot be parsed as a long value 364 * @since 14.0 365 */ 366 @Beta 367 @Nullable 368 @CheckForNull 369 public static Long tryParse(String string) { 370 return tryParse(string, 10); 371 } 372 373 /** 374 * Parses the specified string as a signed long value using the specified radix. The ASCII 375 * character {@code '-'} (<code>'\u002D'</code>) is recognized as the minus sign. 376 * 377 * <p>Unlike {@link Long#parseLong(String, int)}, this method returns {@code null} instead of 378 * throwing an exception if parsing fails. Additionally, this method only accepts ASCII digits, 379 * and returns {@code null} if non-ASCII digits are present in the string. 380 * 381 * <p>Note that strings prefixed with ASCII {@code '+'} are rejected, even under JDK 7, despite 382 * the change to {@link Long#parseLong(String, int)} for that version. 383 * 384 * @param string the string representation of an long value 385 * @param radix the radix to use when parsing 386 * @return the long value represented by {@code string} using {@code radix}, or {@code null} if 387 * {@code string} has a length of zero or cannot be parsed as a long value 388 * @throws IllegalArgumentException if {@code radix < Character.MIN_RADIX} or 389 * {@code radix > Character.MAX_RADIX} 390 * @since 19.0 391 */ 392 @Beta 393 @Nullable 394 @CheckForNull 395 public static Long tryParse(String string, int radix) { 396 if (checkNotNull(string).isEmpty()) { 397 return null; 398 } 399 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) { 400 throw new IllegalArgumentException( 401 "radix must be between MIN_RADIX and MAX_RADIX but was " + radix); 402 } 403 boolean negative = string.charAt(0) == '-'; 404 int index = negative ? 1 : 0; 405 if (index == string.length()) { 406 return null; 407 } 408 int digit = AsciiDigits.digit(string.charAt(index++)); 409 if (digit < 0 || digit >= radix) { 410 return null; 411 } 412 long accum = -digit; 413 414 long cap = Long.MIN_VALUE / radix; 415 416 while (index < string.length()) { 417 digit = AsciiDigits.digit(string.charAt(index++)); 418 if (digit < 0 || digit >= radix || accum < cap) { 419 return null; 420 } 421 accum *= radix; 422 if (accum < Long.MIN_VALUE + digit) { 423 return null; 424 } 425 accum -= digit; 426 } 427 428 if (negative) { 429 return accum; 430 } else if (accum == Long.MIN_VALUE) { 431 return null; 432 } else { 433 return -accum; 434 } 435 } 436 437 private static final class LongConverter extends Converter<String, Long> implements Serializable { 438 static final LongConverter INSTANCE = new LongConverter(); 439 440 @Override 441 protected Long doForward(String value) { 442 return Long.decode(value); 443 } 444 445 @Override 446 protected String doBackward(Long value) { 447 return value.toString(); 448 } 449 450 @Override 451 public String toString() { 452 return "Longs.stringConverter()"; 453 } 454 455 private Object readResolve() { 456 return INSTANCE; 457 } 458 459 private static final long serialVersionUID = 1; 460 } 461 462 /** 463 * Returns a serializable converter object that converts between strings and longs using 464 * {@link Long#decode} and {@link Long#toString()}. The returned converter throws 465 * {@link NumberFormatException} if the input string is invalid. 466 * 467 * <p><b>Warning:</b> please see {@link Long#decode} to understand exactly how strings are parsed. 468 * For example, the string {@code "0123"} is treated as <i>octal</i> and converted to the value 469 * {@code 83L}. 470 * 471 * @since 16.0 472 */ 473 @Beta 474 public static Converter<String, Long> stringConverter() { 475 return LongConverter.INSTANCE; 476 } 477 478 /** 479 * Returns an array containing the same values as {@code array}, but guaranteed to be of a 480 * specified minimum length. If {@code array} already has a length of at least {@code minLength}, 481 * it is returned directly. Otherwise, a new array of size {@code minLength + padding} is 482 * returned, containing the values of {@code array}, and zeroes in the remaining places. 483 * 484 * @param array the source array 485 * @param minLength the minimum length the returned array must guarantee 486 * @param padding an extra amount to "grow" the array by if growth is necessary 487 * @throws IllegalArgumentException if {@code minLength} or {@code padding} is negative 488 * @return an array containing the values of {@code array}, with guaranteed minimum length 489 * {@code minLength} 490 */ 491 public static long[] ensureCapacity(long[] array, int minLength, int padding) { 492 checkArgument(minLength >= 0, "Invalid minLength: %s", minLength); 493 checkArgument(padding >= 0, "Invalid padding: %s", padding); 494 return (array.length < minLength) ? Arrays.copyOf(array, minLength + padding) : array; 495 } 496 497 /** 498 * Returns a string containing the supplied {@code long} values separated by {@code separator}. 499 * For example, {@code join("-", 1L, 2L, 3L)} returns the string {@code "1-2-3"}. 500 * 501 * @param separator the text that should appear between consecutive values in the resulting string 502 * (but not at the start or end) 503 * @param array an array of {@code long} values, possibly empty 504 */ 505 public static String join(String separator, long... array) { 506 checkNotNull(separator); 507 if (array.length == 0) { 508 return ""; 509 } 510 511 // For pre-sizing a builder, just get the right order of magnitude 512 StringBuilder builder = new StringBuilder(array.length * 10); 513 builder.append(array[0]); 514 for (int i = 1; i < array.length; i++) { 515 builder.append(separator).append(array[i]); 516 } 517 return builder.toString(); 518 } 519 520 /** 521 * Returns a comparator that compares two {@code long} arrays <a 522 * href="http://en.wikipedia.org/wiki/Lexicographical_order">lexicographically</a>. That is, it 523 * compares, using {@link #compare(long, long)}), the first pair of values that follow any common 524 * prefix, or when one array is a prefix of the other, treats the shorter array as the lesser. For 525 * example, {@code [] < [1L] < [1L, 2L] < [2L]}. 526 * 527 * <p>The returned comparator is inconsistent with {@link Object#equals(Object)} (since arrays 528 * support only identity equality), but it is consistent with 529 * {@link Arrays#equals(long[], long[])}. 530 * 531 * @since 2.0 532 */ 533 public static Comparator<long[]> lexicographicalComparator() { 534 return LexicographicalComparator.INSTANCE; 535 } 536 537 private enum LexicographicalComparator implements Comparator<long[]> { 538 INSTANCE; 539 540 @Override 541 public int compare(long[] left, long[] right) { 542 int minLength = Math.min(left.length, right.length); 543 for (int i = 0; i < minLength; i++) { 544 int result = Longs.compare(left[i], right[i]); 545 if (result != 0) { 546 return result; 547 } 548 } 549 return left.length - right.length; 550 } 551 552 @Override 553 public String toString() { 554 return "Longs.lexicographicalComparator()"; 555 } 556 } 557 558 /** 559 * Sorts the elements of {@code array} in descending order. 560 * 561 * @since 23.1 562 */ 563 public static void sortDescending(long[] array) { 564 checkNotNull(array); 565 sortDescending(array, 0, array.length); 566 } 567 568 /** 569 * Sorts the elements of {@code array} between {@code fromIndex} inclusive and {@code toIndex} 570 * exclusive in descending order. 571 * 572 * @since 23.1 573 */ 574 public static void sortDescending(long[] array, int fromIndex, int toIndex) { 575 checkNotNull(array); 576 checkPositionIndexes(fromIndex, toIndex, array.length); 577 Arrays.sort(array, fromIndex, toIndex); 578 reverse(array, fromIndex, toIndex); 579 } 580 581 /** 582 * Reverses the elements of {@code array}. This is equivalent to {@code 583 * Collections.reverse(Longs.asList(array))}, but is likely to be more efficient. 584 * 585 * @since 23.1 586 */ 587 public static void reverse(long[] array) { 588 checkNotNull(array); 589 reverse(array, 0, array.length); 590 } 591 592 /** 593 * Reverses the elements of {@code array} between {@code fromIndex} inclusive and {@code toIndex} 594 * exclusive. This is equivalent to {@code 595 * Collections.reverse(Longs.asList(array).subList(fromIndex, toIndex))}, but is likely to be more 596 * efficient. 597 * 598 * @throws IndexOutOfBoundsException if {@code fromIndex < 0}, {@code toIndex > array.length}, or 599 * {@code toIndex > fromIndex} 600 * @since 23.1 601 */ 602 public static void reverse(long[] array, int fromIndex, int toIndex) { 603 checkNotNull(array); 604 checkPositionIndexes(fromIndex, toIndex, array.length); 605 for (int i = fromIndex, j = toIndex - 1; i < j; i++, j--) { 606 long tmp = array[i]; 607 array[i] = array[j]; 608 array[j] = tmp; 609 } 610 } 611 612 /** 613 * Returns an array containing each value of {@code collection}, converted to a {@code long} value 614 * in the manner of {@link Number#longValue}. 615 * 616 * <p>Elements are copied from the argument collection as if by {@code 617 * collection.toArray()}. Calling this method is as thread-safe as calling that method. 618 * 619 * @param collection a collection of {@code Number} instances 620 * @return an array containing the same values as {@code collection}, in the same order, converted 621 * to primitives 622 * @throws NullPointerException if {@code collection} or any of its elements is null 623 * @since 1.0 (parameter was {@code Collection<Long>} before 12.0) 624 */ 625 public static long[] toArray(Collection<? extends Number> collection) { 626 if (collection instanceof LongArrayAsList) { 627 return ((LongArrayAsList) collection).toLongArray(); 628 } 629 630 Object[] boxedArray = collection.toArray(); 631 int len = boxedArray.length; 632 long[] array = new long[len]; 633 for (int i = 0; i < len; i++) { 634 // checkNotNull for GWT (do not optimize) 635 array[i] = ((Number) checkNotNull(boxedArray[i])).longValue(); 636 } 637 return array; 638 } 639 640 /** 641 * Returns a fixed-size list backed by the specified array, similar to {@link 642 * Arrays#asList(Object[])}. The list supports {@link List#set(int, Object)}, but any attempt to 643 * set a value to {@code null} will result in a {@link NullPointerException}. 644 * 645 * <p>The returned list maintains the values, but not the identities, of {@code Long} objects 646 * written to or read from it. For example, whether {@code list.get(0) == list.get(0)} is true for 647 * the returned list is unspecified. 648 * 649 * <p><b>Note:</b> when possible, you should represent your data as an {@link ImmutableLongArray} 650 * instead, which has an {@link ImmutableLongArray#asList asList} view. 651 * 652 * @param backingArray the array to back the list 653 * @return a list view of the array 654 */ 655 public static List<Long> asList(long... backingArray) { 656 if (backingArray.length == 0) { 657 return Collections.emptyList(); 658 } 659 return new LongArrayAsList(backingArray); 660 } 661 662 @GwtCompatible 663 private static class LongArrayAsList extends AbstractList<Long> 664 implements RandomAccess, Serializable { 665 final long[] array; 666 final int start; 667 final int end; 668 669 LongArrayAsList(long[] array) { 670 this(array, 0, array.length); 671 } 672 673 LongArrayAsList(long[] array, int start, int end) { 674 this.array = array; 675 this.start = start; 676 this.end = end; 677 } 678 679 @Override 680 public int size() { 681 return end - start; 682 } 683 684 @Override 685 public boolean isEmpty() { 686 return false; 687 } 688 689 @Override 690 public Long get(int index) { 691 checkElementIndex(index, size()); 692 return array[start + index]; 693 } 694 695 @Override 696 public boolean contains(Object target) { 697 // Overridden to prevent a ton of boxing 698 return (target instanceof Long) && Longs.indexOf(array, (Long) target, start, end) != -1; 699 } 700 701 @Override 702 public int indexOf(Object target) { 703 // Overridden to prevent a ton of boxing 704 if (target instanceof Long) { 705 int i = Longs.indexOf(array, (Long) target, start, end); 706 if (i >= 0) { 707 return i - start; 708 } 709 } 710 return -1; 711 } 712 713 @Override 714 public int lastIndexOf(Object target) { 715 // Overridden to prevent a ton of boxing 716 if (target instanceof Long) { 717 int i = Longs.lastIndexOf(array, (Long) target, start, end); 718 if (i >= 0) { 719 return i - start; 720 } 721 } 722 return -1; 723 } 724 725 @Override 726 public Long set(int index, Long element) { 727 checkElementIndex(index, size()); 728 long oldValue = array[start + index]; 729 // checkNotNull for GWT (do not optimize) 730 array[start + index] = checkNotNull(element); 731 return oldValue; 732 } 733 734 @Override 735 public List<Long> subList(int fromIndex, int toIndex) { 736 int size = size(); 737 checkPositionIndexes(fromIndex, toIndex, size); 738 if (fromIndex == toIndex) { 739 return Collections.emptyList(); 740 } 741 return new LongArrayAsList(array, start + fromIndex, start + toIndex); 742 } 743 744 @Override 745 public boolean equals(@Nullable Object object) { 746 if (object == this) { 747 return true; 748 } 749 if (object instanceof LongArrayAsList) { 750 LongArrayAsList that = (LongArrayAsList) object; 751 int size = size(); 752 if (that.size() != size) { 753 return false; 754 } 755 for (int i = 0; i < size; i++) { 756 if (array[start + i] != that.array[that.start + i]) { 757 return false; 758 } 759 } 760 return true; 761 } 762 return super.equals(object); 763 } 764 765 @Override 766 public int hashCode() { 767 int result = 1; 768 for (int i = start; i < end; i++) { 769 result = 31 * result + Longs.hashCode(array[i]); 770 } 771 return result; 772 } 773 774 @Override 775 public String toString() { 776 StringBuilder builder = new StringBuilder(size() * 10); 777 builder.append('[').append(array[start]); 778 for (int i = start + 1; i < end; i++) { 779 builder.append(", ").append(array[i]); 780 } 781 return builder.append(']').toString(); 782 } 783 784 long[] toLongArray() { 785 return Arrays.copyOfRange(array, start, end); 786 } 787 788 private static final long serialVersionUID = 0; 789 } 790}