• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2024, Google LLC
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  * Neither the name of Google LLC nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 package com.android.tools.smali.util;
32 
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.Objects;
38 import java.util.function.Predicate;
39 
40 public final class IteratorUtils {
41 
getLast(Iterator<T> iterator)42     public static <T extends Object> T getLast(Iterator<T> iterator) {
43         while (true) {
44             T current = iterator.next();
45             if (!iterator.hasNext()) {
46                 return current;
47             }
48         }
49     }
50 
filter( Iterable<T> unfiltered, Predicate<? super T> retainIfTrue)51     public static <T extends Object> AbstractIterator<T> filter(
52             Iterable<T> unfiltered, Predicate<? super T> retainIfTrue) {
53         return filter(unfiltered.iterator(), retainIfTrue);
54     }
55 
filter( Iterator<T> unfiltered, Predicate<? super T> retainIfTrue)56     public static <T extends Object> AbstractIterator<T> filter(
57             Iterator<T> unfiltered, Predicate<? super T> retainIfTrue) {
58         return new AbstractIterator<T>() {
59             @Override
60             protected T computeNext() {
61                 while (unfiltered.hasNext()) {
62                     T next = unfiltered.next();
63                     if (retainIfTrue.test(next)) {
64                         return next;
65                     }
66                 }
67                 return endOfData();
68             }
69         };
70     }
71 
72     public static <T extends Object> List<T> toList(Iterable<T> iterable) {
73         return toList(iterable.iterator());
74     }
75 
76     public static <T extends Object> List<T> toList(Iterator<T> iterator) {
77         ArrayList<T> list = new ArrayList<T>();
78         while (iterator.hasNext()) {
79             list.add(iterator.next());
80         }
81         return list;
82     }
83 
84     public static <T extends Object> void addAll(Collection<T> collection, Iterator<T> iterator) {
85         while (iterator.hasNext()) {
86             collection.add(iterator.next());
87         }
88     }
89 
90     public static boolean elementsEqual(Iterator<?> iterator1, Iterator<?> iterator2) {
91         while (iterator1.hasNext()) {
92             if (!iterator2.hasNext()) {
93                 return false;
94             }
95             Object o1 = iterator1.next();
96             Object o2 = iterator2.next();
97             if (!Objects.equals(o1, o2)) {
98                 return false;
99             }
100         }
101         return !iterator2.hasNext();
102     }
103 
104     public static int size(Iterable<?> iterable) {
105         Iterator<?> iterator = iterable.iterator();
106         int count = 0;
107         while (iterator.hasNext()) {
108             count++;
109             iterator.next();
110         }
111         return count;
112     }
113 }
114