KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgrapht > experimental > equivalence > EquivalenceComparatorChainBase


1 /* ==========================================
2  * JGraphT : a free Java graph-theory library
3  * ==========================================
4  *
5  * Project Info: http://jgrapht.sourceforge.net/
6  * Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh)
7  *
8  * (C) Copyright 2003-2006, by Barak Naveh and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18  * License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this library; if not, write to the Free Software Foundation,
22  * Inc.,
23  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24  */

25 /* -----------------
26  * EquivalenceComparatorChainBase.java
27  * -----------------
28  * (C) Copyright 2005-2006, by Assaf Lehr and Contributors.
29  *
30  * Original Author: Assaf Lehr
31  * Contributor(s): -
32  *
33  * $Id: EquivalenceComparatorChainBase.java 485 2006-06-26 09:12:14Z perfecthash
34  * $
35  *
36  * Changes
37  * -------
38  */

39 package org.jgrapht.experimental.equivalence;
40
41 import java.util.*;
42
43
44 /**
45  * This class implements comparator chaining.
46  *
47  * <p>Usage examples:
48  * <li> <i>graph-theory, node equivalence:</i> You can create a comparator for
49  * the inDegree of a node, another for the total weight of outDegree edges, and
50  * a third which checks the business content of the node. You know that the
51  * first topological comparators has dozens of different groups, but the
52  * buisness comparator has only two, and they are hard to check . The best
53  * performance will be gained by:
54  *
55  * <blockquote><code>
56  * <p>EquivalenceComparatorChainBase eqChain = new
57  * EquivalenceComparatorChainBase(fastNodesDegreeComparator);
58  *
59  * <p>eqChain.addComparatorAfter(ABitSlowerEdgeWeightComparator);
60  *
61  * <p>eqChain.addComparatorAfter(slowestBuisnessContentsComparator);</code>
62  * </blockquote>
63  *
64  * @param <E> the type of the elements in the set
65  * @param <C> the type of the context the element is compared against, e.g. a
66  * Graph
67  *
68  * @author Assaf
69  * @since Jul 22, 2005
70  */

71 public class EquivalenceComparatorChainBase<E, C>
72     implements EquivalenceComparatorChain<E, C>
73 {
74
75     //~ Instance fields -------------------------------------------------------
76

77     private List<EquivalenceComparator<? super E, ? super C>> chain;
78
79     //~ Constructors ----------------------------------------------------------
80

81     /**
82      */

83     public EquivalenceComparatorChainBase(
84         EquivalenceComparator<E, C> firstComaparator)
85     {
86         this.chain =
87             new LinkedList<EquivalenceComparator<? super E, ? super C>>();
88         this.chain.add(firstComaparator);
89     }
90
91     //~ Methods ---------------------------------------------------------------
92

93     /* (non-Javadoc)
94      * @see
95      *
96      *
97      *
98      *
99      *
100      * org.jgrapht.experimental.equivalence.EquivalenceComparatorChain#addComparatorAfter(org.jgrapht.experimental.equivalence.EquivalenceComparator)
101      */

102     @SuppressWarnings JavaDoc("unchecked")
103     public void appendComparator(EquivalenceComparator comparatorAfter)
104     {
105         if (comparatorAfter != null) {
106             this.chain.add(comparatorAfter);
107         }
108     }
109
110     /**
111      * Implements logical AND between the comparators results. Iterates through
112      * the comparators chain until one of them returns false. If none returns
113      * false, this method returns true.
114      *
115      * @see EquivalenceComparator#equivalenceCompare(Object, Object, Object,
116      * Object)
117      */

118     public boolean equivalenceCompare(
119         E arg1,
120         E arg2,
121         C context1,
122         C context2)
123     {
124         for (
125             EquivalenceComparator<? super E, ? super C> currentComparator
126             : this.chain) {
127             if (
128                 !currentComparator.equivalenceCompare(
129                     arg1,
130                     arg2,
131                     context1,
132                     context2)) {
133                 return false;
134             }
135         }
136         return true;
137     }
138
139     /**
140      * Rehashes the concatenation of the results of all single hashcodes.
141      *
142      * @see EquivalenceComparator#equivalenceHashcode(Object, Object)
143      */

144     public int equivalenceHashcode(E arg1, C context)
145     {
146         StringBuffer JavaDoc hashStringBuffer = new StringBuffer JavaDoc();
147         for (
148             ListIterator<EquivalenceComparator<? super E, ? super C>> iter =
149                 this.chain.listIterator();
150             iter.hasNext();) {
151             EquivalenceComparator<? super E, ? super C> currentComparator =
152                 iter.next();
153             int currentHashCode =
154                 currentComparator.equivalenceHashcode(arg1, context);
155             hashStringBuffer.append(currentHashCode);
156
157             // add a delimeter only if needed for next
158
if (iter.hasNext()) {
159                 hashStringBuffer.append('+');
160             }
161         }
162         return hashStringBuffer.toString().hashCode();
163     }
164 }
165
Popular Tags