1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.primitives;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkElementIndex;
21 import static com.google.common.base.Preconditions.checkNotNull;
22 import static com.google.common.base.Preconditions.checkPositionIndexes;
23
24 import com.google.common.annotations.Beta;
25 import com.google.common.annotations.GwtCompatible;
26 import com.google.common.annotations.GwtIncompatible;
27 import com.google.common.base.Converter;
28
29 import java.io.Serializable;
30 import java.util.AbstractList;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.Comparator;
35 import java.util.List;
36 import java.util.RandomAccess;
37
38 import javax.annotation.CheckForNull;
39
40
41
42
43
44
45
46
47
48
49
50
51 @GwtCompatible(emulated = true)
52 public final class Ints {
53 private Ints() {}
54
55
56
57
58
59 public static final int BYTES = Integer.SIZE / Byte.SIZE;
60
61
62
63
64
65
66 public static final int MAX_POWER_OF_TWO = 1 << (Integer.SIZE - 2);
67
68
69
70
71
72
73
74
75 public static int hashCode(int value) {
76 return value;
77 }
78
79
80
81
82
83
84
85
86
87 public static int checkedCast(long value) {
88 int result = (int) value;
89 if (result != value) {
90
91 throw new IllegalArgumentException("Out of range: " + value);
92 }
93 return result;
94 }
95
96
97
98
99
100
101
102
103
104 public static int saturatedCast(long value) {
105 if (value > Integer.MAX_VALUE) {
106 return Integer.MAX_VALUE;
107 }
108 if (value < Integer.MIN_VALUE) {
109 return Integer.MIN_VALUE;
110 }
111 return (int) value;
112 }
113
114
115
116
117
118
119
120
121
122
123
124
125
126 public static int compare(int a, int b) {
127 return (a < b) ? -1 : ((a > b) ? 1 : 0);
128 }
129
130
131
132
133
134
135
136
137
138
139 public static boolean contains(int[] array, int target) {
140 for (int value : array) {
141 if (value == target) {
142 return true;
143 }
144 }
145 return false;
146 }
147
148
149
150
151
152
153
154
155
156
157 public static int indexOf(int[] array, int target) {
158 return indexOf(array, target, 0, array.length);
159 }
160
161
162 private static int indexOf(
163 int[] array, int target, int start, int end) {
164 for (int i = start; i < end; i++) {
165 if (array[i] == target) {
166 return i;
167 }
168 }
169 return -1;
170 }
171
172
173
174
175
176
177
178
179
180
181
182
183 public static int indexOf(int[] array, int[] target) {
184 checkNotNull(array, "array");
185 checkNotNull(target, "target");
186 if (target.length == 0) {
187 return 0;
188 }
189
190 outer:
191 for (int i = 0; i < array.length - target.length + 1; i++) {
192 for (int j = 0; j < target.length; j++) {
193 if (array[i + j] != target[j]) {
194 continue outer;
195 }
196 }
197 return i;
198 }
199 return -1;
200 }
201
202
203
204
205
206
207
208
209
210
211 public static int lastIndexOf(int[] array, int target) {
212 return lastIndexOf(array, target, 0, array.length);
213 }
214
215
216 private static int lastIndexOf(
217 int[] array, int target, int start, int end) {
218 for (int i = end - 1; i >= start; i--) {
219 if (array[i] == target) {
220 return i;
221 }
222 }
223 return -1;
224 }
225
226
227
228
229
230
231
232
233
234 public static int min(int... array) {
235 checkArgument(array.length > 0);
236 int min = array[0];
237 for (int i = 1; i < array.length; i++) {
238 if (array[i] < min) {
239 min = array[i];
240 }
241 }
242 return min;
243 }
244
245
246
247
248
249
250
251
252
253 public static int max(int... array) {
254 checkArgument(array.length > 0);
255 int max = array[0];
256 for (int i = 1; i < array.length; i++) {
257 if (array[i] > max) {
258 max = array[i];
259 }
260 }
261 return max;
262 }
263
264
265
266
267
268
269
270
271
272
273 public static int[] concat(int[]... arrays) {
274 int length = 0;
275 for (int[] array : arrays) {
276 length += array.length;
277 }
278 int[] result = new int[length];
279 int pos = 0;
280 for (int[] array : arrays) {
281 System.arraycopy(array, 0, result, pos, array.length);
282 pos += array.length;
283 }
284 return result;
285 }
286
287
288
289
290
291
292
293
294
295
296
297
298 @GwtIncompatible("doesn't work")
299 public static byte[] toByteArray(int value) {
300 return new byte[] {
301 (byte) (value >> 24),
302 (byte) (value >> 16),
303 (byte) (value >> 8),
304 (byte) value};
305 }
306
307
308
309
310
311
312
313
314
315
316
317
318
319 @GwtIncompatible("doesn't work")
320 public static int fromByteArray(byte[] bytes) {
321 checkArgument(bytes.length >= BYTES,
322 "array too small: %s < %s", bytes.length, BYTES);
323 return fromBytes(bytes[0], bytes[1], bytes[2], bytes[3]);
324 }
325
326
327
328
329
330
331
332
333 @GwtIncompatible("doesn't work")
334 public static int fromBytes(byte b1, byte b2, byte b3, byte b4) {
335 return b1 << 24 | (b2 & 0xFF) << 16 | (b3 & 0xFF) << 8 | (b4 & 0xFF);
336 }
337
338 private static final class IntConverter
339 extends Converter<String, Integer> implements Serializable {
340 static final IntConverter INSTANCE = new IntConverter();
341
342 @Override
343 protected Integer doForward(String value) {
344 return Integer.decode(value);
345 }
346
347 @Override
348 protected String doBackward(Integer value) {
349 return value.toString();
350 }
351
352 @Override
353 public String toString() {
354 return "Ints.stringConverter()";
355 }
356
357 private Object readResolve() {
358 return INSTANCE;
359 }
360 private static final long serialVersionUID = 1;
361 }
362
363
364
365
366
367
368
369 @Beta
370 public static Converter<String, Integer> stringConverter() {
371 return IntConverter.INSTANCE;
372 }
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390 public static int[] ensureCapacity(
391 int[] array, int minLength, int padding) {
392 checkArgument(minLength >= 0, "Invalid minLength: %s", minLength);
393 checkArgument(padding >= 0, "Invalid padding: %s", padding);
394 return (array.length < minLength)
395 ? copyOf(array, minLength + padding)
396 : array;
397 }
398
399
400 private static int[] copyOf(int[] original, int length) {
401 int[] copy = new int[length];
402 System.arraycopy(original, 0, copy, 0, Math.min(original.length, length));
403 return copy;
404 }
405
406
407
408
409
410
411
412
413
414
415 public static String join(String separator, int... array) {
416 checkNotNull(separator);
417 if (array.length == 0) {
418 return "";
419 }
420
421
422 StringBuilder builder = new StringBuilder(array.length * 5);
423 builder.append(array[0]);
424 for (int i = 1; i < array.length; i++) {
425 builder.append(separator).append(array[i]);
426 }
427 return builder.toString();
428 }
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445 public static Comparator<int[]> lexicographicalComparator() {
446 return LexicographicalComparator.INSTANCE;
447 }
448
449 private enum LexicographicalComparator implements Comparator<int[]> {
450 INSTANCE;
451
452 @Override
453 public int compare(int[] left, int[] right) {
454 int minLength = Math.min(left.length, right.length);
455 for (int i = 0; i < minLength; i++) {
456 int result = Ints.compare(left[i], right[i]);
457 if (result != 0) {
458 return result;
459 }
460 }
461 return left.length - right.length;
462 }
463 }
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480 public static int[] toArray(Collection<? extends Number> collection) {
481 if (collection instanceof IntArrayAsList) {
482 return ((IntArrayAsList) collection).toIntArray();
483 }
484
485 Object[] boxedArray = collection.toArray();
486 int len = boxedArray.length;
487 int[] array = new int[len];
488 for (int i = 0; i < len; i++) {
489
490 array[i] = ((Number) checkNotNull(boxedArray[i])).intValue();
491 }
492 return array;
493 }
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509 public static List<Integer> asList(int... backingArray) {
510 if (backingArray.length == 0) {
511 return Collections.emptyList();
512 }
513 return new IntArrayAsList(backingArray);
514 }
515
516 @GwtCompatible
517 private static class IntArrayAsList extends AbstractList<Integer>
518 implements RandomAccess, Serializable {
519 final int[] array;
520 final int start;
521 final int end;
522
523 IntArrayAsList(int[] array) {
524 this(array, 0, array.length);
525 }
526
527 IntArrayAsList(int[] array, int start, int end) {
528 this.array = array;
529 this.start = start;
530 this.end = end;
531 }
532
533 @Override public int size() {
534 return end - start;
535 }
536
537 @Override public boolean isEmpty() {
538 return false;
539 }
540
541 @Override public Integer get(int index) {
542 checkElementIndex(index, size());
543 return array[start + index];
544 }
545
546 @Override public boolean contains(Object target) {
547
548 return (target instanceof Integer)
549 && Ints.indexOf(array, (Integer) target, start, end) != -1;
550 }
551
552 @Override public int indexOf(Object target) {
553
554 if (target instanceof Integer) {
555 int i = Ints.indexOf(array, (Integer) target, start, end);
556 if (i >= 0) {
557 return i - start;
558 }
559 }
560 return -1;
561 }
562
563 @Override public int lastIndexOf(Object target) {
564
565 if (target instanceof Integer) {
566 int i = Ints.lastIndexOf(array, (Integer) target, start, end);
567 if (i >= 0) {
568 return i - start;
569 }
570 }
571 return -1;
572 }
573
574 @Override public Integer set(int index, Integer element) {
575 checkElementIndex(index, size());
576 int oldValue = array[start + index];
577
578 array[start + index] = checkNotNull(element);
579 return oldValue;
580 }
581
582 @Override public List<Integer> subList(int fromIndex, int toIndex) {
583 int size = size();
584 checkPositionIndexes(fromIndex, toIndex, size);
585 if (fromIndex == toIndex) {
586 return Collections.emptyList();
587 }
588 return new IntArrayAsList(array, start + fromIndex, start + toIndex);
589 }
590
591 @Override public boolean equals(Object object) {
592 if (object == this) {
593 return true;
594 }
595 if (object instanceof IntArrayAsList) {
596 IntArrayAsList that = (IntArrayAsList) object;
597 int size = size();
598 if (that.size() != size) {
599 return false;
600 }
601 for (int i = 0; i < size; i++) {
602 if (array[start + i] != that.array[that.start + i]) {
603 return false;
604 }
605 }
606 return true;
607 }
608 return super.equals(object);
609 }
610
611 @Override public int hashCode() {
612 int result = 1;
613 for (int i = start; i < end; i++) {
614 result = 31 * result + Ints.hashCode(array[i]);
615 }
616 return result;
617 }
618
619 @Override public String toString() {
620 StringBuilder builder = new StringBuilder(size() * 5);
621 builder.append('[').append(array[start]);
622 for (int i = start + 1; i < end; i++) {
623 builder.append(", ").append(array[i]);
624 }
625 return builder.append(']').toString();
626 }
627
628 int[] toIntArray() {
629
630 int size = size();
631 int[] result = new int[size];
632 System.arraycopy(array, start, result, 0, size);
633 return result;
634 }
635
636 private static final long serialVersionUID = 0;
637 }
638
639 private static final byte[] asciiDigits = new byte[128];
640
641 static {
642 Arrays.fill(asciiDigits, (byte) -1);
643 for (int i = 0; i <= 9; i++) {
644 asciiDigits['0' + i] = (byte) i;
645 }
646 for (int i = 0; i <= 26; i++) {
647 asciiDigits['A' + i] = (byte) (10 + i);
648 asciiDigits['a' + i] = (byte) (10 + i);
649 }
650 }
651
652 private static int digit(char c) {
653 return (c < 128) ? asciiDigits[c] : -1;
654 }
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676 @Beta
677 @CheckForNull
678 @GwtIncompatible("TODO")
679 public static Integer tryParse(String string) {
680 return tryParse(string, 10);
681 }
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705 @CheckForNull
706 @GwtIncompatible("TODO") static Integer tryParse(
707 String string, int radix) {
708 if (checkNotNull(string).isEmpty()) {
709 return null;
710 }
711 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
712 throw new IllegalArgumentException(
713 "radix must be between MIN_RADIX and MAX_RADIX but was " + radix);
714 }
715 boolean negative = string.charAt(0) == '-';
716 int index = negative ? 1 : 0;
717 if (index == string.length()) {
718 return null;
719 }
720 int digit = digit(string.charAt(index++));
721 if (digit < 0 || digit >= radix) {
722 return null;
723 }
724 int accum = -digit;
725
726 int cap = Integer.MIN_VALUE / radix;
727
728 while (index < string.length()) {
729 digit = digit(string.charAt(index++));
730 if (digit < 0 || digit >= radix || accum < cap) {
731 return null;
732 }
733 accum *= radix;
734 if (accum < Integer.MIN_VALUE + digit) {
735 return null;
736 }
737 accum -= digit;
738 }
739
740 if (negative) {
741 return accum;
742 } else if (accum == Integer.MIN_VALUE) {
743 return null;
744 } else {
745 return -accum;
746 }
747 }
748 }