in framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java [382:546]
private static boolean compare(Object lhs, Object rhsUnknown, int op)
{
if (lhs == null)
{
return false;
}
// If this is a PRESENT operation, then just return true immediately
// since we wouldn't be here if the attribute wasn't present.
if (op == SimpleFilter.PRESENT)
{
return true;
}
//Need a special case here when lhs is a Version and rhs is a VersionRange
//Version is comparable so we need to check this first
if(lhs instanceof Version && op == SimpleFilter.EQ)
{
Object rhs = null;
try
{
rhs = coerceType(lhs, (String) rhsUnknown);
}
catch (Exception ex)
{
//Do nothing will check later if rhs is null
}
if(rhs != null && rhs instanceof VersionRange)
{
return ((VersionRange)rhs).includes((Version)lhs);
}
}
// If the type is comparable, then we can just return the
// result immediately.
if (lhs instanceof Comparable)
{
// Spec says SUBSTRING is false for all types other than string.
if ((op == SimpleFilter.SUBSTRING) && !(lhs instanceof String))
{
return false;
}
Object rhs;
if (op == SimpleFilter.SUBSTRING)
{
rhs = rhsUnknown;
}
else
{
try
{
rhs = coerceType(lhs, (String) rhsUnknown);
}
catch (Exception ex)
{
return false;
}
}
switch (op)
{
case SimpleFilter.EQ :
try
{
return (((Comparable) lhs).compareTo(rhs) == 0);
}
catch (Exception ex)
{
return false;
}
case SimpleFilter.GTE :
try
{
return (((Comparable) lhs).compareTo(rhs) >= 0);
}
catch (Exception ex)
{
return false;
}
case SimpleFilter.LTE :
try
{
return (((Comparable) lhs).compareTo(rhs) <= 0);
}
catch (Exception ex)
{
return false;
}
case SimpleFilter.APPROX :
return compareApproximate(lhs, rhs);
case SimpleFilter.SUBSTRING :
return SimpleFilter.compareSubstring((List<String>) rhs, (String) lhs);
default:
throw new RuntimeException(
"Unknown comparison operator: " + op);
}
}
// Booleans do not implement comparable, so special case them.
else if (lhs instanceof Boolean)
{
Object rhs;
try
{
rhs = coerceType(lhs, (String) rhsUnknown);
}
catch (Exception ex)
{
return false;
}
switch (op)
{
case SimpleFilter.EQ :
case SimpleFilter.GTE :
case SimpleFilter.LTE :
case SimpleFilter.APPROX :
return (lhs.equals(rhs));
default:
throw new RuntimeException(
"Unknown comparison operator: " + op);
}
}
// If the LHS is not a comparable or boolean, check if it is an
// array. If so, convert it to a list so we can treat it as a
// collection.
if (lhs.getClass().isArray())
{
lhs = convertArrayToList(lhs);
}
// If LHS is a collection, then call compare() on each element
// of the collection until a match is found.
if (lhs instanceof Collection)
{
for (Iterator iter = ((Collection) lhs).iterator(); iter.hasNext(); )
{
if (compare(iter.next(), rhsUnknown, op))
{
return true;
}
}
return false;
}
// Spec says SUBSTRING is false for all types other than string.
if ((op == SimpleFilter.SUBSTRING) && !(lhs instanceof String))
{
return false;
}
// Since we cannot identify the LHS type, then we can only perform
// equality comparison.
try
{
return lhs.equals(coerceType(lhs, (String) rhsUnknown));
}
catch (Exception ex)
{
return false;
}
}