blob: f08c1c3f3dc391449871f9769a02a84d773995d0 (
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
|
/*******************************************************************************
* Copyright (c) 2010, 2017 Cloudsmith Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Cloudsmith Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.equinox.internal.p2.metadata.expression;
import java.util.Collection;
import org.eclipse.equinox.p2.metadata.expression.IEvaluationContext;
/**
* Comparisons for magnitude.
*/
final class Compare extends Binary {
static IllegalArgumentException uncomparable(Object lval, Object rval) {
return new IllegalArgumentException("Cannot compare a " + lval.getClass().getName() + " to a " + rval.getClass().getName()); //$NON-NLS-1$//$NON-NLS-2$
}
final boolean compareLess;
final boolean equalOK;
Compare(Expression lhs, Expression rhs, boolean compareLess, boolean equalOK) {
super(lhs, rhs);
this.compareLess = compareLess;
this.equalOK = equalOK;
}
@Override
public Object evaluate(IEvaluationContext context) {
Object lhsVal = lhs.evaluate(context);
Object rhsVal = rhs.evaluate(context);
// Handle collections as per the OSGi LDAP spec
if (lhsVal instanceof Collection<?>) {
for (Object lhsItem : (Collection<?>) lhsVal) {
int cmpResult = CoercingComparator.coerceAndCompare(lhsItem, rhsVal);
if (cmpResult == 0) {
return equalOK;
}
if (cmpResult < 0 && compareLess) {
return true;
}
if (!compareLess) {
return true;
}
}
return false;
}
int cmpResult = CoercingComparator.coerceAndCompare(lhsVal, rhsVal);
return Boolean.valueOf(cmpResult == 0 ? equalOK : (cmpResult < 0 ? compareLess : !compareLess));
}
@Override
public int getExpressionType() {
return compareLess ? (equalOK ? TYPE_LESS_EQUAL : TYPE_LESS) : (equalOK ? TYPE_GREATER_EQUAL : TYPE_GREATER);
}
@Override
public String getOperator() {
return compareLess ? (equalOK ? OPERATOR_LT_EQUAL : OPERATOR_LT) : (equalOK ? OPERATOR_GT_EQUAL : OPERATOR_GT);
}
@Override
public void toLDAPString(StringBuffer buf) {
if (!equalOK)
buf.append("(!"); //$NON-NLS-1$
buf.append('(');
appendLDAPAttribute(buf);
if (equalOK)
buf.append(compareLess ? OPERATOR_LT_EQUAL : OPERATOR_GT_EQUAL);
else
buf.append(compareLess ? OPERATOR_GT_EQUAL : OPERATOR_LT_EQUAL);
appendLDAPValue(buf);
buf.append(')');
if (!equalOK)
buf.append(')');
}
}
|