KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > services > EvaluationResultCacheComparator


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.ui.internal.services;
13
14 import java.util.Comparator JavaDoc;
15
16 import org.eclipse.ui.internal.util.Util;
17
18 /**
19  * <p>
20  * Compares two evaluation result caches ({@link IEvaluationResultCache}). The
21  * cache with the lowest source priority is considered "greater". In the event
22  * of a tie, other characteristics are checked.
23  * </p>
24  * <p>
25  * This class is only intended for use within the
26  * <code>org.eclipse.ui.workbench</code> plug-in.
27  * </p>
28  *
29  * @since 3.2
30  *
31  */

32 public final class EvaluationResultCacheComparator implements Comparator JavaDoc {
33
34     public final int compare(final Object JavaDoc object1, final Object JavaDoc object2) {
35         if (Util.equals(object2, object1)) {
36             return 0;
37         }
38
39         final IEvaluationResultCache cache1 = (IEvaluationResultCache) object1;
40         final IEvaluationResultCache cache2 = (IEvaluationResultCache) object2;
41         int comparison;
42
43         /*
44          * Note: all of the comparisons are flipped intentionally. This allows
45          * those items with greater values to appear earlier when using an
46          * iterator.
47          */

48         // if objects went to the trouble to implement Comparable
49
// we should use it ... this algorithm can accept a natural ordering
50
// that's not compatible with equals.
51
if (object1 instanceof Comparable JavaDoc && object2 instanceof Comparable JavaDoc) {
52             comparison = Util.compare((Comparable JavaDoc) object2,
53                     (Comparable JavaDoc) object1);
54             if (comparison != 0) {
55                 return comparison;
56             }
57         }
58
59         return Util.compareIdentity(cache2, cache1);
60     }
61 }
62
63
Popular Tags