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