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.utils; 20 21 import com.google.common.collect.ComparisonChain; 22 import org.apache.giraph.edge.Edge; 23 import org.apache.hadoop.io.WritableComparable; 24 25 import java.io.Serializable; 26 import java.util.Comparator; 27 28 /** 29 * Comparator for edges. 30 * 31 * @param <I> Vertex id 32 * @param <E> Edge value (needs to be WritableComparable) 33 */ 34 public class EdgeComparator<I extends WritableComparable, 35 E extends WritableComparable> implements Comparator<Edge<I, E>>, 36 Serializable { 37 /** Serialization version. */ 38 private static final long serialVersionUID = 1L; 39 40 @Override 41 public int compare(Edge<I, E> e1, Edge<I, E> e2) { 42 return compareEdges(e1, e2); 43 } 44 45 /** 46 * Compares two edges. 47 * 48 * @param e1 First edge 49 * @param e2 Second edge 50 * @param <I> Vertex id 51 * @param <E> Edge value (needs to be WritableComparable) 52 * @return A negative, zero, or positive value depending 53 */ 54 public static <I extends WritableComparable, E extends WritableComparable> 55 int compareEdges(Edge<I, E> e1, Edge<I, E> e2) { 56 return ComparisonChain.start() 57 .compare(e1.getTargetVertexId(), e2.getTargetVertexId()) 58 .compare(e1.getValue(), e2.getValue()) 59 .result(); 60 } 61 62 /** 63 * Indicates whether two edges are equal. 64 * 65 * @param e1 First edge 66 * @param e2 Second edge 67 * @param <I> Vertex id 68 * @param <E> Edge value (needs to be WritableComparable) 69 * @return Whether the two edges are equal 70 */ 71 public static <I extends WritableComparable, E extends WritableComparable> 72 boolean equal(Edge<I, E> e1, Edge<I, E> e2) { 73 return compareEdges(e1, e2) == 0; 74 } 75 }