1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */18package org.apache.giraph.block_app.library;
1920importstatic com.google.common.base.Preconditions.checkNotNull;
2122import java.util.Iterator;
2324import org.apache.giraph.comm.SendMessageCache.TargetVertexIdIterator;
25import org.apache.giraph.edge.Edge;
26import org.apache.giraph.function.PairPredicate;
27import org.apache.giraph.function.Predicate;
28import org.apache.giraph.function.primitive.PrimitiveRefs.IntRef;
29import org.apache.giraph.function.vertex.SupplierFromVertex;
30import org.apache.giraph.graph.Vertex;
31import org.apache.hadoop.io.Writable;
32import org.apache.hadoop.io.WritableComparable;
3334import com.google.common.collect.AbstractIterator;
35import com.google.common.collect.Iterators;
36import com.google.common.collect.UnmodifiableIterator;
3738/**39 * SupplierFromVertex that extract common information from40 * vertex itself.41 */42 @SuppressWarnings("rawtypes")
43publicclassVertexSuppliers {
44/** Hide constructor */45privateVertexSuppliers() { }
4647/**48 * Supplier which extracts and returns vertex ID.49 * (note - do not modify the object, as it is not returning a copy)50 */51publicstatic52 <I extends WritableComparable, V extends Writable, E extends Writable>
53 SupplierFromVertex<I, V, E, I> vertexIdSupplier() {
54returnnew SupplierFromVertex<I, V, E, I>() {
55 @Override
56public I get(Vertex<I, V, E> vertex) {
57return vertex.getId();
58 }
59 };
60 }
6162/**63 * Supplier which extracts and returns vertex value.64 * (note - doesn't create a copy of vertex value)65 */66publicstatic67 <I extends WritableComparable, V extends Writable, E extends Writable>
68 SupplierFromVertex<I, V, E, V> vertexValueSupplier() {
69returnnew SupplierFromVertex<I, V, E, V>() {
70 @Override
71public V get(Vertex<I, V, E> vertex) {
72return vertex.getValue();
73 }
74 };
75 }
7677/**78 * Supplier which extracts and returns edges object.79 * (note - doesn't create a copy of vertex value)80 */81publicstatic82 <I extends WritableComparable, V extends Writable, E extends Writable>
83 SupplierFromVertex<I, V, E, Iterable<Edge<I, E>>> vertexEdgesSupplier() {
84returnnew SupplierFromVertex<I, V, E, Iterable<Edge<I, E>>>() {
85 @Override
86public Iterable<Edge<I, E>> get(Vertex<I, V, E> vertex) {
87return vertex.getEdges();
88 }
89 };
90 }
9192/**93 * Supplier which extracts and returns Iterator over all neighbor IDs.94 * Note - iterator returns reused object, so you need to "use" them,95 * before calling next() again.96 */97publicstatic98 <I extends WritableComparable, V extends Writable, E extends Writable>
99 SupplierFromVertex<I, V, E, Iterator<I>> vertexNeighborsSupplier() {
100returnnew SupplierFromVertex<I, V, E, Iterator<I>>() {
101 @Override
102public Iterator<I> get(final Vertex<I, V, E> vertex) {
103returnnew TargetVertexIdIterator<>(vertex);
104 }
105 };
106 }
107108/**109 * Supplier which extracts and returns Iterator over neighbor IDs110 * that return true for given predicate.111 * Note - iterator returns reused object, so you need to "use" them,112 * before calling next() again.113 */114publicstatic115 <I extends WritableComparable, V extends Writable, E extends Writable>
116 SupplierFromVertex<I, V, E, Iterator<I>> vertexNeighborsSupplier(
117final Predicate<I> toSupply) {
118returnnew SupplierFromVertex<I, V, E, Iterator<I>>() {
119 @Override
120public Iterator<I> get(final Vertex<I, V, E> vertex) {
121return Iterators.filter(
122new TargetVertexIdIterator<>(vertex),
123new com.google.common.base.Predicate<I>() {
124 @Override
125publicboolean apply(I input) {
126return toSupply.apply(input);
127 }
128 });
129 }
130 };
131 }
132133/**134 * Supplier which gives Iterator over neighbor IDs that return true for given135 * predicate over (index, target)136 * Note - iterator returns reused object, so you need to "use" them,137 * before calling next() again.138 */139publicstatic140 <I extends WritableComparable, V extends Writable, E extends Writable>
141 SupplierFromVertex<I, V, E, Iterator<I>> vertexNeighborsSupplierWithIndex(
142final PairPredicate<IntRef, I> toSupply) {
143returnnew SupplierFromVertex<I, V, E, Iterator<I>>() {
144 @Override
145public Iterator<I> get(final Vertex<I, V, E> vertex) {
146// Every time we return an iterator, we return with a fresh (0) index.147return filterWithIndex(
148new TargetVertexIdIterator<>(vertex), toSupply);
149 }
150 };
151 }
152153/**154 * Returns the elements of {@code unfiltered} that satisfy a155 * predicate over (index, t).156 */157privatestatic <T> UnmodifiableIterator<T> filterWithIndex(
158final Iterator<T> unfiltered, final PairPredicate<IntRef, T> predicate) {
159 checkNotNull(unfiltered);
160 checkNotNull(predicate);
161returnnew AbstractIterator<T>() {
162privatefinal IntRef index = new IntRef(0);
163 @Override protected T computeNext() {
164while (unfiltered.hasNext()) {
165 T element = unfiltered.next();
166boolean res = predicate.apply(index, element);
167 index.value++;
168if (res) {
169return element;
170 }
171 }
172return endOfData();
173 }
174 };
175 }
176 }