Coverage Report - org.apache.giraph.edge.ArrayListEdges
 
Classes in this File Line Coverage Branch Coverage Complexity
ArrayListEdges
0%
0/35
0%
0/10
1.455
 
 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 com.google.common.collect.Lists;
 22  
 import org.apache.giraph.utils.Trimmable;
 23  
 import org.apache.giraph.utils.WritableUtils;
 24  
 import org.apache.hadoop.io.Writable;
 25  
 import org.apache.hadoop.io.WritableComparable;
 26  
 
 27  
 import java.io.DataInput;
 28  
 import java.io.DataOutput;
 29  
 import java.io.IOException;
 30  
 import java.util.ArrayList;
 31  
 import java.util.Iterator;
 32  
 
 33  
 /**
 34  
  * {@link OutEdges} implementation backed by an {@link ArrayList}.
 35  
  * Parallel edges are allowed.
 36  
  *
 37  
  * @param <I> Vertex id
 38  
  * @param <E> Edge value
 39  
  */
 40  0
 public class ArrayListEdges<I extends WritableComparable, E extends Writable>
 41  
     extends ConfigurableOutEdges<I, E>
 42  
     implements MutableOutEdges<I, E>, Trimmable {
 43  
   /** List of edges. */
 44  
   private ArrayList<Edge<I, E>> edgeList;
 45  
 
 46  
   @Override
 47  
   public void initialize(Iterable<Edge<I, E>> edges) {
 48  
     // If the iterable is actually an instance of ArrayList,
 49  
     // we simply copy the reference.
 50  
     // Otherwise we have to add every edge.
 51  0
     if (edges instanceof ArrayList) {
 52  0
       edgeList = (ArrayList<Edge<I, E>>) edges;
 53  
     } else {
 54  0
       edgeList = Lists.newArrayList(edges);
 55  
     }
 56  0
   }
 57  
 
 58  
   @Override
 59  
   public void initialize(int capacity) {
 60  0
     edgeList = Lists.newArrayListWithCapacity(capacity);
 61  0
   }
 62  
 
 63  
   @Override
 64  
   public void initialize() {
 65  0
     edgeList = Lists.newArrayList();
 66  0
   }
 67  
 
 68  
   @Override
 69  
   public void add(Edge<I, E> edge) {
 70  0
     edgeList.add(edge);
 71  0
   }
 72  
 
 73  
   @Override
 74  
   public void remove(I targetVertexId) {
 75  0
     for (Iterator<Edge<I, E>> edges = edgeList.iterator(); edges.hasNext();) {
 76  0
       Edge<I, E> edge = edges.next();
 77  0
       if (edge.getTargetVertexId().equals(targetVertexId)) {
 78  0
         edges.remove();
 79  
       }
 80  0
     }
 81  0
   }
 82  
 
 83  
   @Override
 84  
   public int size() {
 85  0
     return edgeList.size();
 86  
   }
 87  
 
 88  
   @Override
 89  
   public final Iterator<Edge<I, E>> iterator() {
 90  0
     return edgeList.iterator();
 91  
   }
 92  
 
 93  
   @Override
 94  
   @SuppressWarnings("unchecked")
 95  
   public Iterator<MutableEdge<I, E>> mutableIterator() {
 96  
     // The downcast is fine because all concrete Edge implementations are
 97  
     // mutable, but we only expose the mutation functionality when appropriate.
 98  0
     return (Iterator) iterator();
 99  
   }
 100  
 
 101  
   @Override
 102  
   public void write(DataOutput out) throws IOException {
 103  0
     out.writeInt(edgeList.size());
 104  0
     for (Edge<I, E> edge : edgeList) {
 105  0
       edge.getTargetVertexId().write(out);
 106  0
       edge.getValue().write(out);
 107  0
     }
 108  0
   }
 109  
 
 110  
   @Override
 111  
   public void readFields(DataInput in) throws IOException {
 112  0
     int numEdges = in.readInt();
 113  0
     initialize(numEdges);
 114  0
     for (int i = 0; i < numEdges; ++i) {
 115  0
       Edge<I, E> edge = getConf().createEdge();
 116  0
       WritableUtils.readEdge(in, edge);
 117  0
       edgeList.add(edge);
 118  
     }
 119  0
   }
 120  
 
 121  
   @Override
 122  
   public void trim() {
 123  0
     edgeList.trimToSize();
 124  0
   }
 125  
 }