KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > graph > VisitationTimeComparator


1 /*
2  * Generic graph library
3  * Copyright (C) 2000,2003,2004 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 // $Revision: 1.10 $
21

22 package edu.umd.cs.findbugs.graph;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Comparator JavaDoc;
26
27 import edu.umd.cs.findbugs.annotations.SuppressWarnings;
28
29 /**
30  * Comparator to compare GraphVertex objects by their visitation times in a
31  * search; for example, it could compare the finishing times produced
32  * by DepthFirstSearch.
33  */

34 public class VisitationTimeComparator <VertexType extends GraphVertex<VertexType>> implements Comparator JavaDoc<VertexType>,
35 Serializable JavaDoc {
36     private static final long serialVersionUID = 0L;
37     /**
38      * Compare in ascending order.
39      */

40     public static final int ASCENDING = 0;
41
42     /**
43      * Compare in descending order.
44      */

45     public static final int DESCENDING = 1;
46
47     private int[] m_visitationTimeList;
48     private int m_direction;
49
50     /**
51      * Constructor.
52      *
53      * @param visitationTimeList array of visitation times indexed by vertex label
54      * @param direction either ASCENDING or DESCENDING
55      */

56     @SuppressWarnings JavaDoc("EI2")
57     public VisitationTimeComparator(int[] visitationTimeList, int direction) {
58         m_visitationTimeList = visitationTimeList;
59         m_direction = direction;
60
61         if (direction != ASCENDING && direction != DESCENDING)
62             throw new IllegalArgumentException JavaDoc();
63     }
64
65     public int compare(VertexType v1, VertexType v2) {
66         int f1 = m_visitationTimeList[v1.getLabel()];
67         int f2 = m_visitationTimeList[v2.getLabel()];
68
69         if (m_direction == ASCENDING)
70             return f1 - f2;
71         else
72             return f2 - f1;
73     }
74
75 }
76
77 // vim:ts=4
78
Popular Tags