blob: 8b147cf6f058317bef2b30a811ca73f7cda97553 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
/*******************************************************************************
* Copyright (c) 2004-2010 Gabor Bergmann and Daniel Varro
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-v20.html.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package tools.refinery.viatra.runtime.rete.util;
import java.util.Comparator;
/**
* Comparing agent for an ordering. Terminology: the "preferred" item will register as LESS.
*
* @author Gabor Bergmann
*
*/
public abstract class OrderingCompareAgent<T> {
protected T a;
protected T b;
/**
* @param a
* @param b
*/
public OrderingCompareAgent(T a, T b) {
super();
this.a = a;
this.b = b;
}
int result = 0;
protected abstract void doCompare();
/**
* @return the result
*/
public int compare() {
doCompare();
return result;
}
// COMPARISON HELPERS
protected boolean isUnknown() {
return result == 0;
}
/**
* @pre result == 0
*/
protected boolean consider(int partial) {
if (isUnknown())
result = partial;
return isUnknown();
}
protected boolean swallowBoolean(boolean x) {
return x;
}
// PREFERENCE FUNCTIONS
protected static int dontCare() {
return 0;
}
protected static int preferTrue(boolean b1, boolean b2) {
return (b1 ^ b2) ? (b1 ? -1 : +1) : 0;
}
protected static int preferFalse(boolean b1, boolean b2) {
return (b1 ^ b2) ? (b2 ? -1 : +1) : 0;
}
protected static <U> int preferLess(Comparable<U> c1, U c2) {
return c1.compareTo(c2);
}
protected static <U> int preferLess(U c1, U c2, Comparator<U> comp) {
return comp.compare(c1, c2);
}
protected static <U> int preferMore(Comparable<U> c1, U c2) {
return -c1.compareTo(c2);
}
protected static <U> int preferMore(U c1, U c2, Comparator<U> comp) {
return -comp.compare(c1, c2);
}
}
|