private static boolean compare()

in repository/service/src/main/java/org/apache/karaf/cave/repository/service/bundlerepository/CapabilitySet.java [295:384]


    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;
        }

        // 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);
            }
        }

        // 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 (Object o : (Collection) lhs) {
                if (compare(o, rhsUnknown, op)) {
                    return true;
                }
            }

            return false;
        }

        // Spec says SUBSTRING is false for all types other than string.
        if (op == SimpleFilter.SUBSTRING) {
            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;
        }
    }