Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MutableEdgesIterable |
|
| 1.8;1.8 | ||||
MutableEdgesIterable$1 |
|
| 1.8;1.8 |
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 | ||
19 | package org.apache.giraph.edge; | |
20 | ||
21 | import org.apache.giraph.graph.Vertex; | |
22 | import org.apache.hadoop.io.Writable; | |
23 | import org.apache.hadoop.io.WritableComparable; | |
24 | ||
25 | import java.util.Iterator; | |
26 | ||
27 | /** | |
28 | * Helper class to provide a mutable iterable over the edges when the chosen | |
29 | * {@link OutEdges} doesn't offer a specialized one. | |
30 | * | |
31 | * @param <I> Vertex id | |
32 | * @param <E> Edge value | |
33 | */ | |
34 | 0 | public class MutableEdgesIterable<I extends WritableComparable, |
35 | E extends Writable> implements Iterable<MutableEdge<I, E>> { | |
36 | /** Vertex that owns the out-edges. */ | |
37 | private Vertex<I, ?, E> vertex; | |
38 | ||
39 | /** | |
40 | * Constructor. | |
41 | * | |
42 | * @param vertex Owning vertex | |
43 | */ | |
44 | 0 | public MutableEdgesIterable(Vertex<I, ?, E> vertex) { |
45 | 0 | this.vertex = vertex; |
46 | 0 | } |
47 | ||
48 | @Override | |
49 | public Iterator<MutableEdge<I, E>> iterator() { | |
50 | 0 | final MutableEdgesWrapper<I, E> mutableEdgesWrapper = |
51 | 0 | MutableEdgesWrapper.wrap((OutEdges<I, E>) vertex.getEdges(), |
52 | 0 | vertex.getConf()); |
53 | 0 | vertex.setEdges(mutableEdgesWrapper); |
54 | ||
55 | 0 | return new Iterator<MutableEdge<I, E>>() { |
56 | /** Iterator over the old edges. */ | |
57 | 0 | private Iterator<Edge<I, E>> oldEdgesIterator = |
58 | 0 | mutableEdgesWrapper.getOldEdgesIterator(); |
59 | /** New edges data structure. */ | |
60 | 0 | private OutEdges<I, E> newEdges = mutableEdgesWrapper.getNewEdges(); |
61 | ||
62 | @Override | |
63 | public boolean hasNext() { | |
64 | // If the current edge is not null, | |
65 | // we need to add it to the new edges. | |
66 | 0 | Edge<I, E> currentEdge = mutableEdgesWrapper.getCurrentEdge(); |
67 | 0 | if (currentEdge != null) { |
68 | 0 | newEdges.add(currentEdge); |
69 | 0 | mutableEdgesWrapper.setCurrentEdge(null); |
70 | } | |
71 | 0 | if (!oldEdgesIterator.hasNext()) { |
72 | 0 | vertex.setEdges(newEdges); |
73 | 0 | return false; |
74 | } else { | |
75 | 0 | return true; |
76 | } | |
77 | } | |
78 | ||
79 | @Override | |
80 | public MutableEdge<I, E> next() { | |
81 | // If the current edge is not null, | |
82 | // we need to add it to the new edges. | |
83 | 0 | MutableEdge<I, E> currentEdge = |
84 | 0 | mutableEdgesWrapper.getCurrentEdge(); |
85 | 0 | if (currentEdge != null) { |
86 | 0 | newEdges.add(currentEdge); |
87 | } | |
88 | // Read the next edge and return it. | |
89 | 0 | currentEdge = (MutableEdge<I, E>) oldEdgesIterator.next(); |
90 | 0 | mutableEdgesWrapper.setCurrentEdge(currentEdge); |
91 | 0 | return currentEdge; |
92 | } | |
93 | ||
94 | @Override | |
95 | public void remove() { | |
96 | // Set the current edge to null, so that it's not added to the | |
97 | // new edges. | |
98 | 0 | mutableEdgesWrapper.setCurrentEdge(null); |
99 | 0 | mutableEdgesWrapper.decrementEdges(); |
100 | 0 | } |
101 | }; | |
102 | } | |
103 | } |