1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.giraph.types.ops.collections.array;
19
20 import java.util.NoSuchElementException;
21
22 import org.apache.giraph.function.Consumer;
23 import org.apache.giraph.function.Predicate;
24 import org.apache.giraph.types.ops.collections.ResettableIterator;
25
26 /**
27 * Private utilities class
28 *
29 * (needed since Java 7 has no default methods on interfaces,
30 * and we need to extend appropriate fastutil classes)
31 */
32 class WArrayListPrivateUtils {
33 /** Hide constructor */
34 private WArrayListPrivateUtils() { }
35
36 /**
37 * Fast iterator over collection objects, which doesn't allocate new
38 * element for each returned element, and can be iterated multiple times
39 * using reset().
40 *
41 * Object returned by next() is only valid until next() is called again,
42 * because it is reused.
43 *
44 * @param list Collection to iterate over
45 * @param iterationValue reusable iteration value
46 * @return RessettableIterator
47 * @param <T> Element type
48 */
49 static <T> ResettableIterator<T> fastIterator(
50 final WArrayList<T> list, final T iterationValue) {
51 return new ResettableIterator<T>() {
52 private int pos;
53
54 @Override
55 public boolean hasNext() {
56 return pos < list.size();
57 }
58
59 @Override
60 public T next() {
61 if (!hasNext()) {
62 throw new NoSuchElementException();
63 }
64 list.getIntoW(pos, iterationValue);
65 pos++;
66 return iterationValue;
67 }
68
69 @Override
70 public void reset() {
71 pos = 0;
72 }
73
74 @Override
75 public void remove() {
76 throw new UnsupportedOperationException();
77 }
78 };
79 }
80
81 /**
82 * Traverse all elements of the collection, calling given function on each
83 * element. Passed values are valid only during the call to the passed
84 * function, so data needs to be consumed during the function.
85 *
86 * @param list Collection to iterate over
87 * @param f Function to call on each element.
88 * @param iterationValue reusable iteration value
89 * @param <T> Element type
90 */
91 static <T> void fastForEach(
92 WArrayList<T> list, Consumer<T> f, T iterationValue) {
93 for (int i = 0; i < list.size(); ++i) {
94 list.getIntoW(i, iterationValue);
95 f.apply(iterationValue);
96 }
97 }
98
99 /**
100 * Traverse all elements of the collection, calling given function on each
101 * element, or until predicate returns false.
102 * Passed values are valid only during the call to the passed
103 * function, so data needs to be consumed during the function.
104 *
105 * @param list Collection to iterate over
106 * @param f Function to call on each element.
107 * @param iterationValue reusable iteration value
108 * @return true if the predicate returned true for all elements,
109 * false if it returned false for some element.
110 * @param <T> Element type
111 */
112 static <T> boolean fastForEachWhile(
113 WArrayList<T> list, Predicate<T> f, T iterationValue) {
114 for (int i = 0; i < list.size(); ++i) {
115 list.getIntoW(i, iterationValue);
116 if (!f.apply(iterationValue)) {
117 return false;
118 }
119 }
120 return true;
121 }
122 }