Coverage Report - org.apache.giraph.block_app.library.VertexSuppliers
 
Classes in this File Line Coverage Branch Coverage Complexity
VertexSuppliers
0%
0/11
N/A
0
VertexSuppliers$1
0%
0/2
N/A
0
VertexSuppliers$2
0%
0/2
N/A
0
VertexSuppliers$3
0%
0/2
N/A
0
VertexSuppliers$4
0%
0/2
N/A
0
VertexSuppliers$5
0%
0/2
N/A
0
VertexSuppliers$5$1
0%
0/2
N/A
0
VertexSuppliers$6
0%
0/2
N/A
0
VertexSuppliers$7
0%
0/10
0%
0/4
0
 
 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.block_app.library;
 19  
 
 20  
 import static com.google.common.base.Preconditions.checkNotNull;
 21  
 
 22  
 import java.util.Iterator;
 23  
 
 24  
 import org.apache.giraph.comm.SendMessageCache.TargetVertexIdIterator;
 25  
 import org.apache.giraph.edge.Edge;
 26  
 import org.apache.giraph.function.PairPredicate;
 27  
 import org.apache.giraph.function.Predicate;
 28  
 import org.apache.giraph.function.primitive.PrimitiveRefs.IntRef;
 29  
 import org.apache.giraph.function.vertex.SupplierFromVertex;
 30  
 import org.apache.giraph.graph.Vertex;
 31  
 import org.apache.hadoop.io.Writable;
 32  
 import org.apache.hadoop.io.WritableComparable;
 33  
 
 34  
 import com.google.common.collect.AbstractIterator;
 35  
 import com.google.common.collect.Iterators;
 36  
 import com.google.common.collect.UnmodifiableIterator;
 37  
 
 38  
 /**
 39  
  * SupplierFromVertex that extract common information from
 40  
  * vertex itself.
 41  
  */
 42  
 @SuppressWarnings("rawtypes")
 43  0
 public class VertexSuppliers {
 44  
   /** Hide constructor */
 45  0
   private VertexSuppliers() { }
 46  
 
 47  
   /**
 48  
    * Supplier which extracts and returns vertex ID.
 49  
    * (note - do not modify the object, as it is not returning a copy)
 50  
    */
 51  
   public static
 52  
   <I extends WritableComparable, V extends Writable, E extends Writable>
 53  
   SupplierFromVertex<I, V, E, I> vertexIdSupplier() {
 54  0
     return new SupplierFromVertex<I, V, E, I>() {
 55  
       @Override
 56  
       public I get(Vertex<I, V, E> vertex) {
 57  0
         return vertex.getId();
 58  
       }
 59  
     };
 60  
   }
 61  
 
 62  
   /**
 63  
    * Supplier which extracts and returns vertex value.
 64  
    * (note - doesn't create a copy of vertex value)
 65  
    */
 66  
   public static
 67  
   <I extends WritableComparable, V extends Writable, E extends Writable>
 68  
   SupplierFromVertex<I, V, E, V> vertexValueSupplier() {
 69  0
     return new SupplierFromVertex<I, V, E, V>() {
 70  
       @Override
 71  
       public V get(Vertex<I, V, E> vertex) {
 72  0
         return vertex.getValue();
 73  
       }
 74  
     };
 75  
   }
 76  
 
 77  
   /**
 78  
    * Supplier which extracts and returns edges object.
 79  
    * (note - doesn't create a copy of vertex value)
 80  
    */
 81  
   public static
 82  
   <I extends WritableComparable, V extends Writable, E extends Writable>
 83  
   SupplierFromVertex<I, V, E, Iterable<Edge<I, E>>> vertexEdgesSupplier() {
 84  0
     return new SupplierFromVertex<I, V, E, Iterable<Edge<I, E>>>() {
 85  
       @Override
 86  
       public Iterable<Edge<I, E>> get(Vertex<I, V, E> vertex) {
 87  0
         return vertex.getEdges();
 88  
       }
 89  
     };
 90  
   }
 91  
 
 92  
   /**
 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  
    */
 97  
   public static
 98  
   <I extends WritableComparable, V extends Writable, E extends Writable>
 99  
   SupplierFromVertex<I, V, E, Iterator<I>> vertexNeighborsSupplier() {
 100  0
     return new SupplierFromVertex<I, V, E, Iterator<I>>() {
 101  
       @Override
 102  
       public Iterator<I> get(final Vertex<I, V, E> vertex) {
 103  0
         return new TargetVertexIdIterator<>(vertex);
 104  
       }
 105  
     };
 106  
   }
 107  
 
 108  
   /**
 109  
    * Supplier which extracts and returns Iterator over neighbor IDs
 110  
    * that return true for given predicate.
 111  
    * Note - iterator returns reused object, so you need to "use" them,
 112  
    * before calling next() again.
 113  
    */
 114  
   public static
 115  
   <I extends WritableComparable, V extends Writable, E extends Writable>
 116  
   SupplierFromVertex<I, V, E, Iterator<I>> vertexNeighborsSupplier(
 117  
       final Predicate<I> toSupply) {
 118  0
     return new SupplierFromVertex<I, V, E, Iterator<I>>() {
 119  
       @Override
 120  
       public Iterator<I> get(final Vertex<I, V, E> vertex) {
 121  0
         return Iterators.filter(
 122  
             new TargetVertexIdIterator<>(vertex),
 123  0
             new com.google.common.base.Predicate<I>() {
 124  
               @Override
 125  
               public boolean apply(I input) {
 126  0
                 return toSupply.apply(input);
 127  
               }
 128  
             });
 129  
       }
 130  
     };
 131  
   }
 132  
 
 133  
   /**
 134  
    * Supplier which gives Iterator over neighbor IDs that return true for given
 135  
    * predicate over (index, target)
 136  
    * Note - iterator returns reused object, so you need to "use" them,
 137  
    * before calling next() again.
 138  
    */
 139  
   public static
 140  
   <I extends WritableComparable, V extends Writable, E extends Writable>
 141  
   SupplierFromVertex<I, V, E, Iterator<I>> vertexNeighborsSupplierWithIndex(
 142  
       final PairPredicate<IntRef, I> toSupply) {
 143  0
     return new SupplierFromVertex<I, V, E, Iterator<I>>() {
 144  
       @Override
 145  
       public Iterator<I> get(final Vertex<I, V, E> vertex) {
 146  
         // Every time we return an iterator, we return with a fresh (0) index.
 147  0
         return filterWithIndex(
 148  
             new TargetVertexIdIterator<>(vertex), toSupply);
 149  
       }
 150  
     };
 151  
   }
 152  
 
 153  
   /**
 154  
    * Returns the elements of {@code unfiltered} that satisfy a
 155  
    * predicate over (index, t).
 156  
    */
 157  
   private static <T> UnmodifiableIterator<T> filterWithIndex(
 158  
       final Iterator<T> unfiltered, final PairPredicate<IntRef, T> predicate) {
 159  0
     checkNotNull(unfiltered);
 160  0
     checkNotNull(predicate);
 161  0
     return new AbstractIterator<T>() {
 162  0
       private final IntRef index = new IntRef(0);
 163  
       @Override protected T computeNext() {
 164  0
         while (unfiltered.hasNext()) {
 165  0
           T element = unfiltered.next();
 166  0
           boolean res = predicate.apply(index, element);
 167  0
           index.value++;
 168  0
           if (res) {
 169  0
             return element;
 170  
           }
 171  0
         }
 172  0
         return endOfData();
 173  
       }
 174  
     };
 175  
   }
 176  
 }