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 */1819package org.apache.giraph.edge;
2021import org.apache.giraph.graph.Vertex;
22import org.apache.hadoop.io.Writable;
23import org.apache.hadoop.io.WritableComparable;
2425import java.util.Iterator;
2627/**28 * Helper class to provide a mutable iterable over the edges when the chosen29 * {@link OutEdges} doesn't offer a specialized one.30 *31 * @param <I> Vertex id32 * @param <E> Edge value33 */34publicclass MutableEdgesIterable<I extends WritableComparable,
35 E extends Writable> implements Iterable<MutableEdge<I, E>> {
36/**Vertex that owns the out-edges. */37private Vertex<I, ?, E> vertex;
3839/**40 * Constructor.41 *42 * @param vertex Owning vertex43 */44publicMutableEdgesIterable(Vertex<I, ?, E> vertex) {
45this.vertex = vertex;
46 }
4748 @Override
49public Iterator<MutableEdge<I, E>> iterator() {
50final MutableEdgesWrapper<I, E> mutableEdgesWrapper =
51 MutableEdgesWrapper.wrap((OutEdges<I, E>) vertex.getEdges(),
52 vertex.getConf());
53 vertex.setEdges(mutableEdgesWrapper);
5455returnnew Iterator<MutableEdge<I, E>>() {
56/** Iterator over the old edges. */57private Iterator<Edge<I, E>> oldEdgesIterator =
58 mutableEdgesWrapper.getOldEdgesIterator();
59/** New edges data structure. */60private OutEdges<I, E> newEdges = mutableEdgesWrapper.getNewEdges();
6162 @Override
63publicboolean hasNext() {
64// If the current edge is not null,65// we need to add it to the new edges.66 Edge<I, E> currentEdge = mutableEdgesWrapper.getCurrentEdge();
67if (currentEdge != null) {
68 newEdges.add(currentEdge);
69 mutableEdgesWrapper.setCurrentEdge(null);
70 }
71if (!oldEdgesIterator.hasNext()) {
72 vertex.setEdges(newEdges);
73return false;
74 } else {
75returntrue;
76 }
77 }
7879 @Override
80public MutableEdge<I, E> next() {
81// If the current edge is not null,82// we need to add it to the new edges.83 MutableEdge<I, E> currentEdge =
84 mutableEdgesWrapper.getCurrentEdge();
85if (currentEdge != null) {
86 newEdges.add(currentEdge);
87 }
88// Read the next edge and return it.89 currentEdge = (MutableEdge<I, E>) oldEdgesIterator.next();
90 mutableEdgesWrapper.setCurrentEdge(currentEdge);
91return currentEdge;
92 }
9394 @Override
95publicvoid remove() {
96// Set the current edge to null, so that it's not added to the97// new edges.98 mutableEdgesWrapper.setCurrentEdge(null);
99 mutableEdgesWrapper.decrementEdges();
100 }
101 };
102 }
103 }