01: /*******************************************************************************
02: * Copyright (c) 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: ******************************************************************************/package org.eclipse.ui.internal.services;
11:
12: import java.util.Comparator;
13:
14: import org.eclipse.ui.internal.util.Util;
15:
16: /**
17: * <p>
18: * Compares two evaluation result caches ({@link IEvaluationResultCache}). The
19: * cache with the lowest source priority is considered "greater". In the event
20: * of a tie, other characteristics are checked.
21: * </p>
22: * <p>
23: * This class is only intended for use within the
24: * <code>org.eclipse.ui.workbench</code> plug-in.
25: * </p>
26: *
27: * @since 3.2
28: *
29: */
30: public final class EvaluationResultCacheComparator implements
31: Comparator {
32:
33: public final int compare(final Object object1, final Object object2) {
34: if (Util.equals(object2, object1)) {
35: return 0;
36: }
37:
38: final IEvaluationResultCache cache1 = (IEvaluationResultCache) object1;
39: final IEvaluationResultCache cache2 = (IEvaluationResultCache) object2;
40: int comparison;
41:
42: /*
43: * Note: all of the comparisons are flipped intentionally. This allows
44: * those items with greater values to appear earlier when using an
45: * iterator.
46: */
47: // if objects went to the trouble to implement Comparable
48: // we should use it ... this algorithm can accept a natural ordering
49: // that's not compatible with equals.
50: if (object1 instanceof Comparable
51: && object2 instanceof Comparable) {
52: comparison = Util.compare((Comparable) object2,
53: (Comparable) object1);
54: if (comparison != 0) {
55: return comparison;
56: }
57: }
58:
59: return Util.compareIdentity(cache2, cache1);
60: }
61: }
|